-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr2dr.ino
670 lines (538 loc) · 14.3 KB
/
r2dr.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
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
#include "QTRSensors_teensy3.h"
#include "DRV8835_teensy3.h"
#include <Encoder.h>
#include <Pushbutton.h>
//#define DISABLE_SOUND
const uint8_t NumLineSensors = 3;
const int16_t LineSensorTimeout = 1000;
const int16_t LineSensorMin = 200;
const int16_t LineSensorMax = 1000;
const int16_t DomeSensorMin = 200;
const int16_t DomeSensorMax = 400;
QTRSensorsRC lineSensors((uint8_t[]) {14, 15, 16}, NumLineSensors, LineSensorTimeout);
uint16_t lineSensorValues[NumLineSensors];
QTRSensorsRC domeSensor((uint8_t[]) {17}, 1, LineSensorTimeout);
uint16_t domeSensorValue;
DRV8835 driveMotors(7, 5, 8, 6);
DRV8835 domeMotor(10, 9, 10, 9);
const int16_t MaxSpeed = 255;
int16_t lCount, rCount;
uint8_t lEncErr, rEncErr;
Encoder domeEnc(22, 23);
// ~380:1 gearbox * 12 CPR(motor)
// 4550 is exact; see gear ratio at https://www.pololu.com/product/4790
const int16_t DomeCPR = 4550;
Pushbutton btn(0);
enum { WaitForButton, FindLine, FollowLine, Decel, TurnToHome, GoHome } state;
enum
{
ALARM7, // do not use - same as startsnd
ALARM8, // finish
HUM1,
HUM2,
MISC1,
MISC14,
MISC17,
MISC29,
MISC32,
MISC33,
MISC35, // end of line
PROC7, // start
RAZZ5,
SCREAM1,
STARTSND, // power on
WHIST1,
WHIST14,
WHIST15,
WHIST16
} sounds;
const uint8_t IdleSounds[] = { HUM1, HUM2, MISC1, MISC17, MISC29, MISC32, MISC33, RAZZ5, WHIST1, WHIST14, WHIST15, WHIST16 };
const uint8_t IdleSoundCount = 12;
int16_t ls, rs;
const uint16_t AngleScale = 20000;
const uint16_t StepsPerRadian = 960; //1075;
const uint8_t LeapTickPerL = 0;
const uint8_t LeapTickPerR = 200;
const int32_t HomeMaxY = 13000000L;
const int16_t HomeMaxS = 15000L;
int16_t c, s;
int32_t x, y;
#define sign(x) ((x) < 0 ? -1 : 1)
void followLine(int16_t accelMaxSpeed = 255);
const uint8_t SomoClk = 3;
const uint8_t SomoData = 4;
void setup()
{
randomSeed(analogRead(14));
Serial.begin(115200);
setSensorCalibration();
driveMotors.init(20000);
domeMotor.init(20000);
pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
pinMode(21, INPUT_PULLUP);
pinMode(20, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(18), tickL, CHANGE);
attachInterrupt(digitalPinToInterrupt(19), tickL, CHANGE);
attachInterrupt(digitalPinToInterrupt(21), tickR, CHANGE);
attachInterrupt(digitalPinToInterrupt(20), tickR, CHANGE);
lEncErr = 0;
rEncErr = 0;
pinMode(13, OUTPUT);
somoInit();
somoCmd(STARTSND);
homeDome();
state = WaitForButton;
}
uint16_t lastSoundMs = 0;
void loop()
{
/*Serial.print(lCount);
Serial.print('\t');
Serial.print(rCount);
Serial.print('\t');
Serial.print(lEncErr);
Serial.print('\t');
Serial.print(rEncErr);
Serial.println();
delay(10);
return;*/
static uint16_t accelStartMs = 0;
static uint16_t stateStartMs = 0;
static bool accel = false;
static int16_t accelMaxSpeed = 0;
updateDome();
checkFall();
idleSounds();
if (accel)
{
accelMaxSpeed = (uint16_t)(millis() - accelStartMs) / 4; // 0 to 255 over ~1 second
if (accelMaxSpeed >= MaxSpeed)
{
accelMaxSpeed = MaxSpeed;
accel = false;
}
}
switch (state)
{
case WaitForButton:
if (btn.getSingleDebouncedRelease())
{
digitalWrite(13, HIGH);
delay(1000);
c = AngleScale;
s = 0;
x = 0, y = 0;
state = FindLine;
accel = true;
accelStartMs = millis();
somoCmd(PROC7);
}
break;
case FindLine:
driveMotors.setSpeeds(accelMaxSpeed, accelMaxSpeed);
lineSensors.readCalibrated(lineSensorValues);
if (onLine())
{
state = FollowLine;
stateStartMs = millis();
}
break;
case FollowLine:
followLine(accelMaxSpeed);
// to avoid thinking we're done before we're stable on the line,
// don't look for end of line for 1 second
if ((uint16_t)(millis() - stateStartMs) > 1000 && !onLine())
{
state = Decel;
stateStartMs = millis();
digitalWrite(13, LOW);
somoCmd(MISC35);
}
break;
case Decel:
accelMaxSpeed = MaxSpeed - (uint16_t)(millis() - stateStartMs) / 2; // 255 to 0 over ~0.5 second
if (accelMaxSpeed < 0) { accelMaxSpeed = 0; }
if (ls > accelMaxSpeed) { ls = accelMaxSpeed; }
if (rs > accelMaxSpeed) { rs = accelMaxSpeed; }
driveMotors.setSpeeds(ls, rs);
if (accelMaxSpeed == 0)
{
//printDebug();
transform();
//printDebug();
state = TurnToHome;
digitalWrite(13, HIGH);
}
break;
case TurnToHome:
turnToHome();
if (abs(s) < (AngleScale / 18))
{
// pointing within 5 degrees of home direction
state = GoHome;
accel = true;
accelStartMs = millis();
digitalWrite(13, LOW);
}
break;
case GoHome:
goHome(accelMaxSpeed);
if (x > -5000000)
{
driveMotors.setSpeeds(0, 0);
state = WaitForButton;
somoCmd(ALARM8);
}
break;
}
}
void homeDome()
{
// find + edge:
domeMotor.setMASpeed(100);
// wait for black
while (1)
{
domeSensor.readCalibrated(&domeSensorValue);
if (domeSensorValue > 500) { break; }
}
domeMotor.setMASpeed(50);
// wait for white
while (1)
{
domeSensor.readCalibrated(&domeSensorValue);
if (domeSensorValue < 500) { break; }
}
domeMotor.setMASpeed(0);
delay(100);
int16_t posP = domeEnc.read();
// find - edge:
domeMotor.setMASpeed(-50);
// wait for black
while (1)
{
domeSensor.readCalibrated(&domeSensorValue);
if (domeSensorValue > 500) { break; }
}
// wait for white
while (1)
{
domeSensor.readCalibrated(&domeSensorValue);
if (domeSensorValue < 500) { break; }
}
domeMotor.setMASpeed(0);
delay(100);
int16_t posM = domeEnc.read();
// black bar is (posP - posM) wide, and we're now on the - side of it,
// so our current position should be:
domeEnc.write(-(posP - posM) / 2);
}
void updateDome()
{
static int8_t lastCount = 0; // last enc reading
static int16_t wrappedCount = 0;
static uint16_t lastDomeMoveMs = 0;
int8_t count = domeEnc.read();
wrappedCount += (int8_t)(lastCount - count);
lastCount = count;
if (wrappedCount > (DomeCPR/2)) { wrappedCount -= DomeCPR; }
if (wrappedCount < -(DomeCPR/2)) { wrappedCount += DomeCPR; }
const uint8_t MovePeriod = 40; // 40 ms = 25 Hz
uint16_t now = millis();
if ((uint16_t)(now - lastDomeMoveMs) >= MovePeriod)
{
lastDomeMoveMs += MovePeriod;
float domeTarget;
switch (state)
{
case FindLine:
case FollowLine:
case Decel:
case TurnToHome:
case GoHome:
domeTarget = atan2(s, -c) + atan2(y, x);
break;
default:
domeTarget = 0;
}
//domeTarget = ((millis() >> 11) & 1) ? domeTarget : 0;
int16_t targetCount = domeTarget * DomeCPR / 2 / PI;
if (targetCount > DomeCPR/2) { targetCount -= DomeCPR; }
if (targetCount < -DomeCPR/2) { targetCount += DomeCPR; }
int16_t countDiff = (wrappedCount - targetCount);
if (countDiff > DomeCPR/2) { countDiff -= DomeCPR; }
if (countDiff < -DomeCPR/2) { countDiff += DomeCPR; }
int16_t domeSpeed = -countDiff;
domeSpeed = constrain(domeSpeed, -MaxSpeed, MaxSpeed);
domeMotor.setMASpeed(domeSpeed);
}
}
void setSensorCalibration()
{
lineSensors.calibrate(); // force allocate calibrated values
for (uint8_t i = 0; i < NumLineSensors; i++)
{
lineSensors.calibratedMinimumOn[i] = LineSensorMin;
lineSensors.calibratedMaximumOn[i] = LineSensorMax;
}
domeSensor.calibrate();
domeSensor.calibratedMinimumOn[0] = DomeSensorMin;
domeSensor.calibratedMaximumOn[0] = DomeSensorMax;
}
/*
18 B3
19 B2
20 D5
21 D6
*/
void tickL()
{
static uint8_t lastL1 = 0, lastL2 = 0;
uint8_t newL1 = ((GPIOB_PDIR & 0x08) != 0);
uint8_t newL2 = ((GPIOB_PDIR & 0x04) != 0);
if((lastL1 ^ newL1) & (lastL2 ^ newL2)) { lEncErr++; }
int8_t dLCount = (lastL1 ^ newL2) - (int)(newL1 ^ lastL2);
updateLWheelEncoder(dLCount);
lCount += dLCount;
lastL1 = newL1;
lastL2 = newL2;
}
void tickR()
{
static uint8_t lastR1 = 0, lastR2 = 0;
uint8_t newR1 = ((GPIOD_PDIR & 0x40) != 0);
uint8_t newR2 = ((GPIOD_PDIR & 0x20) != 0);
if((lastR1 ^ newR1) & (lastR2 ^ newR2)) { rEncErr++; }
int8_t dRCount = (lastR1 ^ newR2) - (int)(newR1 ^ lastR2);
updateRWheelEncoder(dRCount);
rCount += dRCount;
lastR1 = newR1;
lastR2 = newR2;
}
void updateLWheelEncoder(int8_t dLCount)
{
static uint8_t leapLCount = 0;
if (dLCount != 0)
{
if (LeapTickPerL > 0)
{
leapLCount += dLCount;
if (leapLCount >= LeapTickPerL)
{
dLCount += sign(dLCount);
leapLCount -= LeapTickPerL;
}
}
int16_t dc = + divide(dLCount*s - dLCount*c/2/StepsPerRadian, StepsPerRadian);
int16_t ds = - divide(dLCount*c + dLCount*s/2/StepsPerRadian, StepsPerRadian);
c += dc;
s += ds;
c = constrain(c, -AngleScale, AngleScale);
s = constrain(s, -AngleScale, AngleScale);
x += dLCount * c;
y += dLCount * s;
}
}
void updateRWheelEncoder(int8_t dRCount)
{
static uint8_t leapRCount = 0;
if (dRCount != 0)
{
if (LeapTickPerR > 0)
{
leapRCount += dRCount;
if (leapRCount >= LeapTickPerR)
{
dRCount += sign(dRCount);
leapRCount -= LeapTickPerR;
}
}
int16_t dc = - divide(dRCount*s + dRCount*c/2/StepsPerRadian, StepsPerRadian);
int16_t ds = + divide(dRCount*c - dRCount*s/2/StepsPerRadian, StepsPerRadian);
c += dc;
s += ds;
c = constrain(c, -AngleScale, AngleScale);
s = constrain(s, -AngleScale, AngleScale);
x += dRCount * c;
y += dRCount * s;
}
}
int32_t divide(int32_t a, int32_t b)
{
return (a + sign(a)*(b/2-1)) / b;
}
void transform()
{
double r = hypot((double)x, (double)y);
double nx = (double)x/r; // x = cos(tt)
double ny = (double)y/r; // y = sin(tt)
int32_t new_c = -nx*c-ny*s; // -cos(tt)*cos(f)-sin(tt)*sin(f) = -cos(tt-f) = cos(180-(tt+f))
int32_t new_s = ny*c-nx*s; // sin(tt)*cos(f)-cos(tt)*sin(f) = sin(tt-f) = sin(180 - (tt+f))
c = new_c;
s = new_s;
y = 0;
x = -r;
}
void turnToHome()
{
const int16_t TurnMaxDiff = MaxSpeed / 2;
int32_t err;
if (c < 0)
{
// pointed backwards
err = (s > 0 ? TurnMaxDiff : -TurnMaxDiff);
}
else
{
int32_t target_s = -(y / 10000 * HomeMaxS / (HomeMaxY / 10000));
target_s = constrain(target_s, -HomeMaxS, HomeMaxS);
err = (s - target_s) / 20;
err = constrain(err, -TurnMaxDiff, TurnMaxDiff);
}
driveMotors.setSpeeds(err, -err);
}
void goHome(int16_t accelMaxSpeed)
{
int16_t speed = accelMaxSpeed;
int32_t err;
if (x > -20000000)
{
speed /= 4;
}
/*if (c < 0)
{
// pointed backwards
err = (s > 0 ? speed/2 : -speed/2);
}
else*/
{
int32_t target_s = -(y / 10000 * HomeMaxS / (HomeMaxY / 10000));
target_s = constrain(target_s, -HomeMaxS, HomeMaxS);
err = (s - target_s) / 100;
err = constrain(err, -speed, speed);
}
if (err > 0)
{
driveMotors.setSpeeds(speed, speed - err);
}
else
{
driveMotors.setSpeeds(speed + err, speed);
}
}
boolean onLine()
{
return (lineSensorValues[0] > 500) || (lineSensorValues[1] > 500) || (lineSensorValues[2] > 500);
}
void followLine(int16_t accelMaxSpeed)
{
static uint16_t lastProportional = 0;
static long integral = 0;
const int16_t LfMaxSpeed = accelMaxSpeed, LfMaxDiff = accelMaxSpeed;
// Get the position of the line. Note that we *must* provide
// the "sensors" argument to read_line() here, even though we
// are not interested in the individual sensor readings.
uint16_t position = lineSensors.readLine(lineSensorValues);
// The "proportional" term should be 0 when we are on the line.
int16_t proportional = ((int)position) - 1000;
// Compute the derivative (change) and integral (sum) of the
// position.
int16_t derivative = proportional - lastProportional;
integral += proportional;
// Remember the last position.
lastProportional = proportional;
// Compute the difference between the two motor power settings,
// m1 - m2. If this is a positive number the robot will turn
// to the right. If it is a negative number, the robot will
// turn to the left, and the magnitude of the number determines
// the sharpness of the turn.
int16_t power_difference = proportional/4;// + derivative*6;
// Compute the actual motor settings. We never set either motor
// to a negative value.
if (power_difference > LfMaxDiff) { power_difference = LfMaxDiff; }
if (power_difference < -LfMaxDiff) { power_difference = -LfMaxDiff; }
if (power_difference < 0)
{
ls = LfMaxSpeed + power_difference;
rs = LfMaxSpeed;
}
else
{
ls = LfMaxSpeed;
rs = LfMaxSpeed - power_difference;
}
driveMotors.setSpeeds(ls, rs);
}
void printDebug()
{
Serial.print(s); Serial.print("\t");
Serial.print(c); Serial.print("\t");
Serial.print(hypot(s, c)); Serial.print("\t");
Serial.print(atan2(s, c) * 180 / PI); Serial.print("\t");
Serial.print(x); Serial.print("\t");
Serial.print(y); Serial.print("\t");
Serial.println();
}
void somoInit()
{
pinMode(SomoClk, OUTPUT);
digitalWrite(SomoClk, HIGH);
pinMode(SomoData, OUTPUT);
digitalWrite(SomoData, HIGH);
while (millis() < 1200) {}
}
void somoCmd(uint16_t cmd)
{
#ifndef DISABLE_SOUND
lastSoundMs = millis();
digitalWrite(SomoClk, LOW);
delay(2);
for (int8_t b = 15; b >= 0; b--)
{
digitalWrite(SomoClk, LOW);
digitalWrite(SomoData, (cmd >> b) & 1);
delayMicroseconds(90);
digitalWrite(SomoClk, HIGH);
delayMicroseconds(90);
}
delay(2);
#endif
}
void idleSounds()
{
static uint16_t timeToNextSound = random(7000, 20000);
if ((uint16_t)(millis() - lastSoundMs) >= timeToNextSound)
{
somoCmd(IdleSounds[random(0, IdleSoundCount)]);
timeToNextSound = random(7000, 20000);
}
}
void checkFall()
{
static uint16_t lastCheckMs = millis();
static uint8_t count = 0;
uint16_t now = millis();
if (state != WaitForButton && (uint16_t)(now - lastCheckMs) >= 100)
{
lineSensors.readCalibrated(lineSensorValues);
if (lineSensorValues[0] > 900 &&
lineSensorValues[1] > 900 &&
lineSensorValues[2] > 900)
{
count++;
if (count == 3)
{
driveMotors.setSpeeds(0, 0);
domeMotor.setMASpeed(0);
somoCmd(SCREAM1);
while (1) {}
}
}
else
{
count = 0;
}
lastCheckMs = now;
}
}