-
Notifications
You must be signed in to change notification settings - Fork 0
/
swarmbot1.cpp
328 lines (259 loc) · 8.35 KB
/
swarmbot1.cpp
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* swarmbot1.cpp
*
* Simulating the actions of swarmbot 1
*/
#include "collision.h"
#include "communication.h"
#include "motor_control.h"
#include "photo_sensor.h"
#include "pinmap.h"
/* State variables */
static bool hitWall = false;
#define PUSH_CONST 300
void bot1(void);
/* Helper routines / functions */
inline void flashLed (int ledPin);
inline void actionUntilColor (Colors c, void (*action)(void));
inline bool followColorUntilColor (Colors c1, Colors c2);
void setup()
{
/* set up pins */
pinMode(CALIB_SWITCH, INPUT);
pinMode(BOT_SWITCH, INPUT);
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
Serial.begin(9600);
setupPhotosensor();
setupMotorControl();
setupCollision();
setupCommunication();
}
void loop() {
delay(1000);
isBot1 = digitalRead(BOT_SWITCH);
bot1();
digitalWrite(TRANSMIT_PIN, LOW);
while (1); /* stop forever */
}
void bot1(void)
{
/* Puts bot in motion (`forward()`) */
forward();
/* Loops until collision (boolean is set in ISR) */
while (!hitWall) {
for (int i(0); i < NUM_BUMPERS; ++i) {
if (digitalRead(bumpers[i])) {
hitWall = true;
}
}
delay(1);
}
Serial.println("Hit wall");
/* Moves backward until red and stops */
actionUntilColor(RED, backward);
turn(-80); /* negative to turn right */
actionUntilColor(RED, turnRight);
/* Flash a red LED */
flashLed(RED_LED); /* pauses for 1 second to flash */
/* Follows red until centre yellow */
while (!followColorUntilColor(RED, YELLOW)) {}
Serial.println("Found yellow");
forward();
delay(PUSH_CONST);
turn(-20);
stop();
/* Stops on yellow, flashing yellow LED twice */
flashLed(YELLOW_LED); /* pauses for 1 second to flash */
flashLed(YELLOW_LED); /* pauses for 1 second to flash */
/* experimentally necessary to avoid thick black line */
forward();
delay(75);
/* Follows yellow */
Serial.println("Following yellow until red");
while (!followColorUntilColor(YELLOW, RED)) {}
forward();
delay(PUSH_CONST);
stop();
/* Follows red */
Serial.println("Following red until yellow");
while (!followColorUntilColor(RED, YELLOW)) {}
/* Stops on yellow, turns on yellow LED, turns 180 degrees */
Serial.println("Turning until red");
stop();
digitalWrite(YELLOW_LED, HIGH);
turn(220); /* ensures we escape red and actually turn */
actionUntilColor(RED, turnLeft);
/* Follows red back to the middle */
while (!followColorUntilColor(RED, YELLOW)) {}
Serial.println("Found yellow and about to communicate");
forward();
delay(PUSH_CONST);
stop();
/* Communicates to Bot 2: `START` */
transmit(startMsg, MSG_LEN, TIMEOUT);
Serial.println("flashing green LED");
/* Flashes green LED twice */
flashLed(GREEN_LED); /* pauses for 1 second to flash */
flashLed(GREEN_LED); /* pauses for 1 second to flash */
/* Follows yellow */
while (!followColorUntilColor(YELLOW, RED)) {}
forward();
delay(PUSH_CONST);
stop();
/* Follows red, stops on yellow */
while (!followColorUntilColor(RED, YELLOW)) {}
/* Turns on green LED */
digitalWrite(GREEN_LED, HIGH);
/* Waits for `TOXIC` */
Serial.println("Receiving toxic message");
turnLeft(45);
while (!receive(recMsg, MSG_LEN)) {}
stop();
delay(2 * TIMEOUT); /* don't double receive a message */
/* Flash yellow LED continuously */
digitalWrite(YELLOW_LED, HIGH);
/* Waits for `STOP_YELLOW` */
turnLeft(45);
while (!receive(recMsg, MSG_LEN)) {}
/* Turns off yellow LED */
digitalWrite(YELLOW_LED, LOW);
delay(TIMEOUT); /* don't double receive a message */
/* Waits for `DONE` */
Serial.println("Receiving toxic message");
turnLeft(45);
while (!receive(recMsg, MSG_LEN)) {}
stop();
/* Flashes green LED */
while (1) { flashLed(GREEN_LED); }
}
void bot2(void)
{
/* Waits for `START` */
Serial.println("Waiting for start");
while (!receive(recMsg, MSG_LEN)) {}
delay(500);
/* Flash green LED twice */
Serial.println("Lit green led");
flashLed(GREEN_LED); /* pauses for 1 second to flash */
flashLed(GREEN_LED); /* pauses for 1 second to flash */
/* Puts bot in motion (`forward()`) */
forward();
/* Loops until collision (boolean is set in ISR) */
while (!hitWall) {
for (int i = 0; i < NUM_BUMPERS; ++i) {
if (digitalRead(bumpers[i])) {
hitWall = true;
}
}
delay(1);
}
Serial.println("Hit wall");
/* Moves backward until blue and stops */
actionUntilColor(BLUE, backward);
turn(80); /* positive to turn left */
actionUntilColor(BLUE, turnLeft);
/* Stops on blue, flashing a blue LED */
flashLed(BLUE_LED);
/* Turns a predetermined angle to the right, and follows blue */
while (!followColorUntilColor(BLUE, YELLOW)) {}
Serial.println("Found yellow");
forward();
delay(PUSH_CONST);
stop();
/* Stops on yellow, flashing yellow LED twice */
flashLed(YELLOW_LED);
flashLed(YELLOW_LED);
/* Follows yellow */
Serial.println("Following yellow until blue");
while (!followColorUntilColor(YELLOW, BLUE)) {}
forward();
delay(PUSH_CONST);
stop();
/* Follows blue */
Serial.println("Following blue until yellow");
while (!followColorUntilColor(BLUE, YELLOW)) {}
/* Stops on yellow, turns on yellow LED, turns 180 degrees */
Serial.println("Turning until blue");
stop();
digitalWrite(YELLOW_LED, HIGH);
turn(-180);
actionUntilColor(BLUE, turnRight);
/* Communicates to Bot 1: `TOXIC` */
transmit(startMsg, MSG_LEN, TIMEOUT);
/* Flash yellow continuously */
flashLed(YELLOW_LED);
digitalWrite(YELLOW_LED, HIGH);
/* Follows blue, stops on yellow */
while (!followColorUntilColor(BLUE, YELLOW)) {}
forward();
delay(PUSH_CONST);
stop();
/* Communicates to Bot 1: `STOP_YELLOW` */
transmit(startMsg, MSG_LEN, TIMEOUT);
/* Stop flashing yellow LED */
digitalWrite(YELLOW_LED, LOW);
/* Follows yellow until blue */
while (!followColorUntilColor(YELLOW, BLUE)) {}
forward();
delay(PUSH_CONST);
stop();
/* Follows blue, stops on yellow */
while (!followColorUntilColor(BLUE, YELLOW)) {}
stop();
/* Communicates to Bot 1: `DONE` */
transmit(doneMsg, MSG_LEN, TIMEOUT);
/* Flashes green LED */
while (1) { flashLed(GREEN_LED); }
}
/* Helper Routines */
inline void flashLed(int ledPin) {
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
inline void actionUntilColor(Colors c, void (*action)(void)) {
Colors left, right;
do {
readSensors(left, right);
action();
} while (left != c && right != c);
stop();
}
inline bool followColorUntilColor(Colors c1, Colors c2) {
Colors left, right; readSensors(left, right);
if (left == c2 && right == c2) { stop(); delay(50); return true; }
else if (left == c1 && right == c2) turnRight();
else if (left == c2 && right == c1) turnLeft();
else if (left == c2 && right == BLACK) forward();
else if (left == BLACK && right == c2) forward();
else if (left == c2 && right != c2) turnLeft();
else if (left != c2 && right == c2) turnRight();
else if (left == c1 && right == c1) forward();
else if (left == c1 && right != c1) turnLeft();
else if (left != c1 && right == c1) turnRight();
else if (left != c1 && right != c1) {
Serial.println("Both off c1");
bool turning = false, turningLeft = !isBot1;
int i = 0, prevMillis;
do {
if (!turning) {
prevMillis = millis();
(turningLeft) ? turnLeft() : turnRight();
turning = true; turningLeft = !turningLeft;
}
/* turn other way after 100*i milliseconds */
if (millis() > (200 * i) + prevMillis) {
turning = false; ++i;
}
readSensors(left, right);
if (left == c2 || right == c2) return true;
} while (left != c1 && right != c1);
stop();
}
delay(150);
return false;
}
#undef PUSH_CONST