-
Notifications
You must be signed in to change notification settings - Fork 1
/
OFF_Earth_Code.ino
484 lines (387 loc) · 13.5 KB
/
OFF_Earth_Code.ino
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
Description:
This program was written based off the system analysis written by the
two other members of my team.
The program is a fully automated system with a complete set of coded
instructions for the drill.
This version however will have some lines of code that are specific for
debugging which may or may not be found in the final piece of code.
Version 1.3 is a code for which the defined values may be wrong as we
have not done testing and do not have the exact values for which the
vehicle can run at, the code is purely theoretical at the moment.
Newest version of this code is always stored on Github in my account NoOne2246
under Off-Earth-Project as the file Mining.ino
https://github.com/NoOne2246/Off-Earth-Project/blob/master/Mining.ino
Author:
Christopher Chun-Hung Ho
z5019205
History:
v2.2 29/10/14 Added new methods of going from the ball as well and changed pins.
v2.1 27/10/14 Changed method of moving the drill to actuator.
v2.0 25/10/14 Major Revision of code
v1.8 25/10/14 Changed movement to the ball
v1.7 25/10/14 Added feedback messaging
v1.6 23/10/14 Removed Locate function, added error correction to detect function
also added the new rotation system
v1.5 13/9/14 Changed the movement to another function of its own
fixed up Ping control with the ultrasound library
v1.4 10/9/14 added manual control to the code.
v1.3 5/9/14 initial implementation
changed ClampControl to boolean
v1.2 3/9/14 changed the format of the layout to Arduino style
v1.1 28/8/14 outline the main parts
v1.0 21/8/14 Write out the basics of the code
*/
//-----------------------------------------------------------------------------------------------------
//Include
//-----------------------------------------------------------------------------------------------------
//#include <NewPing.h>
/*http://playground.arduino.cc/Code/NewPing
This is a ping library written by Tim Eckel to use the
HC-S04 ultrasound sensor. The library can return a median
of pings as well as time in microseconds*/
#include <Servo.h>
/*servo library that comes with arduino software*/
//-----------------------------------------------------------------------------------------------------
//Define
//-----------------------------------------------------------------------------------------------------
//sensor value
#define LIGHT 80 //value of ball in the sensor
//timing
#define ROTATION 100 // milli seconds to turn one degree
#define PAUSE 50 //pause for motor to make sure it stops movement
#define TURNSPEED 22 //turn speed of servo
//movement
#define FORWARD 1
#define LEFT 2
#define RIGHT 3
#define STOP 4
//-----------------------------------------------------------------------------------------------------
//Const pins
//-----------------------------------------------------------------------------------------------------
/*Interrupt is on pin 2, this pin is referred to as interupt 0 and will not be
used within the control of the vehicle as it is an interrupt pin to shutdown
the machine after it has been initiated.
*/
//drill pin
const int dForPin = 7; //turn drill forward
const int dBakPin = 8; //turn drill reverse
//clamp pin
const int rota1 = 5; //rotate ball clockwise
const int rota2 = 6; //rotate ball counter clockwise
//Motor Pins
const int motPin1 = 4; //Left motor pin
const int motPin2 = 3; //Right Motor Pin
//on/off overide
const int Override = 0; //Shutdown interrupt
//Trigger Pins
const int trigOn = 2; //on signal pin
const int trigFor = 9; //on signal pin
const int trigBak = 10; //on signal pin
const int trigClos = 11; //on signal pin
//light
const int Ball = A0; //ball light switch
//-----------------------------------------------------------------------------------------------------
//Declaring
//-----------------------------------------------------------------------------------------------------
//Functions Pretty much self explanatory, their use is written in the Functions Section
void drillCtrl();
void turnOff();
void forward();
void moveCtrl(int dir);
void rotate(int angle);
void manualCtrl(int locat);
void printPorts();
//-----------------------------------------------------------------------------------------------------
// main
//-----------------------------------------------------------------------------------------------------
//setup function
void setup(){
Serial.begin(9600); //initialise serial communications
delay(1000);
Serial.println("AT+NAMEBlackhole");
delay(1000);
Serial.print("AT+PIN1123");
delay(1000);
//Set up output pins
pinMode(dForPin, OUTPUT);
pinMode(dBakPin, OUTPUT);
pinMode(rota1, OUTPUT);
pinMode(rota2, OUTPUT);
pinMode(motPin1, OUTPUT);
pinMode(motPin2, OUTPUT);
//set up input pins
pinMode(trigOn, INPUT);
pinMode(trigFor, INPUT);
pinMode(trigBak, INPUT);
pinMode(trigClos, INPUT);
Serial.println("Initialisation Complete");
Serial.println("Hit button or any key to begin");
//Wait for start signal
while(digitalRead(trigOn)==0||!Serial.available()){ //wait until it receives start signal
}
Serial.read();
attachInterrupt(Override, turnOff, CHANGE); //switch off vehicle through interrupt
forward(); //move to the ball
rotate(90);
}
//loop function
void loop (){ //loop has been simplified as it may change to manual control
drillCtrl(); //Run the ball drilling function.
rotate(3); //run rotate ball function
}
//-----------------------------------------------------------------------------------------------------
// subroutines
//-----------------------------------------------------------------------------------------------------
//move forward
void forward(){
moveCtrl(FORWARD); //move forward
while(trigClos==LOW && !Serial.available() && analogRead(Ball) > LIGHT){ //wait until vehicle hits ball
if(Serial.available()){
manualCtrl(1);
}
}
Serial.read();
moveCtrl(STOP); //Stop vehicle
}
//rotate ball
void rotate(int angle){
int time; //create time variable
Serial.print("Angle to rotate:");
Serial.println(angle);
time = angle*ROTATION; //calculate the time to rotate for in ms
Serial.print("Time to rotate:");
Serial.println(time);
Serial.println("Rotating ball");
digitalWrite(rota1, HIGH); //rotate the ball
delay(time); //delay for calculated time
digitalWrite(rota1, LOW); //stop rotating ball
Serial.println("Stop rotating ball");
if(Serial.available()){
manualCtrl(2);
}
}
//movement
void moveCtrl(int dir){
switch(dir){
case FORWARD:
digitalWrite(motPin1, HIGH); //Both motors on
digitalWrite(motPin2, HIGH);
Serial.println("Moving Forward");
break;
case LEFT:
digitalWrite(motPin1, LOW); //Right motor on
digitalWrite(motPin2, HIGH);
Serial.println("Turning Left");
break;
case RIGHT:
digitalWrite(motPin1, HIGH); //left motor on
digitalWrite(motPin2, LOW);
Serial.println("Turning Right");
break;
case STOP:
digitalWrite(motPin1, LOW); //stop both motors
digitalWrite(motPin2, LOW);
Serial.println("Stopping");
break;
default:
break;
}
}
//drill movement
void drillCtrl(){
digitalWrite(dForPin, HIGH); //drill turn forward
Serial.println("Drill Moving Forward");
while (digitalRead(trigFor)==LOW){ //check if stop switch has been hit
if(Serial.available()){
manualCtrl(3);
}
}
Serial.println("Drill stop Forward");
digitalWrite (dForPin, LOW); //stop forward turning of the drill
delay(PAUSE); //pause for moment
digitalWrite (dBakPin, HIGH); //reverse drill
delay(PAUSE);
Serial.println("Drill Moving Backward");
while (digitalRead(trigBak)==LOW){ //check if stop switch has been hit
if(Serial.available()){
manualCtrl(4);
}
}
digitalWrite (dBakPin, LOW); //stop drill
Serial.println("Drill Stop Backward");
}
void manualCtrl(int locat){
Serial.println("Manual Control Mode");
char input = Serial.read();
for (int pin = 3; pin < 10; pin++){
digitalWrite(pin, LOW);
}
while(input!='r'){ //r is return/resume key
switch (input) {
case 'W': case 'w': // Car Forward
Serial.print("---Car moving Forward---\n");
if (input == lastCommand) {
moveCtrl(STOP);
} else {
moveCtrl(FORWARD);
}
printPorts();
break;
case 'A': case 'a': // Car Left
Serial.println("---Car moving left---");
if (input == lastCommand) {
moveCtrl(STOP);
} else {
moveCtrl(LEFT);
}
printPorts();
break;
case 'D': case 'd': // Car Right
Serial.println("---Car moving right---");
if (input == lastCommand) {
moveCtrl(STOP);
} else {
moveCtrl(RIGHT);
}
printPorts();
break;
case 'S': case 's': // Car stop
moveCtrl(STOP);
printPorts();
break;
case 'Z': case 'z': // Clamp rotateLeft
Serial.println("---Clamp rotateLeft---");
if (digitalRead(rota1) == LOW &&
digitalRead(rota2) == LOW) {
digitalWrite(rota1, HIGH);
} else {
digitalWrite(rota1, LOW);
digitalWrite(rota2, LOW);
}
printPorts();
break;
case 'X':
case 'x': // Clamp rotateRight
Serial.println("---Clamp rotateRight---");
if (digitalRead(rota1) == LOW &&
digitalRead(rota2) == LOW) {
digitalWrite(rota2, HIGH);
} else {
digitalWrite(rota1, LOW);
digitalWrite(rota2, LOW);
}
printPorts();
break;
case 'I': case 'i': // Drill Forward
Serial.println("---Drill going forward---");
if (digitalRead(dForPin) == LOW &&
digitalRead(dBakPin) == LOW) {
digitalWrite(dForPin, HIGH);
} else {
digitalWrite(dForPin, LOW);
digitalWrite(dBakPin, LOW);
}
printPorts();
break;
case 'K':
case 'k': // Drill Backward
Serial.println("---Drill going backward---");
if (digitalRead(dForPin) == LOW &&
digitalRead(dBakPin) == LOW) {
digitalWrite(dBakPin, HIGH);
} else {
digitalWrite(dForPin, LOW);
digitalWrite(dBakPin, LOW);
}
printPorts();
break;
case 'P':
case 'p': // Print Ports
Serial.println("---Printing all ports---");
printPorts();
break;
case 'M':
case 'm':
turnOff();
printPorts();
break;
case 'E':
case 'e':
turnOff();
break;
default:
break;
}
lastCommand = input;
while(!Serial.available()){
}
input = Serial.read();
}
for (int pin = 3; pin < 10; pin++){
digitalWrite(pin, LOW);
}
switch(locat){
case 1:
moveCtrl(FORWARD);
break;
case 2:
break;
case 3:
if(digitalRead(trigFor)==LOW){
digitalWrite(dBakPin, LOW);
digitalWrite(dForPin, HIGH);
}
digitalWrite(Drill, HIGH);
break;
case 4:
if(digitalRead(trigBak)==LOW){
digitalWrite(dForPin, LOW);
digitalWrite(dBakPin, HIGH);
}
digitalWrite(Drill, HIGH);
break;
default:
break;
}
Serial.println("Resuming");
return;
}
void printPorts() {
Serial.print("Left Wheel: ");
Serial.println(digitalRead(motPin1));
Serial.print("Right Wheel: ");
Serial.println(digitalRead(motPin2));
Serial.print("Clamp 1: ");
Serial.println(digitalRead(rota1));
Serial.print("Clamp 2: ");
Serial.println(digitalRead(rota2));
Serial.print("Drill Forward: ");
Serial.println(digitalRead(dForPin));
Serial.print("Drill Backward: ");
Serial.println(digitalRead(dBakPin));
Serial.println(" ");
Serial.println(" ");
}
//reset the machine and stop movement
void turnOff(){
if(trigBak==LOW){
digitalWrite (dBakPin, HIGH); //move drill backwards
delay(PAUSE); //give drill time to pick up speed
digitalWrite (dDowPin, HIGH); //drill stop back
while (digitalRead(trigBak)==LOW){ //check if stop switch has been hit
/*
serVal--; //increase servo angle
drillMove.write(serVal); //write new angle
delay(10); //delay 10ms
*/
}
digitalWrite (dDowPin, LOW); //drill stop back
digitalWrite (dBakPin, LOW); //stop backward movement
}
while(1!=0){
if(Serial.available()){
manualCtrl(2);
}
}
}