Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: RateLimiter
Dependents: L298N-Breakout-Test Zavrsni_rad_NXP_cup
HBridgeDCMotor.cpp
00001 #include "HBridgeDCMotor.h" 00002 00003 HBridgeDCMotor::HBridgeDCMotor(PinName gh_a, PinName gl_a, PinName gh_b, PinName gl_b) { 00004 GH_A = new PwmOut(gh_a); 00005 GL_A = new PwmOut(gl_a); 00006 GH_B = new PwmOut(gh_b); 00007 GL_B = new PwmOut(gl_b); 00008 independentGates = true; 00009 configure(1e-3, 20e3, 0.1, -0.1); 00010 init(); 00011 } 00012 00013 HBridgeDCMotor::HBridgeDCMotor(PinName gh_a, PinName gh_b) { 00014 GH_A = new PwmOut(gh_a); 00015 GL_A = NULL; 00016 GH_B = new PwmOut(gh_b); 00017 GL_B = NULL; 00018 independentGates = false; 00019 configure(1e-3, 20e3, 0.1, -0.1); 00020 init(); 00021 } 00022 00023 void HBridgeDCMotor::init() { 00024 dutyCycle = tempDutyCycle = 0; 00025 } 00026 00027 void HBridgeDCMotor::configure(float sampleTime, float switchingFrequency, float rampUpTime, float rampDownTime) { 00028 if (sampleTime < 1e-6) 00029 sampleTime = 1e-3; 00030 if (switchingFrequency < 100) 00031 switchingFrequency = 20e3; 00032 if (rampUpTime < 1) 00033 rampUpTime = 5; 00034 if (rampDownTime < 1) 00035 rampDownTime = 5; 00036 this->sampleTime = sampleTime; 00037 switchingPeriod = 1.0 / switchingFrequency; 00038 GH_A->period(switchingPeriod); 00039 rl.setLimits(1.0/rampUpTime, -1.0/rampDownTime, 0, sampleTime); 00040 } 00041 00042 void HBridgeDCMotor::adjustDutyCycle() { 00043 dutyCycle = rl.out(tempDutyCycle); 00044 if (dutyCycle >= 0 && dutyCycle <= 1) { 00045 GH_B->write(0); 00046 if(independentGates) { 00047 GL_B->write(1); 00048 GL_A->write(0); 00049 } 00050 GH_A->write(dutyCycle); 00051 } else if (dutyCycle >= -1 && dutyCycle < 0) { // opposite direction 00052 GH_A->write(0); 00053 if(independentGates) { 00054 GL_A->write(1); 00055 GL_B->write(0); 00056 } 00057 GH_B->write(-dutyCycle); 00058 } else { 00059 coast(); 00060 } 00061 } 00062 00063 void HBridgeDCMotor::setDutyCycle(float dc) { 00064 if (dc >= -1 && dc <= 1) { 00065 ticker.attach(this, &HBridgeDCMotor::adjustDutyCycle, sampleTime); 00066 tempDutyCycle = dc; 00067 } else { 00068 coast(); 00069 } 00070 } 00071 00072 void HBridgeDCMotor::coast() { 00073 GH_A->write(0); 00074 GH_B->write(0); 00075 if(independentGates) { 00076 GL_A->write(0); 00077 GL_B->write(0); 00078 } 00079 dutyCycle = tempDutyCycle = 0; 00080 rl.reset(); 00081 ticker.detach(); 00082 } 00083 00084 float HBridgeDCMotor::getDutyCycle() { 00085 return dutyCycle; 00086 }
Generated on Fri Jul 15 2022 14:36:58 by
1.7.2