And, in case if you are looking for even more nitty-gritty technical details …
VexOS uses cooperative scheduler, which means that, unless one tasks yields the CPU, other tasks couldn’t run.
In the first, non-competition example, main() task starts sensorMonitoringTask(). However, VexOS will let it run only when the main loop makes a system call that yields the CPU, like vex::task::sleep(). If main task never calls such function the second task will never get to run and vice versa.
int sensorMonitoringTask()
{
while(1) { // loop forever
// do something useful ...
vex::task::sleep( 10 );
}
}
int main()
{
vex::task task1(sensorMonitoringTask); // this will start task
while(1) // main user interface loop
{
if( button1.pressing() )
{
// display something, run some motors, ...
}
vex::task::sleep(10); // yield time to other tasks
}
}
For VRC robots, when you want them to obey competition switch, that controllers must be plugged into during the matches, you will need to follow special template:
vex::competition Competition; // https://help.vexcodingstudio.com/#cpp/namespacevex/classvex_1_1competition/autonomous
// Autonomous Task - This task is used to control your robot during the autonomous phase of a VEX Competition.
void autonomous( void ) {
// run robot forward for 3 seconds and kick the ball
}
// User Control Task - This task is used to control your robot during the user control phase of a VEX Competition.
void usercontrol( void ) {
while (1){ // main loop
// check for user input
// run some motors
vex::task::sleep(10); // yield time to other tasks
}
}
int main() { // Program gets control here
// if necessary run the pre-autonomous functions, like starting sensorMonitoringTask(),
//Set up callbacks for autonomous and driver control periods.
Competition.autonomous( autonomous );
Competition.drivercontrol( usercontrol );
//Prevent main from exiting with an infinite loop.
while(1) { // this essentially does nothing, but yields time to other tasks.
vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
}
}