While developing firmware for an LED-based user feedback system, I needed to implement a very simple state machine in C. After spending a few confused hours sweating in black and white, trying to keep track of state variables in my head “the old way”, I found this blog entry which proposes convenient and concise macro definitions for a simple state machine. Suddenly I was coding in full color, and the structure of the program fell into place quickly. Thanks, Jim! The way a program like this works is by keeping track of the “previous”, “current”, and “next” states in variables. The code inside one of the states is continuously executed, until the “next” state changes. Each time a state is entered or exited, the variables are updated. But by moving the state variable housekeeping to the header file, our switch statement in main() is much more straightforward. Here’s the code for the macros in the header file: #define STATE_ENTRY_ACTION if ( currentState != previousState ) { #define STATE_TRANSITION_TEST previousState = currentState; } if ( nextState == currentState ) { #define STATE_TRANSITION_TEST_EXCLUSIVE } else if ( nextState == currentState ) { #define STATE_EXIT_ACTION } if ( nextState != currentState ) { #define STATE_EXIT_ACTION_EXCLUSIVE } else if ( nextState != currentState ) { #define STATE_END currentState = nextState; } break; The EXCLUSIVE macros are used if you want to ensure that the state machine is run at least three times per state, making the timing of the function call more predictable. Now adding a state here or there, or inserting functions at the right point in the routine is easy: void main() { while( true ) { switch ( state ) { case state_n: STATE_ENTRY_ACTION doColor(); STATE_TRANSITION_TEST checkUserInput(); STATE_EXIT_ACTION fadeColor(); STATE_END } } } I did not include a default case in this example, but for a real application it’s a good idea to always include one.
另外在google上还找到了一个利用这种方法的程序:
// // Use this file for calibrating the drive motors. // // "#include" this just before the User_Autonomous() function // in user_routines_fast.c //
// ---------------------------------------------------------------------------- // Periodic processing of the state machine. // Check for state transitions according to the current state. // // Call this function every 26 milliseconds from inside the autonomous loop. // void Autonmous_Tick( void) { static unsigned char PreviousState = 0; static unsigned char CurrentState = 0; static unsigned char NextState = 0;
static int CountdownTimer;
// Process according to current state. switch ( CurrentState ) { // Always get here once and only once. // Do some initialization and set up for first state. case 0: CurrentState = 1; NextState = 1; break;
case 1:
STATE_ENTRY_ACTION CountdownTimer = 38*10;
pwm0a = 127 + 30; pwm0b = 127 + 30;
STATE_TRANSITION_TEST if ( --CountdownTimer == 0 ) NextState = 2;
STATE_EXIT_ACTION
STATE_END
case 2:
STATE_ENTRY_ACTION CountdownTimer = 38*10;
pwm0a = 127 + 40; pwm0b = 127 + 40;
STATE_TRANSITION_TEST if ( --CountdownTimer == 0 ) NextState = 3;
STATE_EXIT_ACTION
STATE_END
case 3:
STATE_ENTRY_ACTION CountdownTimer = 38*10;
pwm0a = 127 + 50; pwm0b = 127 + 50;
STATE_TRANSITION_TEST if ( --CountdownTimer == 0 ) NextState = 4;
STATE_EXIT_ACTION
STATE_END
case 4:
STATE_ENTRY_ACTION CountdownTimer = 38*10;
pwm0a = 127 + 60; pwm0b = 127 + 60;
STATE_TRANSITION_TEST if ( --CountdownTimer == 0 ) NextState = 5;
STATE_EXIT_ACTION
STATE_END
case 5:
STATE_ENTRY_ACTION CountdownTimer = 38*10;
pwm0a = 127 - 70; pwm0b = 127 - 70;
STATE_TRANSITION_TEST if ( --CountdownTimer == 0 ) NextState = 6;
STATE_EXIT_ACTION
STATE_END
case 6:
STATE_ENTRY_ACTION CountdownTimer = 38*10;
pwm0a = 127 + 80; pwm0b = 127 + 80;
STATE_TRANSITION_TEST if ( --CountdownTimer == 0 ) NextState = 7;
STATE_EXIT_ACTION
STATE_END
case 7:
STATE_ENTRY_ACTION CountdownTimer = 38*10;
pwm0a = 127 + 90; pwm0b = 127 + 90;
STATE_TRANSITION_TEST if ( --CountdownTimer == 0 ) NextState = 8;
STATE_EXIT_ACTION
STATE_END
case 8: // Stop the motors. pwm0a = 127; pwm0b = 127;
文章评论(0条评论)
登录后参与讨论