frequency change?

Fork of BridgeDriver by Jason T

Committer:
jason701802
Date:
Mon Jul 28 21:37:53 2014 +0000
Revision:
9:00e8b7afb2c7
Parent:
8:36e2cd31ccf3
Child:
10:60c3d513b872
Updated documentation

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
jason701802 8:36e2cd31ccf3 9 #define DEFAULT_PWM_PERIOD 0.0002
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 9:00e8b7afb2c7 22 #define EN_ADDR 0x40 //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 9:00e8b7afb2c7 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 9:00e8b7afb2c7 33 * MOTOR_A - Channel 1 and 2.
jason701802 9:00e8b7afb2c7 34 * MOTOR_B - Channel 3 and 4.
jason701802 9:00e8b7afb2c7 35 * MOTOR_C - Channel 5 and 6.
jason701802 9:00e8b7afb2c7 36 * MOTOR_D - Channel 7 and 8.
jason701802 9:00e8b7afb2c7 37 */
jason701802 7:7dabd934ebe4 38 enum Motors{ MOTOR_A,
jason701802 7:7dabd934ebe4 39 MOTOR_B,
jason701802 7:7dabd934ebe4 40 MOTOR_C,
jason701802 7:7dabd934ebe4 41 MOTOR_D
jason701802 1:c8e328389a98 42 };
jason701802 1:c8e328389a98 43
jason701802 9:00e8b7afb2c7 44 /** Create BridgeDriver object
jason701802 9:00e8b7afb2c7 45 *
jason701802 9:00e8b7afb2c7 46 * @param *i2c A pointer to the I2C bus that the Enable control and LED port expanders are on.
jason701802 9:00e8b7afb2c7 47 * @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 48 * @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 49 * @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 50 * @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 51 * @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 52 * @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 53 */
jason701802 1:c8e328389a98 54 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 55
jason701802 9:00e8b7afb2c7 56 /** Destroy BridgeDriver object
jason701802 9:00e8b7afb2c7 57 */
jason701802 0:b1e3fa917367 58 ~BridgeDriver();
jason701802 0:b1e3fa917367 59
jason701802 9:00e8b7afb2c7 60 /** Enables/disables PWM for channel pairs
jason701802 9:00e8b7afb2c7 61 * @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 62 * @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 63 * @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 64 * @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 65 */
jason701802 0:b1e3fa917367 66 void enablePwm(uint8_t enPwmA, uint8_t enPwmB, uint8_t enPwmC, uint8_t enPwmD);
jason701802 9:00e8b7afb2c7 67
jason701802 9:00e8b7afb2c7 68 /** Enables/disables PWM for a given channel pair
jason701802 9:00e8b7afb2c7 69 * @param motor Motor (channel pair) to enale/disable PWM
jason701802 9:00e8b7afb2c7 70 * @param enPwm 1 enables PWM on channel pair for motor control, 0 disables PWM on channel pair for solenoid control.
jason701802 9:00e8b7afb2c7 71 */
jason701802 7:7dabd934ebe4 72 void enablePwm(Motors motor, uint8_t enPwm);
jason701802 0:b1e3fa917367 73
jason701802 9:00e8b7afb2c7 74 /** Enables/disables braking for channel pairs
jason701802 9:00e8b7afb2c7 75 * @param enBrakeA 1 drives output to GND when off, 0 floats output when off
jason701802 9:00e8b7afb2c7 76 * @param enBrakeB 1 drives output to GND when off, 0 floats output when off
jason701802 9:00e8b7afb2c7 77 * @param enBrakeC 1 drives output to GND when off, 0 floats output when off
jason701802 9:00e8b7afb2c7 78 * @param enBrakeD 1 drives output to GND when off, 0 floats output when off
jason701802 9:00e8b7afb2c7 79 */
jason701802 9:00e8b7afb2c7 80 void enableBraking(uint8_t enBrakeA, uint8_t enBrakeB, uint8_t enBrakeC, uint8_t enBrakeD);
jason701802 0:b1e3fa917367 81
jason701802 9:00e8b7afb2c7 82 /** Enables/disables braking for given channel pair
jason701802 9:00e8b7afb2c7 83 * @param enBrakeA 1 drives output to GND when off, 0 floats output when off
jason701802 9:00e8b7afb2c7 84 */
jason701802 7:7dabd934ebe4 85 void enableBraking(Motors motor, uint8_t enBrake);
jason701802 0:b1e3fa917367 86
jason701802 9:00e8b7afb2c7 87 /** Force a specific channel to GND without changing braking default
jason701802 9:00e8b7afb2c7 88 * Will fail if PWM is enabled on given channel. Use forceBrake(Motors motor) to force braking on channels with PWM enabled.
jason701802 9:00e8b7afb2c7 89 * @param ch Channel to drive to GND
jason701802 9:00e8b7afb2c7 90 * @return
jason701802 9:00e8b7afb2c7 91 * 0 on success
jason701802 9:00e8b7afb2c7 92 * non 0 on failure
jason701802 9:00e8b7afb2c7 93 */
jason701802 9:00e8b7afb2c7 94 int forceBrake(uint8_t ch);
jason701802 9:00e8b7afb2c7 95
jason701802 9:00e8b7afb2c7 96 /** Force a specific motor (channel pair) to GND without changing braking default
jason701802 9:00e8b7afb2c7 97 * 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 98 * @param motor Motor (channel pair) to drive to GND
jason701802 9:00e8b7afb2c7 99 * @return
jason701802 9:00e8b7afb2c7 100 * 0 on success
jason701802 9:00e8b7afb2c7 101 * non 0 on failure
jason701802 9:00e8b7afb2c7 102 */
jason701802 9:00e8b7afb2c7 103 int forceBrake(Motors motor);
jason701802 9:00e8b7afb2c7 104
jason701802 9:00e8b7afb2c7 105 /** Force a specific channel to float without changing braking default
jason701802 9:00e8b7afb2c7 106 * Will float both channels of a motor (channel pair) if PWM is enabled for the channel.
jason701802 9:00e8b7afb2c7 107 * @param ch Channel to float.
jason701802 9:00e8b7afb2c7 108 * @return
jason701802 9:00e8b7afb2c7 109 * 0 on success
jason701802 9:00e8b7afb2c7 110 * non 0 on failure
jason701802 9:00e8b7afb2c7 111 */
jason701802 9:00e8b7afb2c7 112 int forceFloat(uint8_t ch);
jason701802 0:b1e3fa917367 113
jason701802 9:00e8b7afb2c7 114 /** Force a specific motor to float without changing braking default
jason701802 9:00e8b7afb2c7 115 * 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 116 * @param motor Motor (channel pair) to float.
jason701802 9:00e8b7afb2c7 117 * @return
jason701802 9:00e8b7afb2c7 118 * 0 on success
jason701802 9:00e8b7afb2c7 119 * non 0 on failure
jason701802 9:00e8b7afb2c7 120 */
jason701802 9:00e8b7afb2c7 121 int forceFloat(Motors motor);
jason701802 9:00e8b7afb2c7 122
jason701802 9:00e8b7afb2c7 123 /** Set all outputs with a single byte (bitwise control).
jason701802 9:00e8b7afb2c7 124 * Will fail if PWM is enabled for any motor (channel pair).
jason701802 9:00e8b7afb2c7 125 * @param state Sets all the outputs. Each bit controls one channel.
jason701802 9:00e8b7afb2c7 126 * 1 drives high
jason701802 9:00e8b7afb2c7 127 * 0 brakes (drives to GND) or floats depending on setting of enBrake.
jason701802 9:00e8b7afb2c7 128 * @return
jason701802 9:00e8b7afb2c7 129 * -1 on failure
jason701802 9:00e8b7afb2c7 130 * 0 if all outputs are set to 0
jason701802 9:00e8b7afb2c7 131 * 1 if any output is set to 1
jason701802 9:00e8b7afb2c7 132 */
jason701802 0:b1e3fa917367 133 int drive(uint8_t state);
jason701802 9:00e8b7afb2c7 134
jason701802 9:00e8b7afb2c7 135 /** Set the output of one channel
jason701802 9:00e8b7afb2c7 136 * @param ch Channel to set.
jason701802 9:00e8b7afb2c7 137 * @param on State to set.
jason701802 9:00e8b7afb2c7 138 * 1 drives high
jason701802 9:00e8b7afb2c7 139 * 0 brakes (drives to GND) or floats depending on setting of enBrake.
jason701802 9:00e8b7afb2c7 140 * @return
jason701802 9:00e8b7afb2c7 141 * -1 on fauilure
jason701802 9:00e8b7afb2c7 142 * 0 if set to 0
jason701802 9:00e8b7afb2c7 143 * 1 if set to 1
jason701802 9:00e8b7afb2c7 144 */
jason701802 0:b1e3fa917367 145 int drive(uint8_t ch, uint8_t on);
jason701802 9:00e8b7afb2c7 146
jason701802 9:00e8b7afb2c7 147 /** Control the speed of a motor
jason701802 9:00e8b7afb2c7 148 * @param motor Motor (channel pair) to control.
jason701802 9:00e8b7afb2c7 149 * @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 150 * @return
jason701802 9:00e8b7afb2c7 151 * -1 on failure
jason701802 9:00e8b7afb2c7 152 * speed of set output on success
jason701802 9:00e8b7afb2c7 153 */
jason701802 9:00e8b7afb2c7 154 float drive(Motors motor, float speed);
jason701802 9:00e8b7afb2c7 155
jason701802 9:00e8b7afb2c7 156 /** Control the speed of a motor
jason701802 9:00e8b7afb2c7 157 * @param motor Motor (channel pair) to control.
jason701802 9:00e8b7afb2c7 158 * @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 159 * @param dir Set direction to drive motor.
jason701802 9:00e8b7afb2c7 160 * 1: forward
jason701802 9:00e8b7afb2c7 161 * -1: reverse
jason701802 9:00e8b7afb2c7 162 * 0: brake, irregardless of setting of enBrake or speed
jason701802 9:00e8b7afb2c7 163 * @return
jason701802 9:00e8b7afb2c7 164 * -1 on failure
jason701802 9:00e8b7afb2c7 165 * speed of set output on success
jason701802 9:00e8b7afb2c7 166 */
jason701802 9:00e8b7afb2c7 167 float drive(Motors motor, int8_t dir, float speed);
jason701802 9:00e8b7afb2c7 168
jason701802 9:00e8b7afb2c7 169
jason701802 9:00e8b7afb2c7 170 /** Display diagnostic information on the LCD
jason701802 9:00e8b7afb2c7 171 * @param *lcd Pointer to TextLCD object to display info on
jason701802 9:00e8b7afb2c7 172 */
jason701802 1:c8e328389a98 173 void diagnostic(TextLCD_I2C *lcd);
jason701802 9:00e8b7afb2c7 174
jason701802 9:00e8b7afb2c7 175 /** Change the PWM period
jason701802 9:00e8b7afb2c7 176 * @param newPWMperiod The new PWM period. In seconds. System default is 0.0002 (5kHz)
jason701802 9:00e8b7afb2c7 177 */
jason701802 8:36e2cd31ccf3 178 void setPWMperiod(float newPWMperiod);
jason701802 0:b1e3fa917367 179
jason701802 0:b1e3fa917367 180
jason701802 0:b1e3fa917367 181
jason701802 0:b1e3fa917367 182 private:
jason701802 0:b1e3fa917367 183 DigitalOut *_d[8];
jason701802 0:b1e3fa917367 184 PwmOut *_p[8];
jason701802 0:b1e3fa917367 185 MCP23017 *_EnCtl;
jason701802 0:b1e3fa917367 186 MCP23017 *_IO;
jason701802 0:b1e3fa917367 187 I2C *_i2c;
jason701802 0:b1e3fa917367 188 uint8_t _enAddr;
jason701802 0:b1e3fa917367 189 uint8_t _ledAddr;
jason701802 1:c8e328389a98 190 uint8_t _pwm[4]; //enabled full bridges
jason701802 1:c8e328389a98 191 uint8_t _pwmCh; //currently actie PWM pins
jason701802 1:c8e328389a98 192 int8_t _dir[4]; //dir of 1 means lower # ch is driving, dir of -1 means higher number ch is driving
jason701802 0:b1e3fa917367 193 uint8_t _braking[4];
jason701802 1:c8e328389a98 194 uint8_t _oldLedState; //for setLed functions. Kept external to overload function
jason701802 8:36e2cd31ccf3 195 float _PWMperiod;
jason701802 0:b1e3fa917367 196
jason701802 0:b1e3fa917367 197 void enableCh(uint8_t ch, uint8_t en);
jason701802 0:b1e3fa917367 198 void setLed(uint8_t ch, uint8_t en);
jason701802 0:b1e3fa917367 199 void setLed(uint8_t state);
jason701802 0:b1e3fa917367 200 void setPwm(uint8_t ch, uint8_t en);
jason701802 0:b1e3fa917367 201
jason701802 0:b1e3fa917367 202
jason701802 0:b1e3fa917367 203
jason701802 0:b1e3fa917367 204 };
jason701802 0:b1e3fa917367 205
jason701802 0:b1e3fa917367 206
jason701802 0:b1e3fa917367 207
jason701802 0:b1e3fa917367 208 #endif