derp

Dependencies:   FastPWM3 mbed

Revision:
7:caebf421f288
Parent:
4:a6669248ce4d
Child:
8:314074b56470
--- a/main.cpp	Fri Mar 18 12:07:14 2016 +0000
+++ b/main.cpp	Wed Mar 30 06:50:11 2016 +0000
@@ -9,7 +9,7 @@
 FastPWM *b;
 FastPWM *c;
 DigitalOut en(EN);
-DigitalOut toggle(PC_10);
+//DigitalOut toggle(PC_10);
 
 PositionSensorEncoder pos(CPR, 0);
 
@@ -17,13 +17,13 @@
 
 int state = 0;
 int adval1, adval2;
-float ia, ib, ic, alpha, beta, d, q, vd, vq, p;
+float ia, ib, ic, alpha, beta, d, q, vd, vq, p = 0.0f;
 
 float ia_supp_offset = 0.0f, ib_supp_offset = 0.0f; //current sensor offset due to bias resistor inaccuracies, etc (mV)
 
 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 = 30.0f;
 
 void commutate();
 void zero_current();
@@ -32,11 +32,11 @@
 
 extern "C" void TIM1_UP_TIM10_IRQHandler(void) {
     if (TIM1->SR & TIM_SR_UIF ) {
-        toggle = 1;
+        //toggle = 1;
         ADC1->CR2  |= 0x40000000; 
         volatile int delay;
         for (delay = 0; delay < 35; delay++);
-        toggle = 0;
+        //toggle = 0;
         adval1 = ADC1->DR;
         adval2 = ADC2->DR;
         commutate();
@@ -80,7 +80,7 @@
     TIM1->EGR |= TIM_EGR_UG;
     
     TIM1->PSC = 0x00; //no prescaler, timer counts up in sync with the peripheral clock
-    TIM1->ARR = 0x4650; //5 Khz
+    TIM1->ARR = 0x1770; //15 Khz
     TIM1->CCER |= ~(TIM_CCER_CC1NP); //Interupt when low side is on.
     TIM1->CR1 |= TIM_CR1_CEN;
     
@@ -143,12 +143,14 @@
 
 void commutate() {
     p = pos.GetElecPosition() - POS_OFFSET;
+         
     if (p < 0) p += 2 * PI;
+    if (p > 2 * PI) p -= 2 * PI;
     
     float sin_p = sinf(p);
     float cos_p = cosf(p);
     
-    //float pos_dac = 0.85f * p / (2 * PI) + 0.05f;
+    //float pos_dac = 0.8f * p / (2 * PI) + 0.1f;
     //DAC->DHR12R2 = (unsigned int) (pos_dac * 4096);
     
     ia = ((float) adval1 / 4096.0f * AVDD - I_OFFSET - ia_supp_offset) / I_SCALE;
@@ -183,11 +185,11 @@
     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;
+    //vq = 1.0f;
     
     float valpha = vd * cos_p - vq * sin_p;
     float vbeta = vd * sin_p + vq * cos_p;
@@ -196,6 +198,8 @@
     float vb = -0.5f * valpha - sqrtf(3) / 2.0f * vbeta;
     float vc = -0.5f * valpha + sqrtf(3) / 2.0f * vbeta;
     
+    //DAC->DHR12R2 = (unsigned int) (-va * 1500 + 2048);
+    
     set_dtc(a, 0.5f + 0.5f * va);
     set_dtc(b, 0.5f + 0.5f * vb);
     set_dtc(c, 0.5f + 0.5f * vc);
@@ -206,6 +210,8 @@
     startup_msg();
     
     for (;;) {
+        //pc.printf("%f\n\r", p);
+        //wait_ms(100);
         /*
         q_ref = 0.0f;
         wait(3);