Auton/Drive Switch Issue

I was having an issue when switching between Auton and Drive and back with my switch, after I switched to auton my robot display broke and the motors kept going infinitely until I stopped them and after I switched back to usercontrol nothing would happen (User control worked originally). Here is my main code :

#include"myRobot.h" // Includes My Library
using namespace vex; // Exports All of the functions and types in namespace "vex"
using namespace DisplayControls; // Exports All of the functions and types in namespace "DisplayControls"
using namespace UserControl;// Exports All of the functions and types in namespace "UserControl"
using namespace Odometry; // Exports All of the functions and types in namespace "Odometry"
using namespace Auton; // Exports Auton Functions
competition Competition; // Delcares the Competion

/******d
 @brief This is what is called when the robot starts
 *******/
int main() { // This is the "entry point" of the program
   Inertial1.calibrate();
    while(Inertial1.isCalibrating()){
        wait(5,msec);
    }
  LF.setPosition(0,deg);
  LR.setPosition(0,deg);
  RF.setPosition(0,deg);
  RR.setPosition(0,deg);
    //thread LiftControl(LiftControls::LiftPid);
    thread Temp(Utilities::TemperatureMonitor::Monitor);
    thread PluggedIn(Utilities::PluggedInMonitor::Monitor);
    thread MeasuringDevies(Utilities::ValueMonitor::Monitor);
    thread ScissorMonitor(LiftControls::MonitorLimitSwitches);
    thread LimitsInit(initAllLimits);
     Competition.drivercontrol(UserControl::usercontrol);
     Competition.bStopAllTasksBetweenModes = false;
     Competition.bStopTasksBetweenModes = false;
    Competition.autonomous(Auton::autonomous);
    
    DisplayControls::   DisplayMain();
    printf("MAJOR ERROR");
}   

And I Also attached all of my code files below
TheUnknowsVer_4.0 (4).zip (5.5 MB)

Well, certainly a rather unique way of organizing code, oh dear, anyway.

was it ever working ? what was the last thing you changed that broke it ?
(and full disclosure, I have not tried to run it and see what’s happening).

I assume you have read this, second time today I’ve linked to to this old topic.

first red flag is event handers registered in usercontrol, every time usercontrol is called you register a further set of handlers.

Ok, lot of issues with this code. However, the most fundamental issue that stopping driver from running after auton is the “old_drive” function that is mostly a blocking function, you need to add a sleep_for in that while loop. Add a sleep here.
I don’t have time to explain reson right now, except know that calling position() on a motor is treated differently from many other API calls and will not allow an automatic task stop.

          while (Continue) {
              this_thread::sleep_for(10);
              double currentAverage =   (LF.position(deg) +LR.position(deg)  + RF.position(deg) + RR.position(deg))/4;
              if (direction == fwd) {

But you have issue with events and tasks. using the code I posted here.

We can look at how tasks are being allocated in your code.

When first run in a disabled state we have this.
disabled_1

Run auton
auton_1

One more thread as expected.
Back to disabled.
auton_2
The auton thread is still running, that’s because you set this flag in main.
(edit: if that flag was true, the thread would still exist but be in a stopped state so the display would still show 15)

    Competition.bStopTasksBetweenModes = false;

edit: see my subsequent comments in a later post.

Now run driver.
driver_1
Lots more threads because of all the event registration.

but if I disable and re-enable driver.
driver_2
even more threads because you register events a second time.

so yea, some issues to solve.

So, if I were to set
Competition.bStopTasksBetweenModes= true
Then it would fix all of the issues with competition threads continuing to run after being disabled and if I were to add

this_thread::sleep_for(5);

to the top of old_Drive() instead of

task::sleep(25);

to the bottom of old_Drive(), it would fix it not being able to read the position values

Also Could I pull up the Competition State and #Threads on my brain or is it on specific to yours?

old_drive had no sleep/delay in the while loop at all. You can place it wherever, it just needs a sleep to allow the scheduler to run.

A bit more background.

I said in another topic a couple of day ago that sleep/delay was not needed with VEXcode projects, well, thats almost true but not 100%, there are a couple of exceptions.

code like this needs a delay.

int count;
while(true) {
    count += 1;
}

Why ? because it contains no calls into the vex API, for example, no calls to motor.spin or Brain.Screen.Print.

The reason is that vexos, when used with VEXcode, uses a cooperative scheduler, tasks have to explicitly yield back to the scheduler to allow other tasks (same as a thread, don’t ask) to run. Way back in 2016 when we wrote this we realized that students were going to forget to add a call to task::sleep inside every loop, so we added code to monitor how long a task had been running and force a call back into the scheduler, this happens every time you call a vex API function, however, there are one or two exceptions to this rule.

motor.position() is one such API that does not do a timeslice check, the reason is that we have to make two calls into vexos, one to set the units that the position is to be returned in, the other to get the actual position (in hindsight this was a mistake, but some ideas were borrowed from RobotC as that was what we had previously worked on). When making two calls like that we have to make sure there is no task swap between those calls (in case another task also tries to use motor.position() with different units), so we briefly disable the timeslice checking.

Your “while(Continue)” loop only had calls getting motor position, so no other tasks could run while that loop was running.

The other API functions that do this would generally involve motor position as well, setPosition() (rarely used) and some of the spinTo functions, these generally would not affect code as they also contain loops with calls to task::sleep.

Competition.bStopTasksBetweenModes= true;

bStopTasksBetweenModes is true by default. This flag makes sure that autonomous and usercontrol are stopped when needed if the robot is disabled. They still exist, so my competition debug code will still show the task is present, but they are not running and in a completed state until restarted.

This flag will have no impact on tasks created as part of event registration, it only impacts the two competition tasks.

A quick note about events on V5.
Every event runs in its own task. When an event is registered by calling, for example, pressed with a callback function, a task is created that blocks on a semaphore (don’t worry if you don’t understand this). When vexos determines that the condition for the event is satisfied, the button is pressed etc. it releases the semaphore the event task is waiting for and allows it to run. Once finished (events really should not block) the semaphore is once again held by the event checking task waiting for a new event.

Competition.bStopAllTasksBetweenModes = true;

This was an attempt to solve the issue you have, it will attempt to stop all tasks created after the main function was entered, so tasks created in global constructors would not be affected.

In your case it will stop all the event handlers, but it will also stop all the tasks you have created in the main function. Utilities::TemperatureMonitor::Monitor etc. so may not be your solution.

You need to think about how you want the code to run, and how to deal with this. It’s not necessarily complicated, but there’s more than one way and any of my solutions may not be what you want to do.

The competition debug code I used is open source and linked in my earlier post. All I added to your main function was

#include "comp_debug.h"
competition Competition; // Delcares the Competion

competition_debug  Cdebug( Competition );

and added the file “comp_debug.h” to the include folder.

The old_drive was suppose to have a delay of 25 milliseconds but I wrote it a while back and edited, well anyway. I changed the beggining while loop of the usercontrol:

while(true){
    if(Competition.isDriverControl()){

And I added it as a new constant thread:

    thread DriverControl(UserControl::usercontrol);

It was be having strange issues with disabling and reenabling so I made it its own separate thread .
So I added this as a delay to the old_Drive() function

    task::sleep(20);

I also used the competition debug code and the threads remained at 24-25 threads, and It works now. Thank you for your help!