The commands.h file is just a single variable. It is an array of a custon data type called commands. The commands data type has four values: an int called command which stores the command type, a long called param1 which stores the first argument for the function, and two more ints called param2 and param3 to hold more arguments if needed.
The robot.c file goes through this structure line by line and executes. For example the default code
{CMD_GYRO_BIAS, 0, 0, 0},
{CMD_WAIT_FOR_BUMP, 100, 0, 0},
{CMD_WAIT, 1000, 0, 0},
{CMD_DRIVE, 1500, 0, 0},
{CMD_WAIT, 4000, 0, 0},
{CMD_TURN, (-1500), 50, 0},
{CMD_WAIT, 3000, 0, 0},
{CMD_DRIVE, 2400, 0, 0},
{CMD_WAIT, 4000, 0, 0},
{CMD_TURN, (PI_MRAD / 2), 50, 0},
{CMD_WAIT, 4000, 0, 0},
{CMD_DRIVE, 2400, 0, 0},
{CMD_WAIT, 4000, 0, 0},
{CMD_TURN, (-1500), 50, 0},
{CMD_WAIT, 1000, 0, 0},
{CMD_DRIVE, 0, 0, 0},
{CMD_KEEP_HEADING, 240000, 100, 0},
{CMD_JUMP, 1, 0, 0},
{NULL, 0, 0, 0}
would execute something like this:
- pause for a few loops to calibrate the gyro
- wait for a sensor to be tripped or a button to be pushed
- wait one second (1000 milliseconds) before executing the next command
- drive forward 1.5 meters (1500 millimeters) and stop
- turn clockwise (negative direction) 1.5 radians (just under 90 degrees) with a tolerance (error zone) or plus or minus 50 milliradians (.05 radians)
- wait another 3 seconds (3000 milliseconds)
- drive forward 2.4 meters
- wait 4 seconds
- turn pi/2 radians (90 degrees) counterclockwise with a tolerance of 50 milliradians
- wait another 4 seconds
- drive forward 2.4 meters
- wait another 4 seconds
- turn 1.5 radians counterclockwise with a tolerance of 50 milliradians
- drive forward 0 millimeters. That is, stop.
- maintain heading for 4 minutes (240000 milliseconds) with a tolerance of .1 radians. That is, if something tries to turn the robot, it will self correct to within .1 radians of where it was.
- return to step 2 (It’s called as 1 since arrays in C start with 0) and repeat.
- the Null statement signals an end of file for the script interpreter. Eventhough it will never get ot stil line, it is still good practice to put it in.
The reason the numbers are so big in the parameters are, if you haven’t guessed, every thing is in thousanths. This eliminates the need for decimal math and makes the program run faster. Most of it is really straight forward, requireing only one argument. It is good practice to fill the unused paramaters with a 0, even though it doesn’t matter what you put in it.
The reason tolerances are needed is that the robot will rarely ever get back to where it thought it was, so if there was no tolerance, the robot would continue to twitch, thinking it was always off by a fraction of a radian.
Anyways, I hope I have cleared thing up a bit. If there is anything else you need, don’t hesitate to ask.
-Tony K