A class for driving a DC motor using a full-bridge (H-bridge) driver.

Dependencies:   RateLimiter

Dependents:   L298N-Breakout-Test Zavrsni_rad_NXP_cup

Committer:
tbjazic
Date:
Fri Dec 11 14:14:56 2015 +0000
Revision:
3:a99a538c067d
Parent:
2:1675a4c00925
Example of use added in docs.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tbjazic 0:d3f1d0d52615 1 #include "mbed.h"
tbjazic 0:d3f1d0d52615 2 #include "RateLimiter.h"
tbjazic 0:d3f1d0d52615 3
tbjazic 2:1675a4c00925 4 /** A class for driving a DC motor using a full-bridge driver (H-bridge).
tbjazic 0:d3f1d0d52615 5 *
tbjazic 2:1675a4c00925 6 * The class has an option to drive all 4 transistors gates independently by using 4 PwmOut objects,
tbjazic 2:1675a4c00925 7 * or to drive the H-bridge with complementary driven transistors using only 2 PwmOut objects.
tbjazic 3:a99a538c067d 8 *
tbjazic 3:a99a538c067d 9 * Example of use:
tbjazic 3:a99a538c067d 10 *
tbjazic 3:a99a538c067d 11 * @code
tbjazic 3:a99a538c067d 12 * #include "mbed.h"
tbjazic 3:a99a538c067d 13 * #include "HBridgeDCMotor.h"
tbjazic 3:a99a538c067d 14 *
tbjazic 3:a99a538c067d 15 * HBridgeDCMotor motor(p21, p22);
tbjazic 3:a99a538c067d 16 *
tbjazic 3:a99a538c067d 17 * int main() {
tbjazic 3:a99a538c067d 18 * float sampleTime = 50e-3, switchingFrequency = 25e3, rampTime = 3;
tbjazic 3:a99a538c067d 19 * motor.configure(sampleTime, switchingFrequency, rampTime, rampTime);
tbjazic 3:a99a538c067d 20 * while(true) {
tbjazic 3:a99a538c067d 21 * motor.setDutyCycle(1);
tbjazic 3:a99a538c067d 22 * wait(10);
tbjazic 3:a99a538c067d 23 * motor.setDutyCycle(-1);
tbjazic 3:a99a538c067d 24 * wait(10);
tbjazic 3:a99a538c067d 25 * }
tbjazic 3:a99a538c067d 26 * }
tbjazic 3:a99a538c067d 27 * @endcode
tbjazic 0:d3f1d0d52615 28 */
tbjazic 0:d3f1d0d52615 29 class HBridgeDCMotor {
tbjazic 0:d3f1d0d52615 30 public:
tbjazic 2:1675a4c00925 31 /** Constructor for independently driven transistors.
tbjazic 2:1675a4c00925 32 * H stands for high side and L for low side transistor.
tbjazic 0:d3f1d0d52615 33 * A and B designate the individual half-bridges.
tbjazic 2:1675a4c00925 34 * @param GH_A PWM signal for driving the high side transistor of the half-bridge A
tbjazic 2:1675a4c00925 35 * @param GL_A PWM signal for driving the low side transistor of the half-bridge A
tbjazic 2:1675a4c00925 36 * @param GH_B PWM signal for driving the high side transistor of the half-bridge B
tbjazic 2:1675a4c00925 37 * @param GL_B PWM signal for driving the low side transistor of the half-bridge B
tbjazic 0:d3f1d0d52615 38 */
tbjazic 0:d3f1d0d52615 39 HBridgeDCMotor(PinName GH_A, PinName GL_A, PinName GH_B, PinName GL_B);
tbjazic 2:1675a4c00925 40
tbjazic 2:1675a4c00925 41 /** Constructor for complementary driven transistors.
tbjazic 2:1675a4c00925 42 * The high side transistors are driven directly, while the low side transistors are
tbjazic 2:1675a4c00925 43 * complemented by the driver itself using logic circuits.
tbjazic 2:1675a4c00925 44 * A and B designate the individual half-bridges.
tbjazic 2:1675a4c00925 45 * @param GH_A PWM signal for driving the high side transistor of the half-bridge A
tbjazic 2:1675a4c00925 46 * @param GH_B PWM signal for driving the high side transistor of the half-bridge B
tbjazic 2:1675a4c00925 47 */
tbjazic 2:1675a4c00925 48 HBridgeDCMotor(PinName GH_A, PinName GH_B);
tbjazic 2:1675a4c00925 49
tbjazic 2:1675a4c00925 50 /** Configure the parameters.
tbjazic 2:1675a4c00925 51 * @param sampleTime sample time in seconds
tbjazic 2:1675a4c00925 52 * @param switchingFrequency switching frequency in Hz
tbjazic 2:1675a4c00925 53 * @param rampUpTime set the ramp up time (in seconds) from 0 to maximum speed
tbjazic 2:1675a4c00925 54 * @param rampDownTime set the ramp down time (in seconds) from maximum speed to 0
tbjazic 2:1675a4c00925 55 */
tbjazic 2:1675a4c00925 56 void configure(float sampleTime, float switchingFrequency, float rampUpTime, float rampDownTime);
tbjazic 2:1675a4c00925 57
tbjazic 2:1675a4c00925 58 /** Set the motor speed by changing a duty cycle value.
tbjazic 2:1675a4c00925 59 * @param dutyCycle Duty cycle value in a range of (-1, 1). Negative value means opposite direction. */
tbjazic 0:d3f1d0d52615 60 void setDutyCycle(float dutyCycle);
tbjazic 2:1675a4c00925 61
tbjazic 2:1675a4c00925 62 /** Set the drive in a coast mode. */
tbjazic 0:d3f1d0d52615 63 void coast();
tbjazic 2:1675a4c00925 64
tbjazic 2:1675a4c00925 65 /** Get the current duty cycle.
tbjazic 2:1675a4c00925 66 * @returns the current value of the duty cycle.
tbjazic 2:1675a4c00925 67 */
tbjazic 0:d3f1d0d52615 68 float getDutyCycle();
tbjazic 2:1675a4c00925 69
tbjazic 0:d3f1d0d52615 70 private:
tbjazic 2:1675a4c00925 71 PwmOut *GH_A, *GL_A, *GH_B, *GL_B; // pointers to PwmOut objects
tbjazic 0:d3f1d0d52615 72 RateLimiter rl;
tbjazic 0:d3f1d0d52615 73 float switchingPeriod, dutyCycle, tempDutyCycle, sampleTime;
tbjazic 2:1675a4c00925 74 bool independentGates;
tbjazic 0:d3f1d0d52615 75 Ticker ticker;
tbjazic 0:d3f1d0d52615 76 void adjustDutyCycle();
tbjazic 2:1675a4c00925 77 void init();
tbjazic 0:d3f1d0d52615 78 };