V5 Callback Function, How to use???

I noticed in VCS that there’s a vex task call back function to bring the code back to an earlier line and i was wondering if anybody could explain to me how to use task::stop(callbackfuntion) ??

That works just like stopTask in ROBOTC.
If a task has been started, task::stop will stop it.
(fyi, there will be minor updates to this API in next VCS release)

So I didn’t use RobotC, but also I’m wondering is there a way to bring a program say it was on line 30 could I use it to say go back to line 10? This maybe completely wrong but i thought that was what the callback function was called?

A callback function is when a function calls another. In this case, a callback is a function you define that you want the task to run. When you construct a task with a callback function, a task is created in parallel that runs along the main task, just like startTask() in robot c, executing the function. Except, in place of defining a task and using it as the argument, you define a callback function and use it as the argument for the task constructor.

int foo() {
  int count = 0;

  while(1) {
    Brain.Screen.printAt( 10, 50, "I am running %d", count++ );
    vex::task::sleep(10);
  }
  
  return(0);
}

int main() {
  // start another task
  vex::task newtask( foo );
  
  // wait
  vex::task::sleep(3000);

  // now stop task
  vex::task::stop( foo );  
}
2 Likes

I think you’re thinking of a GOTO type command, which are wildly frowned upon as they are very bad programming practice. If you’re in C, doing something like

do {
     line 10 stuff;
     line 11-29 stuff;
} while (thing you were going to test for on line 30);

would be the proper way. Loop commands like these were implemented to escape the tyranny of GOTO.

Sorry to comment on a somewhat outdated thread. I’ve used the above example to create another task, and that works fine… but how do you initialize two tasks within the main method?

For example, if I want to create a task for flywheel


int flywheel()

and a task for arm control


int armControl()

… I get an error when trying to do the following (as an example- not actual code):

int main() {
vex::task newtask( flywheel);
vex::task newtask( armControl);
}

You are naming both tasks “newtask” use a unique name each time.

Gahhhh. Thought that was the method to create a new task. Makes sense. Thanks!

The vex::Task is creating a task object named x. Just like
int newTask is an int object.

Does the call back function work with all the commands? If I put motor.rotateFor() inside the callback function, the motor does not move at all. So, I have to put the code inside the main body instead of inside a callback function. Are there any restrictions for what statements can be put inside the call back function?

You can’t use object’s methods as tasks. You’ll need static functions instead. Now, you could make a function that is pretty much just a wrapper for a particular method, generally for a particular object.

Not really. you can call methods on a global instance from pretty much anywhere.
perhaps post the code that’s not working.