Using tasks in VEXcode

I have always avoided using tasks in my program, but now I am trying to switch to using tasks so that I can control the priority of different subroutines. I want to run different tasks for things like tracking position, PID control, displaying info to the screen, etc. I would like to start the tasks when the robot is first turned on, and continue them during autonomous and driver control. I know that to do this I need to set vex::competition.bStopTasksBetweenModes to false. The competition format says that if you set this to false, you need to manage the tasks yourself, but I’m not sure what that means exactly. In this post by @jpearman, he says that he doesn’t use the competition format for programs that continue tasks between modes. How would you go about doing this? I don’t want to mess it up and end up losing a match at my next comp because the code doesn’t run properly :).

I also have a couple other task-related questions. What’s the difference between changing a task’s priority and changing the time it sleeps for? Is there a way to ensure that the code for a task will run fully before the scheduler switches tasks? Is there a VEXcode equivalent of endTimeSlice()? Thanks for any help reguarding any of these questions!

2 Likes

Why not just restart the threads you want in usercontrol?

time that a task sleeps for is the break between each interval, priority simply determines which task is executed first.
One option is to (if you must use tasks for this) is to change a global variable at the end of a task and have the scheduler wait until the variable is updated before starting.
Not sure about the endtimeslice question…

1 Like

Thanks for the reply. The reason I don’t want to restart the tasks in user control is that I would lose the data saved in local variables, so I would have to save everything as global variables (correct me if I’m wrong about this). Saving everything as global variables wouldn’t work for my application. It would take too long to explain exactly what I want to do, but it involves automatically creating tasks for different pages to display on the LCD screen, and using global variables wouldn’t work well for this because I would need a LOT of global variables for it to work. About the endtimeslice, that was apparently a thing in RobotC, and it seemed useful in a tutorial video I found about using tasks in RobotC. Maybe it’s not needed in VEXcode though.

Since, I don’t know what are your goals, I’ll just provide links to several very helpful replies by jpearman about tasks, that I had bookmarked:

1 Like

My goal is to create several tasks to control different elements of the robot as well as the screen, and have them run continuously throughout a match without having to restart them when autonomous and driver control starts. I did find the answer my question about endTimeSlice() in one of the links you posted, in VEXcode it is called task::yield().

1 Like

Update- I started playing around with tasks and using a competition switch, and it seems that by default tasks do continue between modes without restarting and without losing the data saved in local variables, which is exactly what I wanted. That contradicts what I read about tasks in other forum posts, so I’m a little confused about that. But I’m glad it works!

3 Likes

Please, check what happens if you cut power to the robot by disconnecting battery when user control is enabled on the switch and then reconnect the battery. Will all service tasks restart properly for user control mode?

I checked, and when you unplug the battery and plug it back in it exits the program rather then continue it. Maybe it only continues the program on the field control systems?

Could you restart the program from the controller?