-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
153 lines (137 loc) · 4.63 KB
/
main.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
#include "elev.h"
#include "control.h"
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/time.h>
#include <signal.h>
typedef enum state {
UNDEFINED_STATE = 0, // Elevator is at an unknown floor
RUN = 1, // Elevator is moving between floors
STOP = 2, // Elevator is stopped at a floor
REST = 3, // Elevator is stopped and there are no orders
EMERGENCY_STOP = 4 // The emergency stop button is being held down
} state;
const int DELAY = 10000;
void timerHandler(int signum) {}
int main() {
// Initialize hardware
if (!elev_init()) {
printf("Unable to initialize elevator hardware!\n");
return 1;
}
// Initialize system variables
floor floors[N_FLOORS];
for (int i = FLOOR_1; i <= FLOOR_4; i++) {
clearOrders(floors, i);
}
// Create timer structs
struct sigaction sa;
struct itimerval timer;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = &timerHandler;
// Bind timerHandler to SIGALARM action
sigaction(SIGALRM, &sa, NULL);
// Setup timer intervals
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = DELAY;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = DELAY;
// Start timer
setitimer(ITIMER_REAL, &timer, NULL);
floor_num current = UNDEFINED;
elev_motor_direction_t dir = DIRN_UP;
elev_motor_direction_t oldDir = dir;
setMotorDir(dir);
state activeState = UNDEFINED_STATE;
int timeCount = 0;
const int THREE_SEC = 3000000 / DELAY;
while (1) {
// Main state system
switch(activeState) {
case RUN:
updateFloorStatus(floors);
updateLights(floors);
if (updateCurrentFloor(¤t)) {
updateFloorLight(current);
if (handleSensorUpdate(current, floors, dir)) {
activeState = STOP;
oldDir = dir;
dir = DIRN_STOP;
setMotorDir(dir);
setDoorOpen(true);
timeCount = 0;
}
}
break;
case STOP:
updateFloorStatus(floors);
updateLights(floors);
if (timeCount++ == THREE_SEC) {
if (doStartup(¤t, floors, &dir, oldDir, false)) {
activeState = RUN;
setMotorDir(dir);
} else {
activeState = REST;
}
setDoorOpen(false);
}
if (hasOrders(floors, current)) {
clearOrders(floors, current);
timeCount = 0;
}
break;
case REST:
updateFloorStatus(floors);
updateLights(floors);
if (doStartup(¤t, floors, &dir, oldDir, true)) {
activeState = RUN;
setMotorDir(dir);
}
if (hasOrders(floors, current)) {
clearOrders(floors, current);
activeState = STOP;
timeCount = 0;
setDoorOpen(true);
}
break;
case UNDEFINED_STATE:
if (updateCurrentFloor(¤t)) {
updateFloorLight(current);
activeState = REST;
dir = DIRN_STOP;
setMotorDir(DIRN_STOP);
}
break;
case EMERGENCY_STOP:
if (!getStopButton()) {
if (elev_get_floor_sensor_signal() != -1) {
setDoorOpen(true);
activeState = STOP;
timeCount = 0;
} else {
activeState = REST;
}
}
break;
}
// Perform stop button checks if system is not already in emergency stop
if (activeState != UNDEFINED_STATE && activeState != EMERGENCY_STOP && getStopButton()) {
if (activeState != RUN && elev_get_floor_sensor_signal() != -1) {
setDoorOpen(true);
} else if (dir != DIRN_STOP) {
oldDir = dir;
}
dir = DIRN_STOP;
setMotorDir(dir);
activeState = EMERGENCY_STOP;
for (int i = FLOOR_1; i <= FLOOR_4; i++) {
clearOrders(floors, i);
}
updateLights(floors);
}
// Wait for action, in this program this can only be SIGALARM
pause();
}
return 0;
}