Motacon_20200317
Dependents: HelloWorld_MotorKaisei
BLDCmotorDriver.cpp
- Committer:
- tbjazic
- Date:
- 2015-05-24
- Revision:
- 1:786897114846
- Parent:
- 0:5602fba2a7f7
- Child:
- 3:a4b4a8e3f2a0
File content as of revision 1:786897114846:
#include "BLDCmotorDriver.h" 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(pfault) { 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.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) 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::getSector() { // hall 120° if(H1.read()== 1 && H2.read()== 0 && H3.read()== 0) currentSector = 1; else if(H1.read()== 1 && H2.read()== 1 && H3.read()== 0) currentSector = 2; else if(H1.read()== 0 && H2.read()== 1 && H3.read()== 0) currentSector = 3; else if(H1.read()== 0 && H2.read()== 1 && H3.read()== 1) currentSector = 4; else if(H1.read()== 0 && H2.read()== 0 && H3.read()== 1) currentSector = 5; else if(H1.read()== 1 && H2.read()== 0 && H3.read()== 1) currentSector = 6; else { currentSector = 0; fault = 1; } return currentSector; } void BLDCmotorDriver::commutation() { getSector(); if (dutyCycle > 0) { if (currentSector > 6) currentSector = 1; // when will this condition happen? // currentSector++; switch(currentSector) { 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 (currentSector < 1) currentSector = 6; // not necessary // currentSector--; switch(currentSector) { 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::adjustDutyCycle, 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; GH_B = 0; GL_B = 0; GH_C = 0; GL_C = 0; dutyCycle = tempDutyCycle = 0; rl.reset(); ticker.detach(); } float BLDCmotorDriver::getDutyCycle() { return dutyCycle; }