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

Dependencies:   RateLimiter

Dependents:   L298N-Breakout-Test Zavrsni_rad_NXP_cup

Revision:
2:1675a4c00925
Parent:
1:fb5553d9ff4c
Child:
5:7016f3b1ec44
--- a/HBridgeDCMotor.cpp	Wed Jan 14 10:37:57 2015 +0000
+++ b/HBridgeDCMotor.cpp	Fri Dec 11 13:08:15 2015 +0000
@@ -1,39 +1,60 @@
 #include "HBridgeDCMotor.h"
 
-HBridgeDCMotor::HBridgeDCMotor(PinName gh_a, PinName gl_a, PinName gh_b, PinName gl_b) : GH_A(gh_a), GL_A(gl_a), GH_B(gh_b), GL_B(gl_b) {
-    sampleTime = 1e-3;
-    switchingPeriod = 1.0 / 20e3;
-    dutyCycle = tempDutyCycle = 0;
-    GH_A.period(switchingPeriod); // applies to all PwmOut instances
-    rl.setLimits(0.1, -0.1, 0, sampleTime); // initial 10 second ramp
+HBridgeDCMotor::HBridgeDCMotor(PinName gh_a, PinName gl_a, PinName gh_b, PinName gl_b) {
+    GH_A = new PwmOut(gh_a);
+    GL_A = new PwmOut(gl_a);
+    GH_B = new PwmOut(gh_b);
+    GL_B = new PwmOut(gh_b);
+    independentGates = true;
+    configure(1e-3, 20e3, 0.1, -0.1);
+    init();
 }
 
-void HBridgeDCMotor::configure(float sampleTime, float switchingFrequency, float rampUpSlope, float rampDownSlope) {
+HBridgeDCMotor::HBridgeDCMotor(PinName gh_a, PinName gh_b) {
+    GH_A = new PwmOut(gh_a);
+    GL_A = NULL;
+    GH_B = new PwmOut(gh_b);
+    GL_B = NULL;
+    independentGates = false;
+    configure(1e-3, 20e3, 0.1, -0.1);
+    init();
+}
+
+void HBridgeDCMotor::init() {
+    dutyCycle = tempDutyCycle = 0;
+}
+
+void HBridgeDCMotor::configure(float sampleTime, float switchingFrequency, float rampUpTime, float rampDownTime) {
     if (sampleTime < 1e-6)
         sampleTime = 1e-3;
     if (switchingFrequency < 100)
         switchingFrequency = 20e3;
-    if (rampUpSlope < 0 || rampUpSlope > 1)
-        rampUpSlope = 0.1;
-    if (rampDownSlope > 0 || rampDownSlope < -1)
-        rampDownSlope = -0.1;
+    if (rampUpTime < 1)
+        rampUpTime = 5;
+    if (rampDownTime < 1)
+        rampDownTime = 5;
     this->sampleTime = sampleTime;
     switchingPeriod = 1.0 / switchingFrequency;
-    rl.setLimits(rampUpSlope, rampDownSlope, 0, sampleTime);
+    GH_A->period(switchingPeriod);
+    rl.setLimits(1.0/rampUpTime, -1.0/rampDownTime, 0, sampleTime);
 }
 
 void HBridgeDCMotor::adjustDutyCycle() {
     dutyCycle = rl.out(tempDutyCycle);
     if (dutyCycle >= 0 && dutyCycle <= 1) {
-        GH_B = 0;
-        GL_B = 1;
-        GL_A = 0;
-        GH_A = dutyCycle;
+        GH_B->write(0);
+        if(independentGates) {
+            GL_B->write(1);
+            GL_A->write(0);
+        }
+        GH_A->write(dutyCycle);
     } else if (dutyCycle >= -1 && dutyCycle < 0) { // opposite direction
-        GH_A = 0;
-        GL_A = 1;
-        GL_B = 0;
-        GH_B = -dutyCycle;
+        GH_A->write(0);
+        if(independentGates) {
+            GL_A->write(1);
+            GL_B->write(0);
+        }
+        GH_B->write(-dutyCycle);
     } else {
         coast();
     }
@@ -49,10 +70,12 @@
 }
 
 void HBridgeDCMotor::coast() {
-    GH_A = 0;
-    GL_A = 0;
-    GH_B = 0;
-    GL_B = 0;
+    GH_A->write(0);
+    GH_B->write(0);
+    if(independentGates) {
+        GL_A->write(0);
+        GL_B->write(0);
+    }
     dutyCycle = tempDutyCycle = 0;
     rl.reset();
     ticker.detach();