derp

Dependencies:   FastPWM3 mbed

Revision:
12:0811d08424e1
Parent:
11:825203ff4371
Child:
13:728ddd6939d3
Child:
15:8eb1dfbf0d41
--- a/main.cpp	Mon Apr 18 20:08:05 2016 +0000
+++ b/main.cpp	Mon Apr 18 21:06:26 2016 +0000
@@ -33,9 +33,11 @@
 
 #define INTEGRAL_MAX 1.0f
 
-#define THROTTLE_MAX 2000.0f
-#define THROTTLE_ERROR 5000.0f
-#define THROTTLE_MIN 1500.0f 
+#define THROTTLE_MAX 1900.0f
+#define THROTTLE_MIN 1600.0f 
+
+#define THROTTLE_HIGH 5000.0f
+#define THROTTLE_LOW 1550.0f
 
 #define Q_REF_MAX (-200.0)
 
@@ -50,11 +52,11 @@
 int adval1, adval2;
 float ia, ib, ic, alpha, beta, q;
 double vq = 0.0, q_integral = 0.0, last_q = 0.0;
-float throttle = 0.0f;
+volatile float throttle = 0.0f, throttle_scaler = 0.0f;
 
-int edge_seen = 0, watchdog_divider = 0;
+volatile int edge_seen = 0, watchdog_divider = 0;
 
-int err_throttle_too_low = 0, err_throttle_too_high = 0;
+volatile int err_throttle_too_low = 0, err_throttle_too_high = 0;
  
 extern "C" void TIM1_UP_TIM10_IRQHandler(void) {
     if (TIM1->SR & TIM_SR_UIF) {
@@ -85,28 +87,7 @@
         
         q = -alpha * sin_p - beta * cos_p;
         
-        //if (throttle < THROTTLE_MIN || throttle > THROTTLE_ERROR) throttle = THROTTLE_MIN;
-        if (throttle < THROTTLE_MIN) {
-            throttle = THROTTLE_MIN;
-            err_throttle_too_low = 1;
-        }
-        
-        /*
-        if(edge_seen == 0) {
-            throttle = THROTTLE_MIN;        
-            en = 0;
-        } else {
-            en = 1;
-        }
-        */
-        
-        if (throttle > THROTTLE_ERROR) {
-            throttle = THROTTLE_MIN;
-            err_throttle_too_high = 1;
-        }
-        
-        if (throttle > THROTTLE_MAX) throttle = THROTTLE_MAX;
-        float throttle_scaler = (throttle - THROTTLE_MIN) / (THROTTLE_MAX - THROTTLE_MIN);
+        DAC->DHR12R2 = throttle;
         
         double q_err = Q_REF_MAX * (double) throttle_scaler - q;
                 
@@ -121,13 +102,7 @@
         if (vq > 1.0f) vq = 1.0f;
                         
         //DAC->DHR12R2 = (unsigned int) (vq * 2000 + 2048);
-        DAC->DHR12R2 = (unsigned int) (throttle_scaler * 2000 + 2048);
-        
-        if (err_throttle_too_low || err_throttle_too_high || !edge_seen) {
-            en = 0;
-        } else {
-            en = 1;
-        }
+        //DAC->DHR12R2 = (unsigned int) (err_throttle_too_low * 2000 + 2048);
                                             
         *a = 0.5f + 0.5f * vq * sinf(p);
         *b = 0.5f + 0.5f * vq * sinf(p + 2 * PI / 3);
@@ -149,6 +124,33 @@
 
 void throttle_fall() {
     throttle = TIM5->CNT;
+    
+    /*
+    if (throttle < THROTTLE_LOW) {
+        err_throttle_too_low = 1;
+    } else {
+        err_throttle_too_low = 0;
+    }
+    
+    if (throttle > THROTTLE_HIGH) {
+        err_throttle_too_high = 1;
+    } else {
+        err_throttle_too_high = 0;
+    }
+    */
+    
+    if (throttle < THROTTLE_MIN) throttle = THROTTLE_MIN;
+    if (throttle > THROTTLE_MAX) throttle = THROTTLE_MAX;
+    throttle_scaler = (throttle - THROTTLE_MIN) / (THROTTLE_MAX - THROTTLE_MIN);
+    
+    /*
+    if (err_throttle_too_low || err_throttle_too_high || !edge_seen) {
+        en = 0;
+    } else {
+        en = 1;
+    }
+    */
+    
     edge_seen = 1;
 }