-
Notifications
You must be signed in to change notification settings - Fork 26
/
CentrifugeTest.c
207 lines (178 loc) · 6.42 KB
/
CentrifugeTest.c
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "CentrifugeTest.h"
#include "StateMachine.h"
#include <stdio.h>
// CentrifugeTest object structure
typedef struct
{
INT speed;
BOOL pollActive;
} CentrifugeTest;
// Define private instance of motor state machine
CentrifugeTest centrifugeTestObj;
SM_DEFINE(CentrifugeTestSM, ¢rifugeTestObj)
// State enumeration order must match the order of state
// method entries in the state map
enum States
{
ST_IDLE,
ST_COMPLETED,
ST_FAILED,
ST_START_TEST,
ST_ACCELERATION,
ST_WAIT_FOR_ACCELERATION,
ST_DECELERATION,
ST_WAIT_FOR_DECELERATION,
ST_MAX_STATES
};
// State machine state functions
STATE_DECLARE(Idle, NoEventData)
ENTRY_DECLARE(Idle, NoEventData)
STATE_DECLARE(Completed, NoEventData)
STATE_DECLARE(Failed, NoEventData)
STATE_DECLARE(StartTest, NoEventData)
GUARD_DECLARE(StartTest, NoEventData)
STATE_DECLARE(Acceleration, NoEventData)
STATE_DECLARE(WaitForAcceleration, NoEventData)
EXIT_DECLARE(WaitForAcceleration)
STATE_DECLARE(Deceleration, NoEventData)
STATE_DECLARE(WaitForDeceleration, NoEventData)
EXIT_DECLARE(WaitForDeceleration)
// State map to define state function order
BEGIN_STATE_MAP_EX(CentrifugeTest)
STATE_MAP_ENTRY_ALL_EX(ST_Idle, 0, EN_Idle, 0)
STATE_MAP_ENTRY_EX(ST_Completed)
STATE_MAP_ENTRY_EX(ST_Failed)
STATE_MAP_ENTRY_ALL_EX(ST_StartTest, GD_StartTest, 0, 0)
STATE_MAP_ENTRY_EX(ST_Acceleration)
STATE_MAP_ENTRY_ALL_EX(ST_WaitForAcceleration, 0, 0, EX_WaitForAcceleration)
STATE_MAP_ENTRY_EX(ST_Deceleration)
STATE_MAP_ENTRY_ALL_EX(ST_WaitForDeceleration, 0, 0, EX_WaitForDeceleration)
END_STATE_MAP_EX(CentrifugeTest)
EVENT_DEFINE(CFG_Start, NoEventData)
{
BEGIN_TRANSITION_MAP // - Current State -
TRANSITION_MAP_ENTRY(ST_START_TEST) // ST_IDLE
TRANSITION_MAP_ENTRY(CANNOT_HAPPEN) // ST_COMPLETED
TRANSITION_MAP_ENTRY(CANNOT_HAPPEN) // ST_FAILED
TRANSITION_MAP_ENTRY(EVENT_IGNORED) // ST_START_TEST
TRANSITION_MAP_ENTRY(EVENT_IGNORED) // ST_ACCELERATION
TRANSITION_MAP_ENTRY(EVENT_IGNORED) // ST_WAIT_FOR_ACCELERATION
TRANSITION_MAP_ENTRY(EVENT_IGNORED) // ST_DECELERATION
TRANSITION_MAP_ENTRY(EVENT_IGNORED) // ST_WAIT_FOR_DECELERATION
END_TRANSITION_MAP(CentrifugeTest, pEventData)
}
EVENT_DEFINE(CFG_Cancel, NoEventData)
{
BEGIN_TRANSITION_MAP // - Current State -
TRANSITION_MAP_ENTRY(EVENT_IGNORED) // ST_IDLE
TRANSITION_MAP_ENTRY(CANNOT_HAPPEN) // ST_COMPLETED
TRANSITION_MAP_ENTRY(CANNOT_HAPPEN) // ST_FAILED
TRANSITION_MAP_ENTRY(ST_FAILED) // ST_START_TEST
TRANSITION_MAP_ENTRY(ST_FAILED) // ST_ACCELERATION
TRANSITION_MAP_ENTRY(ST_FAILED) // ST_WAIT_FOR_ACCELERATION
TRANSITION_MAP_ENTRY(ST_FAILED) // ST_DECELERATION
TRANSITION_MAP_ENTRY(ST_FAILED) // ST_WAIT_FOR_DECELERATION
END_TRANSITION_MAP(CentrifugeTest, pEventData)
}
EVENT_DEFINE(CFG_Poll, NoEventData)
{
BEGIN_TRANSITION_MAP // - Current State -
TRANSITION_MAP_ENTRY(EVENT_IGNORED) // ST_IDLE
TRANSITION_MAP_ENTRY(EVENT_IGNORED) // ST_COMPLETED
TRANSITION_MAP_ENTRY(EVENT_IGNORED) // ST_FAILED
TRANSITION_MAP_ENTRY(EVENT_IGNORED) // ST_START_TEST
TRANSITION_MAP_ENTRY(ST_WAIT_FOR_ACCELERATION) // ST_ACCELERATION
TRANSITION_MAP_ENTRY(ST_WAIT_FOR_ACCELERATION) // ST_WAIT_FOR_ACCELERATION
TRANSITION_MAP_ENTRY(ST_WAIT_FOR_DECELERATION) // ST_DECELERATION
TRANSITION_MAP_ENTRY(ST_WAIT_FOR_DECELERATION) // ST_WAIT_FOR_DECELERATION
END_TRANSITION_MAP(CentrifugeTest, pEventData)
}
static void StartPoll(void)
{
centrifugeTestObj.pollActive = TRUE;
}
static void StopPoll(void)
{
centrifugeTestObj.pollActive = FALSE;
}
BOOL CFG_IsPollActive(void)
{
return centrifugeTestObj.pollActive;
}
STATE_DEFINE(Idle, NoEventData)
{
printf("%s ST_Idle\n", self->name);
}
ENTRY_DEFINE(Idle, NoEventData)
{
printf("%s EN_Idle\n", self->name);
centrifugeTestObj.speed = 0;
StopPoll();
}
STATE_DEFINE(Completed, NoEventData)
{
printf("%s ST_Completed\n", self->name);
SM_InternalEvent(ST_IDLE, NULL);
}
STATE_DEFINE(Failed, NoEventData)
{
printf("%s ST_Failed\n", self->name);
SM_InternalEvent(ST_IDLE, NULL);
}
// Start the centrifuge test state.
STATE_DEFINE(StartTest, NoEventData)
{
printf("%s ST_StartTest\n", self->name);
SM_InternalEvent(ST_ACCELERATION, NULL);
}
// Guard condition to determine whether StartTest state is executed.
GUARD_DEFINE(StartTest, NoEventData)
{
printf("%s GD_StartTest\n", self->name);
if (centrifugeTestObj.speed == 0)
return TRUE; // Centrifuge stopped. OK to start test.
else
return FALSE; // Centrifuge spinning. Can't start test.
}
// Start accelerating the centrifuge.
STATE_DEFINE(Acceleration, NoEventData)
{
printf("%s ST_Acceleration\n", self->name);
// Start polling while waiting for centrifuge to ramp up to speed
StartPoll();
}
// Wait in this state until target centrifuge speed is reached.
STATE_DEFINE(WaitForAcceleration, NoEventData)
{
printf("%s ST_WaitForAcceleration : Speed is %d\n", self->name, centrifugeTestObj.speed);
if (++centrifugeTestObj.speed >= 5)
SM_InternalEvent(ST_DECELERATION, NULL);
}
// Exit action when WaitForAcceleration state exits.
EXIT_DEFINE(WaitForAcceleration)
{
printf("%s EX_WaitForAcceleration\n", self->name);
// Acceleration over, stop polling
StopPoll();
}
// Start decelerating the centrifuge.
STATE_DEFINE(Deceleration, NoEventData)
{
printf("%s ST_Deceleration\n", self->name);
// Start polling while waiting for centrifuge to ramp down to 0
StartPoll();
}
// Wait in this state until centrifuge speed is 0.
STATE_DEFINE(WaitForDeceleration, NoEventData)
{
printf("%s ST_WaitForDeceleration : Speed is %d\n", self->name, centrifugeTestObj.speed);
if (centrifugeTestObj.speed-- == 0)
SM_InternalEvent(ST_COMPLETED, NULL);
}
// Exit action when WaitForDeceleration state exits.
EXIT_DEFINE(WaitForDeceleration)
{
printf("%s EX_WaitForDeceleration\n", self->name);
// Deceleration over, stop polling
StopPoll();
}