-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcar.h
More file actions
707 lines (575 loc) · 14.9 KB
/
car.h
File metadata and controls
707 lines (575 loc) · 14.9 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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
//Librairies
#include "AFMotor.h"
#include "Servo.h"
#include <Wire.h>
#include "compass.h"
// Constants
int const DEFAULT_SENSOR = 14;
int const DEFAULT_RIGHT = 2;
int const DEFAULT_LEFT = 1;
int const DEFAULT_SERVO = 9;
int const COMPASS_PIN = 0;
int const THERMOMETER_PIN = 2;
int const DEFAULT_SPEED = 200;
int const SERVO_OFFSET = 81; // NOT SURE THIS IS THE EXACT VALUE. Must take better measurements
int const CLEAR_DISTANCE = 20;
// States
int const STATE_VOID = 0;
int const STATE_FORWARD = 1;
#define STATE_BACKWARD −1
int const STATE_RIGHT = 2;
int const STATE_LEFT = -2;
bool const DEBUG = false;
bool const DETAIL_DEBUG = false;
#define TEMPERATURE_PRECISION 9
#define DEFAULT_PIN 3
#include "OneWire.h"
#include "DallasTemperature.h"
OneWire oneWire(THERMOMETER_PIN);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
class Thermometer
{
public:
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
//OneWire oneWire;
// Pass our oneWire reference to Dallas Temperature.
//DallasTemperature sensors;
int pin;
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
Thermometer(int aPin) : pin(aPin)//, oneWire(aPin),sensors(&oneWire)
{
}
/*~Thermometer()
{
delete oneWire;
delete sensors;
}
*/
void init()
{
// start serial port
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices at pin");
Serial.println(pin);
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// Loop through each device, print out address
for(int i=0; i < numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
Serial.print("Setting resolution to ");
Serial.println(TEMPERATURE_PRECISION, DEC);
// set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
Serial.println();
}else{
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}
// function to print the temperature for a device
float ReadTemperature(DeviceAddress deviceAddress)
{
// method 1 - slower
//Serial.print("Temp C: ");
//Serial.print(sensors.getTempC(deviceAddress));
//Serial.print(" Temp F: ");
//Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
// method 2 - faster
return sensors.getTempC(deviceAddress);
/*Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit*/
}
float nothing()
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
//Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
// Serial.println("DONE");
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
// Output the device ID
//Serial.print("Temperature for device: ");
//Serial.print(i,DEC);
// It responds almost immediately. Let's print out the data
return ReadTemperature(tempDeviceAddress); // Use a simple function to print out the data
}
return(0);
//else ghost device! Check your power requirements and cabling
}
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
};
// Main Class : Car
class Car
{
public:
static int DefaultSpeed;
static int ClearDistance;
Car() :
sensorPin(DEFAULT_SENSOR), right(AF_DCMotor(DEFAULT_RIGHT)),
left(AF_DCMotor(DEFAULT_LEFT)), compass(COMPASS_PIN), x(0), y(0), avgSpeed(0), thermometer(THERMOMETER_PIN), state(STATE_VOID)
{
// all the initialization is done in "init()"
};
void init() {
// initializing the compass
compass.init();
// initializing the compass
thermometer.init();
//servo.attach(aServo);
leftSpeed = DEFAULT_SPEED;
rightSpeed = DEFAULT_SPEED;
SetSpeed(DEFAULT_SPEED);
ReleaseMotors();
//for processing distances
leftMean = 0;
rightMean = 0;
leftMaxAngle = 0;
rightMaxAngle = 0;
// Min as max possible value
minDist = 1024;
// initializing distances :
for (int i = 0 ; i<= 180; i++)
{
distances[i] = 0;
}
// putting servo at the center
SetServo(0);
}
// FOR DEBUG ONLY... it seems the servo need to be attached in setup
// This is weird. Check if it can be done earlier
void AttachServo(int pin)
{
servo.attach(pin);
}
//SENSOR RELATED
int GetDistance()
{
return analogRead(sensorPin);
};
// SERVO RELATED
void SetServo(int angle)
{
servo.write(SERVO_OFFSET - angle);
}
int ReadServo()
{
return (SERVO_OFFSET - servo.read());
}
void AltSweepServo()
{
SetServo(-45);
delay(150);
distances[-45+45] = GetDistance();
SetServo(0);
delay(150);
distances[0+45] = GetDistance();
SetServo(45);
delay(150);
distances[45+45] = GetDistance();
}
void SweepServo()
{
if (ReadServo() == -45)
{
for ( int angle = -45; angle <= 45; angle++)
{
SetServo(angle);
delay(5);
distances[angle+45] = GetDistance();
}
}
else
{
SetServo(45);
delay(5);
for ( int angle = 45; angle >= -45; angle--)
{
SetServo(angle);
distances[angle+45] = GetDistance();
delay(5);
}
}
ProcessDistances();
//FOR DEBUG ONLY
if (DETAIL_DEBUG)
{
for (int angle = -45; angle <= 45; angle += 5)
{
for (int i = 0; i<(distances[angle+45]/4); i++)
{
Serial.print(" ");
}
Serial.print("| : ");
Serial.print(angle);
Serial.print(" degrees ; ");
if (minDist == distances[angle + 45])
{
Serial.print("MIN! ");
}
if (angle == leftMaxAngle)
{
Serial.print("LEFT MAX! ");
}
else if (angle == rightMaxAngle)
{
Serial.print("RIGHT MAX! ");
}
Serial.println(" ");
}
}
}
void Forward()
{
right.run(FORWARD);
left.run(FORWARD);
};
void Backward()
{
right.run(BACKWARD);
left.run(BACKWARD);
};
void ReleaseMotors()
{
right.run(RELEASE);
left.run(RELEASE);
};
void Right()
{
right.run(FORWARD);
left.run(RELEASE);
};
void Left()
{
right.run(RELEASE);
left.run(FORWARD);
};
void TurnRight()
{
ReleaseMotors();
right.run(BACKWARD);
left.run(FORWARD);
}
void TurnLeft()
{
ReleaseMotors();
right.run(FORWARD);
left.run(BACKWARD);
}
void SetSpeed(int aSpeed)
{
leftSpeed = aSpeed;
rightSpeed = aSpeed;
UpdateSpeed();
};
void UpdateSpeed()
{
// Protected UpdateSpeed: max speed: 250, min speed: 0
if (rightSpeed <= 250 && rightSpeed >= 0)
{
right.setSpeed(rightSpeed);
}
else
{
Serial.print("Speed overflow : ");
Serial.println(rightSpeed);
right.setSpeed(250);
}
if (leftSpeed <= 250 && leftSpeed >= 0)
{
left.setSpeed(leftSpeed);
}
else
{
Serial.print("Speed overflow : ");
Serial.println(leftSpeed);
left.setSpeed(250);
}
};
void TurnTo(float goal)
{
static float angleEpsilon = 2;
float curAngle = compass.ReadAngle(5);
float goalAngle = goal;
float delta = min( abs(curAngle - goalAngle), abs(curAngle - (goal + 360) ) );
while (delta > angleEpsilon)
{
if ( abs(curAngle - (goal + 360) ) < abs(curAngle - goal) )
goalAngle = goal + 360;
else
goalAngle = goal;
// TODO : Add test to turn to the closest direction (right or left)
curAngle = compass.ReadAngle(5);
delta = min( abs(curAngle - goalAngle), abs(curAngle - (goal + 360) ) );
Serial.print("angle = ");
Serial.println(curAngle);
Serial.print("delta = ");
Serial.println(delta);
Serial.print("Condition : ");
Serial.println(delta > angleEpsilon);
if (goalAngle > curAngle)
TurnRight();
else
TurnLeft();
int duration = 5 + round(300 * ((float) delta/180.0));
int start = millis();
Serial.println(duration);
while (millis() - start < duration && delta > angleEpsilon)
{
curAngle = compass.ReadAngle(5);
delta = min( abs(curAngle - goalAngle), abs(curAngle - (goal + 360) ) );
}
ReleaseMotors();
delay(400);
}
ReleaseMotors();
}
int ProcessDistances()
{
rightMean = 0;
leftMean = 0;
int rightMax = 0;
int leftMax = 0;
bool right = false;
bool left = false;
minDist = 1024;
for (int i=-45 ; i<45; i++)
{
int dist = distances[i+45];
left = i <= -10;
right = i >= 10;
if (right)
{
rightMean += dist;
if (dist > rightMax)
{
rightMax = dist;
rightMaxAngle = i;
}
}
else if (left)
{
leftMean += dist;
if (dist > leftMax)
{
leftMax = dist;
leftMaxAngle = i;
}
}
minDist = dist < minDist ? dist : minDist;
}
rightMean /= 36;
leftMean /= 36;
}
// BEHAVIOR RELATED
void RandomRun()
// When the car encounters an obstacle, turns right until
// there's no obstacle ahead
{
Forward();
if (GetDistance() < CLEAR_DISTANCE)
{
ReleaseMotors();
// And we turn the servo in the right direction (left or right, depending on the sensed distances)
// until the distance in front of the car is clear
TurnRight();
while (GetDistance() < CLEAR_DISTANCE)
{
delay(10);
}
}
}
void Feedback() {
Serial.print(" Angle : ");
Serial.print(compass.ReadAngle());
Serial.print(" Temperature : ");
Serial.print(thermometer.nothing());
Serial.print(" | Distance : ");
Serial.print(GetDistance());
Serial.print(" | Speed : ");
Serial.print(avgSpeed);
Serial.print(" | Position : (");
Serial.print(x);
Serial.print(", ");
Serial.print(y);
Serial.print(")");
Serial.println(" ");
}
void Remote()
{
static int running = 0;
static int turning = 0;
static int sweep = 0;
static const int RUN_BACKWARD = -1;
static const int STOP = 0;
static const int RUN_FORWARD = 1;
static const int TURN_RIGHT = 2;
static const int TURN_LEFT = -2;
static const int SWEEP_SERVO = 3;
static const int TURN_SERVO = 4;
static const int SET_SPEED = 5;
if (Serial.available() > 0)
{
char input[17];
int i = 0;
while ((Serial.available() > 0) && (i<16))
{
input[i] = Serial.read();
i++;
// Delay to avoid skipping consecutive signal readings
delay(5);
}
input[i] = '\0';
String inputStr(input);
int operation = inputStr.substring(0,2).toInt();
int operand1 = inputStr.substring(3,9).toInt();
int operand2 = inputStr.substring(10,16).toInt();
switch (operation)
{
case RUN_FORWARD:
{
//leftSpeed = operand1;
//rightSpeed = operand2;
//UpdateSpeed();
Forward();
state = STATE_FORWARD;
break;
}
case RUN_BACKWARD:
{
//leftSpeed = operand1;
//rightSpeed = operand2;
//UpdateSpeed();
state = -1;
Backward();
break;
}
case TURN_RIGHT:
{
state = STATE_RIGHT;
TurnRight();
break;
}
case TURN_LEFT:
{
state = STATE_LEFT;
TurnLeft();
break;
}
case SWEEP_SERVO :
{
AltSweepServo();
// The serial communication pattern in this mode is:
// ANGLE[newline]
// DISTANCE[newline]
Serial.print("ANGLE : -45 | DISTANCE : ");
Serial.println(distances[-45+45]);
Serial.print("ANGLE : 0 | DISTANCE : ");
Serial.println(distances[0+45]);
Serial.print("ANGLE : 45 | DISTANCE : ");
Serial.println(distances[45+45]);
break;
}
case TURN_SERVO :
{
SetServo(operand1);
break;
}
case SET_SPEED :
{
SetSpeed(operand1);
break;
}
case STOP:
{
state = STATE_VOID;
ReleaseMotors();
break;
}
} // End of switch
}// End of Serial reading
}
void ApplyState()
{
switch (state) {
case STATE_FORWARD:
{
if (GetDistance() < 20)
{
TurnRight();
}
else
{
Forward();
}
break;
}
}
}
//////////////////////
// ATTRIBUTES //
////////////////////
AF_DCMotor right;
AF_DCMotor left;
Servo servo;
Compass compass;
int sensorPin;
int leftSpeed;
int rightSpeed;
int distances[180];
Thermometer thermometer;
//For processing distances :
int leftMean;
int rightMean;
int leftMaxAngle;
int rightMaxAngle;
int minDist;
int state;
int x;
int y;
float avgSpeed;
};
#include "OneWire.h"
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define TEMPERATURE_PRECISION 9
#define DEFAULT_PIN 3