-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMasterArduino.cpp
More file actions
459 lines (353 loc) · 9.66 KB
/
MasterArduino.cpp
File metadata and controls
459 lines (353 loc) · 9.66 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
free pins (if used pls delete from here):
0, 1, 9-11 (PWM), 13.
terminology: (so much complicated acronyms)
US - Ultra sonic.
USR - Ultra sonic sensor on the right.
USL - Ultra sonic sensor on the left.
SS - sound sensor.
slave - the second arduino.
FDS - fire detection system.
FES - fire extinguishing system.
*/
/* libraries */
#include <Wire.h>
#include <arduinoFFT.h>
void room1();
void room2();
void room3();
void room4();
void checkFire();
int getDegrees();
void moveForward();
void moveBackward();
void moveRight();
void moveLeft();
void stopMoving();
int getFrontDis();
int getBackDis();
int getRightDis();
int getLeftDis();
int askSlave(String);
void sendToSlave(String);
/* constants */
// pins for the US sensors
#define pinTrigUSR 2
#define pinEchoUSR 3 // PWM
#define pinTrigUSL 4
#define pinEchoUSL 5 // PWM
// pins for the sound sensor
#define pinSS A0
// pins for the turret (servo)
#define pinTurret 6 // PWM
// pins for the fire detection system
#define pinFDS1 A1
#define pinFDS2 A2
#define pinFDS3 A3
// pins for the fire extinguishing system
// see "ConfigurationsEFS.png" to know how to control it.
#define pinFES1 7 // goes to INA.
#define pinFES2 8 // goes to INB.
// I2C registers' addresses
#define addressGyro 0x68 // The address of the gyro's register (I2C shit).
#define addressSlave 0x45 // The address of the slave's register (lol 69).
// arduinoFFT setup
#define SAMPLES 128 // number of checks.
#define SAMPLING_FREQUENCY 7200 // twice the highest frequency expected.
#define WANTED_FREQUENCY 3600
/* variables */
// some shit for the sound sensor.
arduinoFFT FFT = arduinoFFT();
unsigned int samplingPeriod;
/* main code */
void setup() {
// US sensors
pinMode(pinEchoUSR, INPUT);
pinMode(pinTrigUSR, OUTPUT);
pinMode(pinEchoUSL, INPUT);
pinMode(pinEchoUSL, OUTPUT);
// Sound sensor
pinMode(pinSS, INPUT);
samplingPeriod = round(1000000<(1.0/SAMPLING_FREQUENCY));
// turret
pinMode(pinTurret, OUTPUT);
// FDS
pinMode(pinFDS1, INPUT);
pinMode(pinFDS2, INPUT);
pinMode(pinFDS3, INPUT);
// FES
pinMode(pinFES1, INPUT);
pinMode(pinFES2, INPUT);
// I2C shit
Wire.begin(1); // turn on the I2C and set the address of this device's register.
Wire.beginTransmission(addressGyro); // start the transmission to the gyro.
Wire.write(0x6B); // PWR_MGMT_1 register.
Wire.write(0); // wakes up the sensor.
Wire.endTransmission(true); // releasing the I2C bus.
}
void loop() {
while (!startConditions()); // waits for the required conditions to be true so we can start the run.
room1();
room2();
room3();
room4();
}
// Return true if the robot is above white area and a sound of 3600Hz is being played for atleast 2 seconds.
boolean startConditions()
{
for (int i = 0; i < 4; i++){
delay(500);
if (!(checkWhiteArea() && checkSound())) {
return false;
}
}
return true;
}
//flag if dog is in pos A
boolean dogA = false;
//flag if dog is in pos C
boolean dogC = false;
//runs all the way to the 1st room and till the main intersection
void room1()
{
if (getLeftDis() > 60) //finds if the dog is in A
{
dogA = true;
}
moveForward();
while (getBackDis() > 110); //till the main intersection
stopMoving(); //to stop in the intersection
if (getLeftDis() > 50) //finds if dog is in C
{
dogC = true;
}
moveRight();
while (getRightDis() > 15); //till the wall
stopMoving(); //stop infront of the room
moveBackward();
while (getFrontDis() < 70); // move inside the room
stopMoving();
checkFire(); //needs to be done will check for the the fire
moveForward();
while (getFrontDis() > 15); //gets out of the room
stopMoving(); //stops in front of the room
moveLeft();
while (getRightDis() < 90) //gets back to the intersection
stopMoving(); //stops in the intersection
}
//runs from the main intersection to the 2nd room and gets infront of the next
void room2()
{
moveForward();
while (getFrontDis() > 20);
stopMoving();
moveRight();
delay (1200);
stopMoving();
checkFire();
moveLeft();
delay (1200);
stopMoving();
}
//runs all the way to the 3rd room and to the intersection where the dog is
void room3()
{
if (getLeftDis() <20) //checks where the door is (is it on top or bottom)
{
moveBackward();
while (getFrontDis() < 70);
stopMoving();
}
moveLeft(); //enters the room
delay(1200);
stopMoving();
checkFire();
moveLeft(); //leavs to the exsit
while(getLeftDis() > 10);
stopMoving();
moveBackward(); //goes to the exsit of the room and to the intersection with the dog
while(getFrontDis() < 110);
stopMoving();
}
//runs from the dog intersection all the way home
void room4()
{
if (dogC) //if the dog is in pos C
{
moveBackward(); //gets to the interection next to the room and across from the home
while (getBackDis() > 10);
stopMoving();
moveRight(); //gets inffront of the final room
while (getLeftDis() < 60);
stopMoving();
moveForward(); //enters the room
delay(1200);
stopMoving();
checkFire();
moveBackward(); //esits the room
while (getBackDis() > 10);
stopMoving();
moveRight(); //arives home
while (getLeftDis() < 140);
stopMoving();
//turn LED to high
}
else if(dogA) //if dog is in A
{
moveRight(); //gets in front of the room from under
while (getLeftDis() < 90);
stopMoving();
if (getBackDis() > 20) //checks if you can go in from under
{
moveBackward(); //enters the room
delay(1200);
stopMoving();
checkFire();
moveForward(); //leavs the room
while (getFrontDis() > 10);
stopMoving();
}
moveRight(); //gets to the main interesection
while (getLeftDis() < 138);
stopMoving();
moveBackward(); //gets to the end
while (getBackDis() > 20);
stopMoving();
//turn led to high
}
else //dog is in pos B
{
moveRight(); //gets in front of the room from under
while (getLeftDis() < 90);
stopMoving();
if (getBackDis() > 20) //checks if you can go in from under and gets home
{
moveBackward(); //enters the room
delay(1200);
stopMoving();
checkFire();
moveForward(); //leavs the room
while (getFrontDis() > 10);
stopMoving();
moveRight(); //gets to the main interesection
while (getLeftDis() < 138);
stopMoving();
moveBackward(); //gets to home
while (getBackDis() > 20);
stopMoving();
//turn led to high
}
else
{
moveRight(); //gets to the main interesection
while (getLeftDis() < 138);
stopMoving();
moveBackward(); //gets to home
while (getBackDis() > 20);
stopMoving();
moveLeft(); //gets in front of the room from the top side
while (getLeftDis() > 70);
stopMoving();
moveForward(); //enters the room
delay(1200);
stopMoving();
checkFire();
moveBackward(); //leavs the room
while (getBackDis() > 10);
stopMoving();
moveRight(); //arives home
while (getLeftDis() < 140);
stopMoving();
//turn LED to high
}
}
}
// returns the robot's rotation degree with respect to the initial angle.
int getDegrees() {
Wire.beginTransmission(addressGyro); // start the transmission to the gyro.
Wire.write(0x47); // start reading from this register.
Wire.endTransmission(false); // commit without releasing the I2C bus.
Wire.requestFrom(addressGyro, 2, true); // asking for two registers starting at "0x47".
return (Wire.read() << 8) | Wire.read(); // reading registers "0x47" (GYRO_ZOUT_H) and "0x48" (GYRO_ZOUT_L).
}
// return true if sound of 3600Hz is being played.
boolean checkSound() {
unsigned long microSeconds;
double vReal[SAMPLES];
double vImag[SAMPLES] = {0}; // creates and sets all the array to zeros
for (int i = 0; i < SAMPLES; i++) {
microSeconds = micros();
vReal[i] = analogRead(pinSS); // puts the current value from the sensor in the array
while (micros() < (microSeconds + samplingPeriod)) { // waits some time.
continue;
}
}
// perform fast fourier transform on the samples.
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
int frequency = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY); // the frequency.
return ( (WANTED_FREQUENCY - 40) < frequency ) && ( frequency < (WANTED_FREQUENCY + 40) );
}
// checks if there is white area under the robot.
boolean checkWhiteArea() {
return askSlave("isWhiteArea");
}
// move forward
void moveForward() {
sendToSlave("forward");
}
// move backward
void moveBackward() {
sendToSlave("backward");
}
// move right
void moveRight() {
sendToSlave("right");
}
// move left
void moveLeft() {
sendToSlave("left");
}
// stop moving
void stopMoving() {
sendToSlave("stop");
}
// returns the front distnance.
int getFrontDis() {
return askSlave("disFront");
}
// returns the back distnance.
int getBackDis() {
return askSlave("disBack");
}
// returns the right distnance.
int getRightDis() {
return getDis(pinTrigUSR, pinEchoUSR);
}
// returns the left distnance.
int getLeftDis() {
return getDis(pinTrigUSL, pinEchoUSL);
}
// return the distance of a specific distance sensor
int getDis(int trig, int echo) {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
int duration = pulseIn(echo, HIGH);
return duration * 0.034 / 2; // return the distance
}
// sends string to the slave.
void sendToSlave(char* str) {
Wire.beginTransmission(addressSlave); // start the transmission to the gyro.
Wire.write(str); // start reading from this register.
Wire.endTransmission(true); // commit and releasing the I2C bus.
}
// ask for info fron the slave based on string.
int askSlave(char* str) {
sendToSlave(str);
Wire.requestFrom(addressSlave, 1, true); // asking for one register.
return Wire.read();
}