-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWarman_Arduino_Code.ino
More file actions
376 lines (318 loc) · 11.2 KB
/
Warman_Arduino_Code.ino
File metadata and controls
376 lines (318 loc) · 11.2 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
//===================================================================
//===================================================================
// Masonville Dynamics (Warman) - Snr Design WEC 2021 Source Code
// Developed by Mason K., Samson H., Adrian K, Owen K.,
// Jan 30th, 2021
//===================================================================
//===================================================================
//===================================================================
// Library
//===================================================================
#include <Servo.h>
//===================================================================
// Pin Declarations
//===================================================================
//Motors
int leftMotor = 11;
int hMotor = 12;
int rightMotor = 10;
int revolverMotor = 6;
int extenderMotor = 5;
Servo servo_5;
Servo servo_6;
Servo servo_10;
Servo servo_11;
Servo servo_12;
//Sensors
#define leftEcho A0
#define leftTrig 4
#define rightEcho A1
#define rightTrig 3
#define forwardEcho A2
#define forwardTrig 2
#define backwardEcho A3
#define backwardTrig 7
//Initialized Values
int Room;
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
int safe_distance = 5; // safe distance set to 5 cm
bool safe = true; //Safety declaration
bool lefty = true; //Safety declaration left
bool righty = true; //Safety declaration right
bool delivery = true; //Delivery declaration
int pos = 0;
//=========================================================
//Object Detection Variables
//=========================================================
#define LT_Sides digitalRead(2) //IR sensor (left and right)
#define LT_M digitalRead(3) //IR Sensor (middle)
//=========================================================
//Finite State Machine
//=========================================================
//Enumeration for States (Stop, Idle, And Run)
enum RobotState {stop, idle, left, right, run};
enum RobotState current_state = stop;
//=========================================================
//Functions
//=========================================================
//checkObstacle() - This code determines whether there is any obstacles and returns true or false
bool checkObstacle(){
//State to detect obstacle interference
bool rightDetect = false;
bool leftDetect = false;
bool forwardDetect = false;
bool backwardDetect = false;
//Check each of the four sensors to determine whether there is any obstacle interference
//Uses a helper function to ping
rightDetect = ping(rightEcho, rightTrig);
leftDetect = ping(leftEcho, leftTrig);
forwardDetect = ping(forwardEcho, forwardTrig);
backwardDetect = ping(backwardEcho, backwardTrig);
//I am bad at bitshifting, so I will use two nested OR loops
if((rightDetect||leftDetect)||(forwardDetect || backwardDetect)){
return false;
}else{
return true;
}
}
//checkObstacleleft() - This code determines whether there is any obstacles right and returns true or false
bool checkObstacleleft(){
//State to detect obstacle interference
bool rightDetect = true;
bool leftDetect = false;
bool forwardDetect = true;
bool backwardDetect = false;
//Check each of the four sensors to determine whether there is any obstacle interference
//Uses a helper function to ping
rightDetect = ping(rightEcho, rightTrig);
leftDetect = ping(leftEcho, leftTrig);
forwardDetect = ping(forwardEcho, forwardTrig);
backwardDetect = ping(backwardEcho, backwardTrig);
//I am bad at bitshifting, so I will use two nested OR loops
if((rightDetect||leftDetect)||(forwardDetect || backwardDetect)){
return false;
}else{
return true;
}
}
//checkObstacleright() - This code determines whether there is any obstacles left and returns true or false
bool checkObstacleright(){
//State to detect obstacle interference
bool rightDetect = false;
bool leftDetect = true;
bool forwardDetect = true;
bool backwardDetect = false;
//Check each of the four sensors to determine whether there is any obstacle interference
//Uses a helper function to ping
rightDetect = ping(rightEcho, rightTrig);
leftDetect = ping(leftEcho, leftTrig);
forwardDetect = ping(forwardEcho, forwardTrig);
backwardDetect = ping(backwardEcho, backwardTrig);
//I am bad at bitshifting, so I will use two nested OR loops
if((rightDetect||leftDetect)||(forwardDetect || backwardDetect)){
return false;
}else{
return true;
}
}
//checkObstacleright() - This code determines whether there is any obstacles and returns true or false
bool checkObstacledelivery(){
//State to detect obstacle interference
bool rightDetect = false;
bool leftDetect = false;
bool forwardDetect = true;
bool backwardDetect = false;
//Check each of the four sensors to determine whether there is any obstacle interference
//Uses a helper function to ping
rightDetect = ping(rightEcho, rightTrig);
leftDetect = ping(leftEcho, leftTrig);
forwardDetect = ping(forwardEcho, forwardTrig);
backwardDetect = ping(backwardEcho, backwardTrig);
//I am bad at bitshifting, so I will use two nested OR loops
if((rightDetect||leftDetect)||(forwardDetect || backwardDetect)){
return false;
}else{
return true;
}
}
//ping() is a helper function for checkObstacle(), it takes in a trigger and echo and returns whether there is an obstacle interference
bool ping(int echo, int trigger){
int duration = 0;
//Ping ultrasonic signal
digitalWrite(trigger, LOW);
delayMicroseconds(5);
digitalWrite(trigger, HIGH);
delay(10);
digitalWrite(trigger, LOW);
//Determine the time for bounce
pinMode(echo, INPUT);
duration = pulseIn(echo, HIGH);
//Divide by duration by 58 to get cm
duration = duration/58;
//If distance is less than 25cm, stop the robot to be safe
if(duration < 50){
return true;
}
else{
return false;
}
}
//forward() is a movement function, it allows the robot to move forward
void Forward(){
// sweep the servo from 0 to precise forward rotations in steps
// of 1 degrees
for (pos = 0; pos <= 720; pos += 1) {
// tell servo to go to position in variable 'pos'
servo_10.write(pos);
servo_11.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
}
void stopper(){
servo_10.write(pos);
servo_11.write(pos);
}
//backward() is a movement function, it allows the robot to move backward
void backward(){
// sweep the servo from 0 to precise backward rotations degrees in steps
// of 1 degrees
for (pos = 0; pos <= -720; pos -= 1) {
// tell servo to go to position in variable 'pos'
servo_10.write(pos);
servo_11.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
}
//left() is a movement function, it allows the robot to move left
void Left(){
// sweep the servo from 0 to 180 degrees in steps
// of 1 degrees
for (pos = 0; pos <= 720; pos += 1) {
// tell servo to go to position in variable 'pos'
servo_11.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
}
//right() is a movement function, it allows the robot to move right
void Right(){
// sweep the servo from 0 to 180 degrees in steps
// of 1 degrees
for (pos = 0; pos <= -720; pos -= 1) {
// tell servo to go to position in variable 'pos'
servo_11.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
}
//revolver() is a movement function, it allows the robot to move revolver
void revolver(){
// sweep the servo from 0 to 180 degrees in steps
// of 1 degrees
for (pos = 0; pos <= 720; pos += 0.5) {
// tell servo to go to position in variable 'pos'
servo_6.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
}
//Extender() is a movement function, it allows the robot to move extender
void ExtenderMotor(){
// sweep the servo from 0 to 180 degrees in steps
// of 1 degrees
for (pos = 0; pos <= 360; pos += 5) {
// tell servo to go to position in variable 'pos'
servo_5.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
}
//=========================================================
//=========================================================
// Setup
//=========================================================
//=========================================================
void setup()
{
//Chooses a 9600 baud rate
Serial.begin(9600);
//Motor Pin Declarations
servo_11.attach(leftMotor);
servo_12.attach(hMotor);
servo_10.attach(rightMotor);
servo_6.attach(revolverMotor);
servo_5.attach(extenderMotor);
//Sensor Pin Declarations
pinMode(leftEcho, INPUT);
pinMode(rightEcho, INPUT);
pinMode(forwardEcho, INPUT);
pinMode(backwardEcho, INPUT);
pinMode(leftTrig, OUTPUT);
pinMode(rightTrig, OUTPUT);
pinMode(forwardTrig, OUTPUT);
pinMode(backwardTrig, OUTPUT);
}
//=========================================================
//=========================================================
// Loop
//=========================================================
//=========================================================
void loop()
{
//=========================================================
//Safety Check - Checks to see if there is any interference
//=========================================================
safe = checkObstacle();
lefty = checkObstacleleft();
righty = checkObstacleright();
delivery = checkObstacledelivery();
//Enums are buggy in arduino so we had to use a boolean lol
if(safe){
current_state = run;
}
else if(lefty){
current_state = left;
}
else if(righty){
current_state = right;
}
else if(delivery){
current_state = idle;
}
else{
current_state = stop;
}
//===============================================================================
//Command Check - Checks to see if there is any instructions from the transmitter
//===============================================================================
// TODO: Implement reciever and transmitter code in here
//Changes state based on the two checks
switch (current_state){
case stop:
//All the code the robot should be running when stopped
Serial.println("Robot is currently stopped");
stopper();
break;
case idle:
//All the code the robot should run when idle
Serial.println("Robot await instructions");
revolver();
//timed revolve and then initiate extender arm
delay(300);
ExtenderMotor();
break;
case left:
//All the code the robot should run when left
Left();
case right:
//All the code the robot should run when right
Right();
case run:
//All the code the robot should run forward
Forward();
break;
}
}