Motacon_20200317

BLDCmotorDriver.cpp

Committer:
MPPT51
Date:
2020-03-17
Revision:
7:fb9e3b508237
Parent:
6:959e725b6be9

File content as of revision 7:fb9e3b508237:

#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(LED1){
                     
    sampleTime = 1e-3;
    switchingPeriod = 1.0 / 20e3;   //PWMの周波数を20kHzにしている
    dutyCycle = tempDutyCycle = 0;
    GL_A.period(switchingPeriod); // applies to all PwmOut instances
    rl.setLimits(0.5, -0.5, 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°
    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( !direction ){
            currentSector++;
            if(currentSector > 5){
                currentSector = 0;
            }
            switch(currentSector) {         /*正転*/        
                case 0:               //001     
                    GL_C = 0; GL_B = 0; GL_A = dutyCycle; GH_C = 0; GH_B = 1; GH_A = 0;
                    break;
                case 1:            
                    GL_C = dutyCycle; GL_B = 0; GL_A = 0; GH_C = 0; GH_B = 1; GH_A = 0; 
                    break;
                case 2:             
                    GL_C = dutyCycle; GL_B = 0; GL_A = 0; GH_C = 0; GH_B = 0; GH_A = 1;
                    break;
                case 3:             
                    GL_C = 0; GL_B = dutyCycle; GL_A = 0; GH_C = 0; GH_B = 0; GH_A = 1;
                    break;
                case 4:              
                    GL_C = 0; GL_B = dutyCycle; GL_A = 0; GH_C = 1; GH_B = 0; GH_A = 0;
                    break;    
                case 5:              
                    GL_C = 0; GL_B = 0; GL_A = dutyCycle; GH_C = 1; GH_B = 0; GH_A = 0;
                    break;
            }
        }
        else if ( direction ) {    //dutyがマイナスかつ進行方向が後退になっている場合
            GL_C = dutyCycle; GL_B = dutyCycle; GL_A = dutyCycle; GH_C = 0; GH_B = 0; GH_A = 0;  //回生動作する            
        }
    }
    else if( dutyCycle < 0 ){
        if( direction ){ /*逆転*/
            currentSector--;
            if(currentSector < 0){ 
                currentSector = 5;
            }
            switch(currentSector) {
                case 0:  
                    GL_C = 0; GL_B = 0; GL_A = -dutyCycle; GH_C = 1; GH_B = 0; GH_A = 0;
                    break;
                case 1:                 
                    GL_C = 0; GL_B = 0; GL_A = -dutyCycle; GH_C = 0; GH_B = 1; GH_A = 0;
                    break;
                case 2:
                    GL_C = -dutyCycle; GL_B = 0; GL_A = 0; GH_C = 0; GH_B = 1; GH_A = 0;
                    break;
                case 3:
                    GL_C = -dutyCycle; GL_B = 0; GL_A = 0; GH_C = 0; GH_B = 0; GH_A = 1;
                    break;
                case 4:
                    GL_C = 0; GL_B = -dutyCycle; GL_A = 0; GH_C = 0; GH_B = 0; GH_A = 1;
                    break;
                case 5:
                    GL_C = 0; GL_B = -dutyCycle; GL_A = 0; GH_C = 1; GH_B = 0; GH_A = 0;
                    break;   
             }
        }
        else if ( !direction ) {    //dutyがマイナスかつ進行方向が後退になっている場合
            GL_C = -dutyCycle; GL_B = -dutyCycle; GL_A = -dutyCycle; GH_C = 0; GH_B = 0; GH_A = 0;  //回生動作する            
        }
    } 
    else {
        coast();
    }
}
void BLDCmotorDriver::setDutyCycle(float dc) {
    if (dc >= -1 && dc <= 1) {
        ticker.attach(this, &BLDCmotorDriver::commutation, sampleTime);
        tempDutyCycle = dc;
    } else {
        coast();
    }
}
void BLDCmotorDriver::setDirection(float DS) {
    if (!DS) {
        direction = 0;
    } else {
        direction = 1;
    }
    if( !direction ){
//        GH_A=0;
    }else {
//        GH_A=1;
    }
}
/*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;
}