Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Nucleo_motors HTU21D_HELLOWORLD Major_dHome pixyMajordhome ... more
MD25.h
00001 // *********************************************************************** 00002 // MBED MD25 H-bridge Motor Controller. 00003 // 00004 // Based on Arduino code by Richie Reynolds 00005 // *********************************************************************** 00006 #ifndef MBED_MD25_h 00007 #define MBED_MD25_h 00008 00009 #include "mbed.h" 00010 00011 #define MD25_DEFAULT_ADDRESS 0xB0 00012 // 00013 // modes 00014 // 00015 // Mode Description 00016 // 0 Skid-steer with unsigned speed values (STOP = 128) 00017 // 1 Skid-steer with signed speed values (STOP = 0) 00018 // 2 Forward/turn steer with unsigned values (speed1 to control both motors, speed2 for turn) 00019 // 3 Forward/turn steer with signed values (speed1 to control both motors, speed2 for turn) 00020 // 00021 #define MODE_0 0 00022 #define MODE_1 1 00023 #define MODE_2 2 00024 #define MODE_3 3 00025 00026 // 00027 // register definitions 00028 // 00029 #define MD25_SPEED1_REG 0 00030 #define MD25_SPEED2_REG 1 00031 #define MD25_ENCODER1_REG 2 00032 #define MD25_ENCODER2_REG 6 00033 #define MD25_VOLTAGE_REG 10 00034 #define MD25_CURRENT1_REG 11 00035 #define MD25_CURRENT2_REG 12 00036 #define MD25_SOFTWAREVER_REG 13 00037 #define MD25_ACCELRATE_REG 14 00038 #define MD25_MODE_REG 15 00039 #define MD25_CMD_REG 16 // command register 00040 // 00041 // Command register command set 00042 // 00043 #define MD25_RESET_ENCODERS 0x20 00044 #define MD25_DIABLE SPEED_REGULATION 0x30 00045 #define MD25_ENABLE_SPEED_REGULATION 0x31 00046 #define MD25_DISABLE_TIMEOUT 0x32 00047 #define MD25_ENABLE_TIMEOUT 0x33 00048 00049 /** MD25 class 00050 * 00051 * Allow access to an MD25 Dual 12V 2.8A H-Bridge DC Motor Driver 00052 * 00053 * @code 00054 * MD25 motor_control(p9,p10); // assumes default address of 0xB0 00055 * or 00056 * MD25 motor_control(p9, p10, 0xB0); 00057 * @endcode 00058 */ 00059 class MD25 { 00060 public: 00061 /** Constructor for the MD25 connected to specified I2C pins at a specified address 00062 * 00063 * @param sda I2C data pin 00064 * @param scl I2C clock pin 00065 * @param i2cAddress I2C address 00066 */ 00067 MD25(PinName sda, PinName scl, int MD25_i2cAddress); 00068 00069 /** Constructor for the MD25 connected to specified I2C pins at default address 00070 * 00071 * @param sda I2C data pin 00072 * @param scl I2C clock pin 00073 */ 00074 MD25(PinName sda, PinName scl); 00075 00076 /** Read encoder for channel 1 00077 * 00078 * @return 32-bit signed integer value of current encoder value for channel 1 00079 */ 00080 int32_t getEncoder1(void); 00081 00082 /** Read encoder for channel 2 00083 * 00084 * @return 32-bit signed integer value of current encoder value for channel 2 00085 */ 00086 int32_t getEncoder2(void); 00087 00088 /** set speed registers for both channels 00089 * 00090 * Effect of value is dependent on system mode 00091 * 00092 * @param speed_1 speed register for channel 1 (0->255) 00093 * @param speed_2 speed register for channel 2 (0->255) 00094 */ 00095 void setSpeedRegisters(uint8_t speed_1, uint8_t speed_2); 00096 00097 /** set speed register for channel 1 00098 * 00099 * Effect of value is dependent on system mode 00100 * 00101 * @param speed_1 speed register for channel 1 (0->255) 00102 */ 00103 void setSpeed1Reg(uint8_t speed); 00104 00105 /** set speed register for channel 2 00106 * 00107 * Effect of value is dependent on system mode 00108 * 00109 * @param speed_2 speed register for channel 2 (0->255) 00110 */ 00111 void setSpeed2Reg(uint8_t speed); 00112 00113 /** switch motor 1 off 00114 */ 00115 void stopMotor1(void); 00116 00117 /** switch motor 2 off 00118 */ 00119 void stopMotor2(void); 00120 00121 /** switch both motors off 00122 */ 00123 void stopMotors(void); 00124 00125 /** read current software version 00126 * 00127 * @return version number 00128 */ 00129 uint32_t getSoftwareVersion(void); 00130 00131 /** read battery voltage 00132 * 00133 * @return voltage value in float format 00134 */ 00135 float getBatteryVolts(void); 00136 00137 /** read acceleration rate 00138 * 00139 * @return acceleration rate 00140 */ 00141 uint8_t getAccelerationRate(void); 00142 00143 /** read current from motor channel 1 00144 * 00145 * @return current value 00146 */ 00147 uint8_t getMotor1Current(void); 00148 00149 /** read current from motor channel 2 00150 * 00151 * @return current value in apprx. units of 0.1amp 00152 */ 00153 uint8_t getMotor2Current(void); 00154 00155 /** read current speed register for motor channel 1 00156 * 00157 * @return speed value (0->255); meaning dependent on mode 00158 */ 00159 uint8_t getMotor1Speed(void); 00160 00161 /** read current speed register for motor channel 2 00162 * 00163 * @return speed value (0->255); meaning dependent on mode 00164 */ 00165 uint8_t getMotor2Speed(void); 00166 00167 /** read current mode 00168 * 00169 * @return mode value (0, 1, 2, or 3); default is mode 0 00170 */ 00171 uint8_t getMode(void); 00172 00173 /** set system mode 00174 * 00175 * @param mode value (0, 1, 2, or 3) 00176 */ 00177 void setMode(uint8_t mode); 00178 00179 /** set acceleration rate 00180 * 00181 * @param rate acceleration rate 00182 */ 00183 void setAccelerationRate(uint8_t rate); 00184 00185 /** send command to command register 00186 * 00187 * @param command command code 00188 * 00189 * MD25_RESET_ENCODERS 0x20 00190 * MD25_DIABLE SPEED_REGULATION 0x30 00191 * MD25_ENABLE_SPEED_REGULATION 0x31 00192 * MD25_DISABLE_TIMEOUT 0x32 00193 * MD25_ENABLE_TIMEOUT 0x33 00194 */ 00195 void setCommand(uint8_t command); 00196 00197 private: 00198 I2C _i2c; 00199 uint8_t current_mode; 00200 uint8_t MD25_i2cAddress; 00201 00202 uint8_t readRegisterbyte(uint8_t reg); 00203 void writeRegisterbyte(uint8_t reg, uint8_t value); 00204 }; 00205 00206 #endif // MBED_md25_h
Generated on Wed Jul 20 2022 14:02:13 by
1.7.2