Quote:
Originally Posted by Sparkyshires
Yeah we do, with boolean values holding the state of most mechanism on the robot. I'm being a bit dense, but I fail to see how a switch case could be used for that.
|
If you need more than two states, you could use an unsigned integer or enum (which is effectively a uint with names) and a Switch through all of them.
For example, a shot sequencer last year had three states: idle, fire_execute, and fire_retract. It transitioned in a 'circle', with a condition from idle to fire_execute (fire trigger input), two conditions to return from fire_execute to fire_retract (gun speed accel threshold met, or failure timer met) and a timer to return to idle (shot interval reset timer). We could represent this with the following C code:
Code:
typedef enum {
stIDLE,
stFIRE_EXECUTE,
stFIRE_RETRACT
} fire_seq_t;
fire_seq_t state = stIDLE;
/* Somewhere in a temporal task */
switch(state) {
case stIDLE:
if(FIRE)
{
state = stFIRE_EXECUTE;
}
output_piston = 0;
break;
case stFIRE_EXECUTE:
if(GSA_TRIG || TIMER > FIRE_FAIL_TIME)
{
state = stFIRE_RETRACT;
}
output_piston = 1;
break;
case stFIRE_RETRACT:
if(TIMER > RETRACT_TIME)
{
state = stIDLE;
}
output_piston = 0;
break;
}