for learning

Dependencies:   mbed FastPWM

Revision:
56:6f50d9d3bfee
Parent:
55:b25725257569
Child:
57:f4819de54e7a
diff -r b25725257569 -r 6f50d9d3bfee function_utilities/function_utilities.cpp
--- a/function_utilities/function_utilities.cpp	Wed Feb 26 12:51:52 2020 +0000
+++ b/function_utilities/function_utilities.cpp	Tue Mar 03 12:27:53 2020 +0000
@@ -15,13 +15,15 @@
 // Board Information
 uint8_t BNO = 0;
 uint8_t CONTROL_MODE = 0;
-uint8_t OPERATING_MODE = 0; // (00 : Moog & Rot, 01 : Moog & Lin, 10 : KNR & Rot, 11 : KNR & Lin, 101 : SW & Lin)
+uint8_t OPERATING_MODE = 0; // (00 : Moog & Rot, 01 : Moog & Lin, 10 : KNR & Rot, 11 : KNR & Lin)
 uint8_t SENSING_MODE = 0; // (0 : torque, 1: pressure)
-uint8_t CONTROL_UTILITY_MODE = 0;
 uint8_t CURRENT_CONTROL_MODE = 0; // (0 : pwm, 1 : current control)
 uint8_t FLAG_VALVE_DEADZONE = 0;
+uint8_t SETTING_SWITCH = 0;
+uint8_t SETTING_SWITCH_OLD = 0;
 uint8_t REFERENCE_MODE = 0;
 int16_t CAN_FREQ = 500;
+int CAN_FREQUENCY = 500;
 int16_t DIR_JOINT_ENC = 0;
 int16_t DIR_VALVE = 0;
 int16_t DIR_VALVE_ENC = 0;
@@ -82,8 +84,8 @@
 int16_t STROKE;
 
 
-//int16_t VALVE_LIMIT_PLUS;
-//int16_t VALVE_LIMIT_MINUS;
+int16_t VALVE_LIMIT_PLUS;
+int16_t VALVE_LIMIT_MINUS;
 
 float ENC_PULSE_PER_POSITION;
 float TORQUE_SENSOR_PULSE_PER_TORQUE;
@@ -124,6 +126,7 @@
 float CUR_TORQUE;
 float CUR_PRES_A;
 float CUR_PRES_B;
+int CUR_PWM;
 int CUR_VALVE_POSITION;
 
 unsigned int    TMR2_COUNT_LED1;
@@ -142,20 +145,25 @@
 
 int MODE_POS_FT_TRANS = 0;
 
-float CUR_CURRENT_mA = 0.0f;
-float CUR_PRES_A_BAR = 0.0f;
-float CUR_PRES_B_BAR = 0.0f;
-float CUR_TORQUE_NM = 0.0f;
-float CUR_TORQUE_NM_PRESS = 0.0f;
+////////////////////////////////////////////////////////////////////////////////
+////////////////////////////// SEUNGHOON ADD ///////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
+
+float CUR_PRES_A_BAR;
+float CUR_PRES_B_BAR;
+float CUR_TORQUE_NM;
+float CUR_TORQUE_NM_PRESS;
 
-float PRES_A_VREF = 0.0f;
-float PRES_B_VREF = 0.0f;
-float TORQUE_VREF = 0.0f;
+float PRES_A_VREF;
+float PRES_A_VREF_TEST;
+float PRES_B_VREF;
+float PRES_B_VREF_TEST;
+float TORQUE_VREF;
 
-float VALVE_PWM_RAW_FB = 0.0f;
-float VALVE_PWM_RAW_FF = 0.0f;
-float VALVE_PWM_RAW = 0.0f;
-int VALVE_PWM_VALVE_DZ = 0;
+float VALVE_PWM_RAW_FB;
+float VALVE_PWM_RAW_FF;
+int VALVE_PWM_VALVE_DZ;
+int VALVE_INPUT_PWM;
 
 float VALVE_GAIN_LPM_PER_V[10];
 float VALVE_POS_VS_PWM[25];
@@ -163,25 +171,34 @@
 
 int VALVE_MAX_POS;
 int VALVE_MIN_POS;
+float DDV_CENTER;
 int VALVE_POS_NUM;
 float VALVE_CENTER_OFFSET;
 float VALVE_DZ_MINUS_OFFSET;
 float VALVE_DZ_PLUS_OFFSET;
+int VALVE_CENTER_OFFSET_times10;
 
-int TMR3_COUNT_FINDHOME = 0;
-int TMR3_COUNT_FLOWRATE = 0;
-int TMR3_COUNT_DEADZONE = 0;
-int TMR3_COUNT_PRES_NULL = 0;
-int TMR3_COUNT_TORQUE_NULL = 0;
-int TMR3_COUNT_PRES_CALIB = 0;
-int TMR3_COUNT_REFERENCE = 0;
-int TMR3_COUNT_JOINT = 0;
-int TMR3_COUNT_ROTARY_FRIC_TUNE = 0;
+int TMR3_COUNT_FINDHOME;
+int TMR3_COUNT_FLOWRATE;
+int TMR3_COUNT_DEADZONE;
+int TMR3_COUNT_PRES_NULL;
+int TMR3_COUNT_TORQUE_NULL;
+int TMR3_COUNT_PRES_CALIB;
+int TMR3_COUNT_REFERENCE;
+int TMR3_COUNT_JOINT;
+int TMR3_COUNT_ROTARY_FRIC_TUNE;
 
