-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotor_control_L298P.ino
1260 lines (867 loc) · 29.1 KB
/
motor_control_L298P.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
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
/*
*
*
* Differential Drive Motor control
*
* v.0.26
*
* mobile robot differential drive functions for basic motor/wheel control
* for MotorShield L298P on Arduino UNO
*
* low level actuator functions for basic motor control
* higher low-level commands using rate, time, distance units for motion control
* supports odometry using typical IR wheel encoder sensors & encoder wheels for TT-Motors
*
* versatile functions with intuitive semantic interface
*
* (c) 2022-2023 Mike Knerr
*
*/
//SET ROBOT MODEL TO FWD OR RWD
// USING FWD
#define FRONT_WHEEL_DRIVE
//#define REAR_WHEEL_DRIVE // not implemented yet
///////// L298P Motor Control Board pin assignments //////////
#ifdef FRONT_WHEEL_DRIVE
// using pin assignments for L298P motor control board (shield)
// EN & IN defines are motor board pin-out dependent
// Motor A - RIGHT
#define ENA 10 //motor rotational speed in [0,255] ie.[0,$FF], 0 is stop
#define INA 12 // direction
#define RIGHT_FORWARD_SIGNAL LOW // LOW on A for front wheel drive RIGHT side motor
#define RIGHT_REVERSE_SIGNAL HIGH // HIGH on A for front wheel drive RIGHT side motor
// MOTOR B - LEFT
// pin assignments L298P
#define ENB 11 //motor rotational speed
#define INB 13 //direction
#define LEFT_FORWARD_SIGNAL HIGH // HIGH on A for front wheel drive LEFT side motor
#define LEFT_REVERSE_SIGNAL LOW // LOW on A for front wheel drive LEFT side motor
#endif
#ifdef REAR_WHEEL_DRIVE
// not implemented yet
/*
// REAR WHEEL DRIVE
//
// Motor A - LEFT
#define ENA //speed
#define INA //direction
// MOTOR B - RIGHT
#define ENB //speed
#define INB //direction
*/
#endif
// standard constants
const float pi = 3.141592; // precision is only 6-7 digits on arduino //#define pi 3.1415962535
//ROBOT PHYSICAL PARAMETERS
// physical parameters for wheels, chassis...
const float ROBOT_TRACK_WIDTH = 0.13; // changed 11/12/22 to 0.13 from 0.125; // 090; // 0.085; // meters (85 mm,, 8.5 cm) ,<<<<<<<====== CHECK THIS!
const float ROBOT_CIRC = 2*ROBOT_TRACK_WIDTH; // may want to use this as constant across equations
const float ROBOT_WHEEL_RADIUS = 0.0325; // meters, 32.5 mm = 3.25 cm was diameter = 0.065; //meters (65 mm, 6.5 cm))
const float WHEEL_CIRC = 2*pi*ROBOT_WHEEL_RADIUS; //; //wheel circumference
// MOTOR FUNCTION
// cheap motors sometimes need to get a pulse to start moving w/ L298P?
// at very low speeds or from a stop when not enough stalling current is available
// so have to prime the pump
// these are the global default parameters that can be
// set to use and fine tune the auto prime feature in set_motor_speed(...)
#define AUTO_PRIME true // turns on auto priming for motors at low speeds
#define DEFAULT_MOTOR_PRIME_DURATION 35 // was 50 //100 // in ms
#define DEFAULT_MOTOR_PRIME_RATE 35 //30 //was 30 // % in scale [1,100]
#define MIN_AUTO_PRIME_SPEED 15 //10 //15 //20 //25 // in [0,100] prime motors if set under this speed
// calibrate min starting range //////
#define MINSPEED 45 // this is the min motor speed setting where the motors will turn in [1,100], 6v & Stemedu (yellow) motors
// calibration parameters for angular velocity of wheels to map into [1,100]
// that defines the [min,max] range of motors physical rotation using the motor control board
// min/max rotational rates have be determined from field test of motors
// using rough aproximation by sight not odometry
// was before actually motor test with this function
const float MOTOR_MIN_ANGULAR_VELOCITY = 4.7124; //4; //; //4.7124; // from visual measurement 3.0; //4.0;
// 1 m/s w/ 20cm circumference wheel is
// radians per cm = (2*pi)/20 = 0.3141592653589793
// the min rate (lower bound) motor can turn at is 45, this is motor speed 1 ie. 1 in [1,100] (via map or direct)
// recheck visual from video, at speed 1, turns 15 cm in 1 second
// this is 4.71238898038469 radians per second at speed setting 1
//
// 1 m/s linear w/ 3.25 cm radius wheels is angular velocity of 30.769230769230766... radians per second
const float MOTOR_MAX_ANGULAR_VELOCITY = 18.85; // 20; //30; //25;// //18.85; //visual measurement ~ 3 revolutions @ 100% //31.42; /// ~ 1 m/s in radians per second
// ODOMETRY
// arduino uno pinouts used for odometry sensors
#define INTERRUPT_ENCODER_SENSOR_A 0 // MOTOR/WHEEL A w/ sensor on pin #2
#define INTERRUPT_ENCODER_SENSOR_B 1 // MOTOR/WHEEL B w/ sensor on pin #3
const float ENC_WHEEL_TICKS = 20;
/*
* make sure compiler fixes these variables
* in specific memory locations
* since used by interrupt functions
*/
volatile unsigned int tickCounterA = 0;
volatile unsigned int tickCounterB = 0;
// SE&L
// robots state (pose)
// volatile since are updated in an ISR & too important to move around
volatile float robot_cx = 0; //current x position // anchor in memory current state too important
volatile float robot_cy = 0; //current y position
volatile float robot_phi = 0; //current heading
// functions
//constrain selections to enum type
enum SIDE { RIGHT, LEFT, BOTH, NEITHER };
// STOP is stop immediately
// STOP_WAIT, stops and will pause for duration set in ms
// spin right is clockwise, spin left is counter-clockwise
enum DIRECTION { FORWARD, REVERSE, SPIN_RIGHT, SPIN_LEFT, STOP, STOP_WAIT, NONE}; //none is placeholder
enum MOTOR_TEST_TYPE {RIGHT_MOTOR, LEFT_MOTOR, BOTH_MOTORS, BOTH_MOTORS_SPIN};
#define DEFAULT_STOP_DURATION 1500 // 1.5 sec when using wheel(STOP_WAIT,...) or wheels(...) w/out args
// DEBUG
#define BUZZER 4
#define BEEPS_ON false // true //turn off if dont want runtime progress beeps
#define DEBUG true
// INIT
void setup() {
// int y=f2(2);
// Motor A - RIGHT w/ FWD
pinMode(ENA, OUTPUT);
pinMode(INA, OUTPUT);
// Motor B - LEFT W/ FWD
pinMode(ENB, OUTPUT);
pinMode(INB, OUTPUT);
pinMode(BUZZER, OUTPUT);
// send stop signal(s)
analogWrite(ENA, 0);
analogWrite(ENB, 0);
if (DEBUG) {
Serial.begin(9600);
Serial.print("\n");
Serial.println("Hello...\n");
//Serial.println("Running time in milliseconds:");
//Serial.println("\t");
}
//init state
robot_cx = 0; //current x position
robot_cy = 0; //current y position
robot_phi = 0; //current heading
// attach interrupts to ISRs for odometry
attachInterrupt(INTERRUPT_ENCODER_SENSOR_A,encCounterA,FALLING);
attachInterrupt(INTERRUPT_ENCODER_SENSOR_B,encCounterB,FALLING);
}
// DEBUG
void beep(int dmsec) {
// single beep of duration dmsec
digitalWrite(BUZZER,HIGH);delay(dmsec);digitalWrite(BUZZER,LOW);
}
void beeps(int cnt = 1, float wait = 1, int dur=250, int pause = 250 ) {
// one or more beeps
// default is 1 beep, wait 1 sec before exit,
// 250 ms duration of each beep, 250 ms pause between beeps
// call as beeps(); (is same as beeps(1);
if (BEEPS_ON) {
for (int i=1; i<=cnt; i++) { // lenght of beep and pause betwen are equal
beep(dur);delay(pause); // in milliseconds
}
delay(wait*1000); // wait before exiting in seconds
}
}
// ODOMETRY
static int motorAdir=1;
static int motorBdir=1;
// ISR FOR sensor on Motor A - RIGHT
void encCounterA() {
tickCounterA++;
//robotUpdatePose(); //FUTURE was ok w/ interrupt
}
// ISR FOR sensor on MOTOR B - LEFT
void encCounterB() {
tickCounterB++;
//robotUpdatePose(); //FUTURE was ok w/ interrupt
}
int getEncCountA() {
// since only single encoder sensor per wheel
// motion functions set the direction and
// is retreived here
noInterrupts();
int count = tickCounterA * getMotorAdir();
interrupts();
return count;
}
int getEncCountB() {
noInterrupts();
int count = tickCounterB * getMotorBdir();
interrupts();
return count;
}
void setMotorAdir(DIRECTION dir) {
// set current direction of encoder on motor A
// needed since only one encoder sensor per wheel
// so direction is unknown from encoder sensor signals
if (dir == FORWARD) {
motorAdir = 1;
}
else if (dir == REVERSE) {
motorAdir = -1;
}
// otherwise don't do anything
}
void setMotorBdir(DIRECTION dir) {
// set current direction of encoder on motor A
// needed since only one encoder sensor per wheel
// so direction is unknown from encoder sensor signals
if (dir == FORWARD) {
motorBdir = 1;
}
else if (dir == REVERSE) {
motorBdir = -1;
}
// otherwise don't do anything
}
int getMotorAdir() {
return motorAdir;
}
int getMotorBdir() {
return motorBdir;
}
// LOCALIZATION -- SE&L //
float robotGetPoseX() {
return robot_cx;
}
float robotGetPoseY(){
return robot_cy;
}
float robotGetPosePhi(){
return robot_phi;
}
void robotPoseInit() {
robot_cx = 0.0;
robot_cy = 0.0;
robot_phi = 0.0;
}
void robotSetPose(float x,float y,float phi) {
// set pose manually update current state
robot_cx = x;
robot_cy = y;
robot_phi = phi;
}
void robotUpdatePose() {
// UpdatePose
// called from interrupts to updates state
getEncCountA(); // update pose called from interrupt, was ok
}
int get_motor_speed(int spd) {
// maps [0,100] -> [0,FF] for arduino uno board
if (spd == 0) {
return 0;
}
else {
spd = constrain(spd,1,100); // map does not bound input range
return map(spd,1,100,MINSPEED,255);
}
}
void set_motor_speed(int pin, int rate) {
// relatively low-level function
// use this to automatically manually
// prime motors for slow speeds if using the AUTO_PRIME
// feature, AnalogWrite is builtin this function
// pass the pin # as an arg & rate
int new_speed = get_motor_speed(rate);
if (AUTO_PRIME) { // if set then pulse out to get motor moving
if (new_speed > 0 && new_speed < get_motor_speed(MIN_AUTO_PRIME_SPEED) ) { //let STOP fall through
analogWrite(pin, get_motor_speed(DEFAULT_MOTOR_PRIME_RATE) ); //kick it
delay(DEFAULT_MOTOR_PRIME_DURATION);
}
}
analogWrite(pin,new_speed); // continue with usual speed seting
}
// MOTOR CONTROL //
void wheel(SIDE side, DIRECTION dir, int rate=0, int dur=0, bool nonstop=false) {
// turn by pivoting
// rotate only one wheel at a time
// this will pivot robot on other stationary wheel to turn
// right & left depend on chassis config with motors
switch(side) {
case RIGHT:
if (dir == FORWARD) {
setMotorAdir(FORWARD);
digitalWrite(INA, RIGHT_FORWARD_SIGNAL);
} else if (dir == REVERSE) {
setMotorAdir(REVERSE);
digitalWrite(INA, RIGHT_REVERSE_SIGNAL);
} else if (dir == STOP_WAIT) {
if (rate != 0 && dur == 0) {
dur = rate;
rate = 0;
} else if (rate == 0 && dur == 0) {
dur = DEFAULT_STOP_DURATION;
}
} else if (dir == STOP) { //stop now
dur = 0;
rate = 0;
}
// run motor A at rate
set_motor_speed(ENA, rate);
if (dur != 0) { // is a STOP_WAIT
delay(dur); //wait
}
set_motor_speed(ENA, 0); //then stop
break; //right
case LEFT:
if (dir == FORWARD) {
setMotorBdir(FORWARD);
digitalWrite(INB, LEFT_FORWARD_SIGNAL);
} else if (dir == REVERSE) {
setMotorBdir(REVERSE);
digitalWrite(INB, LEFT_REVERSE_SIGNAL);
} else if (dir == STOP_WAIT) {
if (rate != 0 && dur == 0) {
dur = rate;
rate = 0;
} else if (rate == 0 && dur == 0) {
dur = DEFAULT_STOP_DURATION;
}
} else if (dir == STOP) { // stop now
dur = 0;
rate = 0;
}
set_motor_speed(ENB, rate);
if (dur != 0) { // is a STOP_WAIT
delay(dur); //so wait
}
set_motor_speed(ENB, 0); //then stop
break; //left
}//end switch
}//end wheel
void prime_motor(SIDE side, DIRECTION dir, int rate=DEFAULT_MOTOR_PRIME_RATE,\
int duration=DEFAULT_MOTOR_PRIME_DURATION) {
// this is the manual version to kick start sluggish cheapo motors, use as required
wheel(side,dir,rate,duration);
}
void wheels(SIDE side1, DIRECTION dir1, int rate1=0, int dur1=0, SIDE side2=NEITHER, DIRECTION dir2=NONE, int rate2=0, int dur2=0) {
//void wheels(SIDE side1, DIRECTION dir1, int rate1=0, SIDE side2=NEITHER, DIRECTION dir2=NONE, int rate2=0, int dur=0) {
// in v0.19
// there is no continuous mode with this function
//
bool nonstop;
//
// ignore redundant or invalid cases
//
//if 2 sides need both different
if (side1 == side2) {
return;
}
// ignore BOTH sides, used twice, do nothing
if (side1 == BOTH && side2 == BOTH) {
return;
}
// BOTH has to be used only once as 1st arg
if (side2 == BOTH ) {
return;
}
// shortcut for full stop
if (side1 == BOTH && dir1 == STOP) {
wheel(side1,STOP);
wheel(side2,STOP);
return;
}
//
//shortform syntax for BOTH motors at same rate
//in tandom movement of wheels & SPIN in place
//
if (side1 == BOTH ) {
if (dir1 == FORWARD) {
setMotorAdir(FORWARD);
digitalWrite(INA, RIGHT_FORWARD_SIGNAL);
setMotorBdir(FORWARD);
digitalWrite(INB, LEFT_FORWARD_SIGNAL);
} else if (dir1 == REVERSE) {
setMotorAdir(REVERSE);
digitalWrite(INA, RIGHT_REVERSE_SIGNAL);
setMotorBdir(REVERSE);
digitalWrite(INB, LEFT_REVERSE_SIGNAL);
//clockwise, right reverse, left forward
} else if (dir1 == SPIN_RIGHT) {
setMotorAdir(REVERSE);
digitalWrite(INA, RIGHT_REVERSE_SIGNAL);
setMotorBdir(FORWARD);
digitalWrite(INB, LEFT_FORWARD_SIGNAL);
//counter-clockwise, right forward, left reverse
} else if (dir1 == SPIN_LEFT) {
setMotorAdir(FORWARD);
digitalWrite(INA, RIGHT_FORWARD_SIGNAL);
setMotorBdir(REVERSE);
digitalWrite(INB, LEFT_REVERSE_SIGNAL);
} else if (dir1 == STOP_WAIT) {
if (rate1 != 0 && dur1 == 0) {
dur1 = rate1;
rate1 = 0;
} else if (rate1 == 0 && dur1 == 0) {
dur1 = DEFAULT_STOP_DURATION;
}
} else if (dir1 == STOP) { // stop now
dur1 = 0;
rate1 = 0;
}
set_motor_speed(ENA,rate1);
set_motor_speed(ENB,rate1);
if (dur1 != 0) {
delay(dur1); //wait or keep running
}
//stop both wheels
//for now
set_motor_speed(ENA, 0); //then off signal
set_motor_speed(ENB, 0);
// are these working? yes
//wheel(side1,STOP);
//wheel(side2,STOP);
return;
} //end tandom commands ("BOTH" commands)
//================================================================
// if made it this far, control both wheels seperately at the same time
// this can rotate both motors at different rates
// or at the same rate, and in the same of different directions
// at the same time and have different durations for each wheel in one command
// RIGHT wheel
if (side1 == RIGHT) {
if (dir1 == FORWARD) {
digitalWrite(INA, RIGHT_FORWARD_SIGNAL);
} else if (dir1 == REVERSE) {
digitalWrite(INA, RIGHT_REVERSE_SIGNAL);
} else {
return; // not valid direction , just bail out
}
set_motor_speed(ENA,rate1);
}
if (side2 == RIGHT) {
if (dir2 == FORWARD) {
digitalWrite(INA, RIGHT_FORWARD_SIGNAL);
} else if (dir2 == REVERSE) {
digitalWrite(INA, RIGHT_REVERSE_SIGNAL);
} else {
return; // not valid direction , just bail out
}
set_motor_speed(ENA,rate2);
}
// LEFT wheel
if (side1 == LEFT) {
if (dir1 == FORWARD) {
digitalWrite(INB, LEFT_FORWARD_SIGNAL);
} else if (dir1 == REVERSE) {
digitalWrite(INB, LEFT_REVERSE_SIGNAL);
} else {
return; // not valid direction , just bail out
}
set_motor_speed(ENB,rate1);
}
if (side2 == LEFT) {
if (dir2 == FORWARD) {
digitalWrite(INB, LEFT_FORWARD_SIGNAL);
} else if (dir2 == REVERSE) {
digitalWrite(INB, LEFT_REVERSE_SIGNAL);
} else {
return; // not valid direction , just bail out
}
set_motor_speed(ENB,rate2);
}
// timers for the wheels with different durations
const unsigned long max_time = 300000; //60*1000*5; //5 minutes
//durations are in milliseconds
bool side1_stopped = false;
bool side2_stopped = false;
unsigned long now = millis();
unsigned long dur1_endtime = now + dur1;
unsigned long dur2_endtime = now + dur2;
// both same duration
// may want to skip this short version
// if delay() function not to accurate? for long time periods
if ( dur1 == dur2) {
delay(dur1);
wheel(side1,STOP);
wheel(side2,STOP);
return;
} else {
// countdown by milliseconds
for (int i=0; i<= max_time; i++) {
delay(10); // 1 millisecond
if (millis() >= dur1_endtime) {
wheel(side1,STOP);
side1_stopped = true;
}
if (millis() >= dur2_endtime) {
wheel(side2,STOP);
side2_stopped = true;
}
//both wheels done running their durations so bye
if (side1_stopped && side2_stopped) {
return;
}
}
} //end else
// otherwise something goofed?
wheel(RIGHT,STOP);
wheel(LEFT, STOP);
} //end wheels(...)
//
void unicycle(DIRECTION dir, int rate=0, int dur=0) {
// commands are constrained to the motion of a one wheel unicycle
// implemented on a two wheel differential drive circular chassis robot
switch(dir) {
case FORWARD:
wheels(BOTH,FORWARD,rate,dur);
break;
case REVERSE:
wheels(BOTH,REVERSE,rate,dur);
break;
case SPIN_RIGHT:
wheels(BOTH,SPIN_RIGHT,rate,dur);
break;
case SPIN_LEFT:
wheels(BOTH,SPIN_LEFT,rate,dur);
break;
case STOP:
wheels(BOTH,STOP); //,rate); //use syntax unicycle(STOP,1000); stop 1 sec or unicycle(STOP);
break;
//anything else do nothing
} //end switch
}
void test_motors(MOTOR_TEST_TYPE motor_type ) {
// test motor(s) routine
// to put motors through the paces
// test types are {RIGHT_MOTOR, LEFT_MOTOR, BOTH_MOTORS, BOTH_MOTORS_SPIN}
SIDE side;
DIRECTION dir;
// set side here once only
switch(motor_type) {
case RIGHT_MOTOR:
side = RIGHT;
break;
case LEFT_MOTOR:
side = LEFT;
break;
case BOTH_MOTORS:
side = BOTH;
break;
case BOTH_MOTORS_SPIN:
side = BOTH;
break;
} //end switch
//////////////////////////////////////////////////////////
// test min,avg,max speed levels where motor will rotate
// pause betwen rate changes
/////////////////////////////////////////////////////////
//FORWARD/////////////////////////////
if (motor_type == BOTH_MOTORS_SPIN){
dir = SPIN_RIGHT;
}
else {
dir = FORWARD;
}
// minimum speed
if (side == BOTH) {
wheels(side,dir,1,3000);
wheels(side,STOP_WAIT,1000);
} else {
wheel(side,dir,1,3000);
wheel(side,STOP_WAIT,1000);
}
// 50%
if (side == BOTH) {
wheels(side,dir,50,3000);
wheels(side,STOP_WAIT,1000);
} else {
wheel(side,dir,50,3000);
wheel(side,STOP_WAIT,1000);
}
// 100%
if (side == BOTH) {
wheels(side,dir,100,3000);
wheels(side,STOP_WAIT,1000);
} else {
wheel(side,dir,100,3000);
wheel(side,STOP_WAIT,1000);
}
//wait
delay(1000);
//REVERSE///////////////////////////
if (motor_type == BOTH_MOTORS_SPIN){
dir = SPIN_LEFT;
}
else {
dir = REVERSE;
}
// minimum speed
if (side == BOTH) {
wheels(side,dir,1,3000);
wheels(side,STOP_WAIT,1000);
} else {
wheel(side,dir,1,3000);
wheel(side,STOP_WAIT,1000);
}
// 50%
if (side == BOTH) {
wheels(side,dir,50,3000);
wheels(side,STOP_WAIT,1000);
} else {
wheel(side,dir,50,3000);
wheel(side,STOP_WAIT,1000);
}
// 100%
if (side == BOTH) {
wheels(side,dir,100,3000);
wheels(side,STOP_WAIT,1000);
} else {
wheel(side,dir,100,3000);
wheel(side,STOP_WAIT,1000);
}
//pause
delay(1000);
//ok 9/29
/////////////////////////////////////////////////
/// forward & rev right away @ min,avg,max speeds
/////////////////////////////////////////////////
DIRECTION fdir, rdir;
if (motor_type == BOTH_MOTORS_SPIN){
fdir = SPIN_RIGHT;
rdir = SPIN_LEFT;
}
else {
fdir = FORWARD;
rdir = REVERSE;
}
// minimum speed
if (side == BOTH){
wheels(side,fdir,1,3000);
delay(250);
wheels(side,rdir,1,3000);
}
else {
wheel(side,fdir,1,3000);
delay(250);
wheel(side,rdir,1,3000);
}
delay(750);
// 50%
if (side == BOTH){
wheels(side,fdir,50,3000);
delay(250);
wheels(side,rdir,50,3000);
}
else {
wheel(side,fdir,50,3000);
delay(250);
wheel(side,rdir,50,3000);
}
wheels(BOTH,STOP_WAIT,750);
if (side == BOTH){
wheels(side,fdir,100,3000);
wheels(BOTH,STOP_WAIT,250); //need stop & wait to run after @ higher speeds
wheels(side,rdir,100,3000);
}
else {
wheel(side,fdir,100,3000);
wheel(side,STOP_WAIT,250);
wheel(side,rdir,100,3000);
}
wheels(BOTH,STOP_WAIT,2000);
///////////////////////////////////////////////////////////////////
// increase speed forward, decrease reverse (accelerate/decelerate)
// works also with both 2 wheel SPIN & NON-SPIN
///////////////////////////////////////////////////////////////////
const int p = 40; //space pulses apart by p milliseconds
//FORWARD
if (motor_type == BOTH_MOTORS_SPIN){
dir = SPIN_RIGHT;
}
else {
dir = FORWARD;
}
for(int spd = 1; spd <= 100; spd++)
{
if (side == BOTH){
wheels(side, dir, spd, 50);
} else {
wheel(side, dir, spd, 50);
}
}
//pause
wheels(BOTH, STOP_WAIT,1500);
// REVERSE
if (motor_type == BOTH_MOTORS_SPIN){
dir = SPIN_LEFT;
}
else {
dir = REVERSE;
}
for(int spd = 100; spd >= 1; spd--)
{
if (side == BOTH){
wheels(side,dir,spd, 50);
} else {
wheel(side,dir,spd, 50);
}
}
wheels(side,STOP_WAIT,2000);
return; //END OF TEST
} //end motor test function
///////// math & utility //////////