Program Emergency Stop VEX C++ Pro

PLTW Teacher here. We decided to get ahead of the game and upgrade to V5 now rather than buy legacy stuff as it gets phased out. Right now i’m trying to develop content for my students to start completing some of the PLTW projects. When we were using RobotC, as a requirement, I would typically have my students run an emergency stop loop as a concurrent task to their main code using the stopAllTasks function like the example below. :

task estop()
{
untilBump(stopButton,50);
stopAllTasks();
}
task main()
{
startTask(estop);
untilBump(startButton,50);
while (1==1)
{
~program things~
}
}

I did this so that if their code didn’t work while disconnected from the computer they could hit a button near the outside of their system rather than try to power cycle the cortex. Basically the estop task would start at the beginning of the main task and loop alongside it, constantly checking for the estop bump switch. That way the code could do its thing without interruption and be killed if the estop was pressed.

My big question is how to perform a similar concurrent task in the VCS C++ pro where the concurrent task will constantly check for that bump switch and terminate the main task if it is pressed. Any help is good help.

1 Like

The power buttons on V5 can be used to stop a program if you hold them. That kind of nullifies the need for an emergency stop task. Just hold down the power button on the controller and the program will stop running. I am not however aware of how you would write the code for a stop.

@john_s, that is a good question.

I searched help.vexcodingstudio.com and didn’t find the functions you are looking for either.

However, RobotMesh V5 C++ API contains a reference to disableAll() function, which seems to do exactly what you need.

Right now I am not at the computer with any VEX coding environment and couldn’t check that this is 100% correct code, but I think this is what you are looking for:

int userThread()
{
   while(1) // main loop
   {
    // do something ...
    this_thread::sleep_for( 10 );
   }
}

int main()
{
    vex::thread ut(userThread);

    while(1) // emergency stop loop
    {
      if( emergenctyStopButton.pressing() )
      {
            ut.interrupt();  // stop user thread
            vex::motor::disableAll();
      }
      this_thread::sleep_for( 10 );
    }
}

Reference for vex::thread::interrupt() function.
Also, another related thread here: Emergency Stop Button/Bumper

2 Likes

Thanks! I finally got it working. As a heads up the disableAll() function isn’t working in VCS. I’m not sure if it just hasn’t been added yet or what, but I was able to use the motor.stop() function in place of it and that did the trick. For the time being it looks like each motor will need to be disabled independently, but this is a good work around for now.

1 Like

couple of points.
You should consider switching over to VEXcode if you have a ROBOTC background and are already using C++ Pro in VCS. preview 3 is the latest.

I’ve not yet enabled a stopAllTasks() type function in the VEX sdk, it’s a bit more complicated with the V5, still some debate as to whether that should also remove any event handlers that have been installed. It may be available with VEXcode preview 4, I don’t know when if/when VCS will get an update so it’s stuck on the August version of the SDK for now.

We do not have a disableAll() method in the VEX SDK, that’s something RobotMesh added to their implementation of the API.

2 Likes

you could use this as a simple stop all motors function.

void
stopAllMotors() {
    // find all motors and send stop
    for( int32_t p=vex::PORT1; p <= vex::PORT21; p++ ) {
      vex::motor m( p );
      if( m.installed() ) {
        m.stop();
      }
    }
}
1 Like