-float TUNING_TIME = 600.0f;  // sec
+bool FLAG_REFERENCE_VALVE_PWM;
+bool FLAG_REFERENCE_VALVE_POSITION;
+bool FLAG_REFERENCE_JOINT_POSITION;
+bool FLAG_REFERENCE_JOINT_TORQUE;
+bool FLAG_REFERENCE_PRES_DIFF;
+bool FLAG_REFERENCE_CURRENT;
 
-float REFERENCE_FREQ = 1.0f;
-float REFERENCE_MAG = 0.0f;
+float TUNING_TIME;
+
+float REFERENCE_FREQ;
+float REFERENCE_MAG;
 
 bool FLAG_FIND_HOME;
 
@@ -264,8 +281,6 @@
 int fl_temp_cnt2 = 0;
 int cur_vel_sum = 0;
 
-float Cur_Valve_Open_pulse = 0.0f;
-
 // find home
 int CUR_VELOCITY_OLD = 0;
 int cnt_findhome = 0;
@@ -284,20 +299,11 @@
 
 float freq_fric_tune = 1.0f;
 
+bool FLAG_VALVE_OUTPUT_CALIB = false;
+
 uint32_t TMR3_COUNT_CAN_TX = 0;
 
-// Current Control Variables
-double I_REF = 0.0f;
-double I_REF_fil = 0.0f;
-double I_ERR = 0.0f;
-double I_ERR_INT = 0.0f;
-double I_REF_fil_old = 0.0f;
-double I_REF_fil_diff = 0.0f;
-
-// system id
-int cnt_sysid = 0;
-double freq_sysid_Iref = 0.0f;
-
+float I_REF = 0.0f;
 int TMR3_COUNT_IREF = 0;
 float CUR_CURRENT = 0.0f;
 float u_CUR[3] = {0.0f,0.0f,0.0f};
@@ -308,20 +314,6 @@
 
 float alpha_trans = 0.0f;
 
