A class for driving a DC motor using a full-bridge (H-bridge) driver.
Dependencies: RateLimiter
Dependents: L298N-Breakout-Test Zavrsni_rad_NXP_cup
HBridgeDCMotor.h
00001 #include "mbed.h" 00002 #include "RateLimiter.h" 00003 00004 /** A class for driving a DC motor using a full-bridge driver (H-bridge). 00005 * 00006 * The class has an option to drive all 4 transistors gates independently by using 4 PwmOut objects, 00007 * or to drive the H-bridge with complementary driven transistors using only 2 PwmOut objects. 00008 * 00009 * Example of use: 00010 * 00011 * @code 00012 * #include "mbed.h" 00013 * #include "HBridgeDCMotor.h" 00014 * 00015 * HBridgeDCMotor motor(p21, p22); 00016 * 00017 * int main() { 00018 * float sampleTime = 50e-3, switchingFrequency = 25e3, rampTime = 3; 00019 * motor.configure(sampleTime, switchingFrequency, rampTime, rampTime); 00020 * while(true) { 00021 * motor.setDutyCycle(1); 00022 * wait(10); 00023 * motor.setDutyCycle(-1); 00024 * wait(10); 00025 * } 00026 * } 00027 * @endcode 00028 */ 00029 class HBridgeDCMotor { 00030 public: 00031 /** Constructor for independently driven transistors. 00032 * H stands for high side and L for low side transistor. 00033 * A and B designate the individual half-bridges. 00034 * @param GH_A PWM signal for driving the high side transistor of the half-bridge A 00035 * @param GL_A PWM signal for driving the low side transistor of the half-bridge A 00036 * @param GH_B PWM signal for driving the high side transistor of the half-bridge B 00037 * @param GL_B PWM signal for driving the low side transistor of the half-bridge B 00038 */ 00039 HBridgeDCMotor(PinName GH_A, PinName GL_A, PinName GH_B, PinName GL_B); 00040 00041 /** Constructor for complementary driven transistors. 00042 * The high side transistors are driven directly, while the low side transistors are 00043 * complemented by the driver itself using logic circuits. 00044 * A and B designate the individual half-bridges. 00045 * @param GH_A PWM signal for driving the high side transistor of the half-bridge A 00046 * @param GH_B PWM signal for driving the high side transistor of the half-bridge B 00047 */ 00048 HBridgeDCMotor(PinName GH_A, PinName GH_B); 00049 00050 /** Configure the parameters. 00051 * @param sampleTime sample time in seconds 00052 * @param switchingFrequency switching frequency in Hz 00053 * @param rampUpTime set the ramp up time (in seconds) from 0 to maximum speed 00054 * @param rampDownTime set the ramp down time (in seconds) from maximum speed to 0 00055 */ 00056 void configure(float sampleTime, float switchingFrequency, float rampUpTime, float rampDownTime); 00057 00058 /** Set the motor speed by changing a duty cycle value. 00059 * @param dutyCycle Duty cycle value in a range of (-1, 1). Negative value means opposite direction. */ 00060 void setDutyCycle(float dutyCycle); 00061 00062 /** Set the drive in a coast mode. */ 00063 void coast(); 00064 00065 /** Get the current duty cycle. 00066 * @returns the current value of the duty cycle. 00067 */ 00068 float getDutyCycle(); 00069 00070 private: 00071 PwmOut *GH_A, *GL_A, *GH_B, *GL_B; // pointers to PwmOut objects 00072 RateLimiter rl; 00073 float switchingPeriod, dutyCycle, tempDutyCycle, sampleTime; 00074 bool independentGates; 00075 Ticker ticker; 00076 void adjustDutyCycle(); 00077 void init(); 00078 };
Generated on Fri Jul 15 2022 14:36:58 by 1.7.2