|
最近在把sysml的状态机图写成C代码的时候特意学习了这方面的内容,感觉这个利用宏定义的方法比较简单有效,适合实现轻量级的状态机。在FPGA和ucos下写个了测试任务跑了,很好用。
简单的说是利用一组宏定义:
#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;
然后写状态机的时候就可以像写清单一样简单,比如后面附的那个电机控制程序:
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
带EXCLUSIVE的宏的额外作用是限制每个周期的仅进行一个类型的行为,比如进行了entry类的,就不再执行do类的。
如果大家还有更好的方法,贴出来讨论讨论,少走点弯路。
下面是原文:
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
//
// State machine helper macros.
#define STATE_ENTRY_ACTION if ( CurrentState != PreviousState ) { PreviousState = CurrentState;
#define STATE_TRANSITION_TEST } if ( NextState == CurrentState ) {
#define STATE_TRANSITION_TEST_EXCLUSIVE } else if ( NextState == CurrentState ) {
#define STATE_EXIT_ACTION } if ( NextState != CurrentState ) { CurrentState = NextState;
#define STATE_EXIT_ACTION_EXCLUSIVE } else if ( NextState != CurrentState ) { CurrentState = NextState;
#define STATE_END } break;
// ----------------------------------------------------------------------------
// 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;
// Stay here and do nothing.
break;
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|