motor controller

Dependencies:   mbed plotter

Files at this revision

API Documentation at this revision

Comitter:
dicarloj
Date:
Sun Nov 06 03:09:28 2016 +0000
Parent:
6:7310f31aa49e
Commit message:
works at 200V

Changed in this revision

config.h Show annotated file Show diff for this revision Revisions of this file
foc.cpp Show annotated file Show diff for this revision Revisions of this file
io.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
vehicle_controller.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/config.h	Sat Nov 05 20:20:48 2016 +0000
+++ b/config.h	Sun Nov 06 03:09:28 2016 +0000
@@ -7,13 +7,13 @@
 
 #define K_SERIAL_BAUD 115200 //serial over USB baud rate
 
-#define K_V_BUS             200.0f   //HV battery voltage
+#define K_V_BUS             160.0f   //HV battery voltage
 #define K_Q_CURRENT_MAX     200.0f   //max torque 
-#define K_Q_CURRENT_MIN     -1.0f   //max regen torque
-#define K_P_CURRENT         1.0f    //current loop kp
-#define K_I_CURRENT         0.1f    //current loop ki
-#define K_CURRENT_INT_MAX  20.0f    //current loop integral max
-#define K_AXIS_V_MAX       11000.0f //current loop axis voltage max
+#define K_Q_CURRENT_MIN     0.0f   //max regen torque
+#define K_P_CURRENT         3.0f    //current loop kp
+#define K_I_CURRENT         0.05f    //current loop ki
+#define K_CURRENT_INT_MAX  (K_V_BUS*0.866f)    //current loop integral max
+#define K_AXIS_V_MAX       (K_V_BUS/2.0f) //current loop axis voltage max
 
 
 #define K_POS_OFFSET       (- 5.78f + M_PI + 0.03f) //electrical position offset
--- a/foc.cpp	Sat Nov 05 20:20:48 2016 +0000
+++ b/foc.cpp	Sun Nov 06 03:09:28 2016 +0000
@@ -116,22 +116,27 @@
     float beta_i = (i_c - i_b)/SQRT_3;
 
     //convert from alpha, beta to q,d
-    float alpha = 0.1;
+    float alpha = .3;
     float d_i =  alpha * (alpha_i * cos_theta_e - beta_i * sin_theta_e) + (1 - alpha) * d_i;
     float q_i =  alpha * (alpha_i * sin_theta_e + beta_i * cos_theta_e) + (1 - alpha) * q_i;
-    
+    //DAC->DHR12R1 = (int)(q_i * 200);
     //calculate error integral
-    q_int += (q_command - q_i) * K_I;
-    d_int += (d_command - d_i) * K_I;
+    q_int += (q_command - q_i) * K_I * K_P;
+    d_int += (d_command - d_i) * K_I * K_P;
 
     //prevent integral windup
-    q_int = fabs(q_int) > integral_max ? integral_max * (q_int > 0 ? 1 : -1) : q_int;
-    d_int = fabs(d_int) > integral_max ? integral_max * (d_int > 0 ? 1 : -1) : d_int;
+    //q_int = fabs(q_int) > integral_max ? integral_max * (q_int > 0 ? 1 : -1) : q_int;
+    //d_int = fabs(d_int) > integral_max ? integral_max * (d_int > 0 ? 1 : -1) : d_int;
+    if(q_int > K_CURRENT_INT_MAX) q_int = K_CURRENT_INT_MAX;
+    if(q_int < -K_CURRENT_INT_MAX) q_int = -K_CURRENT_INT_MAX;
+    
+    if(d_int > K_CURRENT_INT_MAX) d_int = K_CURRENT_INT_MAX;
+    if(d_int < -K_CURRENT_INT_MAX) d_int = -K_CURRENT_INT_MAX;
 
     //calculate q, d axis voltages
     float q_v = K_P * (q_command - q_i) + q_int;
     float d_v = K_P * (d_command - d_i) + d_int;
-plot(0, q_v); plot(1, d_v);
+//plot(0, q_v); plot(1, d_v);
     //limit axis voltages
     q_v = fabs(q_v) > axis_v_max ? axis_v_max * (q_v > 0 ? 1 : -1) : q_v;
     d_v = fabs(d_v) > axis_v_max ? axis_v_max * (d_v > 0 ? 1 : -1) : d_v;
--- a/io.cpp	Sat Nov 05 20:20:48 2016 +0000
+++ b/io.cpp	Sun Nov 06 03:09:28 2016 +0000
@@ -104,9 +104,12 @@
     /************************************
                 ADC/DAC START
     *************************************/
+    //     RCC->APB1ENR |= 0x20000000; // Enable clock for DAC
+    // DAC->CR |= 0x00000001; // DAC control reg, both channels ON
+     GPIOA->MODER |= 0x00000300; // PA04 as analog output   
     ADC1->CR2 |= ADC_CR2_ADON; //adc 1 on
     ADC2->CR2 |= ADC_CR2_ADON; //adc 2 on
-    DAC->CR   |= DAC_CR_EN2;   //dac 2 (PA5) on
+    //DAC->CR   |= DAC_CR_EN2;   //dac 2 (PA5) on
     
     //get_serial(0)->printf("Turning on TIMER\n");
     /************************************
@@ -137,7 +140,7 @@
  
     TIM2->CR1   = 0x0001;
     TIM2->SMCR  = TIM_ENCODERMODE_TI12;
-    TIM2->CCMR1 = 0xf1f1;
+    TIM2->CCMR1 = 0x0101;
     TIM2->CCMR2 = 0x0000;
     TIM2->CCER  = 0x0011;
     TIM2->PSC   = 0x0000;
@@ -223,13 +226,16 @@
     return size; //we finished the loop so buffer is full, all bytes read.
 }
 
+
 //interrupt handler for encoder index
 void handle_index()
 {
     if (index_in->read())
     {
         if(index_in->read())
-            TIM2->CNT=0;
+        {
+                TIM2->CNT=0;
+        }
     }
 }
 
--- a/main.cpp	Sat Nov 05 20:20:48 2016 +0000
+++ b/main.cpp	Sun Nov 06 03:09:28 2016 +0000
@@ -6,13 +6,13 @@
 
 
 int main() {
-    init_plotter(9);
+    //init_plotter(9);
     setup();
     while(true)
     {
         wait(0.1);
         //plot(0, 0.2);
-        send_message();
+        //send_message();
     }
     
 }
--- a/vehicle_controller.cpp	Sat Nov 05 20:20:48 2016 +0000
+++ b/vehicle_controller.cpp	Sun Nov 06 03:09:28 2016 +0000
@@ -7,11 +7,13 @@
 float filtered_velocity = 0;
 float alpha = .3;
 float vel_int = 0;
+float filtered_throttle = 0;
+float throt_alpha = .002;
 
 //simple current controller
 float current_control(float throttle)
 {
-    return throttle * K_Q_CURRENT_MAX;
+    return (throttle * alpha + filtered_throttle * (1 - alpha)) * K_Q_CURRENT_MAX;
 }
 
 //servo controller