Delta II | Auton Help

After trying to build an auton, I tested to see if the autonomous would work but it doesn’t seem to work with two while loops. I have a few questions;
Is it possible to run a while loop during auton? And if so, can you run two while loops? When I tried running two while loops, the robot ceases to move. Will this code be possible without a certain wait time even with one while loop(It’s an example)?


task autonomous{

int modeswitch = 0;
while(true){
if (modeswitch==1){
motor[Catapult]=127;
}
else if (modeswitch==-1){
motor[Catapult]=-127;
}
else{
motor[Catapult]=0;
}
}

motor[DriveL]=127;
motor[DriveR]=127;
modeswitch=2;
wait1msec(1000);
modeswitch=0;
motor[DriveL]=0;
motor[DriveR]=0;

}

NOTE: This isn’t for our robot…

The easiest way to do something like this would be using the ROBOTC multitasking system. Remember code runs linearly. Currently the drive motors won’t start until the while loop ends. Which obviously will be never.

Okay, that makes sense.
Is there a way where I can get more information about a “ROBOTC multitasking system?”

@jpearman would be your go-to guy on multitasking.

Take a look at this example. I just grabbed it off the first link upon googling “RobotC multitasking”

#pragma config(Sensor, dgtl1,  LED_1,               sensorDigitalOut)
#pragma config(Sensor, dgtl2,  LED_2,               sensorDigitalOut)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
// Detect if we are running in PC emulation and switch to
// using a simulation of the sensors as we cannot see the real leds
#if (_TARGET == "Emulator")
#define SENSOR      SensorValueSimulations
int SensorValueSimulations[kNumbOfRealSensors];
#else
#define SENSOR      SensorValue
#endif
 
// Flash the first LED  
task flashLed_1()
{
    while( true )
        {
        // output to 1
        SENSOR LED_1 ] = 1;
        wait1Msec( 500 );
 
        // output to 0
        SENSOR LED_1 ] = 0;
        wait1Msec( 500 );
        }
}
 
// Main task - flash the second LED
task main()
{
    // Start the other task
    StartTask( flashLed_1 );
 
    // Do our own thing
    while( true )
        {
        // output to 1
        SENSOR LED_2 ] = 1;
        wait1Msec( 250 );
 
        // output to 0
        SENSOR LED_2 ] = 0;
        wait1Msec( 250 );
        }
}

For running a while loop in autonomous you can do…


While (bifiautonomousmode)
{
Insert auton code here
}

The bifi autonomous mode is basically the competition autonomous task (it’s mentioned in the competition includes file). In theory this should work. As far as running two loops at once I’m not sure why you would want this but anyways… i have zero idea as to if this can be done or not. Like others said James would probably be the one to ask.

The problem with the code and the reason why it doesn’t work as expected is that the while loop condition is


true

and never becomes false. Thus, the code after the while loop will never run since the program never exits the while loop. The reason driver control code still works is probably because you left the variable (I don’t know the exact name) with a name related to stopping all tasks when the robot is disabled set to true. So when the robot is disabled, the autonomous task is stopped and the while loop is no longer running.

If you want to run the drive motors while also running the catapult motors, here’s what you’d need to do:

  1. Make the mode variable global (put the declaration outside and above all tasks and functions) so it can be accessed from anywhere in the program
  2. Create a task (

task myTaskName() { }

) for the catapult motors and while loop, and another one for the drive motors and the code that changes the variables. In the autonomous task, use the


startTask(myTaskName)

function to start the catapult and drivetrain tasks. Note that you need to call


startTask

once for each function, so you’d have two


startTask

calls, one after another.

Also, it looks to me like this code could be made simpler. Unless something more advanced is planned, you could just set the drivetrain and catapult motors at the same time (basically, put everything in a sequence) and the code would be much simpler and less error-prone.

Thank you, that example helped very much!