-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zumo_Drive_hit_detect.ino
512 lines (457 loc) · 14.6 KB
/
Zumo_Drive_hit_detect.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
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
/*
MIT License
Copyright (c) 2024 [Your Name]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <Wire.h>
#include <ZumoShield.h>
#define LED 13
// Accelerometer Settings
#define RA_SIZE 3 // number of readings to include in running average of accelerometer readings
#define XY_ACCELERATION_THRESHOLD 2800 // for detection of contact (~16000 = magnitude of acceleration due to gravity)
// Motor Settings
ZumoMotors motors;
// these might need to be tuned for different motor types
#define REVERSE_SPEED 400 // 0 is stopped, 400 is full speed
#define TURN_SPEED 400
#define SEARCH_SPEED 400
#define SUSTAINED_SPEED 400 // switches to SUSTAINED_SPEED from FULL_SPEED after FULL_SPEED_DURATION_LIMIT ms
#define FULL_SPEED 400
#define STOP_DURATION 150 // ms
#define REVERSE_DURATION 200 // ms
#define TURN_DURATION 200 // ms
#define RIGHT 1
#define LEFT -1
// Adjustments to balance motor speeds for straighter driving
#define LEFT_MOTOR_ADJUSTMENT 4 // Increase left motor speed
#define RIGHT_MOTOR_ADJUSTMENT -5 // Decrease right motor speed
enum ForwardSpeed { SearchSpeed, SustainedSpeed, FullSpeed };
ForwardSpeed _forwardSpeed; // current forward speed setting
unsigned long full_speed_start_time;
#define FULL_SPEED_DURATION_LIMIT 250 // ms
// Sound Effects
ZumoBuzzer buzzer;
const char sound_effect[] PROGMEM = "O4 T100 V15 L4 MS g12>c12>e12>G6>E12 ML>G2"; // "charge" melody
// Timing
unsigned long loop_start_time;
unsigned long last_turn_time;
unsigned long contact_made_time;
#define MIN_DELAY_AFTER_TURN 200 // ms = min delay before detecting contact event
#define MIN_DELAY_BETWEEN_CONTACTS 500 // ms = min delay between detecting new contact event
// RunningAverage class
// based on RunningAverage library for Arduino
template <typename T>
class RunningAverage
{
public:
RunningAverage(void);
RunningAverage(int);
~RunningAverage();
void clear();
void addValue(T);
T getAverage() const;
void fillValue(T, int);
protected:
int _size;
int _cnt;
int _idx;
T _sum;
T * _ar;
static T zero;
};
// Accelerometer Class -- extends the ZumoIMU class to support reading and averaging the x-y acceleration
// vectors from the accelerometer
class Accelerometer : public ZumoIMU
{
typedef struct acc_data_xy
{
unsigned long timestamp;
int x;
int y;
float dir;
} acc_data_xy;
public:
Accelerometer() : ra_x(RA_SIZE), ra_y(RA_SIZE) {};
~Accelerometer() {};
void readAcceleration(unsigned long timestamp);
float len_xy() const;
float dir_xy() const;
int x_avg(void) const;
int y_avg(void) const;
long ss_xy_avg(void) const;
float dir_xy_avg(void) const;
private:
acc_data_xy last;
RunningAverage<int> ra_x;
RunningAverage<int> ra_y;
};
Accelerometer acc;
boolean in_contact; // set when accelerometer detects contact with an obstacle
// forward declaration
void setForwardSpeed(ForwardSpeed speed);
void executeTurnPattern(int pattern);
void setup()
{
// Initialize the Wire library and join the I2C bus as a master
Wire.begin();
// Initialize accelerometer
acc.init();
acc.enableDefault();
randomSeed((unsigned int) millis());
// uncomment if necessary to correct motor directions
//motors.flipLeftMotor(true);
//motors.flipRightMotor(true);
pinMode(LED, HIGH);
buzzer.playMode(PLAY_AUTOMATIC);
// Start immediately without waiting for button press
in_contact = false; // 1 if contact made; 0 if no contact or contact lost
contact_made_time = 0;
last_turn_time = millis(); // prevents false contact detection on initial acceleration
_forwardSpeed = SearchSpeed;
full_speed_start_time = 0;
// Play initial sound effect
buzzer.playFromProgramSpace(sound_effect);
}
void loop()
{
loop_start_time = millis();
acc.readAcceleration(loop_start_time);
if ((_forwardSpeed == FullSpeed) && (loop_start_time - full_speed_start_time > FULL_SPEED_DURATION_LIMIT))
{
setForwardSpeed(SustainedSpeed);
}
// Move straight unless contact is detected
if (check_for_contact()) on_contact_made();
else
{
int speed = getForwardSpeed();
motors.setLeftSpeed(speed + LEFT_MOTOR_ADJUSTMENT);
motors.setRightSpeed(speed + RIGHT_MOTOR_ADJUSTMENT);
}
}
void setForwardSpeed(ForwardSpeed speed)
{
_forwardSpeed = speed;
if (speed == FullSpeed) full_speed_start_time = loop_start_time;
}
int getForwardSpeed()
{
int speed;
switch (_forwardSpeed)
{
case FullSpeed:
speed = FULL_SPEED;
break;
case SustainedSpeed:
speed = SUSTAINED_SPEED;
break;
default:
speed = SEARCH_SPEED;
break;
}
return speed;
}
// check for contact, but ignore readings immediately after turning or losing contact
bool check_for_contact()
{
static long threshold_squared = (long) XY_ACCELERATION_THRESHOLD * (long) XY_ACCELERATION_THRESHOLD;
return (acc.ss_xy_avg() > threshold_squared) && \
(loop_start_time - last_turn_time > MIN_DELAY_AFTER_TURN) && \
(loop_start_time - contact_made_time > MIN_DELAY_BETWEEN_CONTACTS);
}
// sound horn and accelerate on contact -- fight or flight
void on_contact_made()
{
in_contact = true;
contact_made_time = loop_start_time;
setForwardSpeed(FullSpeed);
buzzer.playFromProgramSpace(sound_effect);
// Execute a random turn pattern after detecting contact
executeTurnPattern(random(0, 20));
}
// reset forward speed
void on_contact_lost()
{
in_contact = false;
setForwardSpeed(SearchSpeed);
}
// execute one of twenty random turn patterns
void executeTurnPattern(int pattern)
{
// assume contact lost
on_contact_lost();
switch (pattern)
{
case 0:
// Pattern 1: Simple right turn
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(TURN_SPEED, -TURN_SPEED);
delay(TURN_DURATION);
break;
case 1:
// Pattern 2: Simple left turn
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(-TURN_SPEED, TURN_SPEED);
delay(TURN_DURATION);
break;
case 2:
// Pattern 3: Reverse, then spin right
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(TURN_SPEED, -TURN_SPEED);
delay(TURN_DURATION * 2);
break;
case 3:
// Pattern 4: Reverse, then spin left
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(-TURN_SPEED, TURN_SPEED);
delay(TURN_DURATION * 2);
break;
case 4:
// Pattern 5: Reverse, small right turn, then forward
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(TURN_SPEED / 2, -TURN_SPEED / 2);
delay(TURN_DURATION / 2);
break;
case 5:
// Pattern 6: Reverse, small left turn, then forward
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(-TURN_SPEED / 2, TURN_SPEED / 2);
delay(TURN_DURATION / 2);
break;
case 6:
// Pattern 7: Reverse, right turn, reverse again, then forward
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(TURN_SPEED, -TURN_SPEED);
delay(TURN_DURATION);
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION / 2);
break;
case 7:
// Pattern 8: Reverse, left turn, reverse again, then forward
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(-TURN_SPEED, TURN_SPEED);
delay(TURN_DURATION);
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION / 2);
break;
case 8:
// Pattern 9: Reverse, wide right turn
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(TURN_SPEED, -TURN_SPEED / 2);
delay(TURN_DURATION * 2);
break;
case 9:
// Pattern 10: Reverse, wide left turn
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(-TURN_SPEED, TURN_SPEED / 2);
delay(TURN_DURATION * 2);
break;
case 10:
// Pattern 11: Reverse, short right turn, then forward
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(TURN_SPEED, -TURN_SPEED);
delay(TURN_DURATION / 2);
break;
case 11:
// Pattern 12: Reverse, short left turn, then forward
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(-TURN_SPEED, TURN_SPEED);
delay(TURN_DURATION / 2);
break;
case 12:
// Pattern 13: Reverse, spin right, then short left
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(TURN_SPEED, -TURN_SPEED);
delay(TURN_DURATION);
motors.setSpeeds(-TURN_SPEED / 2, TURN_SPEED / 2);
delay(TURN_DURATION / 2);
break;
case 13:
// Pattern 14: Reverse, spin left, then short right
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(-TURN_SPEED, TURN_SPEED);
delay(TURN_DURATION);
motors.setSpeeds(TURN_SPEED / 2, -TURN_SPEED / 2);
delay(TURN_DURATION / 2);
break;
case 14:
// Pattern 15: Reverse, zigzag right
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(TURN_SPEED, -TURN_SPEED);
delay(TURN_DURATION / 2);
motors.setSpeeds(-TURN_SPEED, TURN_SPEED);
delay(TURN_DURATION / 2);
break;
case 15:
// Pattern 16: Reverse, zigzag left
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(-TURN_SPEED, TURN_SPEED);
delay(TURN_DURATION / 2);
motors.setSpeeds(TURN_SPEED, -TURN_SPEED);
delay(TURN_DURATION / 2);
break;
case 16:
// Pattern 17: Reverse, short right, then spin left
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(TURN_SPEED / 2, -TURN_SPEED / 2);
delay(TURN_DURATION / 2);
motors.setSpeeds(-TURN_SPEED, TURN_SPEED);
delay(TURN_DURATION);
break;
case 17:
// Pattern 18: Reverse, short left, then spin right
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(-TURN_SPEED / 2, TURN_SPEED / 2);
delay(TURN_DURATION / 2);
motors.setSpeeds(TURN_SPEED, -TURN_SPEED);
delay(TURN_DURATION);
break;
case 18:
// Pattern 19: Reverse, right, short left, then forward
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(TURN_SPEED, -TURN_SPEED);
delay(TURN_DURATION);
motors.setSpeeds(-TURN_SPEED / 2, TURN_SPEED / 2);
delay(TURN_DURATION / 2);
break;
case 19:
// Pattern 20: Reverse, left, short right, then forward
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(-TURN_SPEED, TURN_SPEED);
delay(TURN_DURATION);
motors.setSpeeds(TURN_SPEED / 2, -TURN_SPEED / 2);
delay(TURN_DURATION / 2);
break;
}
setForwardSpeed(SearchSpeed);
int speed = getForwardSpeed();
motors.setLeftSpeed(speed + LEFT_MOTOR_ADJUSTMENT);
motors.setRightSpeed(speed + RIGHT_MOTOR_ADJUSTMENT);
last_turn_time = millis();
}
// class Accelerometer -- member function definitions
void Accelerometer::readAcceleration(unsigned long timestamp)
{
readAcc();
if (a.x == last.x && a.y == last.y) return;
last.timestamp = timestamp;
last.x = a.x;
last.y = a.y;
ra_x.addValue(last.x);
ra_y.addValue(last.y);
}
float Accelerometer::len_xy() const
{
return sqrt(last.x*a.x + last.y*a.y);
}
float Accelerometer::dir_xy() const
{
return atan2(last.x, last.y) * 180.0 / M_PI;
}
int Accelerometer::x_avg(void) const
{
return ra_x.getAverage();
}
int Accelerometer::y_avg(void) const
{
return ra_y.getAverage();
}
long Accelerometer::ss_xy_avg(void) const
{
long x_avg_long = static_cast<long>(x_avg());
long y_avg_long = static_cast<long>(y_avg());
return x_avg_long*x_avg_long + y_avg_long*y_avg_long;
}
float Accelerometer::dir_xy_avg(void) const
{
return atan2(static_cast<float>(x_avg()), static_cast<float>(y_avg())) * 180.0 / M_PI;
}
// RunningAverage class
// based on RunningAverage library for Arduino
template <typename T>
T RunningAverage<T>::zero = static_cast<T>(0);
template <typename T>
RunningAverage<T>::RunningAverage(int n)
{
_size = n;
_ar = (T*) malloc(_size * sizeof(T));
clear();
}
template <typename T>
RunningAverage<T>::~RunningAverage()
{
free(_ar);
}
// resets all counters
template <typename T>
void RunningAverage<T>::clear()
{
_cnt = 0;
_idx = 0;
_sum = zero;
for (int i = 0; i< _size; i++) _ar[i] = zero; // needed to keep addValue simple
}
// adds a new value to the data-set
template <typename T>
void RunningAverage<T>::addValue(T f)
{
_sum -= _ar[_idx];
_ar[_idx] = f;
_sum += _ar[_idx];
_idx++;
if (_idx == _size) _idx = 0; // faster than %
if (_cnt < _size) _cnt++;
}
// returns the average of the data-set added so far
template <typename T>
T RunningAverage<T>::getAverage() const
{
if (_cnt == 0) return zero; // NaN ? math.h
return _sum / _cnt;
}
// fill the average with a value
// the param number determines how often value is added (weight)
// number should preferably be between 1 and size
template <typename T>
void RunningAverage<T>::fillValue(T value, int number)
{
clear();
for (int i = 0; i < number; i++)
{
addValue(value);
}
}