Quote:
|
Originally Posted by mtrawls
Look in the file user_routines_fast.c and look for User_Autonomous_Code(). You will put your code here. I suggest implementing a simple state machine, e.g.:
Code:
switch (auton_state) {
case TURN:
{
// put code to turn in desired direction here
// change state when you are done turning
// you'll know this either by a pre-determined time,
// a gyro reading/accumulation, or an encoder system
break;
}
case ACTUATE:
{
// put code here to actuate the relay/move motor
break;
}
...
default:
{ // either you have a default condition, or this should print an error
}
}
You can add as many states as suits your pleasure -- this makes it easy to change how you do the auton mode quickly. Make sure you change the state to the next one once you are done with the current. A little more details/more info on what you want/your robot would help to elicit a more specific response ... but this should get you started.
|
Yeah, that is te basic idea. You would have a static variable at the top of that function named something appropriate like, say, state that is initailized to zero. Like this:
Code:
// this is just after the function header
static unsigned char state = 0;
In your states you would then increment it when the condition for a state being done is satisfied. I.E.:
Code:
switch(state)
{
case 0: // This is your turn state
if(supposed to turn left) // This would be however you can tell whether you are supposed to turn left or right
{
// So, we turn left however you do that
}
else
{
// This is where you turn right
}
// now we check to see if we can go to the next state
if(done turning condition) // This is your encoders or whatever you use to tell if you've turned far enough
state++; // Go to the next state
break;
case 1: // Next state
// Same idea for a long as you want
break;
default:
// If you reach here something went wrong
}
So, the idea is that you increment a state variable when you are done with a state.
PS Just as a side note, I have my defaults in my various switches calling a function named stupid(). All it does is printf() Stupid! and calls itself until it crashes. Anyways, that was kind of random.
