In the Watson version you drop your autonomous code into the file "autonomous.c", specifically into the routine "Autonomous()".
This routine gets executed ~38 times per second in sync with the driver station control transmission packets.
You write your autonomous code in such a way that it does a little bit and goes away. When it gets called again it does a little bit more and goes away again.
Two ways to dead reckon autonomous movements are:
- to keep track of and count the number of times you're Autonomous() has been called - 38 times = 1 second.
- use a system timer and check each time to see if you've driven long enough.
If your code already has a timer setup, then you can experiment with using that. It's the most accurate method.
Counting loops is accurate enough for what you're doing at first, and it's trivial to implement and understand right away, so you can start with that.
On top of these methods of keeping track of time you might want a state machine to keep track of the steps you should be doing.
Here's an example:
Code:
// Drive forward, turn, return, and stop
void Autonomous()
{
static int counter=0; //keep track of loops to use as a crude timer - static keeps it around from call to call
static int autostate=1; //keep track of what step we're supposed to be doing
switch (autostate)
{
case 1: // Drive forward
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 54; //motor is reversed
if (counter>38) //1 second
{
autostate = 2; // move on to the next step
counter = 0; // reset our timer for the next step
}
case 2: // Turnaround
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 200; //motor is reversed
if (counter>76) //2 seconds
{
autostate = 3;
counter = 0;
}
case 3: // Drive forward (returning now)
pwm01 = pwm02 = 200;
pwm03 = pwm04 = 54; //motor is reversed
if (counter>38) //1 second
{
autostate = 4;
counter = 0;
}
case 4: // Stop - What to do when everything else is done
default: // also what to do if an invalid autostate occurs
pwm01 = pwm02 = pwm03 = pwm04 = 127; // Make sure the last thing you do is always stop
}
counter++;
}
Our team code from 2007 is
here, but it's fairly complex as it implements a scripting system for autonomous, so I definitely wouldn't start learning at that level. Our robot was somewhat complex that year too.