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 21:18:59 2011 +0000
Revision:
1:8046f460a725
Parent:
0:e7f4a9247af2
Child:
2:e575d390c730
Mode to store set mode.

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 1:8046f460a725 133 * Actual voltage = (10 * value)
jimherd 1:8046f460a725 134 *
jimherd 1:8046f460a725 135 * @return voltage value in units of 0.1v
jimherd 1:8046f460a725 136 */
jimherd 1:8046f460a725 137 float getBatteryVolts(void);
jimherd 1:8046f460a725 138
jimherd 1:8046f460a725 139 /** read acceleration rate
jimherd 1:8046f460a725 140 *
jimherd 1:8046f460a725 141 * @return acceleration rate
jimherd 1:8046f460a725 142 */
jimherd 1:8046f460a725 143 uint8_t getAccelerationRate(void);
jimherd 1:8046f460a725 144
jimherd 1:8046f460a725 145 uint8_t getMotor1Current(void);
jimherd 1:8046f460a725 146 uint8_t getMotor2Current(void);
jimherd 1:8046f460a725 147 uint8_t getMotor1Speed(void);
jimherd 1:8046f460a725 148 uint8_t getMotor2Speed(void);
jimherd 1:8046f460a725 149 uint8_t getMode(void);
jimherd 0:e7f4a9247af2 150 void setMode(uint8_t mode);
jimherd 0:e7f4a9247af2 151 void setAccelerationRate(uint8_t rate);
jimherd 0:e7f4a9247af2 152 void setCommand(uint8_t command);
jimherd 0:e7f4a9247af2 153
jimherd 0:e7f4a9247af2 154 private:
jimherd 1:8046f460a725 155 I2C _i2c;
jimherd 1:8046f460a725 156 uint8_t current_mode;
jimherd 0:e7f4a9247af2 157 uint8_t MD25_i2cAddress;
jimherd 0:e7f4a9247af2 158
jimherd 1:8046f460a725 159 uint8_t readRegisterbyte(uint8_t reg);
jimherd 1:8046f460a725 160 void writeRegisterbyte(uint8_t reg, uint8_t value);
jimherd 0:e7f4a9247af2 161 };
jimherd 0:e7f4a9247af2 162
jimherd 0:e7f4a9247af2 163 #endif // MBED_md25_h