So I have created a motion algorithm for an odometry system. I have been trying to make the robot perform a function while performing the motion algorithm. To do this, I created two classes:
class Varcond{
private:
double *cond1, *cond2;
std::string compar;
int null = 0;
public:
Varcond(double &a, double &b, std::string c): cond1(&a), cond2(&b), compar(c){};
Varcond(){null = 1;};
bool result(){
if(compar == "<"){
return *cond1 < *cond2;
}
else if(compar == ">"){
return *cond1 > *cond2;
}
else if(compar == "<="){
return *cond1 <= *cond2;
}
else if(compar == ">="){
return *cond1 >= *cond2;
}
else{
return *cond1 == *cond2;
}
}
bool isnull(){
return this->null == 1;
}
};
class exec_func{
private:
Varcond condition;
//initially assigns both pointers as null so I can execute correct function
void (*func)() = NULL;
void (*intfunc)(double) = NULL;
double rev;
public:
//constructors
exec_func(Varcond a, void (*b)()): condition(a), func(*b){};
exec_func(Varcond a, void (*b)(double), double c):condition(a), intfunc(*b), rev(c){};
exec_func(){};
void execute(){
intfunc != NULL ? intfunc(rev): func();
}
bool istrue(){
if(!condition.isnull()){
return condition.result();
}
else{
return false;
}
}
};
The first class creates a custom conditional that can compare the values of two variables. The second basically stores the reference to a function and the condition that it should start at( Run Intakes, disttopoint < target). I tried implementing them in my algorithm like this:
void Turntopoint(parameters){
//do stuff
while(1){
//checks for condition and executes function if neccesary
if(execs.istrue()){
execs.execute();
//Does Stuff
Motors.spin(spd);
task::sleep(10);
}
}
The robot completes the motion algorithm and then executes the function. This is true even when the distance is obviously less than the target. I tested the classes on a c++ compiler and they worked. I put them in a while loop and the function executed when needed. However, is there any reason why the function is executing at the end instead of in the middle of the function? Does it have something to do with the spin command?
Edit: can I do something with events? The documentation seems to indicate that you can only callback functions that are void with no parameters. Is there any way to use events to callback a function with a double parameter?