-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrolfunctions.c
201 lines (184 loc) · 5.6 KB
/
controlfunctions.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
#include "controlfunctions.h"
//the function that initially opens the door
//should set the door open flag to 1
//should set the initialDoorWaitOverFlag to 1
//this portion of the code may actually be done in the part that
//manages opening and closing the door
//will have to be discussed in-person or in group later on
void openDoorRoutine(struct ElevatorData *ed){
if(ed->currentFloor == ed->nextFloor){
int theTime = time(NULL);
ed->lastIRTime = theTime;
ed->doorFlag = 1;
ed->doorOpenFlag = 1;
ed->initialDoorWaitOverFlag = 1;
}
else{
logString("Tried to open doors with moving elevator", LOG_LEVEL_ERROR);
}
//do the function that "opens the door" here
}
//run by openDoorRoutine to close the door after it is complete
void closeDoor(struct ElevatorData *ed){
ed->doorOpenFlag = 0;
}
void floorLightsManager(struct ElevatorData *ed, pthread_mutex_t *mutex, int requestNumber, int toggle){
//if toggle is 0
//lights are probably being turne don
//if toggle is 1
//the door is opening
//and lights are probably being turned off
}
int turnRequestNumberIntofloor(int requestNumber){
if((requestNumber == 0) || (requestNumber == 3)) {
return 1;
}
if ((requestNumber == 2) || (requestNumber == 6)){
return 3;
}
if(requestNumber == 1){
return 2;
}
//two down
if (requestNumber == 4){
return 4;
}
//two up
if (requestNumber == 5){
return 5;
}
return -1;
}
void floorQueueManager(struct ElevatorData *ed, pthread_mutex_t *mutex, int requestNumber){
//ISSUES AND bugs
//WHEN THE CURRENTFLOOR IS THE SAME AS THE NEXT FLOOR DO NOT PUT NEXTFLOOR ON THE QUEUE (fixed, I think this was only happening in specific cases which are now addressed)
//function will now be called through button interrupt ONLY now when the button press is not on the same floor as the elevator.
//when moving between one and two a request to one presently works well, but this may be due to the first bug listed
//
printf("%s%d\n", "JUST GOT ", requestNumber);
int realRequest = turnRequestNumberIntofloor(requestNumber);
pthread_mutex_lock(mutex);
//turn lights on for the floor
//add the pressed buttons into the array of pressed buttons
floorLightsManager(ed, mutex, requestNumber, 0);
int temp;
//if the request is not the floor the elevator is on or moving away from
if(realRequest != -1){
//if the current floor is 2
if(ed->currentFloor == 2 ) {
// push request to front of queue, regardless of what it is
if(realRequest == 3)
{
if(ed->nextFloor == 1){
enqueueFloor(ed, realRequest);
}
else {
temp = ed->nextFloor;
ed->nextFloor = realRequest;
enqueueFloorToFront(ed,temp);
}
}
if(realRequest == 1)
{
if(ed->nextFloor == 3){
enqueueFloor(ed, realRequest);
}
else {
temp = ed->nextFloor;
ed->nextFloor = realRequest;
enqueueFloorToFront(ed,temp);
}
}
else if (realRequest == 4)
{
temp = ed->nextFloor;
ed->nextFloor = 1;
enqueueFloorToFront(ed,temp);
}
else if (realRequest == 5)
{
temp = ed->nextFloor;
ed->nextFloor = 3;
enqueueFloorToFront(ed,temp);
}
printFullQueue(ed);
pthread_mutex_unlock(mutex);
return;
}
if(ed->currentFloor == 3) {
if((realRequest == 2) || (realRequest == 4) || (realRequest == 5)){
// if request is for second floor, add it to front of queue
temp = ed->nextFloor;
ed->nextFloor = 2;
enqueueFloorToFront(ed,temp);
}
if((realRequest == 1) && (ed->nextFloor != 2)) {
//if request is for first floor and we don't need to go to 2, push 1 to front of queue
temp = ed->nextFloor;
ed->nextFloor = 1;
enqueueFloorToFront(ed,temp);
}
if((realRequest == 1) && (ed->nextFloor == 2)) {
// if request is for floor 1, and we need to stop at to 2, push 1 to back of queue
enqueueFloor(ed, realRequest);
}
printFullQueue(ed);
pthread_mutex_unlock(mutex);
return;
}
if(ed->currentFloor == 1) {
if((realRequest == 2) || (realRequest == 4) || (realRequest == 5)){
// if request is for the second floor, add it to front of queue
temp = ed->nextFloor;
ed->nextFloor = 2;
enqueueFloorToFront(ed,temp);
}
if((realRequest == 3) && (ed->nextFloor != 2)) {
//if request is for floor 3, and we don't have to stop at 2, add 3 to front of queue
temp = ed->nextFloor;
ed->nextFloor = 3;
enqueueFloorToFront(ed,temp);
}
if((realRequest == 3) && (ed->nextFloor == 2)) {
//if request is for floor 3, and we have to stop at 2, add 3 to back of queue
enqueueFloor(ed, requestNumber);
}
printFullQueue(ed);
pthread_mutex_unlock(mutex);
return;
}
}
if((ed->currentFloor != 1) && (getQueueSize(ed) == 0)){
//if the queue is empty, the elevator should return to floor 1
enqueueFloor(ed, 1);
}
printFullQueue(ed);
pthread_mutex_unlock(mutex);
}
int toggleMotorUp(int speed){
int toggleMotorDown(int speed){
if(speed > 0 && speed < 1024){
//digitalWrite(GIPO_PIN_MOTOR_UP, 1);
//pwmWrite(GIPO_PIN_MOTOR_PWM, speed);
return 0;
}
else{
return -1;
}
}
}
int toggleMotorDown(int speed){
if(speed > 0 && speed < 1024){
//digitalWrite(GIPO_PIN_MOTOR_DOWN, 1);
//pwmWrite(GIPO_PIN_MOTOR_PWM, speed);
return 0;
}
else{
return -1;
}
}
void toggleMotorOff(){
//digitalWrite(GIPO_PIN_MOTOR_UP, 0);
//digitalWrite(GIPO_PIN_MOTOR_DOWN, 0);
//pwmWrite(GIPO_PIN_MOTOR_PWM, 0);
}