Need help with Multitasking in Vexcode V5

So I’m trying to create a task that constantly runs in the background but for some reason I can’t seem to get it to work with me, I want a void function to loop over and over while the normal main method code runs at the same time, I want it to suspend when I press a button for manual control and resume when another button is pressed.
Here’s what I have so far:

  • The Function
void DiscDetect() {
  int Color;
  while(1) {
    Color = Disc.hue();
    if (Color == 20) {
      Flywheel.spin(forward, 100, pct);
    }
    else {
      Flywheel.stop(hold);
    }
    // don't hog the cpu :)
    vex::task::sleep(25);
  }
}
  • Initialization of task after vexcodeinnit
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  vex::task check(int DiscDetect());

// Main Method
  while (1) {
    // Flywheel Controls
    if (Controller1.ButtonUp.pressing()) {
      vex::task::suspend();
      Flywheel.spin(forward, 100, pct);
    }

    // Emergency Shut-off
    if (Controller1.ButtonB.pressing()) {
      Flywheel.stop(hold);
      vex::task::resume();
    }

    // Prevents wasted resources
    wait(20, msec);
  }
}

Now the initialization near the beginning is the only way I could word it in which the text wouldn’t error out, and I don’t know how to suspend the specific task since it errors if I put “check” in between the parentheses.

Any input will be much appreciated! <3

This line is incorrect. Tasks take a pointer to a function as an argument, so you’d want:

vex::task check(DiscDetect);

See also this thread from James about the lifecycle of threads through each of the states a robot goes thru in a competition (e.g. Disabled, Auton, Disabled, Driver).

As James mentions, you are realistically more likely to go thru states:

  1. At queueing, or before placing the robot on the field, you turn it on and start the program. Main runs and you are in Driver Control mode.
  2. You place the robot and plug your controller into Field Control which Disables your Robot.
  3. Auton starts and you are in auton.
  4. Auton ends and you are Disabled.
  5. Driver control starts and you are in Driver.

During 5, if you disconnect or need to power-cycle your robot, you may also start right back up in Driver.

Depending on what you want this to do, you may want to start the task only in the usercontrol function rather than main.

6 Likes

This line is incorrect. Tasks take a pointer to a function as an argument, so you’d want:

vex::task check(DiscDetect);

I tried this and it has the error line underneath it, it says [Clang] No matching constructor for initialization of 'vex::task' when I hover over it.

image_2022-10-28_092130564

And also this is just a test program I created without the competition format so I could test it first before implementing it into our robots competition code, either way I appreciate you linking the thread!

Fair. So vex::task takes a pointer to a function that returns an integer with no arguments. So you’ll need to change your DiscDetect declaration from:

void DiscDetect()

to:

int DiscDetect()

The compiler may want you to also add a return value to it, in which case it might look like:

int DiscDetect() {
  int Color;
  while(1) {
    Color = Disc.hue();
    if (Color == 20) {
      Flywheel.spin(forward, 100, pct);
    }
    else {
      Flywheel.stop(hold);
    }
    // don't hog the cpu :)
    vex::task::sleep(25);
  }
  return -1;
}

Where the while(1) loop prevents it from actually reaching the return statement (which is fine)

5 Likes

Thanks that works! It’s weird that you have to make it an integer even though it’ll be running at the same time as the mainstream code. I guess it was mainly put in place for adjustments during calculations in real time I guess. Should’ve tried that sooner but I was really insistent on not returning a variable incase it broke something, so never actually returning something is one heck of a bypass to that issue, thank you!

Just letting you know, you shouldn’t have your flywheel motor on hold mode. This will overheat the motor much faster. I would recommend setting the stopping mode to coast.

2 Likes

I keep telling my team that but they won’t listen :laughing:
Hopefully if I show them this they’ll listen to you lol.
I appreciate your worries! :grin: