Sungwoo Kim
/
HydraulicControlBoard_PostLIGHT_Original
Original Version of STM Board
function_utilities/function_utilities.cpp@14:8e7590227d22, 2019-08-29 (annotated)
- Committer:
- Lightvalve
- Date:
- Thu Aug 29 07:38:00 2019 +0000
- Revision:
- 14:8e7590227d22
- Parent:
- 13:747daba9cf59
- Child:
- 15:bd0d12728506
190829
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lightvalve | 11:82d8768d7351 | 1 | #include "setting.h" |
Lightvalve | 11:82d8768d7351 | 2 | #include "SPI_EEP_ENC.h" |
Lightvalve | 11:82d8768d7351 | 3 | #include "function_utilities.h" |
Lightvalve | 14:8e7590227d22 | 4 | #include "function_CAN.h" |
Lightvalve | 11:82d8768d7351 | 5 | |
Lightvalve | 11:82d8768d7351 | 6 | /******************************************************************************* |
Lightvalve | 11:82d8768d7351 | 7 | * VARIABLE |
Lightvalve | 11:82d8768d7351 | 8 | ******************************************************************************/ |
Lightvalve | 11:82d8768d7351 | 9 | |
Lightvalve | 11:82d8768d7351 | 10 | // Board Information |
Lightvalve | 11:82d8768d7351 | 11 | uint8_t BNO = 0; |
Lightvalve | 11:82d8768d7351 | 12 | uint8_t CONTROL_MODE = 0; |
Lightvalve | 11:82d8768d7351 | 13 | uint8_t OPERATING_MODE = 0; |
Lightvalve | 11:82d8768d7351 | 14 | uint8_t SETTING_SWITCH = 0; |
Lightvalve | 11:82d8768d7351 | 15 | uint8_t SETTING_SWITCH_OLD = 0; |
Lightvalve | 11:82d8768d7351 | 16 | uint8_t REFERENCE_MODE = 0; |
Lightvalve | 14:8e7590227d22 | 17 | uint16_t CAN_FREQ = 500; |
Lightvalve | 11:82d8768d7351 | 18 | uint8_t DIR_JOINT_ENC = 0; |
Lightvalve | 11:82d8768d7351 | 19 | uint8_t DIR_VALVE = 0; |
Lightvalve | 11:82d8768d7351 | 20 | uint8_t DIR_VALVE_ENC = 0; |
Lightvalve | 11:82d8768d7351 | 21 | |
Lightvalve | 11:82d8768d7351 | 22 | double SUPPLY_VOLTAGE = 12.0; |
Lightvalve | 11:82d8768d7351 | 23 | double VALVE_VOLTAGE_LIMIT = 5.0; |
Lightvalve | 11:82d8768d7351 | 24 | |
Lightvalve | 11:82d8768d7351 | 25 | double P_GAIN_VALVE_POSITION = 0.0; |
Lightvalve | 11:82d8768d7351 | 26 | double I_GAIN_VALVE_POSITION= 0.0; |
Lightvalve | 11:82d8768d7351 | 27 | double D_GAIN_VALVE_POSITION= 0.0; |
Lightvalve | 11:82d8768d7351 | 28 | double P_GAIN_JOINT_POSITION = 0.0; |
Lightvalve | 11:82d8768d7351 | 29 | double I_GAIN_JOINT_POSITION = 0.0; |
Lightvalve | 11:82d8768d7351 | 30 | double D_GAIN_JOINT_POSITION = 0.0; |
Lightvalve | 11:82d8768d7351 | 31 | double P_GAIN_JOINT_TORQUE = 0.0; |
Lightvalve | 11:82d8768d7351 | 32 | double I_GAIN_JOINT_TORQUE = 0.0; |
Lightvalve | 11:82d8768d7351 | 33 | double D_GAIN_JOINT_TORQUE = 0.0; |
Lightvalve | 11:82d8768d7351 | 34 | |
Lightvalve | 11:82d8768d7351 | 35 | int16_t VALVE_DEADZONE_PLUS; |
Lightvalve | 11:82d8768d7351 | 36 | int16_t VALVE_DEADZONE_MINUS; |
Lightvalve | 11:82d8768d7351 | 37 | |
Lightvalve | 11:82d8768d7351 | 38 | int16_t VELOCITY_COMP_GAIN; |
Lightvalve | 11:82d8768d7351 | 39 | int16_t COMPLIANCE_GAIN; |
Lightvalve | 11:82d8768d7351 | 40 | |
Lightvalve | 11:82d8768d7351 | 41 | int16_t VALVE_CENTER; |
Lightvalve | 11:82d8768d7351 | 42 | |
Lightvalve | 11:82d8768d7351 | 43 | int16_t VALVE_FF; |
Lightvalve | 11:82d8768d7351 | 44 | |
Lightvalve | 11:82d8768d7351 | 45 | int16_t BULK_MODULUS; |
Lightvalve | 11:82d8768d7351 | 46 | |
Lightvalve | 11:82d8768d7351 | 47 | int16_t CHAMBER_VOLUME_A; |
Lightvalve | 11:82d8768d7351 | 48 | int16_t CHAMBER_VOLUME_B; |
Lightvalve | 11:82d8768d7351 | 49 | |
Lightvalve | 11:82d8768d7351 | 50 | int16_t PISTON_AREA_A; |
Lightvalve | 11:82d8768d7351 | 51 | int16_t PISTON_AREA_B; |
Lightvalve | 11:82d8768d7351 | 52 | double PISTON_AREA_alpha; |
Lightvalve | 11:82d8768d7351 | 53 | |
Lightvalve | 11:82d8768d7351 | 54 | |
Lightvalve | 11:82d8768d7351 | 55 | int16_t PRES_SUPPLY; |
Lightvalve | 11:82d8768d7351 | 56 | int16_t PRES_RETURN; |
Lightvalve | 11:82d8768d7351 | 57 | |
Lightvalve | 11:82d8768d7351 | 58 | int16_t ENC_LIMIT_PLUS; |
Lightvalve | 11:82d8768d7351 | 59 | int16_t ENC_LIMIT_MINUS; |
Lightvalve | 11:82d8768d7351 | 60 | |
Lightvalve | 11:82d8768d7351 | 61 | int16_t STROKE; |
Lightvalve | 11:82d8768d7351 | 62 | |
Lightvalve | 11:82d8768d7351 | 63 | |
Lightvalve | 11:82d8768d7351 | 64 | int16_t VALVE_LIMIT_PLUS; |
Lightvalve | 11:82d8768d7351 | 65 | int16_t VALVE_LIMIT_MINUS; |
Lightvalve | 11:82d8768d7351 | 66 | |
Lightvalve | 11:82d8768d7351 | 67 | int16_t ENC_PULSE_PER_POSITION; |
Lightvalve | 11:82d8768d7351 | 68 | int16_t TORQUE_SENSOR_PULSE_PER_TORQUE; |
Lightvalve | 14:8e7590227d22 | 69 | double PRES_SENSOR_A_PULSE_PER_BAR = 4096.0 / 200.0; |
Lightvalve | 14:8e7590227d22 | 70 | double PRES_SENSOR_B_PULSE_PER_BAR = 4096.0 / 200.0; |
Lightvalve | 11:82d8768d7351 | 71 | |
Lightvalve | 11:82d8768d7351 | 72 | int HOMEPOS_OFFSET; |
Lightvalve | 11:82d8768d7351 | 73 | int HOMEPOS_VALVE_OPENING; |
Lightvalve | 11:82d8768d7351 | 74 | |
Lightvalve | 11:82d8768d7351 | 75 | double FRICTION; |
Lightvalve | 11:82d8768d7351 | 76 | double REF_PERIOD; |
Lightvalve | 11:82d8768d7351 | 77 | double REF_MAG; |
Lightvalve | 11:82d8768d7351 | 78 | int REF_NUM; |
Lightvalve | 11:82d8768d7351 | 79 | |
Lightvalve | 11:82d8768d7351 | 80 | |
Lightvalve | 11:82d8768d7351 | 81 | double DAC_REF; |
Lightvalve | 11:82d8768d7351 | 82 | double DAC_RESOL; |
Lightvalve | 11:82d8768d7351 | 83 | double PWM_RESOL; |
Lightvalve | 11:82d8768d7351 | 84 | |
Lightvalve | 11:82d8768d7351 | 85 | int REF_POSITION; |
Lightvalve | 11:82d8768d7351 | 86 | int REF_VELOCITY; |
Lightvalve | 11:82d8768d7351 | 87 | int16_t REF_TORQUE; |
Lightvalve | 11:82d8768d7351 | 88 | int16_t REF_PRES_DIFF; |
Lightvalve | 11:82d8768d7351 | 89 | int16_t REF_PWM; |
Lightvalve | 11:82d8768d7351 | 90 | int16_t REF_VALVE_POSITION; |
Lightvalve | 14:8e7590227d22 | 91 | int16_t REF_CURRENT; |
Lightvalve | 11:82d8768d7351 | 92 | |
Lightvalve | 11:82d8768d7351 | 93 | int REF_MOVE_TIME_5k; |
Lightvalve | 11:82d8768d7351 | 94 | int INIT_REF_PWM; |
Lightvalve | 11:82d8768d7351 | 95 | int INIT_REF_VALVE_POS; |
Lightvalve | 11:82d8768d7351 | 96 | int INIT_REF_POS; |
Lightvalve | 11:82d8768d7351 | 97 | int INIT_REF_VEL; |
Lightvalve | 11:82d8768d7351 | 98 | int INIT_REF_TORQUE; |
Lightvalve | 11:82d8768d7351 | 99 | int INIT_REF_PRES_DIFF; |
Lightvalve | 14:8e7590227d22 | 100 | int INIT_REF_CURRENT; |
Lightvalve | 11:82d8768d7351 | 101 | |
Lightvalve | 11:82d8768d7351 | 102 | int CUR_POSITION; |
Lightvalve | 11:82d8768d7351 | 103 | int CUR_VELOCITY; |
Lightvalve | 11:82d8768d7351 | 104 | double CUR_TORQUE; |
Lightvalve | 11:82d8768d7351 | 105 | double CUR_PRES_A; |
Lightvalve | 11:82d8768d7351 | 106 | double CUR_PRES_B; |
Lightvalve | 11:82d8768d7351 | 107 | int CUR_PWM; |
Lightvalve | 11:82d8768d7351 | 108 | int CUR_VALVE_POSITION; |
Lightvalve | 11:82d8768d7351 | 109 | |
Lightvalve | 11:82d8768d7351 | 110 | unsigned int TMR2_COUNT_LED1; |
Lightvalve | 11:82d8768d7351 | 111 | unsigned int TMR2_COUNT_LED2; |
Lightvalve | 11:82d8768d7351 | 112 | unsigned int TMR2_COUNT_CAN_TX; |
Lightvalve | 11:82d8768d7351 | 113 | |
Lightvalve | 11:82d8768d7351 | 114 | int num_err; |
Lightvalve | 11:82d8768d7351 | 115 | int flag_err[8]; |
Lightvalve | 11:82d8768d7351 | 116 | int flag_err_old[8]; |
Lightvalve | 11:82d8768d7351 | 117 | int flag_err_rt; |
Lightvalve | 11:82d8768d7351 | 118 | |
Lightvalve | 11:82d8768d7351 | 119 | int flag_ref_enable; |
Lightvalve | 11:82d8768d7351 | 120 | |
Lightvalve | 11:82d8768d7351 | 121 | int flag_data_request[5]; |
Lightvalve | 11:82d8768d7351 | 122 | |
Lightvalve | 11:82d8768d7351 | 123 | //////////////////////////////////////////////////////////////////////////////// |
Lightvalve | 11:82d8768d7351 | 124 | ////////////////////////////// SEUNGHOON ADD /////////////////////////////////// |
Lightvalve | 11:82d8768d7351 | 125 | //////////////////////////////////////////////////////////////////////////////// |
Lightvalve | 11:82d8768d7351 | 126 | |
Lightvalve | 11:82d8768d7351 | 127 | double CUR_PRES_A_BAR; |
Lightvalve | 11:82d8768d7351 | 128 | double CUR_PRES_B_BAR; |
Lightvalve | 11:82d8768d7351 | 129 | double CUR_TORQUE_NM; |
Lightvalve | 11:82d8768d7351 | 130 | double CUR_TORQUE_NM_PRESS; |
Lightvalve | 11:82d8768d7351 | 131 | |
Lightvalve | 11:82d8768d7351 | 132 | double PRES_A_VREF; |
Lightvalve | 11:82d8768d7351 | 133 | double PRES_B_VREF; |
Lightvalve | 11:82d8768d7351 | 134 | double TORQUE_VREF; |
Lightvalve | 11:82d8768d7351 | 135 | |
Lightvalve | 11:82d8768d7351 | 136 | double VALVE_PWM_RAW; |
Lightvalve | 11:82d8768d7351 | 137 | double VALVE_PWM_RAW_FB; |
Lightvalve | 11:82d8768d7351 | 138 | double VALVE_PWM_RAW_FF; |
Lightvalve | 11:82d8768d7351 | 139 | int VALVE_PWM_VALVE_DZ; |
Lightvalve | 11:82d8768d7351 | 140 | int VALVE_INPUT_PWM; |
Lightvalve | 11:82d8768d7351 | 141 | |
Lightvalve | 11:82d8768d7351 | 142 | double VALVE_GAIN_LPM_PER_V[10]; |
Lightvalve | 11:82d8768d7351 | 143 | double VALVE_POS_VS_PWM[18]; |
Lightvalve | 11:82d8768d7351 | 144 | long JOINT_VEL[100]; |
Lightvalve | 11:82d8768d7351 | 145 | |
Lightvalve | 11:82d8768d7351 | 146 | int VALVE_MAX_POS; |
Lightvalve | 11:82d8768d7351 | 147 | int VALVE_MIN_POS; |
Lightvalve | 11:82d8768d7351 | 148 | int DDV_CENTER; |
Lightvalve | 11:82d8768d7351 | 149 | int VALVE_POS_NUM; |
Lightvalve | 11:82d8768d7351 | 150 | |
Lightvalve | 14:8e7590227d22 | 151 | int TMR3_COUNT_FINDHOME; |
Lightvalve | 14:8e7590227d22 | 152 | int TMR3_COUNT_FLOWRATE; |
Lightvalve | 14:8e7590227d22 | 153 | int TMR3_COUNT_DEADZONE; |
Lightvalve | 14:8e7590227d22 | 154 | int TMR3_COUNT_PRES_NULL; |
Lightvalve | 14:8e7590227d22 | 155 | int TMR3_COUNT_TORQUE_NULL; |
Lightvalve | 14:8e7590227d22 | 156 | int TMR3_COUNT_PRES_CALIB; |
Lightvalve | 14:8e7590227d22 | 157 | int TMR3_COUNT_REFERENCE; |
Lightvalve | 14:8e7590227d22 | 158 | int TMR3_COUNT_JOINT; |
Lightvalve | 14:8e7590227d22 | 159 | int TMR3_COUNT_ROTARY_FRIC_TUNE; |
Lightvalve | 11:82d8768d7351 | 160 | |
Lightvalve | 11:82d8768d7351 | 161 | bool FLAG_REFERENCE_VALVE_PWM; |
Lightvalve | 11:82d8768d7351 | 162 | bool FLAG_REFERENCE_VALVE_POSITION; |
Lightvalve | 11:82d8768d7351 | 163 | bool FLAG_REFERENCE_JOINT_POSITION; |
Lightvalve | 11:82d8768d7351 | 164 | bool FLAG_REFERENCE_JOINT_TORQUE; |
Lightvalve | 11:82d8768d7351 | 165 | bool FLAG_REFERENCE_PRES_DIFF; |
Lightvalve | 14:8e7590227d22 | 166 | bool FLAG_REFERENCE_CURRENT; |
Lightvalve | 11:82d8768d7351 | 167 | |
Lightvalve | 11:82d8768d7351 | 168 | double TUNING_TIME; |
Lightvalve | 11:82d8768d7351 | 169 | |
Lightvalve | 11:82d8768d7351 | 170 | double REFERENCE_FREQ; |
Lightvalve | 11:82d8768d7351 | 171 | double REFERENCE_MAG; |
Lightvalve | 11:82d8768d7351 | 172 | |
Lightvalve | 11:82d8768d7351 | 173 | bool FLAG_FIND_HOME; |
Lightvalve | 11:82d8768d7351 | 174 | |
Lightvalve | 11:82d8768d7351 | 175 | int MODE_JUMP_STATUS; |
Lightvalve | 11:82d8768d7351 | 176 | enum _JUMP_STATUS { |
Lightvalve | 11:82d8768d7351 | 177 | JUMP_NO_ACT = 0, //0 |
Lightvalve | 11:82d8768d7351 | 178 | JUMP_START, //1 |
Lightvalve | 11:82d8768d7351 | 179 | JUMP_TAKEOFF, //2 |
Lightvalve | 11:82d8768d7351 | 180 | JUMP_FLYING, //3 |
Lightvalve | 11:82d8768d7351 | 181 | JUMP_LANDING, //4 |
Lightvalve | 11:82d8768d7351 | 182 | }; |
Lightvalve | 11:82d8768d7351 | 183 | |
Lightvalve | 13:747daba9cf59 | 184 | double CUR_PRES_DIFF_BAR = 0.0; |
Lightvalve | 13:747daba9cf59 | 185 | double CUR_PRES_A_sum = 0.0; |
Lightvalve | 13:747daba9cf59 | 186 | double CUR_PRES_B_sum = 0.0; |
Lightvalve | 13:747daba9cf59 | 187 | double CUR_PRES_A_mean = 0.0; |
Lightvalve | 13:747daba9cf59 | 188 | double CUR_PRES_B_mean = 0.0; |
Lightvalve | 13:747daba9cf59 | 189 | double CUR_TORQUE_sum = 0.0; |
Lightvalve | 13:747daba9cf59 | 190 | double CUR_TORQUE_mean = 0.0; |
Lightvalve | 13:747daba9cf59 | 191 | double PRES_A_NULL = 200.0; |
Lightvalve | 13:747daba9cf59 | 192 | double PRES_B_NULL = 200.0; |
Lightvalve | 13:747daba9cf59 | 193 | double TORQUE_NULL = 3900; |
Lightvalve | 11:82d8768d7351 | 194 | |
Lightvalve | 13:747daba9cf59 | 195 | double Ref_Valve_Pos_Old = 0.0; |
Lightvalve | 11:82d8768d7351 | 196 | |
Lightvalve | 13:747daba9cf59 | 197 | int VALVE_ID_timer = 0; |
Lightvalve | 13:747daba9cf59 | 198 | int VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 199 | int VALVE_FR_timer = 0; |
Lightvalve | 13:747daba9cf59 | 200 | int VALVE_POS_TMP = 0; |
Lightvalve | 13:747daba9cf59 | 201 | int JOINT_VEL_TMP = 0; |
Lightvalve | 13:747daba9cf59 | 202 | int DDV_POS_AVG = 0; |
Lightvalve | 13:747daba9cf59 | 203 | int VALVE_POS_AVG[100] = {0}; |
Lightvalve | 13:747daba9cf59 | 204 | int VALVE_POS_AVG_OLD = 0; |
Lightvalve | 13:747daba9cf59 | 205 | int data_num = 0; |
Lightvalve | 13:747daba9cf59 | 206 | int ID_index = 0; |
Lightvalve | 13:747daba9cf59 | 207 | int DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 208 | int ID_index_array[100] = {0}; |
Lightvalve | 13:747daba9cf59 | 209 | int FR_index_array[100] = {0}; |
Lightvalve | 13:747daba9cf59 | 210 | int first_check = 0; |
Lightvalve | 13:747daba9cf59 | 211 | double init_time = 0.0; |
Lightvalve | 13:747daba9cf59 | 212 | int VEL_POINT = 0; |
Lightvalve | 13:747daba9cf59 | 213 | int DZ_case = 0; |
Lightvalve | 13:747daba9cf59 | 214 | int START_POS = 0; |
Lightvalve | 13:747daba9cf59 | 215 | int FINAL_POS = 0; |
Lightvalve | 13:747daba9cf59 | 216 | int DZ_DIRECTION = 0; |
Lightvalve | 13:747daba9cf59 | 217 | int FIRST_DZ = 0; |
Lightvalve | 13:747daba9cf59 | 218 | int SECOND_DZ = 0; |
Lightvalve | 13:747daba9cf59 | 219 | int DZ_NUM = 0; |
Lightvalve | 13:747daba9cf59 | 220 | int one_period_end = 0; |
Lightvalve | 13:747daba9cf59 | 221 | double Ref_Vel_Test = 0.0; |
Lightvalve | 13:747daba9cf59 | 222 | long TMR2_FOR_SLOW_LOGGING = 0; |
Lightvalve | 13:747daba9cf59 | 223 | int velcount = 0; |
Lightvalve | 13:747daba9cf59 | 224 | char max_check = 0; |
Lightvalve | 13:747daba9cf59 | 225 | char min_check = 0; |
Lightvalve | 13:747daba9cf59 | 226 | |
Lightvalve | 13:747daba9cf59 | 227 | double valve_pos_err = 0.0, valve_pos_err_old = 0.0, valve_pos_err_diff = 0.0, valve_pos_err_sum = 0.0; |
Lightvalve | 14:8e7590227d22 | 228 | double joint_pos_err = 0.0, joint_pos_err_old = 0.0, joint_pos_err_diff = 0.0, joint_pos_err_diff_fil = 0.0, joint_pos_err_sum = 0.0; |
Lightvalve | 13:747daba9cf59 | 229 | double joint_torq_err = 0.0, joint_torq_err_old = 0.0, joint_torq_err_diff = 0.0, joint_torq_err_sum = 0.0; |
Lightvalve | 13:747daba9cf59 | 230 | double VALVE_PWM_RAW_POS = 0.0, VALVE_PWM_RAW_TORQ = 0.0; |
Lightvalve | 13:747daba9cf59 | 231 | |
Lightvalve | 13:747daba9cf59 | 232 | double CUR_FLOWRATE = 0.0; |
Lightvalve | 13:747daba9cf59 | 233 | double VALVE_FF_VOLTAGE = 0.0; |
Lightvalve | 13:747daba9cf59 | 234 | |
Lightvalve | 13:747daba9cf59 | 235 | int pos_plus_end = 0; |
Lightvalve | 13:747daba9cf59 | 236 | int pos_minus_end = 0; |
Lightvalve | 13:747daba9cf59 | 237 | |
Lightvalve | 13:747daba9cf59 | 238 | bool need_enc_init = false; |
Lightvalve | 13:747daba9cf59 | 239 | |
Lightvalve | 13:747daba9cf59 | 240 | int temp_time = 0; |
Lightvalve | 13:747daba9cf59 | 241 | |
Lightvalve | 13:747daba9cf59 | 242 | double CUR_VELOCITY_sum = 0.0; |
Lightvalve | 13:747daba9cf59 | 243 | double temp_vel_sum = 0.0; |
Lightvalve | 13:747daba9cf59 | 244 | |
Lightvalve | 13:747daba9cf59 | 245 | int DZ_dir = 0; |
Lightvalve | 13:747daba9cf59 | 246 | int DZ_temp_cnt = 0; |
Lightvalve | 13:747daba9cf59 | 247 | int DZ_temp_cnt2 = 0; |
Lightvalve | 13:747daba9cf59 | 248 | int DZ_end = 2; |
Lightvalve | 13:747daba9cf59 | 249 | int flag_flowrate = 0; |
Lightvalve | 13:747daba9cf59 | 250 | int fl_temp_cnt = 0; |
Lightvalve | 13:747daba9cf59 | 251 | int fl_temp_cnt2 = 0; |
Lightvalve | 13:747daba9cf59 | 252 | int cur_vel_sum = 0; |
Lightvalve | 13:747daba9cf59 | 253 | |
Lightvalve | 13:747daba9cf59 | 254 | // find home |
Lightvalve | 13:747daba9cf59 | 255 | int CUR_VELOCITY_OLD = 0; |
Lightvalve | 13:747daba9cf59 | 256 | int cnt_findhome = 0; |
Lightvalve | 13:747daba9cf59 | 257 | int cnt_vel_findhome = 0; |
Lightvalve | 13:747daba9cf59 | 258 | int FINDHOME_VELOCITY = 0; |
Lightvalve | 13:747daba9cf59 | 259 | int FINDHOME_VELOCITY_OLD = 0; |
Lightvalve | 13:747daba9cf59 | 260 | int FINDHOME_POSITION = 0; |
Lightvalve | 13:747daba9cf59 | 261 | int FINDHOME_POSITION_OLD = 0; |
Lightvalve | 13:747daba9cf59 | 262 | |
Lightvalve | 13:747daba9cf59 | 263 | // valve gain |
Lightvalve | 13:747daba9cf59 | 264 | int check_vel_pos_init = 0; |
Lightvalve | 13:747daba9cf59 | 265 | int check_vel_pos_fin = 0; |
Lightvalve | 13:747daba9cf59 | 266 | int check_vel_pos_interv = 0; |
Lightvalve | 13:747daba9cf59 | 267 | int valve_gain_repeat_cnt = 0; |
Lightvalve | 13:747daba9cf59 | 268 | double VALVE_VOLTAGE = 0.0; |
Lightvalve | 13:747daba9cf59 | 269 | |
Lightvalve | 13:747daba9cf59 | 270 | double freq_fric_tune = 1.0; |
Lightvalve | 13:747daba9cf59 | 271 | |
Lightvalve | 13:747daba9cf59 | 272 | bool FLAG_VALVE_OUTPUT_CALIB = false; |
Lightvalve | 11:82d8768d7351 | 273 | |
Lightvalve | 14:8e7590227d22 | 274 | uint32_t TMR3_COUNT_CAN_TX = 0; |
Lightvalve | 14:8e7590227d22 | 275 | |
Lightvalve | 14:8e7590227d22 | 276 | double I_REF = 0.0; |
Lightvalve | 14:8e7590227d22 | 277 | int TMR3_COUNT_IREF = 0; |
Lightvalve | 14:8e7590227d22 | 278 | double CUR_CURRENT = 0.0; |
Lightvalve | 14:8e7590227d22 | 279 | double u_CUR[3] = {0.0,0.0,0.0}; |
Lightvalve | 14:8e7590227d22 | 280 | |
Lightvalve | 11:82d8768d7351 | 281 | /******************************************************************************* |
Lightvalve | 11:82d8768d7351 | 282 | * General math functions |
Lightvalve | 11:82d8768d7351 | 283 | ******************************************************************************/ |
Lightvalve | 11:82d8768d7351 | 284 | |
Lightvalve | 11:82d8768d7351 | 285 | |
Lightvalve | 11:82d8768d7351 | 286 | double dabs(double tx) { |
Lightvalve | 11:82d8768d7351 | 287 | if (tx >= 0.0) |
Lightvalve | 11:82d8768d7351 | 288 | return tx; |
Lightvalve | 11:82d8768d7351 | 289 | else |
Lightvalve | 11:82d8768d7351 | 290 | return -tx; |
Lightvalve | 11:82d8768d7351 | 291 | } |
Lightvalve | 11:82d8768d7351 | 292 | |
Lightvalve | 11:82d8768d7351 | 293 | double change_int_to_efloat(int input) { |
Lightvalve | 11:82d8768d7351 | 294 | int i = 0; |
Lightvalve | 11:82d8768d7351 | 295 | |
Lightvalve | 11:82d8768d7351 | 296 | double output = 0; |
Lightvalve | 11:82d8768d7351 | 297 | int vn = (int) ((double) input / 10.); |
Lightvalve | 11:82d8768d7351 | 298 | int en = input % 10; |
Lightvalve | 11:82d8768d7351 | 299 | |
Lightvalve | 11:82d8768d7351 | 300 | double temp = 1.; |
Lightvalve | 11:82d8768d7351 | 301 | for (i = 0; i < en; i++) |
Lightvalve | 11:82d8768d7351 | 302 | temp *= 0.1; |
Lightvalve | 11:82d8768d7351 | 303 | |
Lightvalve | 11:82d8768d7351 | 304 | output = (double) vn*temp; |
Lightvalve | 11:82d8768d7351 | 305 | return output; |
Lightvalve | 11:82d8768d7351 | 306 | } |
Lightvalve | 11:82d8768d7351 | 307 | |
Lightvalve | 11:82d8768d7351 | 308 | void make_delay(void) { |
Lightvalve | 11:82d8768d7351 | 309 | int i = 0; |
Lightvalve | 11:82d8768d7351 | 310 | |
Lightvalve | 11:82d8768d7351 | 311 | for (i = 0; i < 100000; i++) { |
Lightvalve | 11:82d8768d7351 | 312 | ; |
Lightvalve | 11:82d8768d7351 | 313 | } |
Lightvalve | 11:82d8768d7351 | 314 | |
Lightvalve | 11:82d8768d7351 | 315 | } |
Lightvalve | 11:82d8768d7351 | 316 | |
Lightvalve | 11:82d8768d7351 | 317 | |
Lightvalve | 11:82d8768d7351 | 318 | /******************************************************************************* |
Lightvalve | 11:82d8768d7351 | 319 | * ROM functions |
Lightvalve | 11:82d8768d7351 | 320 | ******************************************************************************/ |
Lightvalve | 14:8e7590227d22 | 321 | void ROM_INIT_DATA(void) |
Lightvalve | 14:8e7590227d22 | 322 | { |
Lightvalve | 11:82d8768d7351 | 323 | static int16_t temp_check_num = LATEST_VERSION; |
Lightvalve | 11:82d8768d7351 | 324 | spi_eeprom_ready(); |
Lightvalve | 14:8e7590227d22 | 325 | //if (spi_eeprom_read(RID_IS_FIRST) != temp_check_num) { |
Lightvalve | 11:82d8768d7351 | 326 | spi_eeprom_ready(); |
Lightvalve | 11:82d8768d7351 | 327 | spi_eeprom_write(RID_IS_FIRST, temp_check_num); |
Lightvalve | 11:82d8768d7351 | 328 | ROM_RESET_DATA(); |
Lightvalve | 14:8e7590227d22 | 329 | //} |
Lightvalve | 11:82d8768d7351 | 330 | ROM_CALL_DATA(); |
Lightvalve | 11:82d8768d7351 | 331 | } |
Lightvalve | 11:82d8768d7351 | 332 | |
Lightvalve | 11:82d8768d7351 | 333 | void ROM_RESET_DATA(void) { |
Lightvalve | 11:82d8768d7351 | 334 | |
Lightvalve | 11:82d8768d7351 | 335 | spi_eeprom_write(RID_BNO, (int16_t) BNO); |
Lightvalve | 11:82d8768d7351 | 336 | spi_eeprom_write(RID_OPERATING_MODE, (int16_t) OPERATING_MODE); |
Lightvalve | 11:82d8768d7351 | 337 | spi_eeprom_write(RID_CAN_FREQ, (int16_t) CAN_FREQ); |
Lightvalve | 11:82d8768d7351 | 338 | spi_eeprom_write(RID_JOINT_ENC_DIR, (int16_t) DIR_JOINT_ENC); |
Lightvalve | 11:82d8768d7351 | 339 | spi_eeprom_write(RID_VALVE_DIR, (int16_t) DIR_VALVE); |
Lightvalve | 11:82d8768d7351 | 340 | spi_eeprom_write(RID_VALVE_ENC_DIR, (int16_t) DIR_VALVE_ENC); |
Lightvalve | 11:82d8768d7351 | 341 | spi_eeprom_write(RID_VOLATGE_SUPPLY, (int16_t) (SUPPLY_VOLTAGE * 10.)); |
Lightvalve | 11:82d8768d7351 | 342 | spi_eeprom_write(RID_VOLTAGE_VALVE, (int16_t) (VALVE_VOLTAGE_LIMIT * 10.)); |
Lightvalve | 11:82d8768d7351 | 343 | spi_eeprom_write(RID_P_GAIN_VALVE_POSITION, (int16_t) P_GAIN_VALVE_POSITION); |
Lightvalve | 11:82d8768d7351 | 344 | spi_eeprom_write(RID_I_GAIN_VALVE_POSITION, (int16_t) I_GAIN_VALVE_POSITION); |
Lightvalve | 11:82d8768d7351 | 345 | spi_eeprom_write(RID_D_GAIN_VALVE_POSITION, (int16_t) D_GAIN_VALVE_POSITION); |
Lightvalve | 11:82d8768d7351 | 346 | spi_eeprom_write(RID_P_GAIN_JOINT_POSITION, (int16_t) P_GAIN_JOINT_POSITION); |
Lightvalve | 11:82d8768d7351 | 347 | spi_eeprom_write(RID_I_GAIN_JOINT_POSITION, (int16_t) I_GAIN_JOINT_POSITION); |
Lightvalve | 11:82d8768d7351 | 348 | spi_eeprom_write(RID_D_GAIN_JOINT_POSITION, (int16_t) D_GAIN_JOINT_POSITION); |
Lightvalve | 11:82d8768d7351 | 349 | spi_eeprom_write(RID_P_GAIN_JOINT_TORQUE, (int16_t) P_GAIN_JOINT_TORQUE); |
Lightvalve | 11:82d8768d7351 | 350 | spi_eeprom_write(RID_I_GAIN_JOINT_TORQUE, (int16_t) I_GAIN_JOINT_TORQUE); |
Lightvalve | 11:82d8768d7351 | 351 | spi_eeprom_write(RID_D_GAIN_JOINT_TORQUE, (int16_t) D_GAIN_JOINT_TORQUE); |
Lightvalve | 11:82d8768d7351 | 352 | spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, (int16_t) (VALVE_DEADZONE_PLUS)); |
Lightvalve | 11:82d8768d7351 | 353 | spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, (int16_t) (VALVE_DEADZONE_MINUS)); |
Lightvalve | 11:82d8768d7351 | 354 | spi_eeprom_write(RID_VELOCITY_COMP_GAIN, (int16_t) VELOCITY_COMP_GAIN); |
Lightvalve | 11:82d8768d7351 | 355 | spi_eeprom_write(RID_COMPLIANCE_GAIN, (int16_t) COMPLIANCE_GAIN); |
Lightvalve | 11:82d8768d7351 | 356 | spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER); |
Lightvalve | 11:82d8768d7351 | 357 | spi_eeprom_write(RID_VALVE_FF, (int16_t) VALVE_FF); |
Lightvalve | 11:82d8768d7351 | 358 | spi_eeprom_write(RID_BULK_MODULUS, (int16_t) BULK_MODULUS); |
Lightvalve | 11:82d8768d7351 | 359 | spi_eeprom_write(RID_CHAMBER_VOLUME_A, (int16_t) CHAMBER_VOLUME_A); |
Lightvalve | 11:82d8768d7351 | 360 | spi_eeprom_write(RID_CHAMBER_VOLUME_B, (int16_t) CHAMBER_VOLUME_B); |
Lightvalve | 11:82d8768d7351 | 361 | spi_eeprom_write(RID_PISTON_AREA_A, (int16_t) PISTON_AREA_A); |
Lightvalve | 11:82d8768d7351 | 362 | spi_eeprom_write(RID_PISTON_AREA_B, (int16_t) PISTON_AREA_B); |
Lightvalve | 11:82d8768d7351 | 363 | spi_eeprom_write(RID_PRES_SUPPLY, (int16_t) PRES_SUPPLY); |
Lightvalve | 11:82d8768d7351 | 364 | spi_eeprom_write(RID_PRES_RETURN, (int16_t) PRES_RETURN); |
Lightvalve | 11:82d8768d7351 | 365 | spi_eeprom_write(RID_ENC_LIMIT_MINUS, (int16_t) ENC_LIMIT_MINUS); |
Lightvalve | 11:82d8768d7351 | 366 | spi_eeprom_write(RID_ENC_LIMIT_PLUS, (int16_t) ENC_LIMIT_PLUS); |
Lightvalve | 11:82d8768d7351 | 367 | spi_eeprom_write(RID_STROKE, (int16_t) STROKE); |
Lightvalve | 11:82d8768d7351 | 368 | spi_eeprom_write(RID_VALVE_LIMIT_MINUS, (int16_t) VALVE_LIMIT_MINUS); |
Lightvalve | 11:82d8768d7351 | 369 | spi_eeprom_write(RID_VALVE_LIMIT_PLUS, (int16_t) VALVE_LIMIT_PLUS); |
Lightvalve | 11:82d8768d7351 | 370 | spi_eeprom_write(RID_ENC_PULSE_PER_POSITION, (int16_t) ENC_PULSE_PER_POSITION); |
Lightvalve | 11:82d8768d7351 | 371 | spi_eeprom_write(RID_TORQUE_SENSOR_PULSE_PER_TORQUE, (int16_t) TORQUE_SENSOR_PULSE_PER_TORQUE); |
Lightvalve | 14:8e7590227d22 | 372 | spi_eeprom_ready(); |
Lightvalve | 14:8e7590227d22 | 373 | PRES_SENSOR_A_PULSE_PER_BAR = 4096.0 / 200.0; |
Lightvalve | 14:8e7590227d22 | 374 | PRES_SENSOR_A_PULSE_PER_BAR = 1.0; |
Lightvalve | 14:8e7590227d22 | 375 | //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0)); |
Lightvalve | 14:8e7590227d22 | 376 | spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int) (6)); |
Lightvalve | 14:8e7590227d22 | 377 | spi_eeprom_ready(); |
Lightvalve | 14:8e7590227d22 | 378 | PRES_SENSOR_B_PULSE_PER_BAR = 4096.0 / 200.0; |
Lightvalve | 14:8e7590227d22 | 379 | spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0)); |
Lightvalve | 11:82d8768d7351 | 380 | spi_eeprom_write(RID_FRICTION, (int16_t) (FRICTION * 10.)); |
Lightvalve | 11:82d8768d7351 | 381 | spi_eeprom_write(RID_HOMEPOS_OFFSET, (int16_t) HOMEPOS_OFFSET); |
Lightvalve | 11:82d8768d7351 | 382 | spi_eeprom_write(RID_HOMEPOS_VALVE_OPENING, (int16_t) HOMEPOS_VALVE_OPENING); |
Lightvalve | 14:8e7590227d22 | 383 | spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0)); |
Lightvalve | 14:8e7590227d22 | 384 | spi_eeprom_write(RID_PRES_A_SENSOR_VREF, (int16_t) (PRES_A_VREF * 1000.0)); |
Lightvalve | 14:8e7590227d22 | 385 | spi_eeprom_write(RID_PRES_B_SENSOR_VREF, (int16_t) (PRES_B_VREF * 1000.0)); |
Lightvalve | 14:8e7590227d22 | 386 | spi_eeprom_write(RID_VALVE_GAIN_PLUS_1, (int16_t) (VALVE_GAIN_LPM_PER_V[0] * 100.0)); |
Lightvalve | 14:8e7590227d22 | 387 | spi_eeprom_write(RID_VALVE_GAIN_PLUS_2, (int16_t) (VALVE_GAIN_LPM_PER_V[2] * 100.0)); |
Lightvalve | 14:8e7590227d22 | 388 | spi_eeprom_write(RID_VALVE_GAIN_PLUS_3, (int16_t) (VALVE_GAIN_LPM_PER_V[4] * 100.0)); |
Lightvalve | 14:8e7590227d22 | 389 | spi_eeprom_write(RID_VALVE_GAIN_PLUS_4, (int16_t) (VALVE_GAIN_LPM_PER_V[6] * 100.0)); |
Lightvalve | 14:8e7590227d22 | 390 | spi_eeprom_write(RID_VALVE_GAIN_PLUS_5, (int16_t) (VALVE_GAIN_LPM_PER_V[8] * 100.0)); |
Lightvalve | 14:8e7590227d22 | 391 | spi_eeprom_write(RID_VALVE_GAIN_MINUS_1, (int16_t) (VALVE_GAIN_LPM_PER_V[1] * 100.0)); |
Lightvalve | 14:8e7590227d22 | 392 | spi_eeprom_write(RID_VALVE_GAIN_MINUS_2, (int16_t) (VALVE_GAIN_LPM_PER_V[3] * 100.0)); |
Lightvalve | 14:8e7590227d22 | 393 | spi_eeprom_write(RID_VALVE_GAIN_MINUS_3, (int16_t) (VALVE_GAIN_LPM_PER_V[5] * 100.0)); |
Lightvalve | 14:8e7590227d22 | 394 | spi_eeprom_write(RID_VALVE_GAIN_MINUS_4, (int16_t) (VALVE_GAIN_LPM_PER_V[7] * 100.0)); |
Lightvalve | 14:8e7590227d22 | 395 | spi_eeprom_write(RID_VALVE_GAIN_MINUS_5, (int16_t) (VALVE_GAIN_LPM_PER_V[9] * 100.0)); |
Lightvalve | 11:82d8768d7351 | 396 | |
Lightvalve | 11:82d8768d7351 | 397 | |
Lightvalve | 11:82d8768d7351 | 398 | } |
Lightvalve | 11:82d8768d7351 | 399 | |
Lightvalve | 11:82d8768d7351 | 400 | void ROM_CALL_DATA(void) { |
Lightvalve | 11:82d8768d7351 | 401 | int i = 0; |
Lightvalve | 11:82d8768d7351 | 402 | BNO = spi_eeprom_read(RID_BNO); |
Lightvalve | 14:8e7590227d22 | 403 | BNO = 11; |
Lightvalve | 11:82d8768d7351 | 404 | OPERATING_MODE = spi_eeprom_read(RID_OPERATING_MODE); |
Lightvalve | 11:82d8768d7351 | 405 | CAN_FREQ = spi_eeprom_read(RID_CAN_FREQ); |
Lightvalve | 11:82d8768d7351 | 406 | |
Lightvalve | 11:82d8768d7351 | 407 | DIR_JOINT_ENC = spi_eeprom_read(RID_JOINT_ENC_DIR); |
Lightvalve | 11:82d8768d7351 | 408 | DIR_VALVE = spi_eeprom_read(RID_VALVE_DIR); |
Lightvalve | 11:82d8768d7351 | 409 | DIR_VALVE_ENC = spi_eeprom_read(RID_VALVE_ENC_DIR); |
Lightvalve | 11:82d8768d7351 | 410 | |
Lightvalve | 14:8e7590227d22 | 411 | SUPPLY_VOLTAGE = (double) spi_eeprom_read(RID_VOLATGE_SUPPLY) / 10.0; |
Lightvalve | 14:8e7590227d22 | 412 | VALVE_VOLTAGE_LIMIT = (double) spi_eeprom_read(RID_VOLTAGE_VALVE) / 10.0; |
Lightvalve | 11:82d8768d7351 | 413 | |
Lightvalve | 11:82d8768d7351 | 414 | P_GAIN_VALVE_POSITION = spi_eeprom_read(RID_P_GAIN_VALVE_POSITION); |
Lightvalve | 11:82d8768d7351 | 415 | I_GAIN_VALVE_POSITION = spi_eeprom_read(RID_I_GAIN_VALVE_POSITION); |
Lightvalve | 11:82d8768d7351 | 416 | D_GAIN_VALVE_POSITION = spi_eeprom_read(RID_D_GAIN_VALVE_POSITION); |
Lightvalve | 11:82d8768d7351 | 417 | |
Lightvalve | 11:82d8768d7351 | 418 | P_GAIN_JOINT_POSITION = spi_eeprom_read(RID_P_GAIN_JOINT_POSITION); |
Lightvalve | 11:82d8768d7351 | 419 | I_GAIN_JOINT_POSITION = spi_eeprom_read(RID_I_GAIN_JOINT_POSITION); |
Lightvalve | 11:82d8768d7351 | 420 | D_GAIN_JOINT_POSITION = spi_eeprom_read(RID_D_GAIN_JOINT_POSITION); |
Lightvalve | 11:82d8768d7351 | 421 | |
Lightvalve | 11:82d8768d7351 | 422 | P_GAIN_JOINT_TORQUE = spi_eeprom_read(RID_P_GAIN_JOINT_TORQUE); |
Lightvalve | 11:82d8768d7351 | 423 | I_GAIN_JOINT_TORQUE = spi_eeprom_read(RID_I_GAIN_JOINT_TORQUE); |
Lightvalve | 11:82d8768d7351 | 424 | D_GAIN_JOINT_TORQUE = spi_eeprom_read(RID_D_GAIN_JOINT_TORQUE); |
Lightvalve | 11:82d8768d7351 | 425 | |
Lightvalve | 11:82d8768d7351 | 426 | VALVE_DEADZONE_PLUS = spi_eeprom_read(RID_VALVE_DEADZONE_PLUS); |
Lightvalve | 11:82d8768d7351 | 427 | VALVE_DEADZONE_MINUS = spi_eeprom_read(RID_VALVE_DEADZONE_MINUS); |
Lightvalve | 11:82d8768d7351 | 428 | |
Lightvalve | 11:82d8768d7351 | 429 | VELOCITY_COMP_GAIN = spi_eeprom_read(RID_VELOCITY_COMP_GAIN); |
Lightvalve | 11:82d8768d7351 | 430 | COMPLIANCE_GAIN = spi_eeprom_read(RID_COMPLIANCE_GAIN); |
Lightvalve | 11:82d8768d7351 | 431 | |
Lightvalve | 11:82d8768d7351 | 432 | VALVE_CENTER = spi_eeprom_read(RID_VALVE_CNETER); |
Lightvalve | 11:82d8768d7351 | 433 | VALVE_FF = spi_eeprom_read(RID_VALVE_FF); |
Lightvalve | 11:82d8768d7351 | 434 | BULK_MODULUS = spi_eeprom_read(RID_BULK_MODULUS); |
Lightvalve | 11:82d8768d7351 | 435 | |
Lightvalve | 11:82d8768d7351 | 436 | CHAMBER_VOLUME_A = spi_eeprom_read(RID_CHAMBER_VOLUME_A); |
Lightvalve | 11:82d8768d7351 | 437 | CHAMBER_VOLUME_B = spi_eeprom_read(RID_CHAMBER_VOLUME_B); |
Lightvalve | 11:82d8768d7351 | 438 | |
Lightvalve | 11:82d8768d7351 | 439 | PISTON_AREA_A = spi_eeprom_read(RID_PISTON_AREA_A); |
Lightvalve | 11:82d8768d7351 | 440 | PISTON_AREA_B = spi_eeprom_read(RID_PISTON_AREA_B); |
Lightvalve | 11:82d8768d7351 | 441 | PISTON_AREA_alpha = (double)PISTON_AREA_B/(double)PISTON_AREA_A; |
Lightvalve | 11:82d8768d7351 | 442 | |
Lightvalve | 11:82d8768d7351 | 443 | PRES_SUPPLY = spi_eeprom_read(RID_PRES_SUPPLY); |
Lightvalve | 11:82d8768d7351 | 444 | PRES_RETURN = spi_eeprom_read(RID_PRES_RETURN); |
Lightvalve | 11:82d8768d7351 | 445 | |
Lightvalve | 11:82d8768d7351 | 446 | ENC_LIMIT_MINUS = spi_eeprom_read(RID_ENC_LIMIT_MINUS); |
Lightvalve | 11:82d8768d7351 | 447 | ENC_LIMIT_PLUS = spi_eeprom_read(RID_ENC_LIMIT_PLUS); |
Lightvalve | 11:82d8768d7351 | 448 | |
Lightvalve | 11:82d8768d7351 | 449 | STROKE = spi_eeprom_read(RID_STROKE); |
Lightvalve | 11:82d8768d7351 | 450 | |
Lightvalve | 11:82d8768d7351 | 451 | VALVE_LIMIT_MINUS = spi_eeprom_read(RID_VALVE_LIMIT_MINUS); |
Lightvalve | 11:82d8768d7351 | 452 | VALVE_LIMIT_PLUS = spi_eeprom_read(RID_VALVE_LIMIT_PLUS); |
Lightvalve | 11:82d8768d7351 | 453 | |
Lightvalve | 11:82d8768d7351 | 454 | ENC_PULSE_PER_POSITION = spi_eeprom_read(RID_ENC_PULSE_PER_POSITION); |
Lightvalve | 11:82d8768d7351 | 455 | TORQUE_SENSOR_PULSE_PER_TORQUE = spi_eeprom_read(RID_TORQUE_SENSOR_PULSE_PER_TORQUE); |
Lightvalve | 11:82d8768d7351 | 456 | PRES_SENSOR_A_PULSE_PER_BAR = (double) spi_eeprom_read(RID_PRES_SENSOR_A_PULSE_PER_BAR) * 0.01; |
Lightvalve | 11:82d8768d7351 | 457 | PRES_SENSOR_B_PULSE_PER_BAR = (double) spi_eeprom_read(RID_PRES_SENSOR_B_PULSE_PER_BAR) * 0.01; |
Lightvalve | 11:82d8768d7351 | 458 | |
Lightvalve | 14:8e7590227d22 | 459 | FRICTION = (double) spi_eeprom_read(RID_FRICTION) / 10.0; |
Lightvalve | 11:82d8768d7351 | 460 | HOMEPOS_OFFSET = (int) spi_eeprom_read(RID_HOMEPOS_OFFSET); |
Lightvalve | 11:82d8768d7351 | 461 | HOMEPOS_VALVE_OPENING = (int) spi_eeprom_read(RID_HOMEPOS_VALVE_OPENING); |
Lightvalve | 11:82d8768d7351 | 462 | |
Lightvalve | 14:8e7590227d22 | 463 | TORQUE_VREF = (double) spi_eeprom_read(RID_TORQUE_SENSOR_VREF) / 1000.0; |
Lightvalve | 11:82d8768d7351 | 464 | |
Lightvalve | 14:8e7590227d22 | 465 | PRES_A_VREF = (double) spi_eeprom_read(RID_PRES_A_SENSOR_VREF) / 1000.0; |
Lightvalve | 14:8e7590227d22 | 466 | PRES_B_VREF = (double) spi_eeprom_read(RID_PRES_B_SENSOR_VREF) / 1000.0; |
Lightvalve | 11:82d8768d7351 | 467 | |
Lightvalve | 14:8e7590227d22 | 468 | VALVE_GAIN_LPM_PER_V[0] = (double) spi_eeprom_read(RID_VALVE_GAIN_PLUS_1) / 100.0; |
Lightvalve | 14:8e7590227d22 | 469 | VALVE_GAIN_LPM_PER_V[2] = (double) spi_eeprom_read(RID_VALVE_GAIN_PLUS_2) / 100.0; |
Lightvalve | 14:8e7590227d22 | 470 | VALVE_GAIN_LPM_PER_V[4] = (double) spi_eeprom_read(RID_VALVE_GAIN_PLUS_3) / 100.0; |
Lightvalve | 14:8e7590227d22 | 471 | VALVE_GAIN_LPM_PER_V[6] = (double) spi_eeprom_read(RID_VALVE_GAIN_PLUS_4) / 100.0; |
Lightvalve | 14:8e7590227d22 | 472 | VALVE_GAIN_LPM_PER_V[8] = (double) spi_eeprom_read(RID_VALVE_GAIN_PLUS_5) / 100.0; |
Lightvalve | 14:8e7590227d22 | 473 | VALVE_GAIN_LPM_PER_V[1] = (double) spi_eeprom_read(RID_VALVE_GAIN_MINUS_1) / 100.0; |
Lightvalve | 14:8e7590227d22 | 474 | VALVE_GAIN_LPM_PER_V[3] = (double) spi_eeprom_read(RID_VALVE_GAIN_MINUS_2) / 100.0; |
Lightvalve | 14:8e7590227d22 | 475 | VALVE_GAIN_LPM_PER_V[5] = (double) spi_eeprom_read(RID_VALVE_GAIN_MINUS_3) / 100.0; |
Lightvalve | 14:8e7590227d22 | 476 | VALVE_GAIN_LPM_PER_V[7] = (double) spi_eeprom_read(RID_VALVE_GAIN_MINUS_4) / 100.0; |
Lightvalve | 14:8e7590227d22 | 477 | VALVE_GAIN_LPM_PER_V[9] = (double) spi_eeprom_read(RID_VALVE_GAIN_MINUS_5) / 100.0; |
Lightvalve | 11:82d8768d7351 | 478 | |
Lightvalve | 11:82d8768d7351 | 479 | for(i=0; i<18; i++) |
Lightvalve | 11:82d8768d7351 | 480 | { |
Lightvalve | 11:82d8768d7351 | 481 | VALVE_POS_VS_PWM[i] = (double) spi_eeprom_read(RID_VALVE_POS_VS_PWM_0 + i); |
Lightvalve | 11:82d8768d7351 | 482 | } |
Lightvalve | 11:82d8768d7351 | 483 | for(i=0; i<100; i++) |
Lightvalve | 11:82d8768d7351 | 484 | { |
Lightvalve | 12:6f2531038ea4 | 485 | JOINT_VEL[i] = (int32_t) ( (spi_eeprom_read(RID_VALVE_POS_VS_FLOWRATE_0 + i) & 0xFFFF) | ((spi_eeprom_read(RID_VALVE_POS_VS_FLOWRATE_0_1 + i) & 0xFFFF) << 16)); |
Lightvalve | 11:82d8768d7351 | 486 | } |
Lightvalve | 11:82d8768d7351 | 487 | |
Lightvalve | 11:82d8768d7351 | 488 | VALVE_MAX_POS = (int) spi_eeprom_read(RID_VALVE_MAX_POS); |
Lightvalve | 11:82d8768d7351 | 489 | VALVE_MIN_POS = (int) spi_eeprom_read(RID_VALVE_MIN_POS); |
Lightvalve | 11:82d8768d7351 | 490 | DDV_CENTER = (int) spi_eeprom_read(RID_DDV_CNETER); |
Lightvalve | 11:82d8768d7351 | 491 | |
Lightvalve | 11:82d8768d7351 | 492 | VALVE_POS_NUM = (int) spi_eeprom_read(RID_VALVE_POS_NUM); |
Lightvalve | 11:82d8768d7351 | 493 | |
Lightvalve | 11:82d8768d7351 | 494 | } |
Lightvalve | 11:82d8768d7351 | 495 | |
Lightvalve | 11:82d8768d7351 | 496 | /******************************************************************************* |
Lightvalve | 11:82d8768d7351 | 497 | * ENCODER functions |
Lightvalve | 11:82d8768d7351 | 498 | ******************************************************************************/ |
Lightvalve | 11:82d8768d7351 | 499 | // A-KHA |
Lightvalve | 11:82d8768d7351 | 500 | #define KF_G1_11 0.083920206005350 |
Lightvalve | 11:82d8768d7351 | 501 | #define KF_G1_12 0.000013905329560 |
Lightvalve | 11:82d8768d7351 | 502 | #define KF_G1_21 -0.000575742328210 |
Lightvalve | 11:82d8768d7351 | 503 | #define KF_G1_22 0.799999939711725 |
Lightvalve | 11:82d8768d7351 | 504 | // K |
Lightvalve | 11:82d8768d7351 | 505 | #define KF_G2_11 0.916079793994650 |
Lightvalve | 11:82d8768d7351 | 506 | #define KF_G2_12 0.000002878711641 |
Lightvalve | 11:82d8768d7351 | 507 | #define KF_G2_21 0.000575742328210 |
Lightvalve | 11:82d8768d7351 | 508 | #define KF_G2_22 0.199999945139809 |
Lightvalve | 11:82d8768d7351 | 509 | |
Lightvalve | 11:82d8768d7351 | 510 | double KF_Y_11 = 0.; |
Lightvalve | 11:82d8768d7351 | 511 | double KF_Y_21 = 0.; |
Lightvalve | 11:82d8768d7351 | 512 | double KF_X_11 = 0.; |
Lightvalve | 11:82d8768d7351 | 513 | double KF_X_21 = 0.; |
Lightvalve | 11:82d8768d7351 | 514 | |
Lightvalve | 11:82d8768d7351 | 515 | long ENC_pos_old = 0, ENC_pos_cur = 0, ENC_pos_diff = 0; |
Lightvalve | 11:82d8768d7351 | 516 | long ENC_RAW = 0, ENC_VEL_RAW = 0, ENC_VEL_KF = 0; |
Lightvalve | 11:82d8768d7351 | 517 | long enc_offset = 0; |
Lightvalve | 11:82d8768d7351 | 518 | |
Lightvalve | 11:82d8768d7351 | 519 | void ENC_UPDATE(void) { |
Lightvalve | 11:82d8768d7351 | 520 | |
Lightvalve | 11:82d8768d7351 | 521 | ENC_pos_cur = spi_enc_read(); |
Lightvalve | 11:82d8768d7351 | 522 | ENC_pos_diff = ENC_pos_cur - ENC_pos_old; |
Lightvalve | 11:82d8768d7351 | 523 | |
Lightvalve | 11:82d8768d7351 | 524 | if (ENC_pos_diff > 1300 || ENC_pos_diff<-1300) { |
Lightvalve | 11:82d8768d7351 | 525 | //MOT_E_STOP(0); |
Lightvalve | 11:82d8768d7351 | 526 | } |
Lightvalve | 11:82d8768d7351 | 527 | |
Lightvalve | 12:6f2531038ea4 | 528 | ENC_VEL_RAW = (int32_t) (ENC_pos_diff * TMR_FREQ_5k); |
Lightvalve | 11:82d8768d7351 | 529 | |
Lightvalve | 11:82d8768d7351 | 530 | KF_Y_11 = ENC_pos_cur; |
Lightvalve | 11:82d8768d7351 | 531 | KF_Y_21 = ENC_VEL_RAW; |
Lightvalve | 11:82d8768d7351 | 532 | 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; |
Lightvalve | 11:82d8768d7351 | 533 | KF_X_21 = KF_G1_21 * KF_X_11 + KF_G1_22 * KF_X_21 + KF_G2_21 * KF_Y_11 + KF_G2_22*KF_Y_21; |
Lightvalve | 12:6f2531038ea4 | 534 | ENC_VEL_KF = (int32_t) KF_X_21; |
Lightvalve | 11:82d8768d7351 | 535 | |
Lightvalve | 14:8e7590227d22 | 536 | pos.sen = (DIR_JOINT_ENC) * ENC_pos_cur + enc_offset; |
Lightvalve | 11:82d8768d7351 | 537 | // CUR_POSITION = (DIR_JOINT_ENC) * ENC_pos_cur; |
Lightvalve | 14:8e7590227d22 | 538 | vel.sen = (DIR_JOINT_ENC) * ENC_VEL_KF; |
Lightvalve | 11:82d8768d7351 | 539 | |
Lightvalve | 11:82d8768d7351 | 540 | // CUR_POSITION = ENC_pos_cur; |
Lightvalve | 11:82d8768d7351 | 541 | // CUR_VELOCITY = ENC_VEL_KF; |
Lightvalve | 11:82d8768d7351 | 542 | |
Lightvalve | 11:82d8768d7351 | 543 | ENC_pos_old = ENC_pos_cur; |
Lightvalve | 11:82d8768d7351 | 544 | } |
Lightvalve | 11:82d8768d7351 | 545 | |
Lightvalve | 11:82d8768d7351 | 546 | void ENC_SET_ZERO(void) { |
Lightvalve | 11:82d8768d7351 | 547 | |
Lightvalve | 11:82d8768d7351 | 548 | spi_enc_set_clear(); |
Lightvalve | 11:82d8768d7351 | 549 | CUR_POSITION = 0; |
Lightvalve | 11:82d8768d7351 | 550 | ENC_pos_old = ENC_pos_cur = 0; |
Lightvalve | 11:82d8768d7351 | 551 | |
Lightvalve | 11:82d8768d7351 | 552 | } |
Lightvalve | 11:82d8768d7351 | 553 | |
Lightvalve | 13:747daba9cf59 | 554 | void ENC_SET(int32_t value_e) { |
Lightvalve | 11:82d8768d7351 | 555 | spi_enc_set_clear(); |
Lightvalve | 13:747daba9cf59 | 556 | enc_offset = value_e; |
Lightvalve | 13:747daba9cf59 | 557 | CUR_POSITION = value_e; |
Lightvalve | 13:747daba9cf59 | 558 | ENC_pos_old = ENC_pos_cur = value_e; |
Lightvalve | 11:82d8768d7351 | 559 | |
Lightvalve | 11:82d8768d7351 | 560 | } |