Motacon_20200317
Dependents: HelloWorld_MotorKaisei
Diff: BLDCmotorDriver.cpp
- Revision:
- 2:7aae78b85e1d
- Parent:
- 0:5602fba2a7f7
--- a/BLDCmotorDriver.cpp Fri May 22 22:43:39 2015 +0000 +++ b/BLDCmotorDriver.cpp Mon Jun 01 13:40:39 2015 +0000 @@ -1,11 +1,24 @@ #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) { +BLDCmotorDriver::BLDCmotorDriver(PinName pGH_A, PinName pGH_B, PinName pGH_C, PinName pGL_A, PinName pGL_B, PinName pGL_C, + PinName pH1, PinName pH2, PinName pH3, PinName pFault) : + GH_A(pGH_A), GH_B(pGH_B), GH_C(pGH_C), GL_A(pGL_A), GL_B(pGL_B), GL_C(pGL_C), + H1(pH1), H2(pH2), H3(pH3), Fault(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 + //H1.mode(PullNone); + //H2.mode(PullNone); + //H3.mode(PullNone); + H1.rise(this, &BLDCmotorDriver::commutation); + H2.rise(this, &BLDCmotorDriver::commutation); + H3.rise(this, &BLDCmotorDriver::commutation); + H1.fall(this, &BLDCmotorDriver::commutation); + H2.fall(this, &BLDCmotorDriver::commutation); + H3.fall(this, &BLDCmotorDriver::commutation); } void BLDCmotorDriver::configure(float sampleTime, float switchingFrequency, float rampUpSlope, float rampDownSlope) { if (sampleTime < 1e-6) @@ -20,57 +33,74 @@ 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(); +int BLDCmotorDriver::getSector(){ // hall 120° + + h1 = H1.read(); + h2 = H2.read(); + h3 = H3.read(); + + + if(h1 == 0 && h2 == 0 && h3 == 1){ + _currentSector = 0; + } + else if(h1 == 0 && h2 == 1 && h3 == 1){ + _currentSector = 1; + } + else if(h1 == 0 && h2 == 1 && h3 == 0){ + _currentSector = 2; + } + else if(h1 == 1 && h2 == 1 && h3 == 0){ + _currentSector = 3; + } + else if(h1 == 1 && h2 == 0 && h3 == 0){ + _currentSector = 4; + } + else if(h1 == 1 && h2 == 0 && h3 == 1){ + _currentSector = 5; + } + previousSector = _currentSector - 1; + difference = _currentSector - previousSector; + if (difference == 1){ + currentSector = _currentSector; + Fault = 0; + } + else Fault = 1; +return currentSector; +} + +void BLDCmotorDriver::commutation() { + dutyCycle = rl.out(tempDutyCycle); + currentSector = getSector(); if (dutyCycle > 0) { - if (sektor > 6) sektor = 1; - sektor++; - switch(sektor) { - case 1: //100 + currentSector++; + if(currentSector > 5)currentSector = 0; + switch(currentSector) { + + case 0: //001 GH_A = dutyCycle; GL_A = 0; GH_B = 0; GL_B = 0; GH_C = 0; + GL_C = 1; + break; + case 1: + GH_A = 0; + GL_A = 0; + GH_B = dutyCycle; + 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; + case 2: + GH_A = 0; + GL_A = 1; + GH_B = dutyCycle; + GL_B = 0; 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 + case 3: GH_A = 0; GL_A = 1; GH_B = 0; @@ -78,75 +108,75 @@ 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 + case 4: GH_A = 0; GL_A = 0; GH_B = 0; GL_B = 1; GH_C = dutyCycle; GL_C = 0; + break; + case 5: + GH_A = dutyCycle; + GL_A = 0; + GH_B = 0; + GL_B = 1; + GH_C = 0; + GL_C = 0; break; - case 4: //011 + } + } else if (dutyCycle < 0) { + currentSector--; + if(currentSector < 0)currentSector = 5; + switch(currentSector) { + case 0: + GH_A = -dutyCycle; + GL_A = 0; + GH_B = 0; + GL_B = 1; + GH_C = 0; + GL_C = 0; + break; + case 1: + GH_A = -dutyCycle; + GL_A = 0; + GH_B = 0; + GL_B = 0; + GH_C = 0; + GL_C = 1; + break; + case 2: + GH_A = 0; + GL_A = 0; + GH_B = -dutyCycle; + GL_B = 0; + GH_C = 0; + GL_C = 1; + break; + case 3: + GH_A = 0; + GL_A = 1; + GH_B = -dutyCycle; + GL_B = 0; + GH_C = 0; + GL_C = 0; + break; + case 4: GH_A = 0; GL_A = 1; GH_B = 0; GL_B = 0; - GH_C = dutyCycle; + 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 + case 5: GH_A = 0; GL_A = 0; - GH_B = dutyCycle; - GL_B = 0; - GH_C = 0; - GL_C = 1; - break; + GH_B = 0; + GL_B = 1; + GH_C = -dutyCycle; + GL_C = 0; + break; } }else { coast(); @@ -154,12 +184,18 @@ } void BLDCmotorDriver::setDutyCycle(float dc) { if (dc >= -1 && dc <= 1) { - ticker.attach(this, &BLDCmotorDriver::komutacijaBLDC, sampleTime); + ticker.attach(this, &BLDCmotorDriver::commutation, sampleTime); tempDutyCycle = dc; } else { coast(); } } +/*void BLDCmotorDriver::adjustDutyCycle() { + dutyCycle = rl.out(tempDutyCycle); + float diff = tempDutyCycle - dutyCycle; + if (diff < 0.01 || diff > -0.01) + ticker.detach(); +}*/ void BLDCmotorDriver::coast() { GH_A = 0; GL_A = 0;