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.
Fork of BridgeDriver by
BridgeDriver.h
00001 #include "mbed.h" 00002 #include "MCP23017.h" 00003 #include "TextLCD.h" //if using diagnostic 00004 00005 #ifndef BRIDGEDRIVER_H 00006 #define BRIDGEDRIVER_H 00007 00008 00009 #define DEFAULT_PWM_PERIOD 0.02 00010 00011 //pin definitions 00012 #define PIN_CH1 P1_18 //hardware PWM1[1] 00013 #define PIN_CH2 P2_0 //hardware PWM1[1] 00014 #define PIN_CH3 P1_20 //hardware PWM1[2] 00015 #define PIN_CH4 P2_1 //hardware PWM1[2] 00016 #define PIN_CH5 P1_21 //hardware PWM1[3] 00017 #define PIN_CH6 P1_23 //hardware PWM1[4] 00018 #define PIN_CH7 P1_24 //hardware PWM1[5] 00019 #define PIN_CH8 P1_26 //hardware PWM1[6] 00020 00021 00022 #define EN_ADDR 0x4C //i2c address of enable pin port expander. 0x40 for rev 1.0, 0x4C for rev 2.0 00023 #define LED_ADDR 0x4E //i2c address of button and LED port expander 00024 00025 /** BridgeDriver class 00026 * For controlling the Test Controller bridges. 00027 */ 00028 class BridgeDriver{ 00029 00030 00031 public: 00032 /** Channel pair labels 00033 * 00034 * MOTOR_A - Channel 1 and 2. 00035 * MOTOR_B - Channel 3 and 4. 00036 * MOTOR_C - Channel 5 and 6. 00037 * MOTOR_D - Channel 7 and 8. 00038 */ 00039 enum Motors{ MOTOR_A, 00040 MOTOR_B, 00041 MOTOR_C, 00042 MOTOR_D 00043 }; 00044 00045 /** Create BridgeDriver object 00046 * 00047 * Enabling PWM for a channel pair links the channels so that they are controlled together. This should be used when connecting a motor between these channels. 00048 * Disabling PWM for a channel pair allows individual on/off control for each channel. 00049 * 00050 * MOTOR_A - Channel 1 and 2. 00051 * MOTOR_B - Channel 3 and 4. 00052 * MOTOR_C - Channel 5 and 6. 00053 * MOTOR_D - Channel 7 and 8. 00054 * @param *i2c A pointer to the I2C bus that the Enable control and LED port expanders are on. 00055 * @param enPwmA (Optional) 1 enables PWM on channels 1 and 2 for motor control, 0 disables PWM on channels 1 and 2 for solenoid control. Defaults to 0. 00056 * @param enPwmB (Optional) 1 enables PWM on channels 3 and 4 for motor control, 0 disables PWM on channels 3 and 4 for solenoid control. Defaults to 0. 00057 * @param enPwmC (Optional) 1 enables PWM on channels 5 and 6 for motor control, 0 disables PWM on channels 5 and 6 for solenoid control. Defaults to 0. 00058 * @param enPwmD (Optional) 1 enables PWM on channels 7 and 8 for motor control, 0 disables PWM on channels 7 and 8 for solenoid control. Defaults to 0. 00059 * @param enAddr (Optional) Address of the Enable control port expander. May be necessary if not using a rev 1.0 board. TODO: update for rev 2.0. 00060 * @param ledAddr (Optional) Address of the LED control port expander. May be necessary if not using rev 1.0 or 2.0 board. 00061 */ 00062 BridgeDriver( I2C *i2c, uint8_t enPwmA = 0, uint8_t enPwmB = 0, uint8_t enPwmC = 0, uint8_t enPwmD = 0, uint8_t enAddr = EN_ADDR, uint8_t ledAddr = LED_ADDR); 00063 00064 /** Destroy BridgeDriver object 00065 */ 00066 ~BridgeDriver(); 00067 00068 /** Enables/disables PWM for channel pairs 00069 * 00070 * Enabling PWM for a channel pair links the channels so that they are controlled together. This should be used when connecting a motor between these channels. 00071 * Disabling PWM for a channel pair allows individual on/off control for each channel. 00072 * 00073 * MOTOR_A - Channel 1 and 2. 00074 * MOTOR_B - Channel 3 and 4. 00075 * MOTOR_C - Channel 5 and 6. 00076 * MOTOR_D - Channel 7 and 8. 00077 * @param enPwmA 1 enables PWM on channels 1 and 2 for motor control, 0 disables PWM on channels 1 and 2 for solenoid control. 00078 * @param enPwmB 1 enables PWM on channels 3 and 4 for motor control, 0 disables PWM on channels 3 and 4 for solenoid control. 00079 * @param enPwmC 1 enables PWM on channels 5 and 6 for motor control, 0 disables PWM on channels 5 and 6 for solenoid control. 00080 * @param enPwmD 1 enables PWM on channels 7 and 8 for motor control, 0 disables PWM on channels 7 and 8 for solenoid control. 00081 */ 00082 void enablePwm(uint8_t enPwmA, uint8_t enPwmB, uint8_t enPwmC, uint8_t enPwmD); 00083 00084 /** Enables/disables PWM for a given channel pair 00085 * 00086 * Enabling PWM for a channel pair links the channels so that they are controlled together. This should be used when connecting a motor between these channels. 00087 * Disabling PWM for a channel pair allows individual on/off control for each channel. 00088 * 00089 * MOTOR_A - Channel 1 and 2. 00090 * MOTOR_B - Channel 3 and 4. 00091 * MOTOR_C - Channel 5 and 6. 00092 * MOTOR_D - Channel 7 and 8. 00093 * @param motor Motor (channel pair) to enale/disable PWM. 00094 * @param enPwm 1 enables PWM on channel pair for motor control, 0 disables PWM on channel pair for solenoid control. 00095 */ 00096 void enablePwm(Motors motor, uint8_t enPwm); 00097 00098 /** Enables/disables braking for channel pairs 00099 * @param enBrakeA 1 drives output to GND when off, 0 floats output when off. 00100 * @param enBrakeB 1 drives output to GND when off, 0 floats output when off. 00101 * @param enBrakeC 1 drives output to GND when off, 0 floats output when off. 00102 * @param enBrakeD 1 drives output to GND when off, 0 floats output when off. 00103 */ 00104 void enableBraking(uint8_t enBrakeA, uint8_t enBrakeB, uint8_t enBrakeC, uint8_t enBrakeD); 00105 00106 /** Enables/disables braking for given channel pair 00107 * @param enBrakeA 1 drives output to GND when off, 0 floats output when off. 00108 */ 00109 void enableBraking(Motors motor, uint8_t enBrake); 00110 00111 /** Force a specific channel to GND without changing braking default 00112 * Will fail if PWM is enabled on given channel. Use forceBrake(Motors motor) to force braking on channels with PWM enabled. 00113 * @param ch Channel to drive to GND 00114 * @return 00115 * 0 on success, 00116 * non 0 on failure. 00117 */ 00118 int forceBrake(uint8_t ch); 00119 00120 /** Force a specific motor (channel pair) to GND without changing braking default 00121 * Will fail if PWM is disabled on given channel. Use forceBrake(uint8_t ch) to force braking on channels with PWM disabled. 00122 * @param motor Motor (channel pair) to drive to GND 00123 * @return 00124 * 0 on success, 00125 * non 0 on failure. 00126 */ 00127 int forceBrake(Motors motor); 00128 00129 /** Force a specific channel to float without changing braking default 00130 * Will float both channels of a motor (channel pair) if PWM is enabled for the channel. 00131 * @param ch Channel to float. 00132 * @return 00133 * 0 on success, 00134 * non 0 on failure. 00135 */ 00136 int forceFloat(uint8_t ch); 00137 00138 /** Force a specific motor to float without changing braking default 00139 * Will fail if PWM is disabled on given channel. Use forceFloat(uint8_t ch) to force floating on channels with PWM disabled. 00140 * @param motor Motor (channel pair) to float. 00141 * @return 00142 * 0 on success, 00143 * non 0 on failure. 00144 */ 00145 int forceFloat(Motors motor); 00146 00147 /** Set all outputs with a single byte (bitwise control). 00148 * Will fail if PWM is enabled for any motor (channel pair). 00149 * @param state Sets all the outputs. Each bit controls one channel. 00150 * 1 drives high, 00151 * 0 brakes (drives to GND) or floats depending on setting of enBrake. 00152 * @return 00153 * -1 on failure, 00154 * 0 if all outputs are set to 0, 00155 * 1 if any output is set to 1. 00156 */ 00157 int drive(uint8_t state); 00158 00159 /** Set the output of one channel 00160 * @param ch Channel to set. 00161 * @param on State to set. 00162 * 1 drives high, 00163 * 0 brakes (drives to GND) or floats depending on setting of enBrake. 00164 * @return 00165 * -1 on fauilure, 00166 * 0 if set to 0, 00167 * 1 if set to 1. 00168 */ 00169 int drive(uint8_t ch, uint8_t on); 00170 00171 /** Control the speed of a motor 00172 * @param motor Motor (channel pair) to control. 00173 * @param speed Speed to set motor to. Acceptable range: -1.0 to 1.0. Speed of 0.0 will coast or brake depending on setting of enBrake. 00174 * @return 00175 * -1 on failure, 00176 * speed of set output on success. 00177 */ 00178 float drive(Motors motor, float speed); 00179 00180 /** Control the speed of a motor 00181 * @param motor Motor (channel pair) to control. 00182 * @param speed Speed to set motor to. Acceptable range: 0.0 to 1.0. Speed of 0.0 will coast or brake depending on setting of enBrake. 00183 * @param dir Set direction to drive motor. 00184 * 1: forward, 00185 * -1: reverse, 00186 * 0: brake, irregardless of setting of enBrake or speed. 00187 * @return 00188 * -1 on failure, 00189 * speed of set output on success. 00190 */ 00191 float drive(Motors motor, int8_t dir, float speed); 00192 00193 00194 /** Display diagnostic information on the LCD 00195 * @param *lcd Pointer to TextLCD object to display info on. 00196 */ 00197 void diagnostic(TextLCD_I2C *lcd); 00198 00199 /** Change the PWM period 00200 * @param newPWMperiod The new PWM period. In seconds. System default is 0.0002 (5kHz). 00201 */ 00202 void setPWMperiod(float newPWMperiod); 00203 00204 00205 00206 private: 00207 DigitalOut *_d[8]; 00208 PwmOut *_p[8]; 00209 MCP23017 *_EnCtl; 00210 MCP23017 *_IO; 00211 I2C *_i2c; 00212 uint8_t _enAddr; 00213 uint8_t _ledAddr; 00214 uint8_t _pwm[4]; //enabled full bridges 00215 uint8_t _pwmCh; //currently actie PWM pins 00216 int8_t _dir[4]; //dir of 1 means lower # ch is driving, dir of -1 means higher number ch is driving 00217 uint8_t _braking[4]; 00218 uint8_t _oldLedState; //for setLed functions. Kept external to overload function 00219 float _PWMperiod; 00220 00221 void enableCh(uint8_t ch, uint8_t en); 00222 void setLed(uint8_t ch, uint8_t en); 00223 void setLed(uint8_t state); 00224 void setPwm(uint8_t ch, uint8_t en); 00225 00226 00227 00228 }; 00229 00230 00231 00232 #endif
Generated on Thu Jul 14 2022 05:57:56 by
1.7.2
