搜索
bottom↓
回复: 65

状态机图的entry\do\exit的简单实现方法

  [复制链接]

出0入0汤圆

发表于 2011-1-18 20:48:37 | 显示全部楼层 |阅读模式
最近在把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周年了!感谢大家的支持与爱护!!

如果想吃一顿饺子,就得从冰箱里取出肉,剁馅儿,倒面粉、揉面、醒面,擀成皮儿,下锅……
一整个繁琐流程,就是为了出锅时那一嘴滚烫流油的热饺子。

如果这个过程,禁不住饿,零食下肚了,饺子出锅时也就不香了……《非诚勿扰3》

出0入0汤圆

发表于 2011-2-9 10:29:57 | 显示全部楼层
好思想 mark!

出0入0汤圆

发表于 2011-3-2 17:46:51 | 显示全部楼层
有意思

出0入0汤圆

发表于 2011-3-2 20:18:30 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-3-2 21:59:31 | 显示全部楼层
状态机是个好东西,努力向各位大虾学习。

出0入0汤圆

发表于 2011-3-3 09:12:59 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-3-3 13:38:12 | 显示全部楼层
努力学习状态机。

出0入0汤圆

发表于 2011-3-5 18:58:22 | 显示全部楼层
学习了。

出0入0汤圆

发表于 2011-3-5 19:17:39 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-3-5 21:59:02 | 显示全部楼层
MARK 状态机 简单实现方法

出0入0汤圆

发表于 2011-3-5 23:15:26 | 显示全部楼层
.

出0入0汤圆

发表于 2011-3-5 23:15:42 | 显示全部楼层
手机党mark

出0入0汤圆

发表于 2011-3-5 23:21:48 | 显示全部楼层
好的留个记号

出0入0汤圆

发表于 2011-3-7 13:37:20 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-3-8 15:50:32 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-3-19 17:07:49 | 显示全部楼层
收藏 之

出0入0汤圆

发表于 2011-3-19 17:29:52 | 显示全部楼层
收藏 之

出0入0汤圆

发表于 2011-3-19 19:24:54 | 显示全部楼层
好东西。谢谢

出0入0汤圆

发表于 2011-3-19 22:27:01 | 显示全部楼层
努力学习 MARK

出0入0汤圆

发表于 2011-5-18 19:45:16 | 显示全部楼层
mark!

出0入0汤圆

发表于 2011-5-18 21:13:14 | 显示全部楼层
学习学习!!

出0入0汤圆

发表于 2011-5-19 10:48:21 | 显示全部楼层
不是很明白

出0入147汤圆

发表于 2011-5-19 11:09:03 | 显示全部楼层
MARK

出0入0汤圆

发表于 2011-5-19 13:12:31 | 显示全部楼层
感觉这样的状态机可读性很不好。。。。。

出0入0汤圆

发表于 2011-5-21 23:06:00 | 显示全部楼层
顶上去,好东西

出0入4汤圆

发表于 2011-5-21 23:09:40 | 显示全部楼层
M

出0入0汤圆

发表于 2011-6-4 15:26:16 | 显示全部楼层
记号

出0入143汤圆

发表于 2011-11-6 17:16:54 | 显示全部楼层
记号,继续学习中

出0入0汤圆

发表于 2011-11-6 17:33:51 | 显示全部楼层
学习了.谢谢!

出0入0汤圆

发表于 2011-11-6 17:43:54 | 显示全部楼层
学习了.谢谢!

出0入0汤圆

发表于 2011-11-8 09:00:21 | 显示全部楼层
mark ,收藏了!

出0入0汤圆

发表于 2011-11-8 17:37:11 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-11-9 16:58:04 | 显示全部楼层
mark,路过

出0入0汤圆

发表于 2011-11-21 09:00:01 | 显示全部楼层
Mark

出0入0汤圆

发表于 2011-11-21 20:12:09 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-12-27 09:13:27 | 显示全部楼层
mark,状态机简写

出0入0汤圆

发表于 2011-12-27 09:20:06 | 显示全部楼层
mark

出0入0汤圆

发表于 2012-2-2 09:13:14 | 显示全部楼层
MARK 状态机

出0入0汤圆

发表于 2012-2-2 13:21:44 | 显示全部楼层
这个真不错

出0入0汤圆

发表于 2012-2-2 20:23:38 | 显示全部楼层
学习了

出0入0汤圆

发表于 2012-2-27 17:22:21 | 显示全部楼层
mark

出0入0汤圆

发表于 2012-2-27 17:50:46 | 显示全部楼层
mark

出0入0汤圆

发表于 2012-4-16 08:07:56 来自手机 | 显示全部楼层
状态机编程是个好的程序实现框架。

出0入0汤圆

发表于 2012-6-1 18:36:04 | 显示全部楼层
mark 一直以来的问题初解决了

出0入0汤圆

发表于 2012-6-1 19:45:34 | 显示全部楼层
收藏,有时间研究研究

出0入0汤圆

发表于 2012-6-2 05:11:00 | 显示全部楼层
很好的思路

出0入0汤圆

发表于 2012-6-3 11:22:04 | 显示全部楼层
看的有点不明白啊,哪位大侠讲一下这个宏定义,

出0入0汤圆

发表于 2012-9-7 15:53:09 | 显示全部楼层
状态机学习!

出0入0汤圆

发表于 2012-9-7 16:07:14 | 显示全部楼层
量子编程,已经有一套成熟的实现。

出0入0汤圆

发表于 2012-11-29 11:24:16 | 显示全部楼层
mark ,收藏了!

出0入0汤圆

发表于 2012-11-29 20:36:17 | 显示全部楼层
我喜欢状态机,标记一下,还会在回来看的

出0入0汤圆

发表于 2014-1-3 17:48:51 | 显示全部楼层
正在学习这方面的东西。Mark

出0入17汤圆

发表于 2014-1-3 22:32:25 | 显示全部楼层

好的留个记号

出0入0汤圆

发表于 2014-1-17 10:19:22 | 显示全部楼层
先记住                             

出0入0汤圆

发表于 2014-1-17 13:19:58 | 显示全部楼层
MARK 状态机 简单实现方法

出0入0汤圆

发表于 2014-1-17 13:36:53 | 显示全部楼层
有用,学习。状态机很好很强大!

出0入0汤圆

发表于 2014-11-4 17:53:09 | 显示全部楼层
学习学习,
mark

出0入0汤圆

发表于 2014-11-4 19:12:33 | 显示全部楼层

状态机编程是个好的程序实现框架

出0入0汤圆

发表于 2014-11-4 20:40:02 | 显示全部楼层
非常谢谢,参考一下

出0入0汤圆

发表于 2014-12-30 13:27:54 | 显示全部楼层
哈哈,好东西被我挖出来了

出0入0汤圆

发表于 2016-5-13 12:46:41 | 显示全部楼层
厉害。好东西 正好用上

出0入0汤圆

发表于 2016-5-13 13:17:24 | 显示全部楼层
学习一下!

出0入0汤圆

发表于 2018-4-28 11:19:19 | 显示全部楼层
学习了,学习分享

出0入0汤圆

发表于 2018-4-28 18:29:03 | 显示全部楼层
想法和好

出0入0汤圆

发表于 2018-12-17 20:02:41 | 显示全部楼层
多谢分享!

出0入0汤圆

发表于 2019-1-21 04:11:14 | 显示全部楼层
mark 状态机
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-3-28 21:57

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表