Library of routines to drive a MD25 motor control board

Dependents:   Nucleo_motors HTU21D_HELLOWORLD Major_dHome pixyMajordhome ... more

Committer:
jimherd
Date:
Fri May 20 22:28:50 2011 +0000
Revision:
2:e575d390c730
Parent:
1:8046f460a725
Updated documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jimherd 0:e7f4a9247af2 1 // ***********************************************************************
jimherd 1:8046f460a725 2 // MBED MD25 H-bridge Motor Controller.
jimherd 0:e7f4a9247af2 3 //
jimherd 0:e7f4a9247af2 4 // Based on Arduino code by Richie Reynolds
jimherd 0:e7f4a9247af2 5 // ***********************************************************************
jimherd 0:e7f4a9247af2 6 #ifndef MBED_MD25_h
jimherd 0:e7f4a9247af2 7 #define MBED_MD25_h
jimherd 0:e7f4a9247af2 8
jimherd 0:e7f4a9247af2 9 #include "mbed.h"
jimherd 0:e7f4a9247af2 10
jimherd 0:e7f4a9247af2 11 #define MD25_DEFAULT_ADDRESS 0xB0
jimherd 0:e7f4a9247af2 12 //
jimherd 0:e7f4a9247af2 13 // modes
jimherd 0:e7f4a9247af2 14 //
jimherd 0:e7f4a9247af2 15 // Mode Description
jimherd 0:e7f4a9247af2 16 // 0 Skid-steer with unsigned speed values (STOP = 128)
jimherd 0:e7f4a9247af2 17 // 1 Skid-steer with signed speed values (STOP = 0)
jimherd 0:e7f4a9247af2 18 // 2 Forward/turn steer with unsigned values (speed1 to control both motors, speed2 for turn)
jimherd 0:e7f4a9247af2 19 // 3 Forward/turn steer with signed values (speed1 to control both motors, speed2 for turn)
jimherd 0:e7f4a9247af2 20 //
jimherd 0:e7f4a9247af2 21 #define MODE_0 0
jimherd 0:e7f4a9247af2 22 #define MODE_1 1
jimherd 0:e7f4a9247af2 23 #define MODE_2 2
jimherd 0:e7f4a9247af2 24 #define MODE_3 3
jimherd 0:e7f4a9247af2 25
jimherd 0:e7f4a9247af2 26 //
jimherd 0:e7f4a9247af2 27 // register definitions
jimherd 0:e7f4a9247af2 28 //
jimherd 0:e7f4a9247af2 29 #define MD25_SPEED1_REG 0
jimherd 0:e7f4a9247af2 30 #define MD25_SPEED2_REG 1
jimherd 0:e7f4a9247af2 31 #define MD25_ENCODER1_REG 2
jimherd 0:e7f4a9247af2 32 #define MD25_ENCODER2_REG 6
jimherd 0:e7f4a9247af2 33 #define MD25_VOLTAGE_REG 10
jimherd 0:e7f4a9247af2 34 #define MD25_CURRENT1_REG 11
jimherd 0:e7f4a9247af2 35 #define MD25_CURRENT2_REG 12
jimherd 0:e7f4a9247af2 36 #define MD25_SOFTWAREVER_REG 13
jimherd 0:e7f4a9247af2 37 #define MD25_ACCELRATE_REG 14
jimherd 0:e7f4a9247af2 38 #define MD25_MODE_REG 15
jimherd 0:e7f4a9247af2 39 #define MD25_CMD_REG 16 // command register
jimherd 0:e7f4a9247af2 40 //
jimherd 0:e7f4a9247af2 41 // Command register command set
jimherd 0:e7f4a9247af2 42 //
jimherd 0:e7f4a9247af2 43 #define MD25_RESET_ENCODERS 0x20
jimherd 0:e7f4a9247af2 44 #define MD25_DIABLE SPEED_REGULATION 0x30
jimherd 0:e7f4a9247af2 45 #define MD25_ENABLE_SPEED_REGULATION 0x31
jimherd 0:e7f4a9247af2 46 #define MD25_DISABLE_TIMEOUT 0x32
jimherd 0:e7f4a9247af2 47 #define MD25_ENABLE_TIMEOUT 0x33
jimherd 0:e7f4a9247af2 48
jimherd 0:e7f4a9247af2 49 /** MD25 class
jimherd 0:e7f4a9247af2 50 *
jimherd 0:e7f4a9247af2 51 * Allow access to an MD25 Dual 12V 2.8A H-Bridge DC Motor Driver
jimherd 0:e7f4a9247af2 52 *
jimherd 0:e7f4a9247af2 53 * @code
jimherd 0:e7f4a9247af2 54 * MD25 motor_control(p9,p10); // assumes default address of 0xB0
jimherd 0:e7f4a9247af2 55 * or
jimherd 0:e7f4a9247af2 56 * MD25 motor_control(p9, p10, 0xB0);
jimherd 0:e7f4a9247af2 57 * @endcode
jimherd 0:e7f4a9247af2 58 */
jimherd 0:e7f4a9247af2 59 class MD25 {
jimherd 0:e7f4a9247af2 60 public:
jimherd 0:e7f4a9247af2 61 /** Constructor for the MD25 connected to specified I2C pins at a specified address
jimherd 0:e7f4a9247af2 62 *
jimherd 0:e7f4a9247af2 63 * @param sda I2C data pin
jimherd 0:e7f4a9247af2 64 * @param scl I2C clock pin
jimherd 0:e7f4a9247af2 65 * @param i2cAddress I2C address
jimherd 0:e7f4a9247af2 66 */
jimherd 0:e7f4a9247af2 67 MD25(PinName sda, PinName scl, int MD25_i2cAddress);
jimherd 0:e7f4a9247af2 68
jimherd 0:e7f4a9247af2 69 /** Constructor for the MD25 connected to specified I2C pins at default address
jimherd 0:e7f4a9247af2 70 *
jimherd 0:e7f4a9247af2 71 * @param sda I2C data pin
jimherd 0:e7f4a9247af2 72 * @param scl I2C clock pin
jimherd 0:e7f4a9247af2 73 */
jimherd 0:e7f4a9247af2 74 MD25(PinName sda, PinName scl);
jimherd 0:e7f4a9247af2 75
jimherd 1:8046f460a725 76 /** Read encoder for channel 1
jimherd 1:8046f460a725 77 *
jimherd 1:8046f460a725 78 * @return 32-bit signed integer value of current encoder value for channel 1
jimherd 1:8046f460a725 79 */
jimherd 1:8046f460a725 80 int32_t getEncoder1(void);
jimherd 1:8046f460a725 81
jimherd 1:8046f460a725 82 /** Read encoder for channel 2
jimherd 1:8046f460a725 83 *
jimherd 1:8046f460a725 84 * @return 32-bit signed integer value of current encoder value for channel 2
jimherd 1:8046f460a725 85 */
jimherd 1:8046f460a725 86 int32_t getEncoder2(void);
jimherd 1:8046f460a725 87
jimherd 1:8046f460a725 88 /** set speed registers for both channels
jimherd 1:8046f460a725 89 *
jimherd 1:8046f460a725 90 * Effect of value is dependent on system mode
jimherd 1:8046f460a725 91 *
jimherd 1:8046f460a725 92 * @param speed_1 speed register for channel 1 (0->255)
jimherd 1:8046f460a725 93 * @param speed_2 speed register for channel 2 (0->255)
jimherd 1:8046f460a725 94 */
jimherd 1:8046f460a725 95 void setSpeedRegisters(uint8_t speed_1, uint8_t speed_2);
jimherd 1:8046f460a725 96
jimherd 1:8046f460a725 97 /** set speed register for channel 1
jimherd 1:8046f460a725 98 *
jimherd 1:8046f460a725 99 * Effect of value is dependent on system mode
jimherd 1:8046f460a725 100 *
jimherd 1:8046f460a725 101 * @param speed_1 speed register for channel 1 (0->255)
jimherd 1:8046f460a725 102 */
jimherd 0:e7f4a9247af2 103 void setSpeed1Reg(uint8_t speed);
jimherd 1:8046f460a725 104
jimherd 1:8046f460a725 105 /** set speed register for channel 2
jimherd 1:8046f460a725 106 *
jimherd 1:8046f460a725 107 * Effect of value is dependent on system mode
jimherd 1:8046f460a725 108 *
jimherd 1:8046f460a725 109 * @param speed_2 speed register for channel 2 (0->255)
jimherd 1:8046f460a725 110 */
jimherd 0:e7f4a9247af2 111 void setSpeed2Reg(uint8_t speed);
jimherd 1:8046f460a725 112
jimherd 1:8046f460a725 113 /** switch motor 1 off
jimherd 1:8046f460a725 114 */
jimherd 1:8046f460a725 115 void stopMotor1(void);
jimherd 1:8046f460a725 116
jimherd 1:8046f460a725 117 /** switch motor 2 off
jimherd 1:8046f460a725 118 */
jimherd 1:8046f460a725 119 void stopMotor2(void);
jimherd 1:8046f460a725 120
jimherd 1:8046f460a725 121 /** switch both motors off
jimherd 1:8046f460a725 122 */
jimherd 1:8046f460a725 123 void stopMotors(void);
jimherd 1:8046f460a725 124
jimherd 1:8046f460a725 125 /** read current software version
jimherd 1:8046f460a725 126 *
jimherd 1:8046f460a725 127 * @return version number
jimherd 1:8046f460a725 128 */
jimherd 1:8046f460a725 129 uint32_t getSoftwareVersion(void);
jimherd 1:8046f460a725 130
jimherd 1:8046f460a725 131 /** read battery voltage
jimherd 1:8046f460a725 132 *
jimherd 2:e575d390c730 133 * @return voltage value in float format
jimherd 1:8046f460a725 134 */
jimherd 1:8046f460a725 135 float getBatteryVolts(void);
jimherd 1:8046f460a725 136
jimherd 1:8046f460a725 137 /** read acceleration rate
jimherd 1:8046f460a725 138 *
jimherd 1:8046f460a725 139 * @return acceleration rate
jimherd 2:e575d390c730 140 */
jimherd 1:8046f460a725 141 uint8_t getAccelerationRate(void);
jimherd 2:e575d390c730 142
jimherd 2:e575d390c730 143 /** read current from motor channel 1
jimherd 2:e575d390c730 144 *
jimherd 2:e575d390c730 145 * @return current value
jimherd 2:e575d390c730 146 */
jimherd 2:e575d390c730 147 uint8_t getMotor1Current(void);
jimherd 1:8046f460a725 148
jimherd 2:e575d390c730 149 /** read current from motor channel 2
jimherd 2:e575d390c730 150 *
jimherd 2:e575d390c730 151 * @return current value in apprx. units of 0.1amp
jimherd 2:e575d390c730 152 */
jimherd 1:8046f460a725 153 uint8_t getMotor2Current(void);
jimherd 2:e575d390c730 154
jimherd 2:e575d390c730 155 /** read current speed register for motor channel 1
jimherd 2:e575d390c730 156 *
jimherd 2:e575d390c730 157 * @return speed value (0->255); meaning dependent on mode
jimherd 2:e575d390c730 158 */
jimherd 1:8046f460a725 159 uint8_t getMotor1Speed(void);
jimherd 2:e575d390c730 160
jimherd 2:e575d390c730 161 /** read current speed register for motor channel 2
jimherd 2:e575d390c730 162 *
jimherd 2:e575d390c730 163 * @return speed value (0->255); meaning dependent on mode
jimherd 2:e575d390c730 164 */
jimherd 1:8046f460a725 165 uint8_t getMotor2Speed(void);
jimherd 2:e575d390c730 166
jimherd 2:e575d390c730 167 /** read current mode
jimherd 2:e575d390c730 168 *
jimherd 2:e575d390c730 169 * @return mode value (0, 1, 2, or 3); default is mode 0
jimherd 2:e575d390c730 170 */
jimherd 1:8046f460a725 171 uint8_t getMode(void);
jimherd 2:e575d390c730 172
jimherd 2:e575d390c730 173 /** set system mode
jimherd 2:e575d390c730 174 *
jimherd 2:e575d390c730 175 * @param mode value (0, 1, 2, or 3)
jimherd 2:e575d390c730 176 */
jimherd 0:e7f4a9247af2 177 void setMode(uint8_t mode);
jimherd 2:e575d390c730 178
jimherd 2:e575d390c730 179 /** set acceleration rate
jimherd 2:e575d390c730 180 *
jimherd 2:e575d390c730 181 * @param rate acceleration rate
jimherd 2:e575d390c730 182 */
jimherd 0:e7f4a9247af2 183 void setAccelerationRate(uint8_t rate);
jimherd 2:e575d390c730 184
jimherd 2:e575d390c730 185 /** send command to command register
jimherd 2:e575d390c730 186 *
jimherd 2:e575d390c730 187 * @param command command code
jimherd 2:e575d390c730 188 *
jimherd 2:e575d390c730 189 * MD25_RESET_ENCODERS 0x20
jimherd 2:e575d390c730 190 * MD25_DIABLE SPEED_REGULATION 0x30
jimherd 2:e575d390c730 191 * MD25_ENABLE_SPEED_REGULATION 0x31
jimherd 2:e575d390c730 192 * MD25_DISABLE_TIMEOUT 0x32
jimherd 2:e575d390c730 193 * MD25_ENABLE_TIMEOUT 0x33
jimherd 2:e575d390c730 194 */
jimherd 0:e7f4a9247af2 195 void setCommand(uint8_t command);
jimherd 0:e7f4a9247af2 196
jimherd 0:e7f4a9247af2 197 private:
jimherd 1:8046f460a725 198 I2C _i2c;
jimherd 1:8046f460a725 199 uint8_t current_mode;
jimherd 0:e7f4a9247af2 200 uint8_t MD25_i2cAddress;
jimherd 0:e7f4a9247af2 201
jimherd 1:8046f460a725 202 uint8_t readRegisterbyte(uint8_t reg);
jimherd 1:8046f460a725 203 void writeRegisterbyte(uint8_t reg, uint8_t value);
jimherd 0:e7f4a9247af2 204 };
jimherd 0:e7f4a9247af2 205
jimherd 0:e7f4a9247af2 206 #endif // MBED_md25_h