I’m just going to throw this code up. It demonstrates a couple of things, how to start and stop tasks, how to monitor competition control to perform actions when the robot goes in and out of enabled. Ask questions as necessary.
#include "robot-config.h"
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: james */
/* Created: Tue Feb 26 2019 */
/* Description: V5 project */
/* */
/*----------------------------------------------------------------------------*/
using namespace vex;
// A global instance of vex::competition
vex::competition Competition;
// global variables to store task instances
vex::task armTask;
vex::task driveTask;
// a task started by usercontrol
int
armFunction() {
int count = 0;
while(true) {
Brain.Screen.printAt( 10, 70, "Arm task %6d", count++ );
// Allow other tasks to run
this_thread::sleep_for(10);
}
return(0);
}
// another task started by usercontrol
int
driveFunction() {
int count = 0;
while(true) {
Brain.Screen.printAt( 10, 90, "Drive task %6d", count++ );
// Allow other tasks to run
this_thread::sleep_for(10);
}
return(0);
}
//
// This task will monitor the competition status and stop
// other tasks created in usercontrol
int
compMonitor() {
bool isEnabled;
while(true) {
if( Competition.isCompetitionSwitch() || Competition.isFieldControl() ) {
if( Competition.isEnabled() ) {
Brain.Screen.printAt( 10, 130, "Comp Enabled" );
isEnabled = true;
}
else
if( !Competition.isEnabled() && isEnabled ) {
Brain.Screen.printAt( 10, 130, "Stop tasks " );
armTask.stop();
driveTask.stop();
isEnabled = false;
}
}
this_thread::sleep_for(20);
}
return(0);
}
/*---------------------------------------------------------------------------*/
/* Pre-Autonomous Functions */
/*---------------------------------------------------------------------------*/
void pre_auton( void ) {
}
/*---------------------------------------------------------------------------*/
/* Autonomous Task */
/*---------------------------------------------------------------------------*/
void autonomous( void ) {
int count = 0;
while (1) {
Brain.Screen.printAt( 10, 50, "Auton running %6d", count++ );
vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources.
}
}
/*---------------------------------------------------------------------------*/
/* User Control Task */
/*---------------------------------------------------------------------------*/
void usercontrol( void ) {
int count = 0;
// start some more tasks
armTask = vex::task( armFunction );
driveTask = vex::task( driveFunction );
// User control code here, inside the loop
while (1) {
Brain.Screen.printAt( 10, 50, "Driver running %6d", count++ );
vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources.
}
}
// Main will set up the competition functions and callbacks.
int main() {
int count = 0;
//Set up callbacks for autonomous and driver control periods.
Competition.autonomous( autonomous );
Competition.drivercontrol( usercontrol );
//Run the pre-autonomous function.
pre_auton();
// start a task to monitor competition control
vex::task compTask( compMonitor );
//Prevent main from exiting with an infinite loop.
while(1) {
Brain.Screen.printAt( 10, 30, "Main %d", count++ );
vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
}
}