Motacon_20200317
Dependents: HelloWorld_MotorKaisei
Diff: BLDCmotorDriver.cpp
- Revision:
- 0:5602fba2a7f7
- Child:
- 1:786897114846
- Child:
- 2:7aae78b85e1d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BLDCmotorDriver.cpp Fri May 22 22:43:39 2015 +0000 @@ -0,0 +1,176 @@ +#include "BLDCmotorDriver.h" + +BLDCmotorDriver::BLDCmotorDriver(PinName gh_a, PinName gl_a, PinName gh_b, PinName gl_b, PinName gh_c, PinName gl_c, PinName h1, PinName h2, PinName h3, PinName led1) : GH_A(gh_a), GL_A(gl_a), GH_B(gh_b), GL_B(gl_b), GH_C(gh_c), GL_C(gl_c), H1(h1), H2(h2), H3(h3), Led1(led1) { + 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 +} +void BLDCmotorDriver::configure(float sampleTime, float switchingFrequency, float rampUpSlope, float rampDownSlope) { + 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; + this->sampleTime = sampleTime; + switchingPeriod = 1.0 / switchingFrequency; + rl.setLimits(rampUpSlope, rampDownSlope, 0, sampleTime); +} +int BLDCmotorDriver::HallRead(){ // hall 120° + + if(H1.read()== 1 && H2.read()== 0 && H3.read()== 0) + sektor = 1; + else if(H1.read()== 1 && H2.read()== 1 && H3.read()== 0) + sektor = 2; + else if(H1.read()== 0 && H2.read()== 1 && H3.read()== 0) + sektor = 3; + else if(H1.read()== 0 && H2.read()== 1 && H3.read()== 1) + sektor = 4; + else if(H1.read()== 0 && H2.read()== 0 && H3.read()== 1) + sektor = 5; + else if(H1.read()== 1 && H2.read()== 0 && H3.read()== 1) + sektor = 6; + else + Led1 = 1; + + return sektor; +} +void BLDCmotorDriver::komutacijaBLDC() { + dutyCycle = rl.out(tempDutyCycle); + sektor = HallRead(); + if (dutyCycle > 0) { + if (sektor > 6) sektor = 1; + sektor++; + switch(sektor) { + case 1: //100 + GH_A = dutyCycle; + GL_A = 0; + GH_B = 0; + GL_B = 0; + GH_C = 0; + GL_C = 1; + break; + case 2: //110 + GH_A = dutyCycle; + GL_A = 0; + GH_B = 0; + GL_B = 1; + GH_C = 0; + GL_C = 0; + break; + case 3: //010 + GH_A = 0; + GL_A = 0; + GH_B = 0; + GL_B = 1; + GH_C = dutyCycle; + GL_C = 0; + break; + case 4: //011 + GH_A = 0; + GL_A = 1; + GH_B = 0; + GL_B = 0; + GH_C = dutyCycle; + GL_C = 0; + break; + case 5: //001 + GH_A = 0; + GL_A = 1; + GH_B = dutyCycle; + GL_B = 0; + GH_C = 0; + GL_C = 0; + break; + case 6: //101 + GH_A = 0; + GL_A = 0; + GH_B = dutyCycle; + GL_B = 0; + GH_C = 0; + GL_C = 1; + break; + } + } else if (dutyCycle < 0) { // + if (sektor < 1) sektor = 6; + sektor--; + switch(sektor) { + case 1: //100 + GH_A = dutyCycle; + GL_A = 0; + GH_B = 0; + GL_B = 0; + GH_C = 0; + GL_C = 1; + break; + case 2: //110 + GH_A = dutyCycle; + GL_A = 0; + GH_B = 0; + GL_B = 1; + GH_C = 0; + GL_C = 0; + break; + case 3: //010 + GH_A = 0; + GL_A = 0; + GH_B = 0; + GL_B = 1; + GH_C = dutyCycle; + GL_C = 0; + break; + case 4: //011 + GH_A = 0; + GL_A = 1; + GH_B = 0; + GL_B = 0; + GH_C = dutyCycle; + GL_C = 0; + break; + case 5: //001 + GH_A = 0; + GL_A = 1; + GH_B = dutyCycle; + GL_B = 0; + GH_C = 0; + GL_C = 0; + break; + case 6: //101 + GH_A = 0; + GL_A = 0; + GH_B = dutyCycle; + GL_B = 0; + GH_C = 0; + GL_C = 1; + break; + } + }else { + coast(); + } +} +void BLDCmotorDriver::setDutyCycle(float dc) { + if (dc >= -1 && dc <= 1) { + ticker.attach(this, &BLDCmotorDriver::komutacijaBLDC, sampleTime); + tempDutyCycle = dc; + } else { + coast(); + } +} +void BLDCmotorDriver::coast() { + GH_A = 0; + GL_A = 0; + GH_B = 0; + GL_B = 0; + GH_C = 0; + GL_C = 0; + dutyCycle = tempDutyCycle = 0; + rl.reset(); + ticker.detach(); +} +float BLDCmotorDriver::getDutyCycle() { + return dutyCycle; +} \ No newline at end of file