-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelev_fsm.c
executable file
·289 lines (270 loc) · 8.38 KB
/
elev_fsm.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <stdio.h>
#include "elev.h"
#include "elev_fsm.h"
#include "timer.h"
#include "elevOrderData.h"
#include "elevPositionData.h"
static state_t state;
state_t fsm_getCurrentState(void){
return state;
}
void fsm_elevInit(void){
printf("Entered elevator init function\n");
setCurrentDirection(DIRN_UP);
int floorSignal = elev_get_floor_sensor_signal();
printf("Init floorSignal = %d \n",floorSignal);
if (floorSignal == -1){ // If between floors
printf("Starting motor\n");
printf("getCurrentDirection returns %d \n",getCurrentDirection());
elev_set_motor_direction(getCurrentDirection());
//Stop when a floor is reached
while(floorSignal == -1){
floorSignal = elev_get_floor_sensor_signal();
if(floorSignal != -1){
printf("Stopping motor\n");
elev_set_motor_direction(DIRN_STOP);
}
}
}
setCurrentPosition(floorSignal);
elev_set_floor_indicator(floorSignal);
setLastFloorVisited(floorSignal);
state = STATE_AT_FLOOR;
printf("Entering state STATE_AT_FLOOR (ref:1)\n");
return;
}
void evStopButtonPressed(){
elev_set_motor_direction(DIRN_STOP);
if(elev_get_floor_sensor_signal() != -1){
elev_set_door_open_lamp(1);
}
deactivateAndDeleteOrders();
state = STATE_STOP_BUTTON_PRESSED;
printf("Entering state STATE_STOP_BUTTON_PRESSED\n");
}
void evStopButtonReleasedAtFloor(){
activateOrdering();
//+sjekk timer og lukk dør hvis den har vært åpen i over 3 sekunder
elev_set_door_open_lamp(0);
state = STATE_AT_FLOOR;
printf("Entering state STATE_AT_FLOOR (ref:2)\n");
}
void evStopButtonReleasedBetweenFloors(){
activateOrdering();
state = STATE_STOP_BUTTON_RELEASED_BETWEEN_FLOORS;
printf("Entering state STATE_STOP_BUTTON_RELEASED_BETWEEN_FLOORS\n");
}
void evAtFloor(){
direction_t dir = getCurrentDirection();
position_t floor = getCurrentPosition(); //Will not be between floors at this point
elev_set_floor_indicator(floor);
//Any request to get off on this floor?
if(getElevPanelFlag(floor)){
printf("Request to get off on this floor detected\n");
//stop
elev_set_motor_direction(DIRN_STOP);
printf(" Stopping motor\n");
//open door
elev_set_door_open_lamp(1);
printf(" Opening doors\n");
//start timer
timer_start();
//delete orders for this floor
deleteFloorOrders(floor);
state = STATE_ELEVATOR_OPEN;
printf(" Entering state STATE_ELEVATOR_OPEN (ref:1)\n");
return;
}
//Any request to get on in current direction?
if(getFloorPanelFlag(floor,dir)){
printf("Request to get on in current direction detected\n");
//stop
elev_set_motor_direction(DIRN_STOP);
printf(" Stopping motor\n");
//open door
elev_set_door_open_lamp(1);
printf(" Opening doors\n");
//start timer
timer_start();
//delete orders for this floor
deleteFloorOrders(floor);
state = STATE_ELEVATOR_OPEN;
printf(" Entering state STATE_ELEVATOR_OPEN (ref:2)\n");
return;
}
//Any requests ahead?
if(dir == DIRN_DOWN){
for (int i = floor-1; i >= 0; i--){
if((getElevPanelFlag(i) || getFloorPanelFlag(i,DIRN_UP) || getFloorPanelFlag(i,DIRN_DOWN)) && state != STATE_CONTINUE_MOVING){
printf("Request ahead detected\n");
elev_set_motor_direction(dir);
printf(" Starting motor\n");
setLastFloorVisited(getCurrentPosition());
state = STATE_CONTINUE_MOVING;
printf(" Entering state STATE_CONTINUE_MOVING (ref:1)\n");
return;
}
}
}
else{
for (int i = floor+1; i < N_FLOORS; i++){
if((getElevPanelFlag(i) || getFloorPanelFlag(i,DIRN_UP) || getFloorPanelFlag(i,DIRN_DOWN)) && state != STATE_CONTINUE_MOVING){
printf("Request ahead detected\n");
elev_set_motor_direction(dir);
printf(" Starting motor\n");
setLastFloorVisited(getCurrentPosition());
state = STATE_CONTINUE_MOVING;
printf(" Entering state STATE_CONTINUE_MOVING (ref:2)\n");
return;
}
}
}
//Any request to get on in opposite direction on current floor?
if(dir == DIRN_DOWN){
if(getFloorPanelFlag(floor, DIRN_UP)){
printf("Request to get on in opposite direction on current floor detected\n");
invertCurrentDirection();
dir = getCurrentDirection();
//Request to get on in current direction on this floor
if(getFloorPanelFlag(floor,dir)){
//stop
elev_set_motor_direction(DIRN_STOP);
printf(" Stopping motor\n");
//open door
elev_set_door_open_lamp(1);
printf(" Opening doors\n");
//start timer
timer_start();
//delete orders for this floor
deleteFloorOrders(floor);
state = STATE_ELEVATOR_OPEN;
printf(" Entering state STATE_ELEVATOR_OPEN (ref:3)\n");
return;
}
}
}
if(dir == DIRN_UP){
if(getFloorPanelFlag(floor, DIRN_DOWN)){
printf("Request to get on in opposite direction on current floor detected\n");
invertCurrentDirection();
dir = getCurrentDirection();
//Request to get on in current direction on this floor
if(getFloorPanelFlag(floor,dir)){
//stop
elev_set_motor_direction(DIRN_STOP);
printf(" Stopping motor\n");
//open door
elev_set_door_open_lamp(1);
printf(" Opening doors\n");
//start timer
timer_start();
//delete orders for this floor
deleteFloorOrders(floor);
state = STATE_ELEVATOR_OPEN;
printf("Entering state STATE_ELEVATOR_OPEN (ref:4)\n");
return;
}
}
}
//Any requests behind?
if(dir == DIRN_DOWN){
for(int i = floor+1; i < N_FLOORS; i++){
if(getElevPanelFlag(i) || getFloorPanelFlag(i,DIRN_UP) || getFloorPanelFlag(i,DIRN_DOWN)){
printf("Requests behind detected\n");
invertCurrentDirection();
printf(" Direction inverted\n");
dir = getCurrentDirection();
elev_set_motor_direction(dir);
printf(" starting motor");
setLastFloorVisited(getCurrentPosition());
state = STATE_CONTINUE_MOVING;
printf(" Entering state STATE_CONTINUE_MOVING (ref:3)\n");
return;
}
}
}
if(dir == DIRN_UP){
for(int i = floor-1; i >= 0; i--){
if(getElevPanelFlag(i) || getFloorPanelFlag(i,DIRN_UP) || getFloorPanelFlag(i,DIRN_DOWN)){
printf("Requests behind detected\n");
invertCurrentDirection();
printf(" Direction inverted\n");
dir = getCurrentDirection();
elev_set_motor_direction(dir);
printf(" starting motor");
setLastFloorVisited(getCurrentPosition());
state = STATE_CONTINUE_MOVING;
printf(" Entering state STATE_CONTINUE_MOVING (ref:4)\n");
return;
}
}
}
}
void evStoppedBetweenFloorFlagSet(){
position_t pos = getCurrentPosition(); //will be between floors
direction_t dir = getCurrentDirection();
unsigned int upperLimitFloorsBelow;
unsigned int lowerLimitFloorsAbove;
switch(pos){
case(FIRST_SECOND):
upperLimitFloorsBelow = 0;
lowerLimitFloorsAbove = 1;
case(SECOND_THIRD):
upperLimitFloorsBelow = 1;
lowerLimitFloorsAbove = 2;
case(THIRD_FOURTH):
upperLimitFloorsBelow = 3;
lowerLimitFloorsAbove = 4;
}
if(dir == DIRN_UP){
for (int i = lowerLimitFloorsAbove; i < N_FLOORS; i++){
if(getElevPanelFlag(i) || getFloorPanelFlag(i,DIRN_UP) || getFloorPanelFlag(i,DIRN_DOWN)){
elev_set_motor_direction(dir);
setLastFloorVisited(getCurrentPosition());
state = STATE_CONTINUE_MOVING;
printf("Entering state STATE_CONTINUE_MOVING (ref:5)\n");
return;
}
}
for (int i = 0; i <= upperLimitFloorsBelow; i++){
if(getElevPanelFlag(i) || getFloorPanelFlag(i,DIRN_UP) || getFloorPanelFlag(i,DIRN_DOWN)){
invertCurrentDirection();
elev_set_motor_direction(dir);
setLastFloorVisited(getCurrentPosition());
state = STATE_CONTINUE_MOVING;
printf("Entering state STATE_CONTINUE_MOVING (ref:6)\n");
return;
}
}
}
else{
for (int i = 0; i <= upperLimitFloorsBelow; i++){
if(getElevPanelFlag(i) || getFloorPanelFlag(i,DIRN_UP) || getFloorPanelFlag(i,DIRN_DOWN)){
elev_set_motor_direction(dir);
setLastFloorVisited(getCurrentPosition());
state = STATE_CONTINUE_MOVING;
printf("Entering state STATE_CONTINUE_MOVING (ref:7)\n");
return;
}
}
for (int i = lowerLimitFloorsAbove; i < N_FLOORS; i++){
if(getElevPanelFlag(i) || getFloorPanelFlag(i,DIRN_UP) || getFloorPanelFlag(i,DIRN_DOWN)){
invertCurrentDirection();
elev_set_motor_direction(dir);
setLastFloorVisited(getCurrentPosition());
state = STATE_CONTINUE_MOVING;
printf("Entering state STATE_CONTINUE_MOVING (ref:8)\n");
return;
}
}
}
}
void fsm_evTimeOut(){
if(timer_isTimeOut()){
state = STATE_AT_FLOOR;
printf("Entering state STATE_AT_FLOOR after timeout\n");
elev_set_door_open_lamp(0);
timer_stop();
return;
}
}