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:
5:7016f3b1ec44
Independent and complementary driving mode option, docs updated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tbjazic 0:d3f1d0d52615 1 #include "HBridgeDCMotor.h"
tbjazic 0:d3f1d0d52615 2
tbjazic 2:1675a4c00925 3 HBridgeDCMotor::HBridgeDCMotor(PinName gh_a, PinName gl_a, PinName gh_b, PinName gl_b) {
tbjazic 2:1675a4c00925 4 GH_A = new PwmOut(gh_a);
tbjazic 2:1675a4c00925 5 GL_A = new PwmOut(gl_a);
tbjazic 2:1675a4c00925 6 GH_B = new PwmOut(gh_b);
tbjazic 2:1675a4c00925 7 GL_B = new PwmOut(gh_b);
tbjazic 2:1675a4c00925 8 independentGates = true;
tbjazic 2:1675a4c00925 9 configure(1e-3, 20e3, 0.1, -0.1);
tbjazic 2:1675a4c00925 10 init();
tbjazic 0:d3f1d0d52615 11 }
tbjazic 0:d3f1d0d52615 12
tbjazic 2:1675a4c00925 13 HBridgeDCMotor::HBridgeDCMotor(PinName gh_a, PinName gh_b) {
tbjazic 2:1675a4c00925 14 GH_A = new PwmOut(gh_a);
tbjazic 2:1675a4c00925 15 GL_A = NULL;
tbjazic 2:1675a4c00925 16 GH_B = new PwmOut(gh_b);
tbjazic 2:1675a4c00925 17 GL_B = NULL;
tbjazic 2:1675a4c00925 18 independentGates = false;
tbjazic 2:1675a4c00925 19 configure(1e-3, 20e3, 0.1, -0.1);
tbjazic 2:1675a4c00925 20 init();
tbjazic 2:1675a4c00925 21 }
tbjazic 2:1675a4c00925 22
tbjazic 2:1675a4c00925 23 void HBridgeDCMotor::init() {
tbjazic 2:1675a4c00925 24 dutyCycle = tempDutyCycle = 0;
tbjazic 2:1675a4c00925 25 }
tbjazic 2:1675a4c00925 26
tbjazic 2:1675a4c00925 27 void HBridgeDCMotor::configure(float sampleTime, float switchingFrequency, float rampUpTime, float rampDownTime) {
tbjazic 1:fb5553d9ff4c 28 if (sampleTime < 1e-6)
tbjazic 1:fb5553d9ff4c 29 sampleTime = 1e-3;
tbjazic 1:fb5553d9ff4c 30 if (switchingFrequency < 100)
tbjazic 1:fb5553d9ff4c 31 switchingFrequency = 20e3;
tbjazic 2:1675a4c00925 32 if (rampUpTime < 1)
tbjazic 2:1675a4c00925 33 rampUpTime = 5;
tbjazic 2:1675a4c00925 34 if (rampDownTime < 1)
tbjazic 2:1675a4c00925 35 rampDownTime = 5;
tbjazic 1:fb5553d9ff4c 36 this->sampleTime = sampleTime;
tbjazic 1:fb5553d9ff4c 37 switchingPeriod = 1.0 / switchingFrequency;
tbjazic 2:1675a4c00925 38 GH_A->period(switchingPeriod);
tbjazic 2:1675a4c00925 39 rl.setLimits(1.0/rampUpTime, -1.0/rampDownTime, 0, sampleTime);
tbjazic 1:fb5553d9ff4c 40 }
tbjazic 1:fb5553d9ff4c 41
tbjazic 0:d3f1d0d52615 42 void HBridgeDCMotor::adjustDutyCycle() {
tbjazic 0:d3f1d0d52615 43 dutyCycle = rl.out(tempDutyCycle);
tbjazic 0:d3f1d0d52615 44 if (dutyCycle >= 0 && dutyCycle <= 1) {
tbjazic 2:1675a4c00925 45 GH_B->write(0);
tbjazic 2:1675a4c00925 46 if(independentGates) {
tbjazic 2:1675a4c00925 47 GL_B->write(1);
tbjazic 2:1675a4c00925 48 GL_A->write(0);
tbjazic 2:1675a4c00925 49 }
tbjazic 2:1675a4c00925 50 GH_A->write(dutyCycle);
tbjazic 0:d3f1d0d52615 51 } else if (dutyCycle >= -1 && dutyCycle < 0) { // opposite direction
tbjazic 2:1675a4c00925 52 GH_A->write(0);
tbjazic 2:1675a4c00925 53 if(independentGates) {
tbjazic 2:1675a4c00925 54 GL_A->write(1);
tbjazic 2:1675a4c00925 55 GL_B->write(0);
tbjazic 2:1675a4c00925 56 }
tbjazic 2:1675a4c00925 57 GH_B->write(-dutyCycle);
tbjazic 0:d3f1d0d52615 58 } else {
tbjazic 0:d3f1d0d52615 59 coast();
tbjazic 0:d3f1d0d52615 60 }
tbjazic 0:d3f1d0d52615 61 }
tbjazic 0:d3f1d0d52615 62
tbjazic 0:d3f1d0d52615 63 void HBridgeDCMotor::setDutyCycle(float dc) {
tbjazic 0:d3f1d0d52615 64 if (dc >= -1 && dc <= 1) {
tbjazic 0:d3f1d0d52615 65 ticker.attach(this, &HBridgeDCMotor::adjustDutyCycle, sampleTime);
tbjazic 0:d3f1d0d52615 66 tempDutyCycle = dc;
tbjazic 0:d3f1d0d52615 67 } else {
tbjazic 0:d3f1d0d52615 68 coast();
tbjazic 0:d3f1d0d52615 69 }
tbjazic 0:d3f1d0d52615 70 }
tbjazic 0:d3f1d0d52615 71
tbjazic 0:d3f1d0d52615 72 void HBridgeDCMotor::coast() {
tbjazic 2:1675a4c00925 73 GH_A->write(0);
tbjazic 2:1675a4c00925 74 GH_B->write(0);
tbjazic 2:1675a4c00925 75 if(independentGates) {
tbjazic 2:1675a4c00925 76 GL_A->write(0);
tbjazic 2:1675a4c00925 77 GL_B->write(0);
tbjazic 2:1675a4c00925 78 }
tbjazic 0:d3f1d0d52615 79 dutyCycle = tempDutyCycle = 0;
tbjazic 0:d3f1d0d52615 80 rl.reset();
tbjazic 0:d3f1d0d52615 81 ticker.detach();
tbjazic 0:d3f1d0d52615 82 }
tbjazic 0:d3f1d0d52615 83
tbjazic 0:d3f1d0d52615 84 float HBridgeDCMotor::getDutyCycle() {
tbjazic 0:d3f1d0d52615 85 return dutyCycle;
tbjazic 0:d3f1d0d52615 86 }