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 13:08:15 2015 +0000
Revision:
2:1675a4c00925
Parent:
1:fb5553d9ff4c
Child:
3:a99a538c067d
Independent and complementary driving mode option, docs updated.

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 0:d3f1d0d52615 8 */
tbjazic 0:d3f1d0d52615 9 class HBridgeDCMotor {
tbjazic 0:d3f1d0d52615 10 public:
tbjazic 2:1675a4c00925 11 /** Constructor for independently driven transistors.
tbjazic 2:1675a4c00925 12 * H stands for high side and L for low side transistor.
tbjazic 0:d3f1d0d52615 13 * A and B designate the individual half-bridges.
tbjazic 2:1675a4c00925 14 * @param GH_A PWM signal for driving the high side transistor of the half-bridge A
tbjazic 2:1675a4c00925 15 * @param GL_A PWM signal for driving the low side transistor of the half-bridge A
tbjazic 2:1675a4c00925 16 * @param GH_B PWM signal for driving the high side transistor of the half-bridge B
tbjazic 2:1675a4c00925 17 * @param GL_B PWM signal for driving the low side transistor of the half-bridge B
tbjazic 0:d3f1d0d52615 18 */
tbjazic 0:d3f1d0d52615 19 HBridgeDCMotor(PinName GH_A, PinName GL_A, PinName GH_B, PinName GL_B);
tbjazic 2:1675a4c00925 20
tbjazic 2:1675a4c00925 21 /** Constructor for complementary driven transistors.
tbjazic 2:1675a4c00925 22 * The high side transistors are driven directly, while the low side transistors are
tbjazic 2:1675a4c00925 23 * complemented by the driver itself using logic circuits.
tbjazic 2:1675a4c00925 24 * A and B designate the individual half-bridges.
tbjazic 2:1675a4c00925 25 * @param GH_A PWM signal for driving the high side transistor of the half-bridge A
tbjazic 2:1675a4c00925 26 * @param GH_B PWM signal for driving the high side transistor of the half-bridge B
tbjazic 2:1675a4c00925 27 */
tbjazic 2:1675a4c00925 28 HBridgeDCMotor(PinName GH_A, PinName GH_B);
tbjazic 2:1675a4c00925 29
tbjazic 2:1675a4c00925 30 /** Configure the parameters.
tbjazic 2:1675a4c00925 31 * @param sampleTime sample time in seconds
tbjazic 2:1675a4c00925 32 * @param switchingFrequency switching frequency in Hz
tbjazic 2:1675a4c00925 33 * @param rampUpTime set the ramp up time (in seconds) from 0 to maximum speed
tbjazic 2:1675a4c00925 34 * @param rampDownTime set the ramp down time (in seconds) from maximum speed to 0
tbjazic 2:1675a4c00925 35 */
tbjazic 2:1675a4c00925 36 void configure(float sampleTime, float switchingFrequency, float rampUpTime, float rampDownTime);
tbjazic 2:1675a4c00925 37
tbjazic 2:1675a4c00925 38 /** Set the motor speed by changing a duty cycle value.
tbjazic 2:1675a4c00925 39 * @param dutyCycle Duty cycle value in a range of (-1, 1). Negative value means opposite direction. */
tbjazic 0:d3f1d0d52615 40 void setDutyCycle(float dutyCycle);
tbjazic 2:1675a4c00925 41
tbjazic 2:1675a4c00925 42 /** Set the drive in a coast mode. */
tbjazic 0:d3f1d0d52615 43 void coast();
tbjazic 2:1675a4c00925 44
tbjazic 2:1675a4c00925 45 /** Get the current duty cycle.
tbjazic 2:1675a4c00925 46 * @returns the current value of the duty cycle.
tbjazic 2:1675a4c00925 47 */
tbjazic 0:d3f1d0d52615 48 float getDutyCycle();
tbjazic 2:1675a4c00925 49
tbjazic 0:d3f1d0d52615 50 private:
tbjazic 2:1675a4c00925 51 PwmOut *GH_A, *GL_A, *GH_B, *GL_B; // pointers to PwmOut objects
tbjazic 0:d3f1d0d52615 52 RateLimiter rl;
tbjazic 0:d3f1d0d52615 53 float switchingPeriod, dutyCycle, tempDutyCycle, sampleTime;
tbjazic 2:1675a4c00925 54 bool independentGates;
tbjazic 0:d3f1d0d52615 55 Ticker ticker;
tbjazic 0:d3f1d0d52615 56 void adjustDutyCycle();
tbjazic 2:1675a4c00925 57 void init();
tbjazic 0:d3f1d0d52615 58 };