Program for current regulation of BLDC motor.

Dependents:   CurrentMeasurement

Fork of CurrentRegulation by Dean Fraj

Revision:
1:9406a55d8a12
Parent:
0:d8dab3dae6f2
Child:
2:f71d1fb67922
--- a/CurrentRegulation.cpp	Thu Aug 20 14:58:23 2015 +0000
+++ b/CurrentRegulation.cpp	Mon Aug 24 11:13:04 2015 +0000
@@ -1,15 +1,11 @@
 #include "CurrentRegulation.h"
+#include "BLDCmotorDriver.h"
+#include "PI.h"
 
-CurrentRegulation::CurrentRegulation(PinName pI_A, PinName pI_B, PinName pI_C, PinName pI_TOTAL):I_A(pI_A), I_B(pI_B), I_C(pI_C), I_TOTAL(pI_TOTAL){
+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,
+                    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){
     setValues(1e-3, 1e3, 40e3, 20e3, 3.3);
-    measure1.attach_us(this, &CurrentRegulation::calculateCurrentA, 500);
-    measure2.attach_us(this, &CurrentRegulation::calculateTotalCurrent, 500);
-}
-
-CurrentRegulation::CurrentRegulation(PinName pI_A, PinName pI_B, PinName pI_C, PinName pI_TOTAL, double R_sh, double R_1, double R_fs, double R_ft, double V_ref):I_A(pI_A), I_B(pI_B), I_C(pI_C), I_TOTAL(pI_TOTAL){
-    setValues(R_sh, R_1, R_fs, R_ft, V_ref);
-    measure1.attach_us(this, &CurrentRegulation::calculateCurrentA, 500);
-    measure2.attach_us(this, &CurrentRegulation::calculateTotalCurrent, 500);
+    measure.attach_us(this, &CurrentRegulation::measuring, 500);
 }
 
 void CurrentRegulation::setValues(double R_sh, double R_1, double R_fs, double R_ft, double V_ref){
@@ -20,28 +16,53 @@
     this->V_ref = V_ref;    
 }
 
-void CurrentRegulation::calculateCurrentA(){
+double CurrentRegulation::calculateCurrentA(){
     double V_outa = (V_ref - (V_ref/2))/(1 - 0) * I_A.read() + (V_ref/2);
-    this->I_A_ = (R_1 * V_outa)/(R_sh * R_fs) - (R_1 * (V_ref/2))/(R_sh * R_fs);
+    return I_A_ = (R_1 * V_outa)/(R_sh * R_fs) - (R_1 * (V_ref/2))/(R_sh * R_fs);
 }
 
-void CurrentRegulation::calculateCurrentB(){
+double CurrentRegulation::calculateCurrentB(){
     double V_outb = (V_ref - (V_ref/2))/(1 - 0) * I_B.read() + (V_ref/2);
-    this->I_B_ = (R_1 * V_outb)/(R_sh * R_fs) - (R_1 * (V_ref/2))/(R_sh * R_fs);
+    return I_B_ = (R_1 * V_outb)/(R_sh * R_fs) - (R_1 * (V_ref/2))/(R_sh * R_fs);
+}
+
+double CurrentRegulation::calculateCurrentC(){
+    double V_outc = (V_ref - (V_ref/2))/(1 - 0) * I_C.read() + (V_ref/2);
+    return I_C_ = (R_1 * V_outc)/(R_sh * R_fs) - (R_1 * (V_ref/2))/(R_sh * R_fs);
+}
+
+double CurrentRegulation::calculateTotalCurrent(){
+    double V_outt = (V_ref - (V_ref/2))/(1 - 0) * I_TOTAL.read()+(V_ref/2);
+    return I_TOTAL_ = (R_1 * V_outt)/(R_sh * R_fs) - (R_1 * (V_ref/2))/(R_sh * R_fs);
 }
 
-void CurrentRegulation::calculateCurrentC(){
-    double V_outc = (V_ref - (V_ref/2))/(1 - 0) * I_C.read() + (V_ref/2);
-    this->I_C_ = (R_1 * V_outc)/(R_sh * R_fs) - (R_1 * (V_ref/2))/(R_sh * R_fs);
-}
-
-void CurrentRegulation::calculateTotalCurrent(){
-    double V_outt = (V_ref - (V_ref/2))/(1 - 0)*I_TOTAL.read()+(V_ref/2);
-    this->I_TOTAL_ = (R_1 * V_outt)/(R_sh * R_fs) - (R_1 * (V_ref/2))/(R_sh * R_fs);
-}
-
-double showResult(){
-    double I_Ar = this->I_A_;
+double CurrentRegulation::phaseCurrent(int currentSector){
+    switch(currentSector){  
+           case 0:                
+                I_Ar = calculateCurrentC();
+                calculateTotalCurrent();
+                break;
+           case 1:            
+                I_Ar = calculateCurrentC(); 
+                calculateTotalCurrent();
+                break;
+           case 2:   
+                I_Ar = calculateCurrentA();   
+                calculateTotalCurrent();
+                break;
+            case 3:             
+                I_Ar = calculateCurrentA(); 
+                calculateTotalCurrent();
+                break;
+            case 4:              
+                I_Ar = calculateCurrentB();  
+                calculateTotalCurrent();
+                break;    
+            case 5:              
+                I_Ar = calculateCurrentB();  
+                calculateTotalCurrent();
+                break;
+        }
     return I_Ar;
 }
 
@@ -58,27 +79,17 @@
     return K_R = (K_oR * T_I)/(K_a * K_ch * K_pv);
 }
 
-/*
-void CurrentMeasurement::phaseCurrent(int currentSector){
-    switch(currentSector){  
-           case 0:                
-                measureCurrentC();
-                break;
-           case 1:            
-                measureCurrentC(); 
-                break;
-           case 2:   
-                measureCurrentA();   
-                break;
-            case 3:             
-                measureCurrentA(); 
-                break;
-            case 4:              
-                measureCurrentB();  
-                break;    
-            case 5:              
-                measureCurrentB();  
-                break;
-        }
+void CurrentRegulation::input(double in){
+    setPoint = in;
 }
-*/
\ No newline at end of file
+
+void CurrentRegulation::measuring(){
+    sector = m.getSector();
+    procesValue = phaseCurrent(sector);
+    K_R = calculateKr();
+    T_d = 500e-6;
+    PI reg(K_R, T_I, T_d);
+    u = setPoint - procesValue;
+    reg.in(u); 
+    reg.out();
+}
\ No newline at end of file