V5 Callback Function, How to use???

  1. 2 months ago

    abennett5139

    Sep 11 Concord, NC 5139

    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) ??

    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. jpearman

    Sep 11 Moderator, ROBOTC Tech Support, V5 Beta Moderator Los Angeles 8888

    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)

  3. abennett5139

    Sep 11 Concord, NC 5139

    @jpearman 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?

  4. henryl

    Sep 11 353X
    Edited 2 months ago by henryl

    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.

  5. jpearman

    Sep 11 Moderator, ROBOTC Tech Support, V5 Beta Moderator Answer Los Angeles 8888
    Edited 2 months ago by jpearman
    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 );  
    }
  6. Edited 2 months ago by John TYler

    @abennett5139 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?

    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.

  7. 2 days ago

    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);
    }
  8. tabor473

    Nov 15 V5 Beta Tester OYES, WPI

    @carp 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.

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

  10. tabor473

    Nov 15 V5 Beta Tester OYES, WPI
    Edited 2 days ago by tabor473

    @carp 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.

 

or Sign Up to reply!