-float V_out=0.0f;
-float V_rem=0.0f; // for anti-windup
-float V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV
-
-float PWM_out=0.0f;
-
-double K_v = 0.0f; // valve flowrate gain
-double mV_PER_mA = 600.0f; // current >> voltage
-double mV_PER_pulse = 0.6f; // pulse >> voltage
-double mA_PER_pulse = 0.001f; // pulse >> current
-
-int timer_while = 0;
-int while_index = 0;
-
 //int h1, h2, h3, h4, h5, h6;
 
 /*******************************************************************************
@@ -408,8 +400,8 @@
     writer.write(RID_ENC_LIMIT_MINUS,(int) ENC_LIMIT_MINUS);
     writer.write(RID_ENC_LIMIT_PLUS,(int) ENC_LIMIT_PLUS);
     writer.write(RID_STROKE,(int) STROKE);
-    //writer.write(RID_VALVE_LIMIT_MINUS,(int) VALVE_LIMIT_MINUS);
-    //writer.write(RID_VALVE_LIMIT_PLUS,(int) VALVE_LIMIT_PLUS);
+    writer.write(RID_VALVE_LIMIT_MINUS,(int) VALVE_LIMIT_MINUS);
+    writer.write(RID_VALVE_LIMIT_PLUS,(int) VALVE_LIMIT_PLUS);
     writer.write(RID_ENC_PULSE_PER_POSITION,(int) (ENC_PULSE_PER_POSITION*10.0f));
     writer.write(RID_TORQUE_SENSOR_PULSE_PER_TORQUE,(int) (TORQUE_SENSOR_PULSE_PER_TORQUE * 100.0f));
     writer.write(RID_PRES_SENSOR_A_PULSE_PER_BAR,(int) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f));
@@ -439,7 +431,7 @@
     }
     writer.write(RID_VALVE_MAX_POS, (int) VALVE_MAX_POS);
     writer.write(RID_VALVE_MIN_POS, (int) VALVE_MIN_POS);
-    //writer.write(RID_DDV_CENTER, (int) (DDV_CENTER * 10.0f));
+    writer.write(RID_DDV_CENTER, (int) (DDV_CENTER * 10.0f));
     writer.write(RID_VALVE_POS_NUM, (int) VALVE_POS_NUM);
     
     writer.write(RID_K_SPRING, (int) K_SPRING);
@@ -452,7 +444,6 @@
 void ROM_CALL_DATA(void)
 {
     BNO = flashReadInt(Rom_Sector, RID_BNO);
-    BNO = 0;
     OPERATING_MODE = flashReadInt(Rom_Sector, RID_OPERATING_MODE);
     SENSING_MODE = flashReadInt(Rom_Sector, RID_SENSING_MODE);
     CURRENT_CONTROL_MODE = flashReadInt(Rom_Sector, RID_CURRENT_CONTROL_MODE);
@@ -489,8 +480,8 @@
     ENC_LIMIT_MINUS = flashReadInt(Rom_Sector, RID_ENC_LIMIT_MINUS);
     ENC_LIMIT_PLUS = flashReadInt(Rom_Sector, RID_ENC_LIMIT_PLUS);
     STROKE = flashReadInt(Rom_Sector, RID_STROKE);
-    //VALVE_LIMIT_MINUS = flashReadInt(Rom_Sector, RID_VALVE_LIMIT_MINUS);
-    //VALVE_LIMIT_PLUS = flashReadInt(Rom_Sector, RID_VALVE_LIMIT_PLUS);
+    VALVE_LIMIT_MINUS = flashReadInt(Rom_Sector, RID_VALVE_LIMIT_MINUS);
+    VALVE_LIMIT_PLUS = flashReadInt(Rom_Sector, RID_VALVE_LIMIT_PLUS);
     ENC_PULSE_PER_POSITION = (float) (flashReadInt(Rom_Sector, RID_ENC_PULSE_PER_POSITION)) * 0.1f;
 //    ENC_PULSE_PER_POSITION = (float) 1024.0f;
     TORQUE_SENSOR_PULSE_PER_TORQUE = (float) (flashReadInt(Rom_Sector, RID_TORQUE_SENSOR_PULSE_PER_TORQUE)) * 0.01f;
@@ -524,7 +515,7 @@
     }
     VALVE_MAX_POS = flashReadInt(Rom_Sector, RID_VALVE_MAX_POS);
     VALVE_MIN_POS = flashReadInt(Rom_Sector, RID_VALVE_MIN_POS);
-    //DDV_CENTER = (float) (flashReadInt(Rom_Sector, RID_DDV_CENTER)) * 0.1f;
+    DDV_CENTER = (float) (flashReadInt(Rom_Sector, RID_DDV_CENTER)) * 0.1f;
     VALVE_POS_NUM = flashReadInt(Rom_Sector, RID_VALVE_POS_NUM);
     
     K_SPRING = flashReadInt(Rom_Sector, RID_K_SPRING);
@@ -561,8 +552,12 @@
     ENC_pos_cur = spi_enc_read();
     ENC_pos_diff = ENC_pos_cur - ENC_pos_old;
 
-    //Kalman Filter
-//    ENC_VEL_RAW = (int32_t) (ENC_pos_diff * TMR_FREQ_5k);
+//    if (ENC_pos_diff > 1300 || ENC_pos_diff<-1300) {
+//        //MOT_E_STOP(0);
+//    }
+
+    ENC_VEL_RAW = (int32_t) (ENC_pos_diff * TMR_FREQ_5k);
+
 //    KF_Y_11 = ENC_pos_cur;
 //    KF_Y_21 = ENC_VEL_RAW;
 //    KF_X_11 = KF_G1_11 * KF_X_11 + KF_G1_12 * KF_X_21 + KF_G2_11 * KF_Y_11 + KF_G2_12*KF_Y_21;
@@ -570,17 +565,19 @@
 //    ENC_VEL_KF = (int32_t) KF_X_21;
 //
 //    pos.sen = (DIR_JOINT_ENC) * ENC_pos_cur + enc_offset;
+//    //    CUR_POSITION = (DIR_JOINT_ENC) * ENC_pos_cur;
 //    vel.sen = (DIR_JOINT_ENC) * ENC_VEL_KF;
-    
-    //Low Pass Filter
-    
+
+    //    CUR_POSITION = ENC_pos_cur;
+    //    CUR_VELOCITY = ENC_VEL_KF;
+
     double NEW_POSITION = (double) ((DIR_JOINT_ENC) * ENC_pos_cur + enc_offset);
-    double NEW_VELOCITY = (double) ((DIR_JOINT_ENC) * ENC_pos_diff * (int) FREQ_10k);
+    double NEW_VELOCITY = (double) ((DIR_JOINT_ENC) * ENC_pos_diff * (int) FREQ_5k);
     
-    double alpha_update_pos = 1.0f/(1.0f + FREQ_10k/(2.0f*3.14f*100.0f));
+    double alpha_update_pos = 1.0f/(1.0f + FREQ_5k/(2.0f*3.14f*50.0f));
     pos.sen = NEW_POSITION;
-    vel.sen = (1.0f - alpha_update_pos) * vel.sen + alpha_update_pos * NEW_VELOCITY; // pulse/s
-
+    vel.sen = (1.0f - alpha_update_pos) * vel.sen + alpha_update_pos * NEW_VELOCITY;    
+        
     ENC_pos_old = ENC_pos_cur;
 }
 
@@ -604,4 +601,3 @@
 
 
 
-