forked from OttoDIY/OttoDIYLib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOtto.cpp
1105 lines (919 loc) · 30.1 KB
/
Otto.cpp
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include <pins_arduino.h>
#endif
#include "Otto.h"
#include <Oscillator.h>
void Otto::init(int YL, int YR, int RL, int RR, bool load_calibration, int Buzzer) {
servo_pins[0] = YL;
servo_pins[1] = YR;
servo_pins[2] = RL;
servo_pins[3] = RR;
attachServos();
isOttoResting=false;
if (load_calibration) {
for (int i = 0; i < 4; i++) {
int servo_trim = EEPROM.read(i);
if (servo_trim > 128) servo_trim -= 256;
servo[i].SetTrim(servo_trim);
}
}
//Buzzer pin:
pinBuzzer = Buzzer;
pinMode(Buzzer,OUTPUT);
}
///////////////////////////////////////////////////////
void Otto::initMATRIX(int DIN, int CS, int CLK, int rotate){
ledmatrix.init( DIN, CS, CLK, 1, rotate); // set up Matrix display
}
void Otto::matrixIntensity(int intensity){
ledmatrix.setIntensity(intensity);
}
///////////////////////////////////////////////////////////////////
//-- ATTACH & DETACH FUNCTIONS ----------------------------------//
///////////////////////////////////////////////////////////////////
void Otto::attachServos(){
servo[0].attach(servo_pins[0]);
servo[1].attach(servo_pins[1]);
servo[2].attach(servo_pins[2]);
servo[3].attach(servo_pins[3]);
}
void Otto::detachServos(){
servo[0].detach();
servo[1].detach();
servo[2].detach();
servo[3].detach();
}
///////////////////////////////////////////////////////////////////
//-- OSCILLATORS TRIMS ------------------------------------------//
///////////////////////////////////////////////////////////////////
void Otto::setTrims(int YL, int YR, int RL, int RR) {
servo[0].SetTrim(YL);
servo[1].SetTrim(YR);
servo[2].SetTrim(RL);
servo[3].SetTrim(RR);
}
void Otto::saveTrimsOnEEPROM() {
for (int i = 0; i < 4; i++){
EEPROM.write(i, servo[i].getTrim());
}
}
///////////////////////////////////////////////////////////////////
//-- BASIC MOTION FUNCTIONS -------------------------------------//
///////////////////////////////////////////////////////////////////
void Otto::_moveServos(int time, int servo_target[]) {
attachServos();
if(getRestState()==true){
setRestState(false);
}
final_time = millis() + time;
if(time>10){
for (int i = 0; i < 4; i++) increment[i] = (servo_target[i] - servo[i].getPosition()) / (time / 10.0);
for (int iteration = 1; millis() < final_time; iteration++) {
partial_time = millis() + 10;
for (int i = 0; i < 4; i++) servo[i].SetPosition(servo[i].getPosition() + increment[i]);
while (millis() < partial_time); //pause
}
}
else{
for (int i = 0; i < 4; i++) servo[i].SetPosition(servo_target[i]);
while (millis() < final_time); //pause
}
// final adjustment to the target. if servo speed limiter is turned on, reaching to the goal may take longer than
// requested time.
bool f = true;
while(f) {
f = false;
for (int i = 0; i < 4; i++) {
if (servo_target[i] != servo[i].getPosition()) {
f = true;
break;
}
}
if (f) {
for (int i = 0; i < 4; i++) {
servo[i].SetPosition(servo_target[i]);
}
partial_time = millis() + 10;
while (millis() < partial_time); //pause
}
};
}
void Otto::_moveSingle(int position, int servo_number) {
if (position > 180) position = 90;
if (position < 0) position = 90;
attachServos();
if(getRestState()==true){
setRestState(false);
}
int servoNumber = servo_number;
if (servoNumber == 0){
servo[0].SetPosition(position);
}
if (servoNumber == 1){
servo[1].SetPosition(position);
}
if (servoNumber == 2){
servo[2].SetPosition(position);
}
if (servoNumber == 3){
servo[3].SetPosition(position);
}
}
void Otto::oscillateServos(int A[4], int O[4], int T, double phase_diff[4], float cycle=1){
for (int i=0; i<4; i++) {
servo[i].SetO(O[i]);
servo[i].SetA(A[i]);
servo[i].SetT(T);
servo[i].SetPh(phase_diff[i]);
}
double ref=millis();
for (double x=ref; x<=T*cycle+ref; x=millis()){
for (int i=0; i<4; i++){
servo[i].refresh();
}
}
}
void Otto::_execute(int A[4], int O[4], int T, double phase_diff[4], float steps = 1.0){
attachServos();
if(getRestState()==true){
setRestState(false);
}
int cycles=(int)steps;
//-- Execute complete cycles
if (cycles >= 1)
for(int i = 0; i < cycles; i++)
oscillateServos(A,O, T, phase_diff);
//-- Execute the final not complete cycle
oscillateServos(A,O, T, phase_diff,(float)steps-cycles);
}
///////////////////////////////////////////////////////////////////
//-- HOME = Otto at rest position -------------------------------//
///////////////////////////////////////////////////////////////////
void Otto::home(){
if(isOttoResting==false){ //Go to rest position only if necessary
int homes[4]={90, 90, 90, 90}; //All the servos at rest position
_moveServos(500,homes); //Move the servos in half a second
detachServos();
isOttoResting=true;
}
}
bool Otto::getRestState(){
return isOttoResting;
}
void Otto::setRestState(bool state){
isOttoResting = state;
}
///////////////////////////////////////////////////////////////////
//-- PREDETERMINED MOTION SEQUENCES -----------------------------//
///////////////////////////////////////////////////////////////////
//-- Otto movement: Jump
//-- Parameters:
//-- steps: Number of steps
//-- T: Period
//---------------------------------------------------------
void Otto::jump(float steps, int T){
int up[]={90,90,150,30};
_moveServos(T,up);
int down[]={90,90,90,90};
_moveServos(T,down);
}
//---------------------------------------------------------
//-- Otto gait: Walking (forward or backward)
//-- Parameters:
//-- * steps: Number of steps
//-- * T : Period
//-- * Dir: Direction: FORWARD / BACKWARD
//---------------------------------------------------------
void Otto::walk(float steps, int T, int dir){
//-- Oscillator parameters for walking
//-- Hip sevos are in phase
//-- Feet servos are in phase
//-- Hip and feet are 90 degrees out of phase
//-- -90 : Walk forward
//-- 90 : Walk backward
//-- Feet servos also have the same offset (for tiptoe a little bit)
int A[4]= {30, 30, 20, 20};
int O[4] = {0, 0, 4, -4};
double phase_diff[4] = {0, 0, DEG2RAD(dir * -90), DEG2RAD(dir * -90)};
//-- Let's oscillate the servos!
_execute(A, O, T, phase_diff, steps);
}
//---------------------------------------------------------
//-- Otto gait: Turning (left or right)
//-- Parameters:
//-- * Steps: Number of steps
//-- * T: Period
//-- * Dir: Direction: LEFT / RIGHT
//---------------------------------------------------------
void Otto::turn(float steps, int T, int dir){
//-- Same coordination than for walking (see Otto::walk)
//-- The Amplitudes of the hip's oscillators are not igual
//-- When the right hip servo amplitude is higher, the steps taken by
//-- the right leg are bigger than the left. So, the robot describes an
//-- left arc
int A[4]= {30, 30, 20, 20};
int O[4] = {0, 0, 4, -4};
double phase_diff[4] = {0, 0, DEG2RAD(-90), DEG2RAD(-90)};
if (dir == LEFT) {
A[0] = 30; //-- Left hip servo
A[1] = 10; //-- Right hip servo
}
else {
A[0] = 10;
A[1] = 30;
}
//-- Let's oscillate the servos!
_execute(A, O, T, phase_diff, steps);
}
//---------------------------------------------------------
//-- Otto gait: Lateral bend
//-- Parameters:
//-- steps: Number of bends
//-- T: Period of one bend
//-- dir: RIGHT=Right bend LEFT=Left bend
//---------------------------------------------------------
void Otto::bend (int steps, int T, int dir){
//Parameters of all the movements. Default: Left bend
int bend1[4]={90, 90, 62, 35};
int bend2[4]={90, 90, 62, 105};
int homes[4]={90, 90, 90, 90};
//Time of one bend, constrained in order to avoid movements too fast.
//T=max(T, 600);
//Changes in the parameters if right direction is chosen
if(dir==-1)
{
bend1[2]=180-35;
bend1[3]=180-60; //Not 65. Otto is unbalanced
bend2[2]=180-105;
bend2[3]=180-60;
}
//Time of the bend movement. Fixed parameter to avoid falls
int T2=800;
//Bend movement
for (int i=0;i<steps;i++)
{
_moveServos(T2/2,bend1);
_moveServos(T2/2,bend2);
delay(T*0.8);
_moveServos(500,homes);
}
}
//---------------------------------------------------------
//-- Otto gait: Shake a leg
//-- Parameters:
//-- steps: Number of shakes
//-- T: Period of one shake
//-- dir: RIGHT=Right leg LEFT=Left leg
//---------------------------------------------------------
void Otto::shakeLeg (int steps,int T,int dir){
//This variable change the amount of shakes
int numberLegMoves=2;
//Parameters of all the movements. Default: Right leg
int shake_leg1[4]={90, 90, 58, 35};
int shake_leg2[4]={90, 90, 58, 120};
int shake_leg3[4]={90, 90, 58, 60};
int homes[4]={90, 90, 90, 90};
//Changes in the parameters if left leg is chosen
if(dir==-1)
{
shake_leg1[2]=180-35;
shake_leg1[3]=180-58;
shake_leg2[2]=180-120;
shake_leg2[3]=180-58;
shake_leg3[2]=180-60;
shake_leg3[3]=180-58;
}
//Time of the bend movement. Fixed parameter to avoid falls
int T2=1000;
//Time of one shake, constrained in order to avoid movements too fast.
T=T-T2;
T=max(T,200*numberLegMoves);
for (int j=0; j<steps;j++)
{
//Bend movement
_moveServos(T2/2,shake_leg1);
_moveServos(T2/2,shake_leg2);
//Shake movement
for (int i=0;i<numberLegMoves;i++)
{
_moveServos(T/(2*numberLegMoves),shake_leg3);
_moveServos(T/(2*numberLegMoves),shake_leg2);
}
_moveServos(500,homes); //Return to home position
}
delay(T);
}
//---------------------------------------------------------
//-- Otto movement: up & down
//-- Parameters:
//-- * steps: Number of jumps
//-- * T: Period
//-- * h: Jump height: SMALL / MEDIUM / BIG
//-- (or a number in degrees 0 - 90)
//---------------------------------------------------------
void Otto::updown(float steps, int T, int h){
//-- Both feet are 180 degrees out of phase
//-- Feet amplitude and offset are the same
//-- Initial phase for the right foot is -90, so that it starts
//-- in one extreme position (not in the middle)
int A[4]= {0, 0, h, h};
int O[4] = {0, 0, h, -h};
double phase_diff[4] = {0, 0, DEG2RAD(-90), DEG2RAD(90)};
//-- Let's oscillate the servos!
_execute(A, O, T, phase_diff, steps);
}
//---------------------------------------------------------
//-- Otto movement: swinging side to side
//-- Parameters:
//-- steps: Number of steps
//-- T : Period
//-- h : Amount of swing (from 0 to 50 aprox)
//---------------------------------------------------------
void Otto::swing(float steps, int T, int h){
//-- Both feets are in phase. The offset is half the amplitude
//-- It causes the robot to swing from side to side
int A[4]= {0, 0, h, h};
int O[4] = {0, 0, h/2, -h/2};
double phase_diff[4] = {0, 0, DEG2RAD(0), DEG2RAD(0)};
//-- Let's oscillate the servos!
_execute(A, O, T, phase_diff, steps);
}
//---------------------------------------------------------
//-- Otto movement: swinging side to side without touching the floor with the heel
//-- Parameters:
//-- steps: Number of steps
//-- T : Period
//-- h : Amount of swing (from 0 to 50 aprox)
//---------------------------------------------------------
void Otto::tiptoeSwing(float steps, int T, int h){
//-- Both feets are in phase. The offset is not half the amplitude in order to tiptoe
//-- It causes the robot to swing from side to side
int A[4]= {0, 0, h, h};
int O[4] = {0, 0, h, -h};
double phase_diff[4] = {0, 0, 0, 0};
//-- Let's oscillate the servos!
_execute(A, O, T, phase_diff, steps);
}
//---------------------------------------------------------
//-- Otto gait: Jitter
//-- Parameters:
//-- steps: Number of jitters
//-- T: Period of one jitter
//-- h: height (Values between 5 - 25)
//---------------------------------------------------------
void Otto::jitter(float steps, int T, int h){
//-- Both feet are 180 degrees out of phase
//-- Feet amplitude and offset are the same
//-- Initial phase for the right foot is -90, so that it starts
//-- in one extreme position (not in the middle)
//-- h is constrained to avoid hit the feets
h=min(25,h);
int A[4]= {h, h, 0, 0};
int O[4] = {0, 0, 0, 0};
double phase_diff[4] = {DEG2RAD(-90), DEG2RAD(90), 0, 0};
//-- Let's oscillate the servos!
_execute(A, O, T, phase_diff, steps);
}
//---------------------------------------------------------
//-- Otto gait: Ascending & turn (Jitter while up&down)
//-- Parameters:
//-- steps: Number of bends
//-- T: Period of one bend
//-- h: height (Values between 5 - 15)
//---------------------------------------------------------
void Otto::ascendingTurn(float steps, int T, int h){
//-- Both feet and legs are 180 degrees out of phase
//-- Initial phase for the right foot is -90, so that it starts
//-- in one extreme position (not in the middle)
//-- h is constrained to avoid hit the feets
h=min(13,h);
int A[4]= {h, h, h, h};
int O[4] = {0, 0, h+4, -h+4};
double phase_diff[4] = {DEG2RAD(-90), DEG2RAD(90), DEG2RAD(-90), DEG2RAD(90)};
//-- Let's oscillate the servos!
_execute(A, O, T, phase_diff, steps);
}
//---------------------------------------------------------
//-- Otto gait: Moonwalker. Otto moves like Michael Jackson
//-- Parameters:
//-- Steps: Number of steps
//-- T: Period
//-- h: Height. Typical valures between 15 and 40
//-- dir: Direction: LEFT / RIGHT
//---------------------------------------------------------
void Otto::moonwalker(float steps, int T, int h, int dir){
//-- This motion is similar to that of the caterpillar robots: A travelling
//-- wave moving from one side to another
//-- The two Otto's feet are equivalent to a minimal configuration. It is known
//-- that 2 servos can move like a worm if they are 120 degrees out of phase
//-- In the example of Otto, the two feet are mirrored so that we have:
//-- 180 - 120 = 60 degrees. The actual phase difference given to the oscillators
//-- is 60 degrees.
//-- Both amplitudes are equal. The offset is half the amplitud plus a little bit of
//- offset so that the robot tiptoe lightly
int A[4]= {0, 0, h, h};
int O[4] = {0, 0, h/2+2, -h/2 -2};
int phi = -dir * 90;
double phase_diff[4] = {0, 0, DEG2RAD(phi), DEG2RAD(-60 * dir + phi)};
//-- Let's oscillate the servos!
_execute(A, O, T, phase_diff, steps);
}
//----------------------------------------------------------
//-- Otto gait: Crusaito. A mixture between moonwalker and walk
//-- Parameters:
//-- steps: Number of steps
//-- T: Period
//-- h: height (Values between 20 - 50)
//-- dir: Direction: LEFT / RIGHT
//-----------------------------------------------------------
void Otto::crusaito(float steps, int T, int h, int dir){
int A[4]= {25, 25, h, h};
int O[4] = {0, 0, h/2+ 4, -h/2 - 4};
double phase_diff[4] = {90, 90, DEG2RAD(0), DEG2RAD(-60 * dir)};
//-- Let's oscillate the servos!
_execute(A, O, T, phase_diff, steps);
}
//---------------------------------------------------------
//-- Otto gait: Flapping
//-- Parameters:
//-- steps: Number of steps
//-- T: Period
//-- h: height (Values between 10 - 30)
//-- dir: direction: FOREWARD, BACKWARD
//---------------------------------------------------------
void Otto::flapping(float steps, int T, int h, int dir){
int A[4]= {12, 12, h, h};
int O[4] = {0, 0, h - 10, -h + 10};
double phase_diff[4] = {DEG2RAD(0), DEG2RAD(180), DEG2RAD(-90 * dir), DEG2RAD(90 * dir)};
//-- Let's oscillate the servos!
_execute(A, O, T, phase_diff, steps);
}
///////////////////////////////////////////////////////////////////
//-- MOUTHS & ANIMATIONS ----------------------------------------//
///////////////////////////////////////////////////////////////////
void Otto::setLed(byte X, byte Y, byte value){
ledmatrix.setDot( X, Y, value);
}
// EXAMPLE putAnimationMouth(dreamMouth,0);
void Otto::putAnimationMouth(unsigned long int aniMouth, int index){
ledmatrix.writeFull(PROGMEM_getAnything (&Gesturetable[aniMouth][index]));
}
//EXAMPLE putMouth(smile);
void Otto::putMouth(unsigned long int mouth, bool predefined){
if (predefined){
// Here a direct entry into the Progmem Mouthttable is used!!
ledmatrix.writeFull(PROGMEM_getAnything(&Mouthtable[mouth]));
}
else{
ledmatrix.writeFull(mouth);
}
}
void Otto::clearMouth(){
ledmatrix.clearMatrix();
}
void Otto::writeText(const char * s, byte scrollspeed){
int a ;
int b ;
for(a = 0; s[a] != '\0'; a++){
b = a +1 ;
if (b > 9 ) b = 9; // only maximum of nine characters allowed
}
for(int charNUMBER = 0; charNUMBER <b; charNUMBER++){
if ((* s < 48) || (* s > 91)) {
if (* s == 32){
ledmatrix.sendChar (44, charNUMBER, b, scrollspeed);
}
else
{
ledmatrix.sendChar (43, charNUMBER, b, scrollspeed);
}
}
else
{
ledmatrix.sendChar ((* s - 48), charNUMBER, b, scrollspeed);
}
* s++;
}
}
///////////////////////////////////////////////////////////////////
//-- SOUNDS -----------------------------------------------------//
///////////////////////////////////////////////////////////////////
void Otto::_tone (float noteFrequency, long noteDuration, int silentDuration){
// tone(10,261,500);
// delay(500);
if(silentDuration==0){silentDuration=1;}
tone(Otto::pinBuzzer, noteFrequency, noteDuration);
delay(noteDuration); //milliseconds to microseconds
//noTone(PIN_Buzzer);
delay(silentDuration);
}
void Otto::bendTones (float initFrequency, float finalFrequency, float prop, long noteDuration, int silentDuration){
//Examples:
// bendTones (880, 2093, 1.02, 18, 1);
// bendTones (note_A5, note_C7, 1.02, 18, 0);
if(silentDuration==0){silentDuration=1;}
if(initFrequency < finalFrequency)
{
for (int i=initFrequency; i<finalFrequency; i=i*prop) {
_tone(i, noteDuration, silentDuration);
}
} else{
for (int i=initFrequency; i>finalFrequency; i=i/prop) {
_tone(i, noteDuration, silentDuration);
}
}
}
void Otto::sing(int songName){
switch(songName){
case S_connection:
_tone(note_E5,50,30);
_tone(note_E6,55,25);
_tone(note_A6,60,10);
break;
case S_disconnection:
_tone(note_E5,50,30);
_tone(note_A6,55,25);
_tone(note_E6,50,10);
break;
case S_buttonPushed:
bendTones (note_E6, note_G6, 1.03, 20, 2);
delay(30);
bendTones (note_E6, note_D7, 1.04, 10, 2);
break;
case S_mode1:
bendTones (note_E6, note_A6, 1.02, 30, 10); //1318.51 to 1760
break;
case S_mode2:
bendTones (note_G6, note_D7, 1.03, 30, 10); //1567.98 to 2349.32
break;
case S_mode3:
_tone(note_E6,50,100); //D6
_tone(note_G6,50,80); //E6
_tone(note_D7,300,0); //G6
break;
case S_surprise:
bendTones(800, 2150, 1.02, 10, 1);
bendTones(2149, 800, 1.03, 7, 1);
break;
case S_OhOoh:
bendTones(880, 2000, 1.04, 8, 3); //A5 = 880
delay(200);
for (int i=880; i<2000; i=i*1.04) {
_tone(note_B5,5,10);
}
break;
case S_OhOoh2:
bendTones(1880, 3000, 1.03, 8, 3);
delay(200);
for (int i=1880; i<3000; i=i*1.03) {
_tone(note_C6,10,10);
}
break;
case S_cuddly:
bendTones(700, 900, 1.03, 16, 4);
bendTones(899, 650, 1.01, 18, 7);
break;
case S_sleeping:
bendTones(100, 500, 1.04, 10, 10);
delay(500);
bendTones(400, 100, 1.04, 10, 1);
break;
case S_happy:
bendTones(1500, 2500, 1.05, 20, 8);
bendTones(2499, 1500, 1.05, 25, 8);
break;
case S_superHappy:
bendTones(2000, 6000, 1.05, 8, 3);
delay(50);
bendTones(5999, 2000, 1.05, 13, 2);
break;
case S_happy_short:
bendTones(1500, 2000, 1.05, 15, 8);
delay(100);
bendTones(1900, 2500, 1.05, 10, 8);
break;
case S_sad:
bendTones(880, 669, 1.02, 20, 200);
break;
case S_confused:
bendTones(1000, 1700, 1.03, 8, 2);
bendTones(1699, 500, 1.04, 8, 3);
bendTones(1000, 1700, 1.05, 9, 10);
break;
case S_fart1:
bendTones(1600, 3000, 1.02, 2, 15);
break;
case S_fart2:
bendTones(2000, 6000, 1.02, 2, 20);
break;
case S_fart3:
bendTones(1600, 4000, 1.02, 2, 20);
bendTones(4000, 3000, 1.02, 2, 20);
break;
}
}
///////////////////////////////////////////////////////////////////
//-- GESTURES ---------------------------------------------------//
///////////////////////////////////////////////////////////////////
void Otto::playGesture(int gesture){
int gesturePOSITION[4];
switch(gesture){
case OttoHappy:
_tone(note_E5,50,30);
putMouth(smile);
sing(S_happy_short);
swing(1,800,20);
sing(S_happy_short);
home();
putMouth(happyOpen);
break;
case OttoSuperHappy:
putMouth(happyOpen);
sing(S_happy);
putMouth(happyClosed);
tiptoeSwing(1,500,20);
putMouth(happyOpen);
sing(S_superHappy);
putMouth(happyClosed);
tiptoeSwing(1,500,20);
home();
putMouth(happyOpen);
break;
case OttoSad:
putMouth(sad);
gesturePOSITION[0] = 110;//int sadPos[6]= {110, 70, 20, 160};
gesturePOSITION[1] = 70;
gesturePOSITION[2] = 20;
gesturePOSITION[3] = 160;
_moveServos(700, gesturePOSITION);
bendTones(880, 830, 1.02, 20, 200);
putMouth(sadClosed);
bendTones(830, 790, 1.02, 20, 200);
putMouth(sadOpen);
bendTones(790, 740, 1.02, 20, 200);
putMouth(sadClosed);
bendTones(740, 700, 1.02, 20, 200);
putMouth(sadOpen);
bendTones(700, 669, 1.02, 20, 200);
putMouth(sad);
delay(500);
home();
delay(300);
putMouth(happyOpen);
break;
case OttoSleeping:
gesturePOSITION[0] = 100;//int bedPos[6]= {100, 80, 60, 120};
gesturePOSITION[1] = 80;
gesturePOSITION[2] = 60;
gesturePOSITION[3] = 120;
_moveServos(700, gesturePOSITION);
for(int i=0; i<4;i++){
putAnimationMouth(dreamMouth,0);
bendTones (100, 200, 1.04, 10, 10);
putAnimationMouth(dreamMouth,1);
bendTones (200, 300, 1.04, 10, 10);
putAnimationMouth(dreamMouth,2);
bendTones (300, 500, 1.04, 10, 10);
delay(500);
putAnimationMouth(dreamMouth,1);
bendTones (400, 250, 1.04, 10, 1);
putAnimationMouth(dreamMouth,0);
bendTones (250, 100, 1.04, 10, 1);
delay(500);
}
putMouth(lineMouth);
sing(S_cuddly);
home();
putMouth(happyOpen);
break;
case OttoFart:
gesturePOSITION[0] = 90;// int fartPos_1[6]= {90, 90, 145, 122};
gesturePOSITION[1] = 90;
gesturePOSITION[2] = 145;
gesturePOSITION[3] = 122;
_moveServos(500,gesturePOSITION);
delay(300);
putMouth(lineMouth);
sing(S_fart1);
putMouth(tongueOut);
delay(250);
gesturePOSITION[0] = 90;// int fartPos_2[6]= {90, 90, 80, 122};
gesturePOSITION[1] = 90;
gesturePOSITION[2] = 80;
gesturePOSITION[3] = 122;
_moveServos(500,gesturePOSITION);
delay(300);
putMouth(lineMouth);
sing(S_fart2);
putMouth(tongueOut);
delay(250);
gesturePOSITION[0] = 90;// int fartPos_3[6]= {90, 90, 145, 80};
gesturePOSITION[1] = 90;
gesturePOSITION[2] = 145;
gesturePOSITION[3] = 80;
_moveServos(500,gesturePOSITION);
delay(300);
putMouth(lineMouth);
sing(S_fart3);
putMouth(tongueOut);
delay(300);
home();
delay(500);
putMouth(happyOpen);
break;
case OttoConfused:
gesturePOSITION[0] = 110;//int confusedPos[6]= {110, 70, 90, 90};
gesturePOSITION[1] = 70;
gesturePOSITION[2] = 90;
gesturePOSITION[3] = 90;
_moveServos(300, gesturePOSITION);
putMouth(confused);
sing(S_confused);
delay(500);
home();
putMouth(happyOpen);
break;
case OttoLove:
putMouth(heart);
sing(S_cuddly);
crusaito(2,1500,15,1);
home();
sing(S_happy_short);
putMouth(happyOpen);
break;
case OttoAngry:
gesturePOSITION[0] = 90;//int angryPos[6]= {90, 90, 70, 110};
gesturePOSITION[1] = 90;
gesturePOSITION[2] = 70;
gesturePOSITION[3] = 110;
_moveServos(300, gesturePOSITION);
putMouth(angry);
_tone(note_A5,100,30);
bendTones(note_A5, note_D6, 1.02, 7, 4);
bendTones(note_D6, note_G6, 1.02, 10, 1);
bendTones(note_G6, note_A5, 1.02, 10, 1);
delay(15);
bendTones(note_A5, note_E5, 1.02, 20, 4);
delay(400);
gesturePOSITION[0] = 110;//int headLeft[6]= {110, 110, 90, 90};
gesturePOSITION[1] = 110;
gesturePOSITION[2] = 90;
gesturePOSITION[3] = 90;
_moveServos(200, gesturePOSITION);
bendTones(note_A5, note_D6, 1.02, 20, 4);
gesturePOSITION[0] = 70;//int headRight[6]= {70, 70, 90, 90};
gesturePOSITION[1] = 70;
gesturePOSITION[2] = 90;
gesturePOSITION[3] = 90;
_moveServos(200, gesturePOSITION);
bendTones(note_A5, note_E5, 1.02, 20, 4);
home();
putMouth(happyOpen);
break;
case OttoFretful:
putMouth(angry);
bendTones(note_A5, note_D6, 1.02, 20, 4);
bendTones(note_A5, note_E5, 1.02, 20, 4);
delay(300);
putMouth(lineMouth);
for(int i=0; i<4; i++){
gesturePOSITION[0] = 90;//int fretfulPos[6]= {90, 90, 90, 110};
gesturePOSITION[1] = 90;
gesturePOSITION[2] = 90;
gesturePOSITION[3] = 110;
_moveServos(100, gesturePOSITION);
home();
}
putMouth(angry);
delay(500);
home();
putMouth(happyOpen);
break;
case OttoMagic:
//Initial note frecuency = 400
//Final note frecuency = 1000
// Reproduce the animation four times
for(int i = 0; i<4; i++){
int noteM = 400;
for(int index = 0; index<6; index++){
putAnimationMouth(adivinawi,index);
bendTones(noteM, noteM+100, 1.04, 10, 10); //400 -> 1000
noteM+=100;
}
clearMouth();
bendTones(noteM-100, noteM+100, 1.04, 10, 10); //900 -> 1100
for(int index = 0; index<6; index++){
putAnimationMouth(adivinawi,index);
bendTones(noteM, noteM+100, 1.04, 10, 10); //1000 -> 400
noteM-=100;
}
}
delay(300);
putMouth(happyOpen);
break;
case OttoWave:
// Reproduce the animation four times
for(int i = 0; i<2; i++){
int noteW = 500;
for(int index = 0; index<10; index++){
putAnimationMouth(wave,index);
bendTones(noteW, noteW+100, 1.02, 10, 10);
noteW+=101;
}
for(int index = 0; index<10; index++){
putAnimationMouth(wave,index);
bendTones(noteW, noteW+100, 1.02, 10, 10);