-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsonic-car.ino
366 lines (317 loc) · 7.35 KB
/
sonic-car.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
/* Martin Porebski
* 12/10/2017
* Arduino Uno, Atmel Mega 328p, au 1722
* Arduino UNO control for the car
*/
// use the standard arduino servo library
#include <Servo.h>
// Define which pin is used for the motor
// RM = right motor
#define RMF 5 // foward
#define RMB 4 // back
// LM = left motor
#define LMF 6 // foward
#define LMB 7 // back
// Declare servo variables
#define ServoPin 8
#define SC 82 // center positon
#define SL 180 // left
#define SR 0 // right
#define SWait 1000 // wait for the turn to complete
Servo servoControl;
// Sonic sensor variables
// Define sensor pins
#define TriggerPin A0 // used to communicate to sensor to start a ping (send out a sound signal)
#define EchoPin A1 // used to recieve the information from the sensor about the sound reflection (echo)
// Sensor's physical constraints, us = microseconds
#define MaxSensorDist 500 // how far can the sensor detect in cm
#define MaxSensorDelay 5800 // max time in us the sensor should take to send out a signal
#define USTime1CM 57 // time us it takes the soundwave to travel 1cm the and back (2cm total)
#define PingOverhead 5 // overhead us
// Sensor constants
unsigned int NoEcho = MaxSensorDist; // what to return when there is no reflection (over max detection dist)
unsigned int MaxDetectDistCM = 200; // detect up to 200cm, no need for 500cm, saves time
// Function variables
unsigned int MaxEchoDistTime;
unsigned long EchoTimeout;
// --------------------------------------- Arduino
void setup()
{
// set the pins connected to the motor to output
pinMode(RMB, OUTPUT);
pinMode(RMF, OUTPUT);
pinMode(LMB, OUTPUT);
pinMode(LMF, OUTPUT);
// connect the servo
servoControl.attach(ServoPin);
// center the main sensor
servoCenter();
// setup the sensors
sensorSetup();
}
/* Program Loop
* Detect for obstacles, if detected turn, else keep driving straight
*/
int maxWall = 40;
void loop()
{
servoCenter();
if(readDistance() > maxWall)
{
servoLeft();
if(readDistance() > maxWall)
{
servoRight();
if(readDistance() > maxWall)
{
goStraight();
}
else
{
carTurn(SR);
}
}
else
{
carTurn(SL);
}
}
else
{
carTurn(SL);
}
/*
// occasianly turn left or tight (wherever the wall is)
delay(50);
goStraight();
if(readDistance() > 20)
{
goStraight();
}
else
{
goStop();
servoLeft();
if(readDistance() > 20)
{
servoCenter();
while(readDistance() <= 20)
{
goLeft();
}
}
else
{
servoCenter();
while(readDistance() <= 20)
{
goRight();
}
}
}*/
}
void carTurn(int dir)
{
int centerDist = 0;
int sideDist = 0;
while((centerDist < maxWall) && (sideDist > (maxWall+5) || sideDist < (maxWall-5)))
{
if(dir == SR)
{
goLeft();
}
else
{
goRight();
}
delay(100);
goStop();
servoCenter();
centerDist = readDistance();
servoTurn(dir);
sideDist = readDistance();
}
}
// --------------------------------------- Car control
void goStop()
{
// right motor
digitalWrite(RMF, LOW);
digitalWrite(RMB, LOW);
// left motor
digitalWrite(LMF, LOW);
digitalWrite(LMB, LOW);
}
void goStraight()
{
// right motor
digitalWrite(RMF, HIGH);
digitalWrite(RMB, LOW);
// left motor
digitalWrite(LMF, HIGH);
digitalWrite(LMB, LOW);
}
void goBack()
{
// right motor
digitalWrite(RMF, LOW);
digitalWrite(RMB, HIGH);
// left motor
digitalWrite(LMF, LOW);
digitalWrite(LMB, HIGH);
}
void goLeft()
{
// right motor
digitalWrite(RMF, LOW);
digitalWrite(RMB, LOW);
// left motor
digitalWrite(LMF, HIGH);
digitalWrite(LMB, LOW);
}
void goRight()
{
// right motor
digitalWrite(RMF, HIGH);
digitalWrite(RMB, LOW);
// left motor
digitalWrite(LMF, LOW);
digitalWrite(LMB, LOW);
}
void spinLeft()
{
// right motor
digitalWrite(RMF, LOW);
digitalWrite(RMB, HIGH);
// left motor
digitalWrite(LMF, HIGH);
digitalWrite(LMB, LOW);
}
void spinRight()
{
// right motor
digitalWrite(RMF, HIGH);
digitalWrite(RMB, LOW);
// left motor
digitalWrite(LMF, LOW);
digitalWrite(LMB, HIGH);
}
// to do velocity
void travelStraight(int speed, double distance)
{
// keep the velocity in [0 .. 100] range
speed = max(speed, 0);
speed = min(speed, 100);
// right motor
digitalWrite(RMF, HIGH);
digitalWrite(RMB, LOW);
// left motor
digitalWrite(LMF, HIGH);
digitalWrite(LMB, LOW);
}
// --------------------------------------- Servo control
void servoTurn(int deg)
{
servoControl.write(deg);
delay(SWait);
}
void servoCenter()
{
servoControl.write(SC);
delay(SWait);
}
void servoLeft()
{
servoControl.write(SL);
delay(SWait);
}
void servoRight()
{
servoControl.write(SR);
delay(SWait);
}
// --------------------------------------- Sensor control
/* https://github.com/JRodrigoTech/Ultrasonic-HC-SR04/blob/master/Ultrasonic/Ultrasonic.cpp
* https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home
*/
void sensorSetup()
{
// Calculate the max reflection distance in us
MaxEchoDistTime = (MaxDetectDistCM + 1) * USTime1CM;
pinMode(EchoPin, INPUT);
pinMode(TriggerPin, OUTPUT);
// Allow for the analog pins to setup
delay(1000);
// Read a few values for the sensor calibration
for(int i = 0; i < 4; i++)
{
readDistance();
delay(200);
}
}
int readDistance()
{
delay(100);
return sonarSignal() != 0 ? sonarSignal() : NoEcho;
}
// sound filter values between 0 .. 60
boolean sendOutSoundWave()
{
// set the trigger pin for the sensor to output
pinMode(TriggerPin, OUTPUT);
// the trigger pin should be off (low) at the start
digitalWrite(TriggerPin, LOW);
// wait for the pin to go low
delayMicroseconds(10);
// turn on the sensor by sending a high voltage through the pin = trigger the sensor to send out a sound signal (ping)
digitalWrite(TriggerPin, HIGH);
// wait for the sensor to detect the high on from the pin
delayMicroseconds(10);
// turn off the pin
digitalWrite(TriggerPin, LOW);
pinMode(TriggerPin, INPUT);
// if the pin is sending data in, the previous read hasn't completed yet
if (digitalRead(EchoPin))
{
return false;
}
else
{
// Set the timeout for the signal to be send
EchoTimeout = micros() + MaxEchoDistTime + MaxSensorDelay;
// Wait for the signal to send
while (!digitalRead(EchoPin))
{
// If it takes too long, return
if (micros() > EchoTimeout)
{
return false;
}
}
// Singal sent, start the reflection (echo) timeout
EchoTimeout = micros() + MaxEchoDistTime;
return true;
}
}
unsigned int sonarSignal()
{
boolean signalSent = sendOutSoundWave();
// If something went wrong, no signal was sent, return
if (signalSent == false)
{
return NoEcho;
}
else
{
// Read from the pin = wait for the sound signal to come back (the echo = the reflection)
while (digitalRead(EchoPin))
{
// If it timesout it means we are beyond the max distance
if (micros() > EchoTimeout)
{
return NoEcho;
}
}
return ((micros() - (EchoTimeout - MaxEchoDistTime) - PingOverhead) / USTime1CM); // Calculate ping time, include overhead.
}
}