We need help with code in autonomous. We want our robot to turn (left or right) stop and then actuate a relay and move a speed controller motor. We need to know how to make the code and where to put it in the default code.
Thanks team 634
We need help with code in autonomous. We want our robot to turn (left or right) stop and then actuate a relay and move a speed controller motor. We need to know how to make the code and where to put it in the default code.
Thanks team 634
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.:
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:
// 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.:
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.
Might I suggest using an enum or define statements to name the states. state = 3.14159 is a bit more esoteric than #define AT_TARGET 3.14159 and then saying state = AT_TARGET (a bit less demanding on programmer’s all-too-vexed memory too). This also helps because you don’t always want to say sate++ – for example, in my code I have a state to turn left and another to turn right, and it all depends on which sensor sees it; so there is an if statement assigning state a different value based on the condition.