yezhong yezhong / Motor_DRV8323RH_for_2019-

Dependencies:   mbed-dev-f303 FastPWM3

Files at this revision

API Documentation at this revision

Comitter:
benkatz
Date:
Sat Jul 14 22:03:52 2018 +0000
Parent:
43:dfb72608639c
Child:
45:aadebe074af6
Commit message:
updating mini cheetah version;

Changed in this revision

Calibration/calibration.cpp Show annotated file Show diff for this revision Revisions of this file
Config/current_controller_config.h Show annotated file Show diff for this revision Revisions of this file
FOC/foc.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
--- a/Calibration/calibration.cpp	Fri May 25 16:10:46 2018 +0000
+++ b/Calibration/calibration.cpp	Sat Jul 14 22:03:52 2018 +0000
@@ -6,6 +6,8 @@
 #include "foc.h"
 #include "PreferenceWriter.h"
 #include "user_config.h"
+#include "motor_config.h"
+#include "current_controller_config.h"
 
 void order_phases(PositionSensor *ps, GPIOStruct *gpio, ControllerStruct *controller, PreferenceWriter *prefs){   
     
@@ -14,10 +16,10 @@
     printf("\n\r Checking phase ordering\n\r");
     float theta_ref = 0;
     float theta_actual = 0;
-    float v_d = .25;                                                             //Put all volts on the D-Axis
-    float v_q = 0.0;
+    float v_d = .15f;                                                             //Put all volts on the D-Axis
+    float v_q = 0.0f;
     float v_u, v_v, v_w = 0;
-    float dtc_u, dtc_v, dtc_w = .5;
+    float dtc_u, dtc_v, dtc_w = .5f;
     int sample_counter = 0;
     
     ///Set voltage angle to zero, wait for rotor position to settle
@@ -86,10 +88,10 @@
     int raw_b[n] = {0};
     float theta_ref = 0;
     float theta_actual = 0;
-    float v_d = .25;                                                             // Put volts on the D-Axis
-    float v_q = 0.0;
+    float v_d = .15f;                                                             // Put volts on the D-Axis
+    float v_q = 0.0f;
     float v_u, v_v, v_w = 0;
-    float dtc_u, dtc_v, dtc_w = .5;
+    float dtc_u, dtc_v, dtc_w = .5f;
     
         
     ///Set voltage angle to zero, wait for rotor position to settle
--- a/Config/current_controller_config.h	Fri May 25 16:10:46 2018 +0000
+++ b/Config/current_controller_config.h	Sat Jul 14 22:03:52 2018 +0000
@@ -2,12 +2,13 @@
 #define CURRENT_CONTROLLER_CONFIG_H
 
 // Current controller///
-#define K_D .05f                     // Volts/Amp
-#define K_Q .05f                     // Volts/Amp
-#define K_SCALE 0.0001f            // K_loop/Loop BW (Hz) 0.0042
-#define KI_D 0.0255f                  // 1/samples
-#define KI_Q 0.0255f                  // 1/samples
+#define K_D .05f                    // Loop gain,  Volts/Amp
+#define K_Q .05f                    // Loop gain,  Volts/Amp
+#define K_SCALE 0.0001f             // K_loop/Loop BW (Hz) 0.0042
+#define KI_D 0.0255f                // PI zero, in radians per sample
+#define KI_Q 0.0255f                // PI zero, in radians per sample
 #define V_BUS 24.0f                 // Volts
+#define OVERMODULATION 1.2f         // 1.0 = no overmodulation
 
 #define D_INT_LIM V_BUS/(K_D*KI_D)  // Amps*samples
 #define Q_INT_LIM V_BUS/(K_Q*KI_Q)  // Amps*samples
--- a/FOC/foc.cpp	Fri May 25 16:10:46 2018 +0000
+++ b/FOC/foc.cpp	Sat Jul 14 22:03:52 2018 +0000
@@ -92,9 +92,9 @@
        
        float s = FastSin(theta); 
        float c = FastCos(theta);                            
-       //dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q);    //dq0 transform on currents
-       controller->i_d = 0.6666667f*(c*controller->i_a + (0.86602540378f*s-.5f*c)*controller->i_b + (-0.86602540378f*s-.5f*c)*controller->i_c);   ///Faster DQ0 Transform
-       controller->i_q = 0.6666667f*(-s*controller->i_a - (-0.86602540378f*c-.5f*s)*controller->i_b - (0.86602540378f*c-.5f*s)*controller->i_c);
+       dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q);    //dq0 transform on currents
+       //controller->i_d = 0.6666667f*(c*controller->i_a + (0.86602540378f*s-.5f*c)*controller->i_b + (-0.86602540378f*s-.5f*c)*controller->i_c);   ///Faster DQ0 Transform
+       //controller->i_q = 0.6666667f*(-s*controller->i_a - (-0.86602540378f*c-.5f*s)*controller->i_b - (0.86602540378f*c-.5f*s)*controller->i_c);
         
         controller->i_q_filt = 0.95f*controller->i_q_filt + 0.05f*controller->i_q;
         observer->i_d_m = controller->i_d;
@@ -136,12 +136,12 @@
        //controller->v_d = v_d_ff;
        //controller->v_q = v_q_ff; 
        
