This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RobotBjorn.ino
457 lines (410 loc) · 10.2 KB
/
RobotBjorn.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
// Include SoftwareSerial to enable extra serial ports for Xbee etc
#include <SoftwareSerial.h>
// Toogle Sensors
const char SensorLeftPin = A3;
const char SensorMiddlePin = A4;
const char SensorRightPin = A5;
// free
// used: A0, A1 for Motorshield
// Motor
const int EngineSpeed = 192;
// Recommended: 192 for 5x AA, 96 for 1x 9V 64 over USB
// Min: 0 Max: 255
// Deactivated for Gearbox
// Horn
const char HornPin = 5;
// Debug
const boolean Debug = false;
// Motherboard
const boolean Due = false; // is this an arduino due?
const boolean Mega = false; // is this an arduino mega?
// Hardware revision
const boolean IsMotorShield = true; // v1.0 hardware or compatible hardware
const boolean IsCherokey = false; // v2.0 hardware
// Channel A = right engine
const char MotorChannelApin = 12;
const char MotorBrakeApin = 9;
const char MotorChannelAPWMpin = 3;
// Channel B = left engine
const char MotorChannelBpin = 13;
const char MotorBrakeBpin = 8;
const char MotorChannelBPWMpin = 11;
// Time in ms to turn left or right
const int turntime = 2000; // 1000 = 1/8
// Startup Procedure
void setup() {
// Open Serial Pin
Serial.begin(9600);
/*if(Mega)
{ Serial1.begin(9600);
Serial2.begin(9600);
Serial3.begin(9600); } // Not Uno compatible
*/
//Setup Channel A
pinMode(MotorChannelApin, OUTPUT); //Initiates Motor Channel A pin
pinMode(MotorBrakeApin, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(MotorChannelBpin, OUTPUT); //Initiates Motor Channel B pin
pinMode(MotorBrakeBpin, OUTPUT); //Initiates Brake Channel B pin
// Sensor
pinMode(SensorLeftPin, INPUT);
pinMode(SensorMiddlePin, INPUT);
pinMode(SensorRightPin, INPUT);
// Turn on Headlights
HeadLights(1);
TailLights(1);
Serial.println('Lights are on');
// Brake on start
Brake();
Serial.println('Startup Brake initialized');
//Serial.println("Run keyboard control");
// Ending pin configuration and other startup routines
Serial.println('Startup procedure completed');
}
// Execute Procedure
void loop() {
//Test
//Test();
// Respond to Keyboard
//KeyboardControl();
// AutoPilot
AutoPilot();
}
// Test Function
void Test() {
// Dance
DriveForward();
delay(2500);
DriveReverse();
delay(2500);
TurnLeft();
delay(2500);
TurnRight();
delay(2500);
Horn();
Horn();
Horn();
delay(2500);
}
void AutoPilot() {
if (SensorMiddle() ) {
// On something insight (Middle)
Brake(); // Brake
Serial.println("Stopped because I detected something");
if (Debug) {
Serial.println("SensorMiddle return");
Serial.println(SensorMiddle() );
Serial.println("SensorLeft return");
Serial.println(SensorLeft() );
Serial.println("SensorRight return");
Serial.println(SensorRight() );
}
if (!SensorLeft() ) { // Look Left
Serial.println("Going left");
TurnLeft();
delay(turntime );
Brake;
}
else if (!SensorRight() ) { // Or look right
Serial.println("Going right");
TurnRight();
delay(turntime );
Brake;
}
else if (SensorMiddle() && SensorLeft() && SensorRight() ) {
// stuck
Serial.println("Stuck. Reversing and turnaround");
TurnLeft();
delay(turntime * 2);
// Reverse
DriveReverse();
delay(2000);
Brake();
}
else { // Or do nothing
Serial.println("Cannot decide which way to go");
if (Debug) {
Serial.println("SensorLeft return");
Serial.println(SensorLeft() );
Serial.println("SensorRight return");
Serial.println(SensorRight() );
}
Horn();
delay(2000);
}
}
else if (SensorLeft() ) {
// you see something on the left
Serial.println("Might hit something: left");
Brake();
TurnRight();
delay(turntime);
Brake();
}
else if (SensorRight() ) {
// you see something on the right
Serial.println("Might hit something: right");
Brake();
TurnLeft();
delay(turntime);
Brake();
}
// If everything is going fine
else {
DriveForward();
}
// End If
}
void KeyboardControl() {
if (Serial.available()) {
char val = Serial.read();
if (val != -1) // if some value inputted
{
switch (val)
{
case 'w'://Move Forward
DriveForward();
break;
case 's'://Move Backward
DriveReverse();
break;
case 'a'://Turn Left
Brake();
TurnLeft();
break;
case 'd'://Turn Right
Brake();
TurnRight();
break;
case 'z':
Serial.println("Hello");
break;
case 'x':
//stop();
Brake();
break;
// Gearbox
case '0'://Gear 0
Gearbox(0);
break;
case '1'://Gear 0
Gearbox(1);
break;
case '2'://Gear 0
Gearbox(2);
break;
case '3'://Gear 0
Gearbox(3);
break;
case '4'://Gear 0
Gearbox(4);
break;
case '5'://Gear 0
Gearbox(5);
break;
case '6'://Gear 0
Gearbox(6);
break;
case '7'://Gear 0
Gearbox(7);
break;
case '8'://Gear 0
Gearbox(8);
break;
case '9'://Gear 0
Gearbox(9);
break;
case 't'://Test
Serial.println("Enabling Test Procedure");
Test();
Serial.println("Done");
break;
case 'h'://Horn
Serial.println("Feeling horny?");
Horn();
break;
}
}
else ; //stop();
}
}
void Gearbox( int gear) {
// Depends motor PWM by 'gear'
int EngineSpeed = map(gear, 0, 9, 0, 255);
if (Debug) {
Serial.print("Gear: ");
Serial.println(gear);
Serial.print("PWM Speed: ");
Serial.println(EngineSpeed);
Serial.print("Percentage power: ");
Serial.print( EngineSpeed / 255 );
Serial.println("%");
}
}
// Brake functions
void BrakeLeft() {
digitalWrite(MotorBrakeBpin, HIGH); //Engage the Brake for Channel B
}
void BrakeRight() {
digitalWrite(MotorBrakeApin, HIGH); //Engage the Brake for Channel A
}
void Brake() {
// Brake light
BrakeLight(1);
// Brake
BrakeLeft();
BrakeRight();
// Brake light Off
BrakeLight(0);
}
// Drive in reverse function
void ReverseLeft() {
//Motor B backward @ full speed
digitalWrite(MotorChannelBpin, LOW); //Establishes backward direction of Channel B
digitalWrite(MotorBrakeBpin, LOW); //Disengage the Brake for Channel B
analogWrite(MotorChannelBPWMpin, EngineSpeed); //Spins the motor on Channel B
}
void ReverseRight() {
//Motor A backwards @ full speed
digitalWrite(MotorChannelApin, LOW); //Establishes forward direction of Channel A
digitalWrite(MotorBrakeApin, LOW); //Disengage the Brake for Channel A
analogWrite(MotorChannelAPWMpin, EngineSpeed); //Spins the motor on Channel A
}
void DriveReverse() {
// Lights on
ReverseLight(1);
// Reverse
ReverseLeft();
ReverseRight();
// Lights off
ReverseLight(0);
}
// Drive specific way function
void DriveLeft() {
//Motor B forward @ full speed
digitalWrite(MotorChannelBpin, HIGH); //Establishes forward direction of Channel B
digitalWrite(MotorBrakeBpin, LOW); //Disengage the Brake for Channel B
analogWrite(MotorChannelBPWMpin, EngineSpeed); //Spins the motor on Channel B
}
void DriveRight() {
//Motor A forward @ full speed
digitalWrite(MotorChannelApin, HIGH); //Establishes forward direction of Channel A
digitalWrite(MotorBrakeApin, LOW); //Disengage the Brake for Channel A
analogWrite(MotorChannelAPWMpin, EngineSpeed); //Spins the motor on Channel A at half speed
}
void DriveForward() {
DriveLeft();
DriveRight();
}
// Turn Left
void TurnLeft() {
LeftLight(1);
ReverseLeft();
DriveRight();
LeftLight(0);
}
// Turn Right
void TurnRight() {
RightLight(1);
ReverseRight();
DriveLeft();
RightLight(0);
}
// Sensor Left
boolean SensorLeft() {
if (digitalRead(SensorLeftPin) == LOW) return 1;
else {
return 0;
}
}
// Sensor Middle
boolean SensorMiddle() {
if (digitalRead(SensorMiddlePin) == LOW) return 1;
else {
return 0;
}
}
// Sensor Right
boolean SensorRight() {
if (digitalRead(SensorRightPin) == LOW) return 1;
else {
return 0;
}
}
// Current Sensing Channel A
int CurrentSenseA() {
// Max: 3.3 V equals 2A
// Output: value in mA
if (Due) {
return map( analogRead(A0) , 0, 1023, 0, 2000);
}
else {
return map( analogRead(A0) , 0, 676, 0, 2000);
}
}
// Current Sensing Channel B
int CurrentSenseB() {
// Max: 3.3 V equals 2A
// Output: value in mA
if (Due) {
return map( analogRead(A1) , 0, 1023, 0, 2000);
}
else {
return map( analogRead(A1) , 0, 676, 0, 2000);
}
}
// Sensor Left Debug
void SensorLeftDebug() {
Serial.println("Reading on ");
Serial.println( SensorLeftPin );
Serial.println(digitalRead(SensorLeftPin) );
Serial.println("Reading sensor left function");
Serial.println(SensorLeft() );
}
// Sensor Middle Debug
void SensorMiddleDebug() {
Serial.println("Reading on ");
Serial.println( SensorMiddlePin );
Serial.println(digitalRead(SensorMiddlePin) );
Serial.println("Reading sensor middle function");
Serial.println(SensorMiddle() );
}
// Sensor Right Debug
void SensorRightDebug() {
Serial.println("Reading on ");
Serial.println( SensorRightPin );
Serial.println(digitalRead(SensorRightPin) );
Serial.println("Reading sensor right function");
Serial.println(SensorRight() );
}
// Horn / Beeper
void Horn() {
const int HornTime = 1000;
const int NoteFreq = 262; // Frequency 262 = C4
//tone(pin, frequency, duration)
tone(HornPin, NoteFreq, HornTime);
}
// Head Lights
void HeadLights(boolean OnOff) {
// Enable Headlight left
// Enable Headlight right
}
// Tail Lights
void TailLights(boolean OnOff) {
// Enable Taillight Left
// Enable Taillight Right
}
// Brake Light
void BrakeLight(boolean OnOff) {
// Enable Breaklight left
// Enable Breaklight right
}
// Turn Left Light
void LeftLight(boolean OnOff) {
}
// Turn Right Light
void RightLight(boolean OnOff) {
}
// White lights when in reverse
void ReverseLight(boolean OnOff) {
}