Just a regular publish

Dependencies:   mbed imu_driver

Committer:
open4416
Date:
Fri Dec 20 04:51:03 2019 +0000
Revision:
14:bcf5cb4d08a5
Parent:
13:ac024885d0bf
Child:
18:780366f2534c
Add AHRS/IMU in, done by Jacky

Who changed what in which revision?

UserRevisionLine numberNew contents of line
open4416 0:c4747ebbe0b4 1 #include "mbed.h"
open4416 8:f8b1402c8f3c 2 #include "main.h"
open4416 9:c99eeafa6fa3 3 #include "imu_driver.hpp"
open4416 8:f8b1402c8f3c 4 #define pi 3.14159265359f
open4416 8:f8b1402c8f3c 5 #define d2r 0.01745329252f
open4416 2:c7a3a8c1aeed 6 #define dt 0.01f
open4416 6:fbe401163489 7
open4416 5:8116016abee0 8 DigitalOut Aux_Rly(PC_10,0); //Control aux relay, 1 active
open4416 9:c99eeafa6fa3 9 DigitalOut Fault_Ind(PC_12,0); //Indicate fault bt flashing, 1 active
open4416 2:c7a3a8c1aeed 10 DigitalOut LED(D13, 0); //Internal LED output, general purpose
open4416 6:fbe401163489 11 AnalogIn AUX_1(PC_0); //Auxilaru analog sensor
open4416 13:ac024885d0bf 12 AnalogIn AUX_2(PC_3);
open4416 6:fbe401163489 13 AnalogIn AUX_3(PC_2);
open4416 6:fbe401163489 14 AnalogIn AUX_4(PC_1);
open4416 7:f674ef860c9c 15 AnalogIn SDn_sense(PB_0); //Shutdown circuit driving end detection
open4416 13:ac024885d0bf 16 AnalogIn Brk_sense(PA_4); //Brake sensor readings
open4416 9:c99eeafa6fa3 17 CAN can1(PB_8, PB_9, 1000000); //1Mbps, contain critical torque command message
open4416 9:c99eeafa6fa3 18 SPI spi2(PB_15, PB_14, PB_13); //1Mbps default, MOSI MISO SCLK, forIMU
open4416 14:bcf5cb4d08a5 19 ImuDriver <spi2, PC_4, PB_2, PB_1> imu(ImuExtiRcvBothMsg); //SPI instance, reset, data ready, slave select
open4416 9:c99eeafa6fa3 20 Serial pc(USBTX, USBRX, 115200);
open4416 9:c99eeafa6fa3 21 Ticker ticker1; //100Hz task
open4416 5:8116016abee0 22 CANMessage can_msg_Rx;
open4416 5:8116016abee0 23 CANMessage can_msg_Tx;
open4416 2:c7a3a8c1aeed 24
open4416 2:c7a3a8c1aeed 25 void timer1_interrupt(void)
open4416 2:c7a3a8c1aeed 26 {
open4416 6:fbe401163489 27 HSTick += 1;
open4416 6:fbe401163489 28 LSTick += 1;
open4416 6:fbe401163489 29 if (HSTick > 9) { // 100Hz
open4416 6:fbe401163489 30 HST_EXFL = 1;
open4416 6:fbe401163489 31 HSTick = 0;
open4416 6:fbe401163489 32 }
open4416 6:fbe401163489 33 if (LSTick > 99) { // 10Hz
open4416 6:fbe401163489 34 LST_EXFL = 1;
open4416 6:fbe401163489 35 LSTick = 0;
open4416 6:fbe401163489 36 }
open4416 2:c7a3a8c1aeed 37 }
open4416 0:c4747ebbe0b4 38
open4416 0:c4747ebbe0b4 39 int main()
open4416 0:c4747ebbe0b4 40 {
open4416 6:fbe401163489 41 //Init CAN network
open4416 9:c99eeafa6fa3 42 CAN_init(); // Note now in Gloable test mode only for testing 2019/11/17
open4416 6:fbe401163489 43
open4416 6:fbe401163489 44 //Start House keeping task
open4416 6:fbe401163489 45 printf("VDU start up, pend for module online\n");
open4416 6:fbe401163489 46 ticker1.attach_us(&timer1_interrupt, 1000); //1 ms Systick
open4416 6:fbe401163489 47 while(1) {
open4416 6:fbe401163489 48
open4416 6:fbe401163489 49 // Do high speed loop
open4416 6:fbe401163489 50 if (HST_EXFL == 1) {
open4416 6:fbe401163489 51 HST_EXFL = 0;
open4416 6:fbe401163489 52
open4416 9:c99eeafa6fa3 53 // Get IMU, Auxs, Max Temperature
open4416 9:c99eeafa6fa3 54 Cooler();
open4416 6:fbe401163489 55 IMU_read();
open4416 6:fbe401163489 56 Aux_read();
open4416 9:c99eeafa6fa3 57 Module_WD();
open4416 6:fbe401163489 58
open4416 6:fbe401163489 59 // Run state machine
open4416 6:fbe401163489 60 switch (VDU_STAT) {
open4416 6:fbe401163489 61
open4416 6:fbe401163489 62 case VDU_PowerOn:
open4416 6:fbe401163489 63 /* Power on state
open4416 6:fbe401163489 64 * Description:
open4416 6:fbe401163489 65 * Simple start up sequence will be done here
open4416 6:fbe401163489 66 * Do:
open4416 6:fbe401163489 67 * VDU internal POST
open4416 6:fbe401163489 68 * Wait till modules + PSU online
open4416 6:fbe401163489 69 * To VDU_Idle (RTD off):
open4416 6:fbe401163489 70 * Prepare for 4WD main program
open4416 6:fbe401163489 71 * To VDU_Fault:
open4416 6:fbe401163489 72 * Run the error handling service
open4416 6:fbe401163489 73 */
open4416 6:fbe401163489 74
open4416 6:fbe401163489 75 //Basic IMU test
open4416 6:fbe401163489 76 POST();
open4416 6:fbe401163489 77
open4416 6:fbe401163489 78 //Check if state transition only when all module online
open4416 14:bcf5cb4d08a5 79 if (VDU_FLT != 0) { //Check if any error
open4416 14:bcf5cb4d08a5 80 VDU_STAT = VDU_Fault;
open4416 14:bcf5cb4d08a5 81 printf("POST Fault\n");
open4416 14:bcf5cb4d08a5 82 FLT_print = 1;
open4416 14:bcf5cb4d08a5 83 } else if ((FL_online*FR_online*RL_online*RR_online*PSU_online)!=0) {
open4416 14:bcf5cb4d08a5 84 // } else if (1) { //2019/11/30 only use for force online debug
open4416 14:bcf5cb4d08a5 85 //All module online & POST pass
open4416 14:bcf5cb4d08a5 86 VDU_STAT = VDU_Idle;
open4416 14:bcf5cb4d08a5 87 printf("All module online, VDU now Idle\n");
open4416 6:fbe401163489 88 } //Else keep in state VDU_PowerOn
open4416 6:fbe401163489 89 break;
open4416 6:fbe401163489 90
open4416 6:fbe401163489 91 case VDU_Idle:
open4416 6:fbe401163489 92 /* Controller Idle state
open4416 6:fbe401163489 93 * Description:
open4416 6:fbe401163489 94 * Normal latched state, wait for RTD_HMI set from PSU
open4416 6:fbe401163489 95 * 4WD in running but output mask to 0
open4416 6:fbe401163489 96 * Do:
open4416 6:fbe401163489 97 * 4WD controller
open4416 6:fbe401163489 98 * Check:
open4416 6:fbe401163489 99 * RUN faults if any
open4416 6:fbe401163489 100 * To VDU_Run:
open4416 6:fbe401163489 101 * Initialize parameters for start up, set RTD_cmd
open4416 6:fbe401163489 102 * To VDU_Fault:
open4416 6:fbe401163489 103 * Run the error handling service
open4416 6:fbe401163489 104 */
open4416 6:fbe401163489 105
open4416 6:fbe401163489 106 //Forced RTD_HMI for debug purpose 2019/11/14
open4416 13:ac024885d0bf 107 // RTD_HMI = 1; //Should be set if can bus received data
open4416 6:fbe401163489 108 //Forced RTD_HMI for debug purpose 2019/11/14
open4416 6:fbe401163489 109
open4416 6:fbe401163489 110 RUNT(); //Run test
open4416 6:fbe401163489 111 AWD(); //AWD main program
open4416 1:8a9ac822aab7 112
open4416 14:bcf5cb4d08a5 113 if (VDU_FLT != 0) { //Check if any error
open4416 6:fbe401163489 114 VDU_STAT = VDU_Fault;
open4416 7:f674ef860c9c 115 printf("Idle 2 Fault\n");
open4416 6:fbe401163489 116 FLT_print = 1;
open4416 6:fbe401163489 117 } else if (RTD_HMI != 0) { //Or command to run threw PSU
open4416 6:fbe401163489 118 //Prepare to send out RTD and start motor
open4416 6:fbe401163489 119 Idle2Run();
open4416 6:fbe401163489 120 VDU_STAT = VDU_Run;
open4416 6:fbe401163489 121 printf("Idle 2 Run\n");
open4416 6:fbe401163489 122 } //Else keep in state
open4416 6:fbe401163489 123 break;
open4416 6:fbe401163489 124
open4416 6:fbe401163489 125 case VDU_Run:
open4416 6:fbe401163489 126 /* Controller Run state
open4416 6:fbe401163489 127 * Description:
open4416 6:fbe401163489 128 * Normal latched state, after RTD_HMI is set from PSU
open4416 6:fbe401163489 129 * Same to Idle state except RTD_cmd is set
open4416 6:fbe401163489 130 * Do:
open4416 6:fbe401163489 131 * 4WD controller
open4416 6:fbe401163489 132 * Check:
open4416 6:fbe401163489 133 * RUN faults if any
open4416 6:fbe401163489 134 * To VDU_Idle:
open4416 6:fbe401163489 135 * Initialize parameters for idling, reset RTD_cmd
open4416 6:fbe401163489 136 * To VDU_Fault:
open4416 6:fbe401163489 137 * Run the error handling service
open4416 6:fbe401163489 138 */
open4416 6:fbe401163489 139
open4416 6:fbe401163489 140 RUNT(); //Run test
open4416 6:fbe401163489 141 AWD(); //AWD main program
open4416 6:fbe401163489 142
open4416 6:fbe401163489 143 //Temporary debug posting area 2019/11/14
open4416 6:fbe401163489 144 //printf("%d,%d\n", Encoder_cnt, Encoder_del);
open4416 6:fbe401163489 145 //printf("%d\n\r", (int16_t)Tmodule);//
open4416 6:fbe401163489 146 //printf("%d\n\r", (int16_t)Vbus);
open4416 0:c4747ebbe0b4 147
open4416 14:bcf5cb4d08a5 148 if (VDU_FLT != 0) { //Check if any error
open4416 6:fbe401163489 149 VDU_STAT = VDU_Fault;
open4416 7:f674ef860c9c 150 printf("Run 2 Fault\n");
open4416 6:fbe401163489 151 FLT_print = 1;
open4416 6:fbe401163489 152 } else if (RTD_HMI != 1) { //Or command to stop threw can bus
open4416 6:fbe401163489 153 Run2Idle();
open4416 6:fbe401163489 154 VDU_STAT = VDU_Idle;
open4416 6:fbe401163489 155 printf("Run 2 Idle\n");
open4416 6:fbe401163489 156 } //Else keep in state
open4416 6:fbe401163489 157 break;
open4416 6:fbe401163489 158
open4416 6:fbe401163489 159 case VDU_Fault:
open4416 6:fbe401163489 160 /* Controller Fault state
open4416 6:fbe401163489 161 * Description:
open4416 6:fbe401163489 162 * Fault latched state if any faults is detected
open4416 6:fbe401163489 163 * Same to Idle state except keep till RTD_HMI reset
open4416 6:fbe401163489 164 * Do:
open4416 6:fbe401163489 165 * Nothing, like a piece of shit
open4416 6:fbe401163489 166 * Check:
open4416 6:fbe401163489 167 * RUN faults if any
open4416 6:fbe401163489 168 * To VDU_PowerOn:
open4416 6:fbe401163489 169 * Restart VDU
open4416 6:fbe401163489 170 */
open4416 6:fbe401163489 171
open4416 6:fbe401163489 172 RUNT(); //Run test
open4416 6:fbe401163489 173
open4416 6:fbe401163489 174 if (RST_HMI == 1) { //PSU reset to clear error
open4416 6:fbe401163489 175 RST_cmd = 1;
open4416 14:bcf5cb4d08a5 176 FLT_print = 0; //Stop error printing
open4416 6:fbe401163489 177 VDU_STAT = VDU_PowerOn;
open4416 14:bcf5cb4d08a5 178 printf("VDU rebooting...\n");
open4416 6:fbe401163489 179 } //Else keep in state
open4416 6:fbe401163489 180 break;
open4416 6:fbe401163489 181 }
open4416 6:fbe401163489 182
open4416 7:f674ef860c9c 183 // Shit out torque distribution and special command
open4416 6:fbe401163489 184 if(VDU_STAT == VDU_Run) {
open4416 6:fbe401163489 185 //Allow output torque
open4416 6:fbe401163489 186 Tx_Tcmd_CAN1();
open4416 6:fbe401163489 187 } else if(RST_cmd != 0) {
open4416 7:f674ef860c9c 188 //Send out reset cmd once
open4416 6:fbe401163489 189 Tx_CLRerr_CAN1();
open4416 6:fbe401163489 190 } else {
open4416 6:fbe401163489 191 //Force RTD off when not in VDU_Run
open4416 6:fbe401163489 192 Tx_Estop_CAN1();
open4416 6:fbe401163489 193 }
open4416 6:fbe401163489 194
open4416 14:bcf5cb4d08a5 195 //2019/12/18 Add here to test IMU, newer version use inturrupt get data
open4416 14:bcf5cb4d08a5 196 // pc.printf("%.3f,%.3f,%.3f\n\r", imu.ahrsProcessedData.attitude[0], imu.ahrsProcessedData.attitude[1], imu.ahrsProcessedData.attitude[2]);
open4416 14:bcf5cb4d08a5 197 // pc.printf("%.3f,%.3f,%.3f\n\r", imu.imuProcessedData.rate[0], imu.imuProcessedData.rate[1], imu.imuProcessedData.rate[2]);
open4416 14:bcf5cb4d08a5 198 // pc.printf("%.3f,%.3f,%.3f\n\r", imu.imuProcessedData.accel[0], imu.imuProcessedData.accel[1], imu.imuProcessedData.accel[2]);
open4416 14:bcf5cb4d08a5 199
open4416 6:fbe401163489 200 }
open4416 7:f674ef860c9c 201 // End of high speed loop
open4416 6:fbe401163489 202
open4416 9:c99eeafa6fa3 203 // Do low speed state reporting 10 Hz
open4416 6:fbe401163489 204 if (LST_EXFL == 1) {
open4416 6:fbe401163489 205 LST_EXFL = 0;
open4416 8:f8b1402c8f3c 206 Cooler();
open4416 5:8116016abee0 207 Tx_Qdrv_CAN1();
open4416 6:fbe401163489 208
open4416 6:fbe401163489 209 // Print out error mesagge if exist, 1Hz repeative
open4416 6:fbe401163489 210 if(FLT_print != 0) {
open4416 7:f674ef860c9c 211 FLT_print_cnt += FLT_print;
open4416 6:fbe401163489 212 if(FLT_print_cnt > 19) {
open4416 6:fbe401163489 213 printf("POST FL,FR,RL,RR\n0x%04X 0x%04X 0x%04X 0x%04X\n", FL_FLT_Post,FR_FLT_Post,RL_FLT_Post,RR_FLT_Post);
open4416 7:f674ef860c9c 214 printf("RUN FL,FR,RL,RR\n0x%04X 0x%04X 0x%04X 0x%04X\n", FL_FLT_Run,FR_FLT_Run,RL_FLT_Run,RR_FLT_Run);
open4416 7:f674ef860c9c 215 printf("VDU\n0x%04X\n\n", VDU_FLT);
open4416 6:fbe401163489 216 FLT_print_cnt = 0;
open4416 6:fbe401163489 217 }
open4416 6:fbe401163489 218 }
open4416 14:bcf5cb4d08a5 219
open4416 14:bcf5cb4d08a5 220 LED = !LED;
open4416 7:f674ef860c9c 221 }
open4416 7:f674ef860c9c 222 // End of low speed state reporting
open4416 6:fbe401163489 223
open4416 7:f674ef860c9c 224 } // end of while
open4416 0:c4747ebbe0b4 225 }
open4416 0:c4747ebbe0b4 226
open4416 6:fbe401163489 227 void Idle2Run(void)
open4416 6:fbe401163489 228 {
open4416 6:fbe401163489 229 RTD_cmd = 1;
open4416 6:fbe401163489 230 }
open4416 6:fbe401163489 231
open4416 6:fbe401163489 232 void Run2Idle(void)
open4416 6:fbe401163489 233 {
open4416 6:fbe401163489 234 RTD_cmd = 0;
open4416 6:fbe401163489 235 }
open4416 6:fbe401163489 236
open4416 6:fbe401163489 237 void POST(void)
open4416 6:fbe401163489 238 {
open4416 8:f8b1402c8f3c 239 //Check IMU status abnormal
open4416 14:bcf5cb4d08a5 240 if(fabsf(YR_imu) > 250) { //half turn per sec, strange
open4416 6:fbe401163489 241 VDU_FLT |= IMUSTA_VDUFLTCode; //IMU status error
open4416 6:fbe401163489 242 }
open4416 6:fbe401163489 243 }
open4416 6:fbe401163489 244
open4416 6:fbe401163489 245 void RUNT(void)
open4416 6:fbe401163489 246 {
open4416 8:f8b1402c8f3c 247 //POST
open4416 6:fbe401163489 248 POST();
open4416 6:fbe401163489 249
open4416 8:f8b1402c8f3c 250 //Check module response timeout
open4416 6:fbe401163489 251 if((FL_online*FR_online*RL_online*RR_online) == 0) {
open4416 6:fbe401163489 252 VDU_FLT |= MODOFL_VDUFLTCode; //Module timeout
open4416 6:fbe401163489 253 }
open4416 6:fbe401163489 254 if(PSU_online == 0) {
open4416 6:fbe401163489 255 VDU_FLT |= PSUOFL_VDUFLTCode; //PSU timeout
open4416 6:fbe401163489 256 }
open4416 6:fbe401163489 257
open4416 6:fbe401163489 258 //Check ShutDrv voltage
open4416 8:f8b1402c8f3c 259 if(VDU_STAT == VDU_Run) {
open4416 8:f8b1402c8f3c 260 if(SDn_voltage < 8.0f) {
open4416 8:f8b1402c8f3c 261 VDU_FLT |= ShDVol_VDUFLTCode; //Shutdown circuit unclosed or uv
open4416 8:f8b1402c8f3c 262 }
open4416 8:f8b1402c8f3c 263 }
open4416 6:fbe401163489 264 }
open4416 6:fbe401163489 265
open4416 6:fbe401163489 266 void Aux_read(void)
open4416 6:fbe401163489 267 {
open4416 7:f674ef860c9c 268 //Capture analog in at once
open4416 13:ac024885d0bf 269 AUX_1_u16 = AUX_1.read_u16() >> 4U;
open4416 13:ac024885d0bf 270 AUX_2_u16 = AUX_2.read_u16() >> 4U;
open4416 13:ac024885d0bf 271 AUX_3_u16 = AUX_3.read_u16() >> 4U;
open4416 13:ac024885d0bf 272 AUX_4_u16 = AUX_4.read_u16() >> 4U;
open4416 8:f8b1402c8f3c 273 SDn_voltage = 18.81f*SDn_sense.read(); //Read in Shutdown circuit voltage in output end
open4416 13:ac024885d0bf 274 Brk_voltage = 5.5f*Brk_sense.read();
open4416 6:fbe401163489 275 }
open4416 6:fbe401163489 276
open4416 6:fbe401163489 277 void IMU_read(void)
open4416 6:fbe401163489 278 {
open4416 14:bcf5cb4d08a5 279 //Get readings threw back ground, direction not checked 2019/12/20
open4416 14:bcf5cb4d08a5 280 YR_imu = imu.imuProcessedData.rate[2];
open4416 14:bcf5cb4d08a5 281 Ax_imu = imu.imuProcessedData.accel[0];
open4416 14:bcf5cb4d08a5 282 Ay_imu = imu.imuProcessedData.accel[1];
open4416 14:bcf5cb4d08a5 283 Roll_imu = imu.ahrsProcessedData.attitude[0];
open4416 14:bcf5cb4d08a5 284 Pitch_imu = imu.ahrsProcessedData.attitude[1];
open4416 6:fbe401163489 285 }
open4416 6:fbe401163489 286
open4416 6:fbe401163489 287 void AWD(void)
open4416 6:fbe401163489 288 {
open4416 8:f8b1402c8f3c 289 if(AWD_HMI) {
open4416 8:f8b1402c8f3c 290 // A simple version is put here for reading
open4416 8:f8b1402c8f3c 291 Vb_est = 0.25f * (FL_W_ele + FR_W_ele + RL_W_ele + RR_W_ele);
open4416 8:f8b1402c8f3c 292 YR_ref = Steer_HMI*d2r*Vb_est/(Base+EG*Vb_est*Vb_est);
open4416 8:f8b1402c8f3c 293 Mz_reg = (YR_ref - YR_imu) * K_yaw; //K_yaw unfinished 2019/11/15
open4416 8:f8b1402c8f3c 294 sig = 0.5f - HCG*Trq_HMI/(Base*Rwhl*Mtot*g0);
open4416 8:f8b1402c8f3c 295 FL_Tcmd = (0.5f*Trq_HMI - Mz_reg*Rwhl/Track)*sig;
open4416 8:f8b1402c8f3c 296 FR_Tcmd = (0.5f*Trq_HMI + Mz_reg*Rwhl/Track)*sig;
open4416 8:f8b1402c8f3c 297 RL_Tcmd = (0.5f*Trq_HMI - Mz_reg*Rwhl/Track)*(1.0f-sig);
open4416 8:f8b1402c8f3c 298 RR_Tcmd = (0.5f*Trq_HMI + Mz_reg*Rwhl/Track)*(1.0f-sig);
open4416 8:f8b1402c8f3c 299 } else {
open4416 8:f8b1402c8f3c 300 FL_Tcmd = 0.25f*Trq_HMI;
open4416 8:f8b1402c8f3c 301 FR_Tcmd = 0.25f*Trq_HMI;
open4416 8:f8b1402c8f3c 302 RL_Tcmd = 0.25f*Trq_HMI;
open4416 8:f8b1402c8f3c 303 RR_Tcmd = 0.25f*Trq_HMI;
open4416 8:f8b1402c8f3c 304 }
open4416 8:f8b1402c8f3c 305 }
open4416 6:fbe401163489 306
open4416 8:f8b1402c8f3c 307 void ASL(void)
open4416 8:f8b1402c8f3c 308 {
open4416 8:f8b1402c8f3c 309 //Filter out each motor W_ele and get approximate accel, compare with IMU
open4416 8:f8b1402c8f3c 310 //Policy is set as "degrade only" coefficient exp(K_ASL*delAccel)
open4416 8:f8b1402c8f3c 311 //Fill out if enough time
open4416 6:fbe401163489 312 }
open4416 6:fbe401163489 313
open4416 5:8116016abee0 314 void Rx_CAN1(void)
open4416 5:8116016abee0 315 {
open4416 5:8116016abee0 316 LED = 1;
open4416 7:f674ef860c9c 317 int16_t tmp;
open4416 7:f674ef860c9c 318
open4416 5:8116016abee0 319 if(can1.read(can_msg_Rx)) {
open4416 7:f674ef860c9c 320 switch(can_msg_Rx.id) { //Filtered input message
open4416 7:f674ef860c9c 321 // Start of 100Hz msg
open4416 8:f8b1402c8f3c 322 case FL_HSB_ID://1
open4416 7:f674ef860c9c 323 //HSB from FL motor drive
open4416 7:f674ef860c9c 324 FL_DSM = can_msg_Rx.data[6] & 0x01; //Get DSM_STAT
open4416 7:f674ef860c9c 325 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 7:f674ef860c9c 326 FL_W_ele = tmp*1.0f;
open4416 7:f674ef860c9c 327 tmp = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 328 FL_Trq_fil3 = tmp * 0.01f;
open4416 7:f674ef860c9c 329 tmp = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 330 FL_Trq_est = tmp * 0.01f;
open4416 7:f674ef860c9c 331 FL_online = 3;
open4416 7:f674ef860c9c 332 break;
open4416 7:f674ef860c9c 333
open4416 8:f8b1402c8f3c 334 case FR_HSB_ID://2
open4416 7:f674ef860c9c 335 //HSB from FR motor drive
open4416 7:f674ef860c9c 336 FR_DSM = can_msg_Rx.data[6] & 0x01; //Get DSM_STAT
open4416 7:f674ef860c9c 337 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 7:f674ef860c9c 338 FR_W_ele = tmp*1.0f;
open4416 7:f674ef860c9c 339 tmp = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 340 FR_Trq_fil3 = tmp * 0.01f;
open4416 7:f674ef860c9c 341 tmp = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 342 FR_Trq_est = tmp * 0.01f;
open4416 7:f674ef860c9c 343 FR_online = 3;
open4416 7:f674ef860c9c 344 break;
open4416 7:f674ef860c9c 345
open4416 8:f8b1402c8f3c 346 case RL_HSB_ID://3
open4416 7:f674ef860c9c 347 //HSB from RL motor drive
open4416 7:f674ef860c9c 348 RL_DSM = can_msg_Rx.data[6] & 0x01; //Get DSM_STAT
open4416 7:f674ef860c9c 349 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 7:f674ef860c9c 350 RL_W_ele = tmp*1.0f;
open4416 7:f674ef860c9c 351 tmp = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 352 RL_Trq_fil3 = tmp * 0.01f;
open4416 7:f674ef860c9c 353 tmp = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 354 RL_Trq_est = tmp * 0.01f;
open4416 7:f674ef860c9c 355 RL_online = 3;
open4416 7:f674ef860c9c 356 break;
open4416 7:f674ef860c9c 357
open4416 8:f8b1402c8f3c 358 case RR_HSB_ID://4
open4416 7:f674ef860c9c 359 //HSB from RR motor drive
open4416 7:f674ef860c9c 360 RR_DSM = can_msg_Rx.data[6] & 0x01; //Get DSM_STAT
open4416 7:f674ef860c9c 361 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 7:f674ef860c9c 362 RR_W_ele = tmp*1.0f;
open4416 7:f674ef860c9c 363 tmp = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 364 RR_Trq_fil3 = tmp * 0.01f;
open4416 7:f674ef860c9c 365 tmp = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 366 RR_Trq_est = tmp * 0.01f;
open4416 7:f674ef860c9c 367 RR_online = 3;
open4416 7:f674ef860c9c 368 break;
open4416 7:f674ef860c9c 369
open4416 8:f8b1402c8f3c 370 case HMI_cmd_ID://5
open4416 7:f674ef860c9c 371 //HMI command from PSU
open4416 7:f674ef860c9c 372 AWD_HMI = can_msg_Rx.data[6] & 0x01; //Get AWD switch
open4416 7:f674ef860c9c 373 RST_HMI = can_msg_Rx.data[5] & 0x01; //Get RST request
open4416 7:f674ef860c9c 374 RTD_HMI = can_msg_Rx.data[4] & 0x01; //Get RTD switch
open4416 7:f674ef860c9c 375 tmp = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 376 Steer_HMI = tmp * 0.01f;
open4416 7:f674ef860c9c 377 tmp = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 378 Trq_HMI = tmp * 0.01f;
open4416 7:f674ef860c9c 379 PSU_online = 3;
open4416 7:f674ef860c9c 380 break;
open4416 7:f674ef860c9c 381 // end of 100Hz msg
open4416 7:f674ef860c9c 382
open4416 7:f674ef860c9c 383 // Start of 10Hz msg
open4416 8:f8b1402c8f3c 384 case FL_LSB_ID://6
open4416 7:f674ef860c9c 385 //LSB from FL motor drive
open4416 7:f674ef860c9c 386 tmp = can_msg_Rx.data[7] << 8 | can_msg_Rx.data[6];
open4416 7:f674ef860c9c 387 FL_Tmotor = tmp*0.01f;
open4416 7:f674ef860c9c 388 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 7:f674ef860c9c 389 FL_Tmodule = tmp*0.01f;
open4416 7:f674ef860c9c 390 FL_FLT_Run = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 391 FL_FLT_Post = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 392 break;
open4416 7:f674ef860c9c 393
open4416 8:f8b1402c8f3c 394 case FR_LSB_ID://7
open4416 7:f674ef860c9c 395 //LSB from FR motor drive
open4416 7:f674ef860c9c 396 tmp = can_msg_Rx.data[7] << 8 | can_msg_Rx.data[6];
open4416 7:f674ef860c9c 397 FR_Tmotor = tmp*0.01f;
open4416 7:f674ef860c9c 398 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 7:f674ef860c9c 399 FR_Tmodule = tmp*0.01f;
open4416 7:f674ef860c9c 400 FR_FLT_Run = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 401 FR_FLT_Post = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 402 break;
open4416 7:f674ef860c9c 403
open4416 8:f8b1402c8f3c 404 case RL_LSB_ID://8
open4416 7:f674ef860c9c 405 //LSB from RL motor drive
open4416 7:f674ef860c9c 406 tmp = can_msg_Rx.data[7] << 8 | can_msg_Rx.data[6];
open4416 7:f674ef860c9c 407 RL_Tmotor = tmp*0.01f;
open4416 7:f674ef860c9c 408 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 7:f674ef860c9c 409 RL_Tmodule = tmp*0.01f;
open4416 7:f674ef860c9c 410 RL_FLT_Run = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 411 RL_FLT_Post = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 412 break;
open4416 7:f674ef860c9c 413
open4416 8:f8b1402c8f3c 414 case RR_LSB_ID://9
open4416 7:f674ef860c9c 415 //LSB from RR motor drive
open4416 7:f674ef860c9c 416 tmp = can_msg_Rx.data[7] << 8 | can_msg_Rx.data[6];
open4416 8:f8b1402c8f3c 417 RR_Tmotor = tmp*0.01f;
open4416 7:f674ef860c9c 418 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 8:f8b1402c8f3c 419 RR_Tmodule = tmp*0.01f;
open4416 8:f8b1402c8f3c 420 RR_FLT_Run = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 8:f8b1402c8f3c 421 RR_FLT_Post = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 422 break;
open4416 7:f674ef860c9c 423 // end of 10Hz msg
open4416 7:f674ef860c9c 424 }
open4416 5:8116016abee0 425 }
open4416 5:8116016abee0 426 LED = 0;
open4416 5:8116016abee0 427 }
open4416 5:8116016abee0 428
open4416 6:fbe401163489 429 void Tx_CLRerr_CAN1(void)
open4416 2:c7a3a8c1aeed 430 {
open4416 9:c99eeafa6fa3 431 Tx_Estop_CAN1(); //disable as default
open4416 9:c99eeafa6fa3 432 RST_cmd = 0; //clear out on shot
open4416 6:fbe401163489 433 }
open4416 6:fbe401163489 434
open4416 6:fbe401163489 435 void Tx_Estop_CAN1(void)
open4416 6:fbe401163489 436 {
open4416 9:c99eeafa6fa3 437 RTD_cmd = 0; //force disable
open4416 6:fbe401163489 438 Tx_Tcmd_CAN1();
open4416 2:c7a3a8c1aeed 439 }
open4416 2:c7a3a8c1aeed 440
open4416 9:c99eeafa6fa3 441 void Tx_Tcmd_CAN1(void) // 100 Hz
open4416 2:c7a3a8c1aeed 442 {
open4416 7:f674ef860c9c 443 int16_t tmp;
open4416 7:f674ef860c9c 444 tmp = (int16_t) (FL_Tcmd * 100.0f);
open4416 7:f674ef860c9c 445 temp_msg[0] = tmp;
open4416 7:f674ef860c9c 446 temp_msg[1] = tmp >> 8U;
open4416 7:f674ef860c9c 447 temp_msg[2] = RTD_cmd;
open4416 7:f674ef860c9c 448 temp_msg[3] = RST_cmd;
open4416 14:bcf5cb4d08a5 449 temp_msg[4] = 0U;
open4416 14:bcf5cb4d08a5 450 temp_msg[5] = 0U;
open4416 14:bcf5cb4d08a5 451 temp_msg[6] = 0U;
open4416 14:bcf5cb4d08a5 452 temp_msg[7] = 0U;
open4416 8:f8b1402c8f3c 453 can_msg_Tx = CANMessage(FL_CMD_ID,temp_msg,8,CANData,CANStandard);
open4416 6:fbe401163489 454 CANpendTX();
open4416 6:fbe401163489 455 can1.write(can_msg_Tx);
open4416 6:fbe401163489 456
open4416 7:f674ef860c9c 457 tmp = (int16_t) (FR_Tcmd * 100.0f);
open4416 7:f674ef860c9c 458 temp_msg[0] = tmp;
open4416 7:f674ef860c9c 459 temp_msg[1] = tmp >> 8U;
open4416 7:f674ef860c9c 460 temp_msg[2] = RTD_cmd;
open4416 7:f674ef860c9c 461 temp_msg[3] = RST_cmd;
open4416 8:f8b1402c8f3c 462 can_msg_Tx = CANMessage(FR_CMD_ID,temp_msg,8,CANData,CANStandard);
open4416 6:fbe401163489 463 CANpendTX();
open4416 6:fbe401163489 464 can1.write(can_msg_Tx);
open4416 6:fbe401163489 465
open4416 7:f674ef860c9c 466 tmp = (int16_t) (RL_Tcmd * 100.0f);
open4416 7:f674ef860c9c 467 temp_msg[0] = tmp;
open4416 7:f674ef860c9c 468 temp_msg[1] = tmp >> 8U;
open4416 7:f674ef860c9c 469 temp_msg[2] = RTD_cmd;
open4416 7:f674ef860c9c 470 temp_msg[3] = RST_cmd;
open4416 8:f8b1402c8f3c 471 can_msg_Tx = CANMessage(RL_CMD_ID,temp_msg,8,CANData,CANStandard);
open4416 6:fbe401163489 472 CANpendTX();
open4416 6:fbe401163489 473 can1.write(can_msg_Tx);
open4416 6:fbe401163489 474
open4416 7:f674ef860c9c 475 tmp = (int16_t) (RR_Tcmd * 100.0f);
open4416 7:f674ef860c9c 476 temp_msg[0] = tmp;
open4416 7:f674ef860c9c 477 temp_msg[1] = tmp >> 8U;
open4416 7:f674ef860c9c 478 temp_msg[2] = RTD_cmd;
open4416 7:f674ef860c9c 479 temp_msg[3] = RST_cmd;
open4416 8:f8b1402c8f3c 480 can_msg_Tx = CANMessage(RR_CMD_ID,temp_msg,8,CANData,CANStandard);
open4416 6:fbe401163489 481 CANpendTX();
open4416 6:fbe401163489 482 can1.write(can_msg_Tx);
open4416 6:fbe401163489 483
open4416 2:c7a3a8c1aeed 484 }
open4416 2:c7a3a8c1aeed 485
open4416 6:fbe401163489 486 void Tx_Qdrv_CAN1(void) // 10 Hz
open4416 0:c4747ebbe0b4 487 {
open4416 8:f8b1402c8f3c 488 int16_t tmp;
open4416 6:fbe401163489 489 // Auxilary sensor reading shitting out
open4416 7:f674ef860c9c 490 temp_msg[0] = AUX_1_u16;
open4416 7:f674ef860c9c 491 temp_msg[1] = AUX_1_u16 >> 8U;
open4416 7:f674ef860c9c 492 temp_msg[2] = AUX_2_u16;
open4416 7:f674ef860c9c 493 temp_msg[3] = AUX_2_u16 >> 8U;
open4416 7:f674ef860c9c 494 temp_msg[4] = AUX_3_u16;
open4416 7:f674ef860c9c 495 temp_msg[5] = AUX_3_u16 >> 8U;
open4416 7:f674ef860c9c 496 temp_msg[6] = AUX_4_u16;
open4416 7:f674ef860c9c 497 temp_msg[7] = AUX_4_u16 >> 8U;
open4416 6:fbe401163489 498 can_msg_Tx = CANMessage(AUX_sense_ID,temp_msg,8,CANData,CANStandard);
open4416 6:fbe401163489 499 CANpendTX();
open4416 6:fbe401163489 500 can1.write(can_msg_Tx);
open4416 6:fbe401163489 501
open4416 6:fbe401163489 502 // Qdrive internal state shitting out
open4416 8:f8b1402c8f3c 503 temp_msg[0] = VDU_FLT;
open4416 8:f8b1402c8f3c 504 temp_msg[1] = VDU_FLT >> 8U;
open4416 8:f8b1402c8f3c 505 temp_msg[2] = VDU_STAT;
open4416 14:bcf5cb4d08a5 506 temp_msg[3] = (int8_t)(SDn_voltage*10.0f);
open4416 14:bcf5cb4d08a5 507 temp_msg[4] = (int8_t)(Brk_voltage*10.0f);
open4416 14:bcf5cb4d08a5 508 temp_msg[5] = 0U;
open4416 14:bcf5cb4d08a5 509 temp_msg[6] = 0U;
open4416 14:bcf5cb4d08a5 510 temp_msg[7] = 0U;
open4416 6:fbe401163489 511 can_msg_Tx = CANMessage(Qdrv_stat_ID,temp_msg,8,CANData,CANStandard);
open4416 6:fbe401163489 512 CANpendTX();
open4416 6:fbe401163489 513 can1.write(can_msg_Tx);
open4416 2:c7a3a8c1aeed 514
open4416 6:fbe401163489 515 // IMU attitude readings shitting out, 10Hz on CAN but 100Hz for internal use
open4416 8:f8b1402c8f3c 516 tmp = (int16_t) (YR_imu * 10000.0f);
open4416 8:f8b1402c8f3c 517 temp_msg[0] = tmp;
open4416 8:f8b1402c8f3c 518 temp_msg[1] = tmp >> 8U;
open4416 8:f8b1402c8f3c 519 tmp = (int16_t) (Roll_imu * 100.0f);
open4416 8:f8b1402c8f3c 520 temp_msg[2] = tmp;
open4416 8:f8b1402c8f3c 521 temp_msg[3] = tmp >> 8U;
open4416 8:f8b1402c8f3c 522 tmp = (int16_t) (Pitch_imu * 100.0f);
open4416 8:f8b1402c8f3c 523 temp_msg[4] = tmp;
open4416 8:f8b1402c8f3c 524 temp_msg[5] = tmp >> 8U;
open4416 8:f8b1402c8f3c 525 temp_msg[6] = (int8_t)Ax_imu;
open4416 8:f8b1402c8f3c 526 temp_msg[7] = (int8_t)Ay_imu;
open4416 6:fbe401163489 527 can_msg_Tx = CANMessage(IMU_sense_ID,temp_msg,8,CANData,CANStandard);
open4416 6:fbe401163489 528 CANpendTX();
open4416 6:fbe401163489 529 can1.write(can_msg_Tx);
open4416 6:fbe401163489 530 }
open4416 6:fbe401163489 531
open4416 6:fbe401163489 532 void CANpendTX(void)
open4416 6:fbe401163489 533 {
open4416 6:fbe401163489 534 //Pend till TX box has empty slot, timeout will generate error
open4416 6:fbe401163489 535 uint32_t timeout = 0;
open4416 6:fbe401163489 536 while(!(CAN1->TSR & CAN_TSR_TME_Msk)) {
open4416 6:fbe401163489 537 //Wait till non empty
open4416 6:fbe401163489 538 timeout += 1;
open4416 6:fbe401163489 539 if(timeout > 10000) {
open4416 6:fbe401163489 540 // Put some timeout error handler
open4416 6:fbe401163489 541 break;
open4416 6:fbe401163489 542 }
open4416 0:c4747ebbe0b4 543 }
open4416 0:c4747ebbe0b4 544 }
open4416 1:8a9ac822aab7 545
open4416 6:fbe401163489 546 void CAN_init(void)
open4416 6:fbe401163489 547 {
open4416 6:fbe401163489 548 //Set CAN system
open4416 6:fbe401163489 549 SET_BIT(CAN1->MCR, CAN_MCR_ABOM); // Enable auto reboot after bus off
open4416 6:fbe401163489 550 can1.filter(FL_HSB_ID,0xFFFF,CANStandard,0); // ID filter listing mode
open4416 6:fbe401163489 551 can1.filter(FR_HSB_ID,0xFFFF,CANStandard,1);
open4416 6:fbe401163489 552 can1.filter(RL_HSB_ID,0xFFFF,CANStandard,2);
open4416 6:fbe401163489 553 can1.filter(RR_HSB_ID,0xFFFF,CANStandard,3);
open4416 6:fbe401163489 554 can1.filter(FL_LSB_ID,0xFFFF,CANStandard,4);
open4416 6:fbe401163489 555 can1.filter(FR_LSB_ID,0xFFFF,CANStandard,5);
open4416 6:fbe401163489 556 can1.filter(RL_LSB_ID,0xFFFF,CANStandard,6);
open4416 6:fbe401163489 557 can1.filter(RR_LSB_ID,0xFFFF,CANStandard,7);
open4416 8:f8b1402c8f3c 558 can1.filter(HMI_cmd_ID,0xFFFF,CANStandard,8); // PSU online monitoring
open4416 14:bcf5cb4d08a5 559 // can1.mode(CAN::GlobalTest); // Add only for testing 2019/11/13
open4416 8:f8b1402c8f3c 560 can1.attach(&Rx_CAN1, CAN::RxIrq); // CAN1 Recieve Irq
open4416 6:fbe401163489 561 }
open4416 2:c7a3a8c1aeed 562
open4416 6:fbe401163489 563 void Module_WD(void)
open4416 6:fbe401163489 564 {
open4416 9:c99eeafa6fa3 565 //Module online dissipitive indicator
open4416 6:fbe401163489 566 if (FL_online != 0) {
open4416 6:fbe401163489 567 FL_online -= 1;
open4416 6:fbe401163489 568 }
open4416 6:fbe401163489 569 if (FR_online != 0) {
open4416 6:fbe401163489 570 FR_online -= 1;
open4416 6:fbe401163489 571 }
open4416 6:fbe401163489 572 if (RL_online != 0) {
open4416 6:fbe401163489 573 RL_online -= 1;
open4416 6:fbe401163489 574 }
open4416 6:fbe401163489 575 if (RR_online != 0) {
open4416 6:fbe401163489 576 RR_online -= 1;
open4416 6:fbe401163489 577 }
open4416 6:fbe401163489 578 if (PSU_online != 0) {
open4416 6:fbe401163489 579 PSU_online -= 1;
open4416 6:fbe401163489 580 }
open4416 6:fbe401163489 581 }
open4416 6:fbe401163489 582
open4416 8:f8b1402c8f3c 583 void Cooler(void)
open4416 8:f8b1402c8f3c 584 {
open4416 8:f8b1402c8f3c 585 //Cooling auto control, unfinish 2019/11/15
open4416 9:c99eeafa6fa3 586 Max_Tmotor = max_fval(FL_Tmotor, FR_Tmotor, RL_Tmotor, RR_Tmotor);
open4416 9:c99eeafa6fa3 587 Max_Tmodule = max_fval(FL_Tmodule, FR_Tmodule, RL_Tmodule, RR_Tmodule);
open4416 8:f8b1402c8f3c 588 if(0) {
open4416 8:f8b1402c8f3c 589 Aux_Rly = 1;
open4416 8:f8b1402c8f3c 590 } else {
open4416 8:f8b1402c8f3c 591 Aux_Rly = 0;
open4416 8:f8b1402c8f3c 592 }
open4416 8:f8b1402c8f3c 593 }
open4416 8:f8b1402c8f3c 594
open4416 9:c99eeafa6fa3 595 int16_t max_uval(int16_t i1, int16_t i2, int16_t i3, int16_t i4)
open4416 2:c7a3a8c1aeed 596 {
open4416 2:c7a3a8c1aeed 597 int16_t max = i1;
open4416 2:c7a3a8c1aeed 598 if(i2 > max) max = i2;
open4416 2:c7a3a8c1aeed 599 if(i3 > max) max = i3;
open4416 6:fbe401163489 600 if(i4 > max) max = i4;
open4416 2:c7a3a8c1aeed 601 return max;
open4416 6:fbe401163489 602 }
open4416 9:c99eeafa6fa3 603
open4416 9:c99eeafa6fa3 604 float max_fval(float i1, float i2, float i3, float i4)
open4416 9:c99eeafa6fa3 605 {
open4416 9:c99eeafa6fa3 606 float max = i1;
open4416 9:c99eeafa6fa3 607 if(i2 > max) max = i2;
open4416 9:c99eeafa6fa3 608 if(i3 > max) max = i3;
open4416 9:c99eeafa6fa3 609 if(i4 > max) max = i4;
open4416 9:c99eeafa6fa3 610 return max;
open4416 9:c99eeafa6fa3 611 }
open4416 8:f8b1402c8f3c 612 // pc.printf("SOC: %.2f\n", Module_Total*0.01f);