-       limit_norm(&controller->v_d, &controller->v_q, 1.0f*controller->v_bus);       // Normalize voltage vector to lie within curcle of radius v_bus
-       //abc(controller->theta_elec, controller->v_d, controller->v_q, &controller->v_u, &controller->v_v, &controller->v_w); //inverse dq0 transform on voltages
+       limit_norm(&controller->v_d, &controller->v_q, OVERMODULATION*controller->v_bus);       // Normalize voltage vector to lie within curcle of radius v_bus
+       abc(controller->theta_elec, controller->v_d, controller->v_q, &controller->v_u, &controller->v_v, &controller->v_w); //inverse dq0 transform on voltages
     
-        controller->v_u = c*controller->v_d - s*controller->v_q;                // Faster Inverse DQ0 transform
-        controller->v_v = (0.86602540378f*s-.5f*c)*controller->v_d - (-0.86602540378f*c-.5f*s)*controller->v_q;
-        controller->v_w = (-0.86602540378f*s-.5f*c)*controller->v_d - (0.86602540378f*c-.5f*s)*controller->v_q;
+        //controller->v_u = c*controller->v_d - s*controller->v_q;                // Faster Inverse DQ0 transform
+        //controller->v_v = (0.86602540378f*s-.5f*c)*controller->v_d - (-0.86602540378f*c-.5f*s)*controller->v_q;
+        //controller->v_w = (-0.86602540378f*s-.5f*c)*controller->v_d - (0.86602540378f*c-.5f*s)*controller->v_q;
        svm(controller->v_bus, controller->v_u, controller->v_v, controller->v_w, &controller->dtc_u, &controller->dtc_v, &controller->dtc_w); //space vector modulation
 
        observer->i_d_dot = 0.5f*(controller->v_d - 2.0f*(observer->i_d_est*R_PHASE - controller->dtheta_elec*L_Q*observer->i_q_est))/L_D;   //feed-forward voltage
--- a/main.cpp	Fri May 25 16:10:46 2018 +0000
+++ b/main.cpp	Sat Jul 14 22:03:52 2018 +0000
@@ -9,7 +9,7 @@
 #define SETUP_MODE 4
 #define ENCODER_MODE 5
 
-#define VERSION_NUM "1.5"
+#define VERSION_NUM "1.6"
 
 
 float __float_reg[64];                                                          // Floats stored in flash
@@ -41,7 +41,7 @@
 Serial pc(PA_2, PA_3);
 
 
-CAN          can(PB_8, PB_9);      // CAN Rx pin name, CAN Tx pin name
+CAN          can(PB_8, PB_9, 1000000);      // CAN Rx pin name, CAN Tx pin name
 CANMessage   rxMsg;
 CANMessage   txMsg;
 
@@ -83,12 +83,19 @@
 void enter_menu_state(void){
     printf("\n\r\n\r\n\r");
     printf(" Commands:\n\r");
+    wait_us(10);
     printf(" m - Motor Mode\n\r");
+    wait_us(10);
     printf(" c - Calibrate Encoder\n\r");
+    wait_us(10);
     printf(" s - Setup\n\r");
+    wait_us(10);
     printf(" e - Display Encoder\n\r");
+    wait_us(10);
     printf(" z - Set Zero Position\n\r");
+    wait_us(10);
     printf(" esc - Exit to Menu\n\r");
+    wait_us(10);
     state_change = 0;
     gpio.enable->write(0);
     gpio.led->write(0);
@@ -96,13 +103,21 @@
 
 void enter_setup_state(void){
     printf("\n\r\n\r Configuration Options \n\r\n\n");
+    wait_us(10);
     printf(" %-4s %-31s %-5s %-6s %-5s\n\r\n\r", "prefix", "parameter", "min", "max", "current value");
+    wait_us(10);
     printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "b", "Current Bandwidth (Hz)", "100", "2000", I_BW);
+    wait_us(10);
     printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "i", "CAN ID", "0", "127", CAN_ID);
+    wait_us(10);
     printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "m", "CAN Master ID", "0", "127", CAN_MASTER);
+    wait_us(10);
     printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "l", "Torque Limit (N-m)", "0.0", "18.0", TORQUE_LIMIT);
+    wait_us(10);
     printf(" %-4s %-31s %-5s %-6s %d\n\r", "t", "CAN Timeout (cycles)(0 = none)", "0", "100000", CAN_TIMEOUT);
+    wait_us(10);
     printf("\n\r To change a value, type 'prefix''value''ENTER'\n\r i.e. 'b1000''ENTER'\n\r\n\r");
+    wait_us(10);
     state_change = 0;
     }
     
@@ -327,7 +342,6 @@
     }
        
 int main() {
-    can.frequency(1000000);                                                     // set bit rate to 1Mbps
     controller.v_bus = V_BUS;
     controller.mode = 0;
     Init_All_HW(&gpio);                                                         // Setup PWM, ADC, GPIO
@@ -348,7 +362,6 @@
     
     NVIC_SetPriority(CAN1_RX0_IRQn, 3);
     can.filter(CAN_ID<<21, 0xFFE00004, CANStandard, 0);
-    //can.filter(CAN_ID, 0xF, CANStandard, 0);
                                                                     
     txMsg.id = CAN_MASTER;
     txMsg.len = 6;