working-est copy with class-based code. still open loop

Dependencies:   mbed

Fork of analoghalls6 by N K

Revision:
6:99ee0ce47fb2
Parent:
5:ee1e6c84c302
Child:
7:76d6ceb23e0d
Child:
9:d3b70c15baa9
--- a/loopdriver.cpp	Tue Mar 03 06:28:10 2015 +0000
+++ b/loopdriver.cpp	Wed Mar 04 15:33:32 2015 +0000
@@ -30,50 +30,48 @@
     _upd_ticker.attach_us(&update, 1000000 / _update_frequency);
 }
 
-void LoopDriver::Clarke(float a, float b, float *alpha, float *beta) {
-    *alpha = a;
-    *beta = 1.0f / sqrt(3.0f) * a + 2.0f / sqrt(3.0f) * b;
+void LoopDriver::Clarke(float c, float b, float *alpha, float *beta) {
+    *alpha = b;
+    //*beta = 1.0f / sqrt(3.0f) * a + 2.0f / sqrt(3.0f) * b;
+    *beta = (b + 2.0f * c)/sqrt(3.0f);
 }
 
 void LoopDriver::Parke(float alpha, float beta, float theta, float *d, float *q) {
     float cos = LutCos(theta);
     float sin = LutSin(theta);
-    *d = alpha * cos + beta * sin;
-    *q = -alpha * sin + beta * cos;
+    *d = alpha * cos - beta * sin;
+    *q = -alpha * sin - beta * cos;
 }
 
 void LoopDriver::InverseParke(float d, float q, float theta, float *alpha, float *beta) {
     float cos = LutCos(theta);
     float sin = LutSin(theta);
-    *alpha = cos * d - sin * q;
-    *beta = sin * d + cos * q;                                     
+    //*alpha = cos * d - sin * q;
+    //*beta = -(sin * d + cos * q);  
+    
+    *alpha = cos * 1.0f;
+    *beta = -sin * 1.0f;                                    
 }
 
 float LoopDriver::LutSin(float theta) {
     if (theta < 0.0f) theta += 360.0f;
     if (theta >= 360.0f) theta -= 360.0f;
-    return (sinetab[(int) theta]*2)-1.0f; //shift range 0:1 to -1:1
+    return sinetab[(int) theta] * 2.0f - 1.0f;
 }
 
 float LoopDriver::LutCos(float theta) {
     return LutSin(90.0f - theta);
 }
 
-void LoopDriver::update() {
-    /*
-    _inverter->SetDtcA(LutSin(_motor->angle));
-    _inverter->SetDtcB(LutSin(_motor->angle - 120.0f));
-    _inverter->SetDtcC(LutSin(_motor->angle + 120.0f));
-    */
-    
+void LoopDriver::update() {    
     float alpha, beta, d, q, ref_d, ref_q, valpha, vbeta;
-    Clarke(_motor->I_a, _motor->I_b, &alpha, &beta);
+    Clarke(_motor->I_c, _motor->I_b, &alpha, &beta);
+    test_alpha = alpha;
+    test_beta = beta;
     Parke(alpha, beta, _motor->angle, &d, &q);
     _reference->GetReference(_motor->angle, &ref_d, &ref_q);
     float vd = _pid_d->Update(ref_d, d);
     float vq = _pid_q->Update(ref_q, q);
     InverseParke(vd, vq, _motor->angle, &valpha, &vbeta);
-    _modulator->Update(valpha, vbeta);
-    
-    
+    _modulator->Update(valpha, vbeta);   
 }