frequency change?

Fork of BridgeDriver by Jason T

Committer:
mehatfie
Date:
Mon Nov 10 22:55:46 2014 +0000
Revision:
13:6561ddb98d6e
Parent:
12:4db83c772b51
don't know

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jason701802 0:b1e3fa917367 1 #include "mbed.h"
jason701802 0:b1e3fa917367 2 #include "MCP23017.h"
jason701802 1:c8e328389a98 3 #include "TextLCD.h" //if using diagnostic
jason701802 0:b1e3fa917367 4
jason701802 5:c1f32ce026f6 5 #ifndef BRIDGEDRIVER_H
jason701802 0:b1e3fa917367 6 #define BRIDGEDRIVER_H
jason701802 0:b1e3fa917367 7
jason701802 4:87768972db3f 8
mehatfie 13:6561ddb98d6e 9 #define DEFAULT_PWM_PERIOD 0.02
jason701802 4:87768972db3f 10
jason701802 0:b1e3fa917367 11 //pin definitions
jason701802 0:b1e3fa917367 12 #define PIN_CH1 P1_18 //hardware PWM1[1]
jason701802 0:b1e3fa917367 13 #define PIN_CH2 P2_0 //hardware PWM1[1]
jason701802 0:b1e3fa917367 14 #define PIN_CH3 P1_20 //hardware PWM1[2]
jason701802 0:b1e3fa917367 15 #define PIN_CH4 P2_1 //hardware PWM1[2]
jason701802 0:b1e3fa917367 16 #define PIN_CH5 P1_21 //hardware PWM1[3]
jason701802 0:b1e3fa917367 17 #define PIN_CH6 P1_23 //hardware PWM1[4]
jason701802 0:b1e3fa917367 18 #define PIN_CH7 P1_24 //hardware PWM1[5]
jason701802 0:b1e3fa917367 19 #define PIN_CH8 P1_26 //hardware PWM1[6]
jason701802 0:b1e3fa917367 20
jason701802 0:b1e3fa917367 21
jason701802 12:4db83c772b51 22 #define EN_ADDR 0x4C //i2c address of enable pin port expander. 0x40 for rev 1.0, 0x4C for rev 2.0
jason701802 0:b1e3fa917367 23 #define LED_ADDR 0x4E //i2c address of button and LED port expander
jason701802 0:b1e3fa917367 24
jason701802 9:00e8b7afb2c7 25 /** BridgeDriver class
jason701802 10:60c3d513b872 26 * For controlling the Test Controller bridges.
jason701802 9:00e8b7afb2c7 27 */
jason701802 0:b1e3fa917367 28 class BridgeDriver{
jason701802 1:c8e328389a98 29
jason701802 1:c8e328389a98 30
jason701802 1:c8e328389a98 31 public:
jason701802 9:00e8b7afb2c7 32 /** Channel pair labels
jason701802 11:2481432a0988 33 *
jason701802 9:00e8b7afb2c7 34 * MOTOR_A - Channel 1 and 2.
jason701802 9:00e8b7afb2c7 35 * MOTOR_B - Channel 3 and 4.
jason701802 9:00e8b7afb2c7 36 * MOTOR_C - Channel 5 and 6.
jason701802 9:00e8b7afb2c7 37 * MOTOR_D - Channel 7 and 8.
jason701802 9:00e8b7afb2c7 38 */
jason701802 7:7dabd934ebe4 39 enum Motors{ MOTOR_A,
jason701802 7:7dabd934ebe4 40 MOTOR_B,
jason701802 7:7dabd934ebe4 41 MOTOR_C,
jason701802 7:7dabd934ebe4 42 MOTOR_D
jason701802 1:c8e328389a98 43 };
jason701802 1:c8e328389a98 44
jason701802 9:00e8b7afb2c7 45 /** Create BridgeDriver object
jason701802 9:00e8b7afb2c7 46 *
jason701802 11:2481432a0988 47 * 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.
jason701802 11:2481432a0988 48 * Disabling PWM for a channel pair allows individual on/off control for each channel.
jason701802 11:2481432a0988 49 *
jason701802 11:2481432a0988 50 * MOTOR_A - Channel 1 and 2.
jason701802 11:2481432a0988 51 * MOTOR_B - Channel 3 and 4.
jason701802 11:2481432a0988 52 * MOTOR_C - Channel 5 and 6.
jason701802 11:2481432a0988 53 * MOTOR_D - Channel 7 and 8.
jason701802 9:00e8b7afb2c7 54 * @param *i2c A pointer to the I2C bus that the Enable control and LED port expanders are on.
jason701802 9:00e8b7afb2c7 55 * @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.
jason701802 9:00e8b7afb2c7 56 * @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.
jason701802 9:00e8b7afb2c7 57 * @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.
jason701802 9:00e8b7afb2c7 58 * @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.
jason701802 9:00e8b7afb2c7 59 * @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.
jason701802 9:00e8b7afb2c7 60 * @param ledAddr (Optional) Address of the LED control port expander. May be necessary if not using rev 1.0 or 2.0 board.
jason701802 9:00e8b7afb2c7 61 */
jason701802 1:c8e328389a98 62 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);
jason701802 0:b1e3fa917367 63
jason701802 9:00e8b7afb2c7 64 /** Destroy BridgeDriver object
jason701802 9:00e8b7afb2c7 65 */
jason701802 0:b1e3fa917367 66 ~BridgeDriver();
jason701802 0:b1e3fa917367 67
jason701802 9:00e8b7afb2c7 68 /** Enables/disables PWM for channel pairs
jason701802 11:2481432a0988 69 *
jason701802 11:2481432a0988 70 * 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.
jason701802 11:2481432a0988 71 * Disabling PWM for a channel pair allows individual on/off control for each channel.
jason701802 11:2481432a0988 72 *
jason701802 11:2481432a0988 73 * MOTOR_A - Channel 1 and 2.
jason701802 11:2481432a0988 74 * MOTOR_B - Channel 3 and 4.
jason701802 11:2481432a0988 75 * MOTOR_C - Channel 5 and 6.
jason701802 11:2481432a0988 76 * MOTOR_D - Channel 7 and 8.
jason701802 9:00e8b7afb2c7 77 * @param enPwmA 1 enables PWM on channels 1 and 2 for motor control, 0 disables PWM on channels 1 and 2 for solenoid control.
jason701802 9:00e8b7afb2c7 78 * @param enPwmB 1 enables PWM on channels 3 and 4 for motor control, 0 disables PWM on channels 3 and 4 for solenoid control.
jason701802 9:00e8b7afb2c7 79 * @param enPwmC 1 enables PWM on channels 5 and 6 for motor control, 0 disables PWM on channels 5 and 6 for solenoid control.
jason701802 9:00e8b7afb2c7 80 * @param enPwmD 1 enables PWM on channels 7 and 8 for motor control, 0 disables PWM on channels 7 and 8 for solenoid control.
jason701802 9:00e8b7afb2c7 81 */
jason701802 0:b1e3fa917367 82 void enablePwm(uint8_t enPwmA, uint8_t enPwmB, uint8_t enPwmC, uint8_t enPwmD);
jason701802 9:00e8b7afb2c7 83
jason701802 9:00e8b7afb2c7 84 /** Enables/disables PWM for a given channel pair
jason701802 11:2481432a0988 85 *
jason701802 11:2481432a0988 86 * 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.
jason701802 11:2481432a0988 87 * Disabling PWM for a channel pair allows individual on/off control for each channel.
jason701802 11:2481432a0988 88 *
jason701802 11:2481432a0988 89 * MOTOR_A - Channel 1 and 2.
jason701802 11:2481432a0988 90 * MOTOR_B - Channel 3 and 4.
jason701802 11:2481432a0988 91 * MOTOR_C - Channel 5 and 6.
jason701802 11:2481432a0988 92 * MOTOR_D - Channel 7 and 8.
jason701802 10:60c3d513b872 93 * @param motor Motor (channel pair) to enale/disable PWM.
jason701802 9:00e8b7afb2c7 94 * @param enPwm 1 enables PWM on channel pair for motor control, 0 disables PWM on channel pair for solenoid control.
jason701802 9:00e8b7afb2c7 95 */
jason701802 7:7dabd934ebe4 96 void enablePwm(Motors motor, uint8_t enPwm);
jason701802 0:b1e3fa917367 97
jason701802 9:00e8b7afb2c7 98 /** Enables/disables braking for channel pairs
jason701802 10:60c3d513b872 99 * @param enBrakeA 1 drives output to GND when off, 0 floats output when off.
jason701802 10:60c3d513b872 100 * @param enBrakeB 1 drives output to GND when off, 0 floats output when off.
jason701802 10:60c3d513b872 101 * @param enBrakeC 1 drives output to GND when off, 0 floats output when off.
jason701802 10:60c3d513b872 102 * @param enBrakeD 1 drives output to GND when off, 0 floats output when off.
jason701802 9:00e8b7afb2c7 103 */
jason701802 9:00e8b7afb2c7 104 void enableBraking(uint8_t enBrakeA, uint8_t enBrakeB, uint8_t enBrakeC, uint8_t enBrakeD);
jason701802 0:b1e3fa917367 105
jason701802 9:00e8b7afb2c7 106 /** Enables/disables braking for given channel pair
jason701802 10:60c3d513b872 107 * @param enBrakeA 1 drives output to GND when off, 0 floats output when off.
jason701802 9:00e8b7afb2c7 108 */
jason701802 7:7dabd934ebe4 109 void enableBraking(Motors motor, uint8_t enBrake);
jason701802 0:b1e3fa917367 110
jason701802 9:00e8b7afb2c7 111 /** Force a specific channel to GND without changing braking default
jason701802 9:00e8b7afb2c7 112 * Will fail if PWM is enabled on given channel. Use forceBrake(Motors motor) to force braking on channels with PWM enabled.
jason701802 9:00e8b7afb2c7 113 * @param ch Channel to drive to GND
jason701802 9:00e8b7afb2c7 114 * @return
jason701802 10:60c3d513b872 115 * 0 on success,
jason701802 10:60c3d513b872 116 * non 0 on failure.
jason701802 9:00e8b7afb2c7 117 */
jason701802 9:00e8b7afb2c7 118 int forceBrake(uint8_t ch);
jason701802 9:00e8b7afb2c7 119
jason701802 9:00e8b7afb2c7 120 /** Force a specific motor (channel pair) to GND without changing braking default
jason701802 9:00e8b7afb2c7 121 * Will fail if PWM is disabled on given channel. Use forceBrake(uint8_t ch) to force braking on channels with PWM disabled.
jason701802 9:00e8b7afb2c7 122 * @param motor Motor (channel pair) to drive to GND
jason701802 9:00e8b7afb2c7 123 * @return
jason701802 10:60c3d513b872 124 * 0 on success,
jason701802 10:60c3d513b872 125 * non 0 on failure.
jason701802 9:00e8b7afb2c7 126 */
jason701802 9:00e8b7afb2c7 127 int forceBrake(Motors motor);
jason701802 9:00e8b7afb2c7 128
jason701802 9:00e8b7afb2c7 129 /** Force a specific channel to float without changing braking default
jason701802 9:00e8b7afb2c7 130 * Will float both channels of a motor (channel pair) if PWM is enabled for the channel.
jason701802 9:00e8b7afb2c7 131 * @param ch Channel to float.
jason701802 9:00e8b7afb2c7 132 * @return
jason701802 10:60c3d513b872 133 * 0 on success,
jason701802 10:60c3d513b872 134 * non 0 on failure.
jason701802 9:00e8b7afb2c7 135 */
jason701802 9:00e8b7afb2c7 136 int forceFloat(uint8_t ch);
jason701802 0:b1e3fa917367 137
jason701802 9:00e8b7afb2c7 138 /** Force a specific motor to float without changing braking default
jason701802 9:00e8b7afb2c7 139 * Will fail if PWM is disabled on given channel. Use forceFloat(uint8_t ch) to force floating on channels with PWM disabled.
jason701802 9:00e8b7afb2c7 140 * @param motor Motor (channel pair) to float.
jason701802 9:00e8b7afb2c7 141 * @return
jason701802 10:60c3d513b872 142 * 0 on success,
jason701802 10:60c3d513b872 143 * non 0 on failure.
jason701802 9:00e8b7afb2c7 144 */
jason701802 9:00e8b7afb2c7 145 int forceFloat(Motors motor);
jason701802 9:00e8b7afb2c7 146
jason701802 9:00e8b7afb2c7 147 /** Set all outputs with a single byte (bitwise control).
jason701802 9:00e8b7afb2c7 148 * Will fail if PWM is enabled for any motor (channel pair).
jason701802 9:00e8b7afb2c7 149 * @param state Sets all the outputs. Each bit controls one channel.
jason701802 10:60c3d513b872 150 * 1 drives high,
jason701802 9:00e8b7afb2c7 151 * 0 brakes (drives to GND) or floats depending on setting of enBrake.
jason701802 9:00e8b7afb2c7 152 * @return
jason701802 10:60c3d513b872 153 * -1 on failure,
jason701802 10:60c3d513b872 154 * 0 if all outputs are set to 0,
jason701802 10:60c3d513b872 155 * 1 if any output is set to 1.
jason701802 9:00e8b7afb2c7 156 */
jason701802 0:b1e3fa917367 157 int drive(uint8_t state);
jason701802 9:00e8b7afb2c7 158
jason701802 9:00e8b7afb2c7 159 /** Set the output of one channel
jason701802 9:00e8b7afb2c7 160 * @param ch Channel to set.
jason701802 9:00e8b7afb2c7 161 * @param on State to set.
jason701802 10:60c3d513b872 162 * 1 drives high,
jason701802 9:00e8b7afb2c7 163 * 0 brakes (drives to GND) or floats depending on setting of enBrake.
jason701802 9:00e8b7afb2c7 164 * @return
jason701802 10:60c3d513b872 165 * -1 on fauilure,
jason701802 10:60c3d513b872 166 * 0 if set to 0,
jason701802 10:60c3d513b872 167 * 1 if set to 1.
jason701802 9:00e8b7afb2c7 168 */
jason701802 0:b1e3fa917367 169 int drive(uint8_t ch, uint8_t on);
jason701802 9:00e8b7afb2c7 170
jason701802 9:00e8b7afb2c7 171 /** Control the speed of a motor
jason701802 9:00e8b7afb2c7 172 * @param motor Motor (channel pair) to control.
jason701802 10:60c3d513b872 173 * @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.
jason701802 9:00e8b7afb2c7 174 * @return
jason701802 10:60c3d513b872 175 * -1 on failure,
jason701802 10:60c3d513b872 176 * speed of set output on success.
jason701802 9:00e8b7afb2c7 177 */
jason701802 9:00e8b7afb2c7 178 float drive(Motors motor, float speed);
jason701802 9:00e8b7afb2c7 179
jason701802 9:00e8b7afb2c7 180 /** Control the speed of a motor
jason701802 9:00e8b7afb2c7 181 * @param motor Motor (channel pair) to control.
jason701802 10:60c3d513b872 182 * @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.
jason701802 9:00e8b7afb2c7 183 * @param dir Set direction to drive motor.
jason701802 10:60c3d513b872 184 * 1: forward,
jason701802 10:60c3d513b872 185 * -1: reverse,
jason701802 10:60c3d513b872 186 * 0: brake, irregardless of setting of enBrake or speed.
jason701802 9:00e8b7afb2c7 187 * @return
jason701802 10:60c3d513b872 188 * -1 on failure,
jason701802 10:60c3d513b872 189 * speed of set output on success.
jason701802 9:00e8b7afb2c7 190 */
jason701802 9:00e8b7afb2c7 191 float drive(Motors motor, int8_t dir, float speed);
jason701802 9:00e8b7afb2c7 192
jason701802 9:00e8b7afb2c7 193
jason701802 9:00e8b7afb2c7 194 /** Display diagnostic information on the LCD
jason701802 10:60c3d513b872 195 * @param *lcd Pointer to TextLCD object to display info on.
jason701802 9:00e8b7afb2c7 196 */
jason701802 1:c8e328389a98 197 void diagnostic(TextLCD_I2C *lcd);
jason701802 9:00e8b7afb2c7 198
jason701802 9:00e8b7afb2c7 199 /** Change the PWM period
jason701802 10:60c3d513b872 200 * @param newPWMperiod The new PWM period. In seconds. System default is 0.0002 (5kHz).
jason701802 9:00e8b7afb2c7 201 */
jason701802 8:36e2cd31ccf3 202 void setPWMperiod(float newPWMperiod);
jason701802 0:b1e3fa917367 203
jason701802 0:b1e3fa917367 204
jason701802 0:b1e3fa917367 205
jason701802 0:b1e3fa917367 206 private:
jason701802 0:b1e3fa917367 207 DigitalOut *_d[8];
jason701802 0:b1e3fa917367 208 PwmOut *_p[8];
jason701802 0:b1e3fa917367 209 MCP23017 *_EnCtl;
jason701802 0:b1e3fa917367 210 MCP23017 *_IO;
jason701802 0:b1e3fa917367 211 I2C *_i2c;
jason701802 0:b1e3fa917367 212 uint8_t _enAddr;
jason701802 0:b1e3fa917367 213 uint8_t _ledAddr;
jason701802 1:c8e328389a98 214 uint8_t _pwm[4]; //enabled full bridges
jason701802 1:c8e328389a98 215 uint8_t _pwmCh; //currently actie PWM pins
jason701802 1:c8e328389a98 216 int8_t _dir[4]; //dir of 1 means lower # ch is driving, dir of -1 means higher number ch is driving
jason701802 0:b1e3fa917367 217 uint8_t _braking[4];
jason701802 1:c8e328389a98 218 uint8_t _oldLedState; //for setLed functions. Kept external to overload function
jason701802 8:36e2cd31ccf3 219 float _PWMperiod;
jason701802 0:b1e3fa917367 220
jason701802 0:b1e3fa917367 221 void enableCh(uint8_t ch, uint8_t en);
jason701802 0:b1e3fa917367 222 void setLed(uint8_t ch, uint8_t en);
jason701802 0:b1e3fa917367 223 void setLed(uint8_t state);
jason701802 0:b1e3fa917367 224 void setPwm(uint8_t ch, uint8_t en);
jason701802 0:b1e3fa917367 225
jason701802 0:b1e3fa917367 226
jason701802 0:b1e3fa917367 227
jason701802 0:b1e3fa917367 228 };
jason701802 0:b1e3fa917367 229
jason701802 0:b1e3fa917367 230
jason701802 0:b1e3fa917367 231
jason701802 0:b1e3fa917367 232 #endif