Program for current regulation of BLDC motor.

Dependents:   CurrentMeasurement

Fork of CurrentRegulation by Dean Fraj

Committer:
dfraj
Date:
Tue Aug 25 19:14:01 2015 +0000
Revision:
2:f71d1fb67922
Parent:
1:9406a55d8a12
v0.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dfraj 0:d8dab3dae6f2 1 #include "CurrentRegulation.h"
dfraj 1:9406a55d8a12 2 #include "BLDCmotorDriver.h"
dfraj 1:9406a55d8a12 3 #include "PI.h"
dfraj 0:d8dab3dae6f2 4
dfraj 1:9406a55d8a12 5 CurrentRegulation::CurrentRegulation(PinName pI_A, PinName pI_B, PinName pI_C, PinName pI_TOTAL, PinName pGH_A, PinName pGH_B, PinName pGH_C, PinName pGL_A, PinName pGL_B, PinName pGL_C,
dfraj 1:9406a55d8a12 6 PinName pH1, PinName pH2, PinName pH3, PinName pFault):I_A(pI_A), I_B(pI_B), I_C(pI_C), I_TOTAL(pI_TOTAL), m(pGH_A, pGH_B, pGH_C, pGL_A, pGL_B, pGL_C, pH1, pH2, pH3, LED1){
dfraj 0:d8dab3dae6f2 7 setValues(1e-3, 1e3, 40e3, 20e3, 3.3);
dfraj 2:f71d1fb67922 8 //measure.attach_us(this, &CurrentRegulation::measuring, 500);
dfraj 0:d8dab3dae6f2 9 }
dfraj 0:d8dab3dae6f2 10
dfraj 0:d8dab3dae6f2 11 void CurrentRegulation::setValues(double R_sh, double R_1, double R_fs, double R_ft, double V_ref){
dfraj 0:d8dab3dae6f2 12 this->R_sh = R_sh;
dfraj 0:d8dab3dae6f2 13 this->R_1 = R_1;
dfraj 0:d8dab3dae6f2 14 this->R_fs = R_fs;
dfraj 0:d8dab3dae6f2 15 this->R_ft = R_ft;
dfraj 0:d8dab3dae6f2 16 this->V_ref = V_ref;
dfraj 0:d8dab3dae6f2 17 }
dfraj 0:d8dab3dae6f2 18
dfraj 1:9406a55d8a12 19 double CurrentRegulation::calculateCurrentA(){
dfraj 0:d8dab3dae6f2 20 double V_outa = (V_ref - (V_ref/2))/(1 - 0) * I_A.read() + (V_ref/2);
dfraj 1:9406a55d8a12 21 return I_A_ = (R_1 * V_outa)/(R_sh * R_fs) - (R_1 * (V_ref/2))/(R_sh * R_fs);
dfraj 0:d8dab3dae6f2 22 }
dfraj 0:d8dab3dae6f2 23
dfraj 1:9406a55d8a12 24 double CurrentRegulation::calculateCurrentB(){
dfraj 0:d8dab3dae6f2 25 double V_outb = (V_ref - (V_ref/2))/(1 - 0) * I_B.read() + (V_ref/2);
dfraj 1:9406a55d8a12 26 return I_B_ = (R_1 * V_outb)/(R_sh * R_fs) - (R_1 * (V_ref/2))/(R_sh * R_fs);
dfraj 1:9406a55d8a12 27 }
dfraj 1:9406a55d8a12 28
dfraj 1:9406a55d8a12 29 double CurrentRegulation::calculateCurrentC(){
dfraj 1:9406a55d8a12 30 double V_outc = (V_ref - (V_ref/2))/(1 - 0) * I_C.read() + (V_ref/2);
dfraj 1:9406a55d8a12 31 return I_C_ = (R_1 * V_outc)/(R_sh * R_fs) - (R_1 * (V_ref/2))/(R_sh * R_fs);
dfraj 1:9406a55d8a12 32 }
dfraj 1:9406a55d8a12 33
dfraj 1:9406a55d8a12 34 double CurrentRegulation::calculateTotalCurrent(){
dfraj 1:9406a55d8a12 35 double V_outt = (V_ref - (V_ref/2))/(1 - 0) * I_TOTAL.read()+(V_ref/2);
dfraj 1:9406a55d8a12 36 return I_TOTAL_ = (R_1 * V_outt)/(R_sh * R_fs) - (R_1 * (V_ref/2))/(R_sh * R_fs);
dfraj 0:d8dab3dae6f2 37 }
dfraj 0:d8dab3dae6f2 38
dfraj 1:9406a55d8a12 39 double CurrentRegulation::phaseCurrent(int currentSector){
dfraj 1:9406a55d8a12 40 switch(currentSector){
dfraj 1:9406a55d8a12 41 case 0:
dfraj 1:9406a55d8a12 42 I_Ar = calculateCurrentC();
dfraj 1:9406a55d8a12 43 calculateTotalCurrent();
dfraj 1:9406a55d8a12 44 break;
dfraj 1:9406a55d8a12 45 case 1:
dfraj 1:9406a55d8a12 46 I_Ar = calculateCurrentC();
dfraj 1:9406a55d8a12 47 calculateTotalCurrent();
dfraj 1:9406a55d8a12 48 break;
dfraj 1:9406a55d8a12 49 case 2:
dfraj 1:9406a55d8a12 50 I_Ar = calculateCurrentA();
dfraj 1:9406a55d8a12 51 calculateTotalCurrent();
dfraj 1:9406a55d8a12 52 break;
dfraj 1:9406a55d8a12 53 case 3:
dfraj 1:9406a55d8a12 54 I_Ar = calculateCurrentA();
dfraj 1:9406a55d8a12 55 calculateTotalCurrent();
dfraj 1:9406a55d8a12 56 break;
dfraj 1:9406a55d8a12 57 case 4:
dfraj 1:9406a55d8a12 58 I_Ar = calculateCurrentB();
dfraj 1:9406a55d8a12 59 calculateTotalCurrent();
dfraj 1:9406a55d8a12 60 break;
dfraj 1:9406a55d8a12 61 case 5:
dfraj 1:9406a55d8a12 62 I_Ar = calculateCurrentB();
dfraj 1:9406a55d8a12 63 calculateTotalCurrent();
dfraj 1:9406a55d8a12 64 break;
dfraj 1:9406a55d8a12 65 }
dfraj 0:d8dab3dae6f2 66 return I_Ar;
dfraj 0:d8dab3dae6f2 67 }
dfraj 0:d8dab3dae6f2 68
dfraj 2:f71d1fb67922 69 void CurrentRegulation::getValue(){
dfraj 2:f71d1fb67922 70 sector = m.getSector();
dfraj 2:f71d1fb67922 71 procesValue = phaseCurrent(sector);
dfraj 2:f71d1fb67922 72 }
dfraj 2:f71d1fb67922 73
dfraj 2:f71d1fb67922 74 void CurrentRegulation::measuring(){
dfraj 2:f71d1fb67922 75 measure.attach_us(this, &CurrentRegulation::getValue, 500);
dfraj 2:f71d1fb67922 76 }
dfraj 2:f71d1fb67922 77
dfraj 0:d8dab3dae6f2 78 double CurrentRegulation::calculateKr(){
dfraj 0:d8dab3dae6f2 79 T_pv = 1e-4;
dfraj 0:d8dab3dae6f2 80 T_ch = 25e-6;
dfraj 0:d8dab3dae6f2 81 zeta = 1.2;
dfraj 0:d8dab3dae6f2 82 K_ch = 50.0;
dfraj 0:d8dab3dae6f2 83 K_pv = 1.0;
dfraj 0:d8dab3dae6f2 84 T_I = 1.428e-3;
dfraj 0:d8dab3dae6f2 85 K_a = 4.76;
dfraj 0:d8dab3dae6f2 86 T_suma = (T_pv + T_ch);
dfraj 0:d8dab3dae6f2 87 K_oR = 1/(4 * (zeta * zeta) * T_suma);
dfraj 0:d8dab3dae6f2 88 return K_R = (K_oR * T_I)/(K_a * K_ch * K_pv);
dfraj 0:d8dab3dae6f2 89 }
dfraj 0:d8dab3dae6f2 90
dfraj 2:f71d1fb67922 91 /*void CurrentRegulation::input(double in){
dfraj 2:f71d1fb67922 92 setPoint = 41.25 * in;
dfraj 0:d8dab3dae6f2 93 }
dfraj 2:f71d1fb67922 94 */
dfraj 1:9406a55d8a12 95
dfraj 2:f71d1fb67922 96 void CurrentRegulation::setOutput(double in){
dfraj 1:9406a55d8a12 97 K_R = calculateKr();
dfraj 1:9406a55d8a12 98 T_d = 500e-6;
dfraj 1:9406a55d8a12 99 PI reg(K_R, T_I, T_d);
dfraj 2:f71d1fb67922 100 setPoint = 41.25 * in;
dfraj 1:9406a55d8a12 101 u = setPoint - procesValue;
dfraj 1:9406a55d8a12 102 reg.in(u);
dfraj 2:f71d1fb67922 103 output = reg.out();
dfraj 2:f71d1fb67922 104 m.setDutyCycle(output);
dfraj 1:9406a55d8a12 105 }