Library to drive the Zumo shield from pololu.

Dependents:   Nucleo_Zumo_BLE_IDB04A1

https://www.pololu.com/category/169/zumo-robot-for-arduino

Committer:
bcostm
Date:
Mon Oct 12 11:32:33 2015 +0000
Revision:
0:c69b20870374
Initial version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:c69b20870374 1 #include "ZumoShield.h"
bcostm 0:c69b20870374 2 #include "mbed.h"
bcostm 0:c69b20870374 3
bcostm 0:c69b20870374 4 ZumoShield::ZumoShield(PinName m1_pwm_pin, PinName m1_dir_pin,
bcostm 0:c69b20870374 5 PinName m2_pwm_pin, PinName m2_dir_pin) :
bcostm 0:c69b20870374 6 // PinName a0_pin, PinName a1_pin, PinName a2_pin, PinName a3_pin, PinName a4_pin, PinName a5_pin) :
bcostm 0:c69b20870374 7 m1pwm(m1_pwm_pin),
bcostm 0:c69b20870374 8 m1dir(m1_dir_pin),
bcostm 0:c69b20870374 9 m2pwm(m2_pwm_pin),
bcostm 0:c69b20870374 10 m2dir(m2_dir_pin)
bcostm 0:c69b20870374 11 /*
bcostm 0:c69b20870374 12 a0sens(a0_pin),
bcostm 0:c69b20870374 13 a1sens(a1_pin),
bcostm 0:c69b20870374 14 a2sens(a2_pin),
bcostm 0:c69b20870374 15 a3sens(a3_pin),
bcostm 0:c69b20870374 16 a4sens(a4_pin),
bcostm 0:c69b20870374 17 a5sens(a5_pin)
bcostm 0:c69b20870374 18 */
bcostm 0:c69b20870374 19 {
bcostm 0:c69b20870374 20 }
bcostm 0:c69b20870374 21
bcostm 0:c69b20870374 22 void ZumoShield::left_motor(float speed)
bcostm 0:c69b20870374 23 {
bcostm 0:c69b20870374 24 if (speed >= 0) {
bcostm 0:c69b20870374 25 m2pwm = speed;
bcostm 0:c69b20870374 26 m2dir = 0;
bcostm 0:c69b20870374 27 } else {
bcostm 0:c69b20870374 28 m2pwm = -speed;
bcostm 0:c69b20870374 29 m2dir = 1;
bcostm 0:c69b20870374 30 }
bcostm 0:c69b20870374 31 }
bcostm 0:c69b20870374 32
bcostm 0:c69b20870374 33 void ZumoShield::right_motor(float speed)
bcostm 0:c69b20870374 34 {
bcostm 0:c69b20870374 35 if (speed >= 0) {
bcostm 0:c69b20870374 36 m1pwm = speed;
bcostm 0:c69b20870374 37 m1dir = 1;
bcostm 0:c69b20870374 38 } else {
bcostm 0:c69b20870374 39 m1pwm = -speed;
bcostm 0:c69b20870374 40 m1dir = 0;
bcostm 0:c69b20870374 41 }
bcostm 0:c69b20870374 42 }
bcostm 0:c69b20870374 43
bcostm 0:c69b20870374 44 void ZumoShield::turn_left(float speed)
bcostm 0:c69b20870374 45 {
bcostm 0:c69b20870374 46 left_motor(0);
bcostm 0:c69b20870374 47 right_motor(speed);
bcostm 0:c69b20870374 48 }
bcostm 0:c69b20870374 49
bcostm 0:c69b20870374 50 void ZumoShield::turn_right(float speed)
bcostm 0:c69b20870374 51 {
bcostm 0:c69b20870374 52 left_motor(speed);
bcostm 0:c69b20870374 53 right_motor(0);
bcostm 0:c69b20870374 54 }
bcostm 0:c69b20870374 55
bcostm 0:c69b20870374 56 void ZumoShield::left(float speed)
bcostm 0:c69b20870374 57 {
bcostm 0:c69b20870374 58 left_motor(-speed);
bcostm 0:c69b20870374 59 right_motor(speed);
bcostm 0:c69b20870374 60 }
bcostm 0:c69b20870374 61
bcostm 0:c69b20870374 62 void ZumoShield::right(float speed)
bcostm 0:c69b20870374 63 {
bcostm 0:c69b20870374 64 left_motor(speed);
bcostm 0:c69b20870374 65 right_motor(-speed);
bcostm 0:c69b20870374 66 }
bcostm 0:c69b20870374 67
bcostm 0:c69b20870374 68 void ZumoShield::forward(float speed)
bcostm 0:c69b20870374 69 {
bcostm 0:c69b20870374 70 if (speed == 0) {
bcostm 0:c69b20870374 71 left_motor(0);
bcostm 0:c69b20870374 72 right_motor(0);
bcostm 0:c69b20870374 73 } else {
bcostm 0:c69b20870374 74 left_motor(speed);
bcostm 0:c69b20870374 75 right_motor(speed);
bcostm 0:c69b20870374 76 }
bcostm 0:c69b20870374 77 }
bcostm 0:c69b20870374 78
bcostm 0:c69b20870374 79 void ZumoShield::backward(float speed)
bcostm 0:c69b20870374 80 {
bcostm 0:c69b20870374 81 if (speed == 0) {
bcostm 0:c69b20870374 82 left_motor(0);
bcostm 0:c69b20870374 83 right_motor(0);
bcostm 0:c69b20870374 84 } else {
bcostm 0:c69b20870374 85 left_motor(-speed);
bcostm 0:c69b20870374 86 right_motor(-speed);
bcostm 0:c69b20870374 87 }
bcostm 0:c69b20870374 88 }
bcostm 0:c69b20870374 89
bcostm 0:c69b20870374 90 void ZumoShield::stop(int motor)
bcostm 0:c69b20870374 91 {
bcostm 0:c69b20870374 92 if (motor == 1) {
bcostm 0:c69b20870374 93 stopLeft();
bcostm 0:c69b20870374 94 } else if (motor == 2) {
bcostm 0:c69b20870374 95 stopRight();
bcostm 0:c69b20870374 96 }
bcostm 0:c69b20870374 97 }
bcostm 0:c69b20870374 98
bcostm 0:c69b20870374 99 void ZumoShield::stopLeft()
bcostm 0:c69b20870374 100 {
bcostm 0:c69b20870374 101 m2pwm = 0;
bcostm 0:c69b20870374 102 m2dir = 0;
bcostm 0:c69b20870374 103 }
bcostm 0:c69b20870374 104
bcostm 0:c69b20870374 105 void ZumoShield::stopRight()
bcostm 0:c69b20870374 106 {
bcostm 0:c69b20870374 107 m1pwm = 0;
bcostm 0:c69b20870374 108 m1dir = 0;
bcostm 0:c69b20870374 109 }
bcostm 0:c69b20870374 110
bcostm 0:c69b20870374 111 void ZumoShield::stopAll()
bcostm 0:c69b20870374 112 {
bcostm 0:c69b20870374 113 stopLeft();
bcostm 0:c69b20870374 114 stopRight();
bcostm 0:c69b20870374 115 }
bcostm 0:c69b20870374 116
bcostm 0:c69b20870374 117 /* TODO
bcostm 0:c69b20870374 118 float ZumoShield::position()
bcostm 0:c69b20870374 119 {
bcostm 0:c69b20870374 120 return output;
bcostm 0:c69b20870374 121 }
bcostm 0:c69b20870374 122 */