-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
645 lines (524 loc) · 12.2 KB
/
main.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
#include <Servo.h>
int LEFT_TIP_TOE_POS = 60;
int RIGHT_TIP_TOE_POS = 120;
int MAX_POS = 160;
int MIN_POS = 20;
class JointCtrl {
public:
int start = 90;
int current = 90;
int target = 90;
int rate = 1;
Servo joint;
void attach(Servo _joint)
{
joint = _joint;
}
void reset()
{
joint.write(start);
}
int increment()
{
if(current + rate > target)
{
return target - current;
}
return rate;
}
int decrement()
{
if(current - rate < target)
{
return current - target;
}
return rate;
}
void step()
{
if(current != target && target >= MIN_POS && target <= MAX_POS)
{
if(current < target)
{
// Serial.println("stepping increment: ");
// Serial.println(increment());
// current = current + 1;
current = current + increment();
}
if(current > target)
{
// Serial.println("stepping decrement: ");
// Serial.println(decrement());
// current = current - 1;
current = current - decrement();
}
joint.write(current);
}
}
boolean hasReachedTarget()
{
return current == target;
}
};
class BodyCtrl {
public:
int idxLeftLeg = 0;
int idxLeftFoot = 1;
int idxRightLeg = 2;
int idxRightFoot = 3;
JointCtrl* joints[4];
JointCtrl* leftLeg()
{
return joints[idxLeftLeg];
}
JointCtrl* leftFoot()
{
return joints[idxLeftFoot];
}
JointCtrl* rightLeg()
{
return joints[idxRightLeg];
}
JointCtrl* rightFoot()
{
return joints[idxRightFoot];
}
void setJoints(JointCtrl* _leftLeg, JointCtrl* _leftFoot, JointCtrl* _rightLeg, JointCtrl* _rightFoot)
{
joints[idxLeftLeg] = _leftLeg;
joints[idxLeftFoot] = _leftFoot;
joints[idxRightLeg] = _rightLeg;
joints[idxRightFoot] = _rightFoot;
}
void reset()
{
for(int i = 0; i < 4; i++)
{
joints[i]->reset();
}
}
void setAllTarget(int target)
{
for(int i = 0; i < 4; i++)
{
joints[i]->target = target;
}
}
void setTargets(int LL, int LF, int RL, int RF, int rate)
{
joints[idxLeftLeg]->target = LL;
joints[idxLeftLeg]->rate = rate;
joints[idxLeftFoot]->target = LF;
joints[idxLeftFoot]->rate = rate;
joints[idxRightLeg]->target = RL;
joints[idxRightLeg]->rate = rate;
joints[idxRightFoot]->target = RF;
joints[idxRightLeg]->rate = rate;
}
boolean hasReachedTarget()
{
for(int i = 0; i < 4; i++)
{
if(!joints[i]->hasReachedTarget())
{
return false;
}
}
return true;
}
void step()
{
for(int i = 0; i < 4; i++)
{
joints[i]->step();
}
delay(20);
}
void tipToe()
{
JointCtrl *l = leftFoot();
l->target = LEFT_TIP_TOE_POS;
JointCtrl *r = rightFoot();
r->target = RIGHT_TIP_TOE_POS;
}
boolean isTipToe()
{
JointCtrl *l = leftFoot();
JointCtrl *r = rightFoot();
if(l->current == LEFT_TIP_TOE_POS && r->current == RIGHT_TIP_TOE_POS)
{
return true;
}
return false;
}
boolean isHome()
{
for(int i = 0; i < 4; i++)
{
if(joints[i]->current != joints[i]->start)
{
return false;
}
}
return true;
}
};
class Action {
public:
int leftLegPos;
int leftFootPos;
int rightLegPos;
int rightFootPos;
int rate;
Action() {
leftFootPos = 90;
leftLegPos = 90;
rightLegPos = 90;
rightFootPos = 90;
rate = 1;
}
};
class ActionLink {
public:
Action current;
ActionLink* next;
ActionLink(Action a)
{
current = a;
next = nullptr;
}
};
class ActionManager {
public:
int size = 5;
Action* queue[5];
int top = -1;
BodyCtrl* bodyCtrl;
boolean done;
ActionLink* head;
ActionLink* tail;
ActionManager()
{
head = nullptr;
tail = nullptr;
bodyCtrl = nullptr;
done = false;
}
void next()
{
boolean reachedTargets = bodyCtrl->hasReachedTarget();
if(reachedTargets)
{
Action* a = pop();
if(a != nullptr)
{
Serial.println("setting targets...");
Serial.println(a->leftLegPos);
Serial.println(a->leftFootPos);
Serial.println(a->rightLegPos);
Serial.println(a->rightFootPos);
Serial.println(a->rate);
done = false;
bodyCtrl->setTargets(a->leftLegPos, a->leftFootPos, a->rightLegPos, a->rightFootPos, a->rate);
} else {
done = true;
}
}
bodyCtrl->step();
}
void add(Action action)
{
done = false;
ActionLink* link = new ActionLink(action);
link->current = action;
if(head == nullptr)
{
Serial.println("setting first link...");
head = link;
tail = link;
} else {
Serial.println("appending link to tail...");
tail->next = link;
tail = link;
}
}
Action* pop()
{
if(head == nullptr || tail == nullptr)
{
return nullptr;
}
ActionLink* tmp = head;
if(head->next == nullptr)
{
Serial.println("setting head and tail to null");
head = nullptr;
tail = nullptr;
}else{
head = head->next;
}
return &tmp->current;
}
};
ActionManager* actionManager = new ActionManager();
int servoPinLegRight = 2;
int servoPinLegLeft = 3;
int servoPinFootRight = 4;
int servoPinFootLeft = 5;
// ultrasonic
int trigPin = 8; // TRIG pin
int echoPin = 9; // ECHO pin
float duration_us, distance_cm;
Action actionBallerina()
{
Action a = Action();
a.leftFootPos = 60;
a.leftLegPos = 120;
a.rightFootPos = 120;
a.rightLegPos = 60;
return a;
}
Action actionReverseBallerina()
{
Action a = Action();
a.leftFootPos = 120;
a.leftLegPos = 60;
a.rightFootPos = 60;
a.rightLegPos = 120;
return a;
}
Action actionHipFlexionOut()
{
Action a = Action();
a.leftFootPos = 90;
a.leftLegPos = 120;
a.rightFootPos = 90;
a.rightLegPos = 60;
return a;
}
Action actionHipFlexionIn()
{
Action a = Action();
a.leftFootPos = 90;
a.leftLegPos = 60;
a.rightFootPos = 90;
a.rightLegPos = 120;
return a;
}
Action actionTipToe()
{
Action a = Action();
a.leftFootPos = 120;
a.leftLegPos = 90;
a.rightFootPos = 60;
a.rightLegPos = 90;
return a;
}
Action actionRightTipToe()
{
Action a = Action();
a.rightFootPos = 60;
a.rightLegPos = 90;
return a;
}
Action actionLeftTipToe()
{
Action a = Action();
a.leftFootPos = 120;
a.leftLegPos = 90;
return a;
}
Action actionReverseTipToe()
{
Action a = Action();
a.leftFootPos = 60;
a.leftLegPos = 90;
a.rightFootPos = 120;
a.rightLegPos = 90;
return a;
}
Action actionReset()
{
Action a = Action();
a.leftFootPos = 90;
a.leftLegPos = 90;
a.rightFootPos = 90;
a.rightLegPos = 90;
return a;
}
Action actionRightLeg(int hip, int foot)
{
Action a = actionReset();
a.rightLegPos = hip;
a.rightFootPos = foot;
return a;
}
Action actionLeftLeg(int hip, int foot)
{
Action a = actionReset();
a.leftLegPos = hip;
a.leftFootPos = foot;
return a;
}
Action actionLeftFoot(int pos) {
Action a = actionReset();
a.leftFootPos = pos;
return a;
}
void movementTestFeet()
{
actionManager->add(actionTipToe());
actionManager->add(actionReverseTipToe());
actionManager->add(actionReset());
}
void movementTestHips()
{
actionManager->add(actionHipFlexionIn());
actionManager->add(actionHipFlexionOut());
actionManager->add(actionReset());
}
void movementTestAllJoints()
{
actionManager->add(actionBallerina());
actionManager->add(actionReset());
actionManager->add(actionReverseBallerina());
actionManager->add(actionReset());
}
void movementWalk(int steps)
{
for(int i = 0; i < steps; i++)
{
Action r = actionRightLeg(120, 70);
r.leftLegPos = 120;
r.leftFootPos = 70;
Action l = actionLeftLeg(60, 110);
l.rightLegPos = 60;
l.rightFootPos = 110;
actionManager->add(r);
actionManager->add(l);
}
actionManager->add(actionReset());
}
void movementWalkBackwards(int steps)
{
for(int i = 0; i < steps; i++)
{
Action r = actionRightLeg(60, 110);
r.rightLegPos = 60;
r.rightFootPos = 110;
Action l = actionLeftLeg(120, 70);
l.leftLegPos = 120;
l.leftFootPos = 70;
actionManager->add(r);
actionManager->add(l);
}
actionManager->add(actionReset());
}
void routineTestJoints() {
movementTestFeet();
movementTestHips();
movementTestAllJoints();
movementTestFeet();
movementTestHips();
movementTestAllJoints();
}
void setup() {
Serial.begin(9600);
Servo servoLeftLeg;
Servo servoLeftFoot;
Servo servoRightLeg;
Servo servoRightFoot;
servoLeftLeg.attach(servoPinLegLeft);
servoLeftFoot.attach(servoPinFootLeft);
servoRightLeg.attach(servoPinLegRight);
servoRightFoot.attach(servoPinFootRight);
JointCtrl* leftLeg = new JointCtrl();
JointCtrl* leftFoot = new JointCtrl();
JointCtrl* rightLeg = new JointCtrl();
JointCtrl* rightFoot = new JointCtrl();
leftLeg->attach(servoLeftLeg);
leftFoot->attach(servoLeftFoot);
rightLeg->attach(servoRightLeg);
rightFoot->attach(servoRightFoot);
BodyCtrl* bodyCtrl = new BodyCtrl();
bodyCtrl->setJoints(leftLeg, leftFoot, rightLeg, rightFoot);
actionManager->bodyCtrl = bodyCtrl;
// configure the trigger pin to output mode
pinMode(trigPin, OUTPUT);
// configure the echo pin to input mode
pinMode(echoPin, INPUT);
actionManager->add(actionReset());
}
float getDistance()
{
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration_us = pulseIn(echoPin, HIGH);
distance_cm = 0.017 * duration_us;
return distance_cm;
}
void react_close()
{
long n = random(4);
switch (n)
{
case 0:
actionManager->add(actionTipToe());
break;
case 1:
actionManager->add(actionLeftTipToe());
break;
case 2:
actionManager->add(actionRightTipToe());
break;
case 3:
actionManager->add(actionReverseTipToe());
break;
}
actionManager->add(actionReset());
}
int DISTANCE_CLOSE = 10;
int DISTANCE_MED = 20;
int DISTANCE_FAR = 30;
bool is_close = false;
bool is_medium = false;
bool is_far = false;
void loop() {
float d = getDistance();
if(d < DISTANCE_CLOSE)
{
if(!is_close)
{
react_close();
}
is_close = true;
is_far = false;
is_medium = false;
}
if(d > DISTANCE_CLOSE && d < DISTANCE_FAR)
{
if(is_close)
{
movementWalk(2);
}
is_close = false;
is_medium = true;
is_far = false;
}
if(d > DISTANCE_FAR)
{
if(!is_far)
{
actionManager->add(actionReverseTipToe());
actionManager->add(actionReset());
}
is_close = false;
is_medium = false;
is_far = true;
}
while(!actionManager->done)
{
actionManager->next();
}
}