我是靠谱客的博主 难过手机,这篇文章主要介绍基于真值表的有限状态机实现,现在分享给大家,希望可以做个参考。

在ECU的处理逻辑中往往摆脱不了状态机,为了使状态编程清晰可靠,可以将状态跳转条件组成真值表,将状态跳转用模型实现,一方面减少逻辑的复杂度,另一方面可以将代码变得更清晰减少出错。以下是我的思路,希望能给大家提供一些帮助,具体实现如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
static volatile Uint32 fsm_tmr[STATE_CNT+1] = {0}; void fsm_timer_init(void) { MemSet((Uint16 *)fsm_tmr, 0, sizeof(fsm_tmr)); } void fsm_set_timer(Uint16 index, Uint32 tout) { fsm_tmr[index] = tout; } Uint32 fsm_get_timer(Uint16 index) { return fsm_tmr[index]; } Uint16 fsm_chk_timer(Uint16 index) { return (!fsm_tmr[index]); } void fsm_1ms_irq(void) { Uint16 j; for(j = 0; j < STATE_CNT+1; j++) { if(fsm_tmr[j]) { fsm_tmr[j]--; } } } typedef enum { STARTUP, INIT, STANDBY, CHARG, SHUTDOWN, STATE_CNT }FSM_STATE; Uint16 jump_matrix[STARTUP][INIT]; Uint16 fsm_state_min_time[FSM_STATE] = {200, 200, 200, 200, 200}; #define MATRIX_INIT(cond,dly) (((!!(cond))<<15) | (dly)) void update_matrix(void) { jump_matrix[STARTUP][INIT] = MATRIX_INIT(1,0); jump_matrix[INIT][STANDBY] = MATRIX_INIT(1,0); jump_matrix[INIT][SHUTDOWN] = MATRIX_INIT(1,0); } typedef struct { (void)(*entry_ev)(void); (void)(*during_ev)(void); (void)(*exit_ev)(void); }FSM_EV; FSM_EV ev_list[STATE_CNT] = { .[INIT] = {}, .[STANDBY] = {} }; typedef struct { FSM_STATE state; Uint16 (*matrix)[STATE_CNT][STATE_CNT]; FSM_EV *event; (void)(*state_change_notification)(FSM_STATE, FSM_STATE); }FSM_INS; FSM_INS fsm; void fsm_state_change_notification(FSM_STATE prev_state, FSM_STATE curr_state) { } void fsm_init(void) { fsm.state = STARTUP; fsm.matrix = jump_matrix; fsm.event = ev_list; fsm.state_change_notification = fsm_state_change_notification; } void fsm_run(void) { Uint16 i; update_matrix(); (fsm.event[fsm.state]->state_ev)(); for(i = 0; i < STATE_CNT; i++) { //lookup table if(fsm.matrx[fsm.state][i] & 0x8000) { if(fsm_chk_timer(i) && fsm_chk_timer(STATE_CNT)) { //(fsm.event[fsm.state]->exit_ev)(); (fsm.event[fsm.state]->state_change_notification)(fsm.state, i); fsm.state = i; fsm_timer_init(); fsm_set_timer(STATE_CNT, fsm_state_min_time[fsm.state]*MS_T); //(fsm.event_list[fsm.state]->entry_ev)(); //(fsm.event_list[fsm.state]->during_ev)(); break; } } else { fsm_set_timer(i, fsm.matrx[fsm.state][i]&0x7FFF); } } }

 

最后

以上就是难过手机最近收集整理的关于基于真值表的有限状态机实现的全部内容,更多相关基于真值表内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部