derp

Dependencies:   FastPWM3 mbed

Revision:
3:9b20da3f0055
Parent:
2:eabe8feaaabb
Child:
4:a6669248ce4d
--- a/main.cpp	Fri Mar 18 10:07:35 2016 +0000
+++ b/main.cpp	Fri Mar 18 10:52:45 2016 +0000
@@ -23,16 +23,19 @@
 
 float d_integral = 0.0f, q_integral = 0.0f;
 float last_d = 0.0f, last_q = 0.0f;
-float d_ref = 0.0f, q_ref = -50.0f;
+float d_ref = -0.0f, q_ref = -50.0f;
 
-float d_filtered = 0.0f;
-float q_filtered = 0.0f;
+void main_loop();
+void zero_current();
+void config_globals();
+void startup_msg();
 
 extern "C" void TIM1_UP_TIM10_IRQHandler(void) {
     if (TIM1->SR & TIM_SR_UIF ) {
         adval1 = ADC1->DR;
         adval2 = ADC2->DR;
         ADC1->CR2  |= 0x40000000; 
+        //while((ADC1->SR & ADC_SR_EOC) == 0 || (ADC2->SR & ADC_SR_EOC) == 0) {}
     }
     TIM1->SR = 0x00;
 }
@@ -135,22 +138,21 @@
 }    
 
 void main_loop() {
-    //float p = pos.GetElecPosition() - POS_OFFSET + PI / 2;
     p = pos.GetElecPosition() - POS_OFFSET;
     if (p < 0) p += 2 * PI;
     
     float sin_p = sinf(p);
     float cos_p = cosf(p);
     
-    //float pos_dac = 0.85f * p / (2 * PI) + 0.05f;
-    //DAC->DHR12R2 = (unsigned int) (pos_dac * 4096);
+    float pos_dac = 0.85f * p / (2 * PI) + 0.05f;
+    DAC->DHR12R2 = (unsigned int) (pos_dac * 4096);
     
     ia = ((float) adval1 / 4096.0f * AVDD - I_OFFSET - ia_supp_offset) / I_SCALE;
     ib = ((float) adval2 / 4096.0f * AVDD - I_OFFSET - ib_supp_offset) / I_SCALE;
     ic = -ia - ib;
     
-    float u = ib;//ic;
-    float v = ic;//ia;
+    float u = ib;
+    float v = ic;
     
     alpha = u;
     beta = 1 / sqrtf(3.0f) * u + 2 / sqrtf(3.0f) * v;
@@ -158,11 +160,8 @@
     d = alpha * cos_p - beta * sin_p;
     q = -alpha * sin_p - beta * cos_p;
     
-    d_filtered = 0.9f * d_filtered + 0.1f * d;
-    q_filtered = 0.9f * q_filtered + 0.1f * q;
-    
-    float d_err = d_ref - d;//_filtered;
-    float q_err = q_ref - q;//_filtered;
+    float d_err = d_ref - d;
+    float q_err = q_ref - q;
     
     d_integral += d_err * KI;
     q_integral += q_err * KI;
@@ -180,15 +179,9 @@
     if (vq < -1.0f) vq = -1.0f;
     if (vq > 1.0f) vq = 1.0f;
     
-    DAC->DHR12R2 = (unsigned int) (-q * 20 + 2048);
+    //DAC->DHR12R2 = (unsigned int) (-q * 20 + 2048);
     //DAC->DHR12R2 = (unsigned int) (-vd * 2000 + 2048);
     
-    //vd = 0.0f;
-    //vq = -1.0f;
-    
-    //float phase = asinf(vd / sqrtf(vq * vq + vd * vd));
-    //DAC->DHR12R2 = (unsigned int) (phase / (PI / 2.0f) * 2000 + 2048);
-    
     float valpha = vd * cos_p - vq * sin_p;
     float vbeta = vd * sin_p + vq * cos_p;
     
@@ -199,10 +192,6 @@
     set_dtc(a, 0.5f + 0.5f * va);
     set_dtc(b, 0.5f + 0.5f * vb);
     set_dtc(c, 0.5f + 0.5f * vc);
-    
-    //set_dtc(a, 0.5f + 0.5f * cosf(p));
-    //set_dtc(b, 0.5f + 0.5f * cosf(p + 2 * PI / 3));
-    //set_dtc(c, 0.5f + 0.5f * cosf(p - 2 * PI / 3));
 }
     
 int main() {
@@ -212,14 +201,7 @@
     Ticker loop;
     loop.attach_us(main_loop, 200);
     
-    //vd = 0.0f;
-    //vq = 1.0f;
-    
     for (;;) {
-        
-        printf("%f\n\r", vd);
-        wait_ms(100);
-        
         /*
         q_ref = 0.0f;
         wait(3);
@@ -227,18 +209,8 @@
         state = !state;
         q_ref = -50.0f;
         wait(3);
-        */
-        
-        /*
-        vq = 0.0f;
-        wait(3);
         toggle = state;
         state = !state;
-        vq = -1.0f;
-        wait(3);
         */
-        
-        //toggle = state;
-        //state = !state;
     }
 }