Just a regular publish

Dependencies:   mbed imu_driver

Committer:
open4416
Date:
Tue Mar 10 14:56:08 2020 +0000
Revision:
22:4f764f7cd0b3
Parent:
21:e01a019fae2f
Child:
23:100661e2ad70
version use in test 2020/3/10

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 19:d68f21173c23 3
open4416 19:d68f21173c23 4 // define this for newversion test mode
open4416 19:d68f21173c23 5 //#define IMU_direct
open4416 19:d68f21173c23 6
open4416 19:d68f21173c23 7 #ifndef IMU_direct
open4416 9:c99eeafa6fa3 8 #include "imu_driver.hpp"
open4416 19:d68f21173c23 9 #else
open4416 19:d68f21173c23 10 typedef struct AhrsRawData {
open4416 19:d68f21173c23 11 uint16_t status;
open4416 19:d68f21173c23 12 int16_t rate[3];
open4416 19:d68f21173c23 13 int16_t accel[3];
open4416 19:d68f21173c23 14 uint16_t temperature;
open4416 19:d68f21173c23 15 int16_t attitude[3];
open4416 19:d68f21173c23 16 } AhrsData;
open4416 19:d68f21173c23 17 #define Read_VG (0x3D)
open4416 19:d68f21173c23 18 #define ACCL2F (4000.0f)
open4416 19:d68f21173c23 19 #define GYRO2F (100.0f)
open4416 19:d68f21173c23 20 #define AHRS2F (90.0f)
open4416 19:d68f21173c23 21 #define VG_len 11U
open4416 19:d68f21173c23 22 #endif
open4416 19:d68f21173c23 23
open4416 8:f8b1402c8f3c 24 #define pi 3.14159265359f
open4416 8:f8b1402c8f3c 25 #define d2r 0.01745329252f
open4416 2:c7a3a8c1aeed 26 #define dt 0.01f
open4416 6:fbe401163489 27
open4416 5:8116016abee0 28 DigitalOut Aux_Rly(PC_10,0); //Control aux relay, 1 active
open4416 9:c99eeafa6fa3 29 DigitalOut Fault_Ind(PC_12,0); //Indicate fault bt flashing, 1 active
open4416 2:c7a3a8c1aeed 30 DigitalOut LED(D13, 0); //Internal LED output, general purpose
open4416 6:fbe401163489 31 AnalogIn AUX_1(PC_0); //Auxilaru analog sensor
open4416 13:ac024885d0bf 32 AnalogIn AUX_2(PC_3);
open4416 6:fbe401163489 33 AnalogIn AUX_3(PC_2);
open4416 6:fbe401163489 34 AnalogIn AUX_4(PC_1);
open4416 7:f674ef860c9c 35 AnalogIn SDn_sense(PB_0); //Shutdown circuit driving end detection
open4416 13:ac024885d0bf 36 AnalogIn Brk_sense(PA_4); //Brake sensor readings
open4416 9:c99eeafa6fa3 37 CAN can1(PB_8, PB_9, 1000000); //1Mbps, contain critical torque command message
open4416 9:c99eeafa6fa3 38 SPI spi2(PB_15, PB_14, PB_13); //1Mbps default, MOSI MISO SCLK, forIMU
open4416 19:d68f21173c23 39 #ifndef IMU_direct
open4416 14:bcf5cb4d08a5 40 ImuDriver <spi2, PC_4, PB_2, PB_1> imu(ImuExtiRcvBothMsg); //SPI instance, reset, data ready, slave select
open4416 19:d68f21173c23 41 #else
open4416 19:d68f21173c23 42 AhrsData ahrsdata;
open4416 19:d68f21173c23 43 DigitalOut cs(PB_1,1);
open4416 19:d68f21173c23 44 InterruptIn drdy(PB_2);
open4416 19:d68f21173c23 45 #endif
open4416 9:c99eeafa6fa3 46 Serial pc(USBTX, USBRX, 115200);
open4416 9:c99eeafa6fa3 47 Ticker ticker1; //100Hz task
open4416 5:8116016abee0 48 CANMessage can_msg_Rx;
open4416 5:8116016abee0 49 CANMessage can_msg_Tx;
open4416 2:c7a3a8c1aeed 50
open4416 2:c7a3a8c1aeed 51 void timer1_interrupt(void)
open4416 2:c7a3a8c1aeed 52 {
open4416 6:fbe401163489 53 HSTick += 1;
open4416 6:fbe401163489 54 LSTick += 1;
open4416 6:fbe401163489 55 if (HSTick > 9) { // 100Hz
open4416 6:fbe401163489 56 HST_EXFL = 1;
open4416 6:fbe401163489 57 HSTick = 0;
open4416 6:fbe401163489 58 }
open4416 6:fbe401163489 59 if (LSTick > 99) { // 10Hz
open4416 6:fbe401163489 60 LST_EXFL = 1;
open4416 6:fbe401163489 61 LSTick = 0;
open4416 6:fbe401163489 62 }
open4416 2:c7a3a8c1aeed 63 }
open4416 0:c4747ebbe0b4 64
open4416 0:c4747ebbe0b4 65 int main()
open4416 0:c4747ebbe0b4 66 {
open4416 6:fbe401163489 67 //Init CAN network
open4416 9:c99eeafa6fa3 68 CAN_init(); // Note now in Gloable test mode only for testing 2019/11/17
open4416 6:fbe401163489 69
open4416 6:fbe401163489 70 //Start House keeping task
open4416 6:fbe401163489 71 printf("VDU start up, pend for module online\n");
open4416 19:d68f21173c23 72 #ifdef IMU_direct
open4416 19:d68f21173c23 73 drdy.fall(&IMU_isr); //IMU interrupt service
open4416 19:d68f21173c23 74 spi2.format(16, 3);
open4416 19:d68f21173c23 75 //spi2.frequency(); //As default
open4416 19:d68f21173c23 76 #endif
open4416 21:e01a019fae2f 77
open4416 21:e01a019fae2f 78 wait_ms(2500);
open4416 6:fbe401163489 79 ticker1.attach_us(&timer1_interrupt, 1000); //1 ms Systick
open4416 6:fbe401163489 80 while(1) {
open4416 6:fbe401163489 81
open4416 6:fbe401163489 82 // Do high speed loop
open4416 6:fbe401163489 83 if (HST_EXFL == 1) {
open4416 6:fbe401163489 84 HST_EXFL = 0;
open4416 6:fbe401163489 85
open4416 9:c99eeafa6fa3 86 // Get IMU, Auxs, Max Temperature
open4416 9:c99eeafa6fa3 87 Cooler();
open4416 6:fbe401163489 88 IMU_read();
open4416 6:fbe401163489 89 Aux_read();
open4416 9:c99eeafa6fa3 90 Module_WD();
open4416 6:fbe401163489 91
open4416 6:fbe401163489 92 // Run state machine
open4416 6:fbe401163489 93 switch (VDU_STAT) {
open4416 6:fbe401163489 94
open4416 6:fbe401163489 95 case VDU_PowerOn:
open4416 6:fbe401163489 96 /* Power on state
open4416 6:fbe401163489 97 * Description:
open4416 6:fbe401163489 98 * Simple start up sequence will be done here
open4416 6:fbe401163489 99 * Do:
open4416 6:fbe401163489 100 * VDU internal POST
open4416 6:fbe401163489 101 * Wait till modules + PSU online
open4416 6:fbe401163489 102 * To VDU_Idle (RTD off):
open4416 6:fbe401163489 103 * Prepare for 4WD main program
open4416 6:fbe401163489 104 * To VDU_Fault:
open4416 6:fbe401163489 105 * Run the error handling service
open4416 6:fbe401163489 106 */
open4416 6:fbe401163489 107
open4416 6:fbe401163489 108 //Basic IMU test
open4416 6:fbe401163489 109 POST();
open4416 22:4f764f7cd0b3 110 // printf("%d,%d,%d,%d,%d\n",FL_online,FR_online,RL_online,RR_online,PSU_online);
open4416 6:fbe401163489 111 //Check if state transition only when all module online
open4416 14:bcf5cb4d08a5 112 if (VDU_FLT != 0) { //Check if any error
open4416 14:bcf5cb4d08a5 113 VDU_STAT = VDU_Fault;
open4416 20:e9daae390513 114 RST_HMI = 0; //Ensure new RST action after error
open4416 14:bcf5cb4d08a5 115 printf("POST Fault\n");
open4416 14:bcf5cb4d08a5 116 FLT_print = 1;
open4416 14:bcf5cb4d08a5 117 } else if ((FL_online*FR_online*RL_online*RR_online*PSU_online)!=0) {
open4416 14:bcf5cb4d08a5 118 // } else if (1) { //2019/11/30 only use for force online debug
open4416 14:bcf5cb4d08a5 119 //All module online & POST pass
open4416 14:bcf5cb4d08a5 120 VDU_STAT = VDU_Idle;
open4416 14:bcf5cb4d08a5 121 printf("All module online, VDU now Idle\n");
open4416 6:fbe401163489 122 } //Else keep in state VDU_PowerOn
open4416 6:fbe401163489 123 break;
open4416 6:fbe401163489 124
open4416 6:fbe401163489 125 case VDU_Idle:
open4416 6:fbe401163489 126 /* Controller Idle state
open4416 6:fbe401163489 127 * Description:
open4416 6:fbe401163489 128 * Normal latched state, wait for RTD_HMI set from PSU
open4416 6:fbe401163489 129 * 4WD in running but output mask to 0
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_Run:
open4416 6:fbe401163489 135 * Initialize parameters for start up, set 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 //Forced RTD_HMI for debug purpose 2019/11/14
open4416 13:ac024885d0bf 141 // RTD_HMI = 1; //Should be set if can bus received data
open4416 6:fbe401163489 142 //Forced RTD_HMI for debug purpose 2019/11/14
open4416 6:fbe401163489 143
open4416 6:fbe401163489 144 RUNT(); //Run test
open4416 6:fbe401163489 145 AWD(); //AWD main program
open4416 1:8a9ac822aab7 146
open4416 14:bcf5cb4d08a5 147 if (VDU_FLT != 0) { //Check if any error
open4416 6:fbe401163489 148 VDU_STAT = VDU_Fault;
open4416 20:e9daae390513 149 RST_HMI = 0; //Ensure new RST action after error
open4416 7:f674ef860c9c 150 printf("Idle 2 Fault\n");
open4416 6:fbe401163489 151 FLT_print = 1;
open4416 6:fbe401163489 152 } else if (RTD_HMI != 0) { //Or command to run threw PSU
open4416 6:fbe401163489 153 //Prepare to send out RTD and start motor
open4416 6:fbe401163489 154 Idle2Run();
open4416 6:fbe401163489 155 VDU_STAT = VDU_Run;
open4416 6:fbe401163489 156 printf("Idle 2 Run\n");
open4416 6:fbe401163489 157 } //Else keep in state
open4416 6:fbe401163489 158 break;
open4416 6:fbe401163489 159
open4416 6:fbe401163489 160 case VDU_Run:
open4416 6:fbe401163489 161 /* Controller Run state
open4416 6:fbe401163489 162 * Description:
open4416 6:fbe401163489 163 * Normal latched state, after RTD_HMI is set from PSU
open4416 6:fbe401163489 164 * Same to Idle state except RTD_cmd is set
open4416 6:fbe401163489 165 * Do:
open4416 6:fbe401163489 166 * 4WD controller
open4416 6:fbe401163489 167 * Check:
open4416 6:fbe401163489 168 * RUN faults if any
open4416 6:fbe401163489 169 * To VDU_Idle:
open4416 6:fbe401163489 170 * Initialize parameters for idling, reset RTD_cmd
open4416 6:fbe401163489 171 * To VDU_Fault:
open4416 6:fbe401163489 172 * Run the error handling service
open4416 6:fbe401163489 173 */
open4416 6:fbe401163489 174
open4416 6:fbe401163489 175 RUNT(); //Run test
open4416 6:fbe401163489 176 AWD(); //AWD main program
open4416 6:fbe401163489 177
open4416 6:fbe401163489 178 //Temporary debug posting area 2019/11/14
open4416 6:fbe401163489 179 //printf("%d,%d\n", Encoder_cnt, Encoder_del);
open4416 6:fbe401163489 180 //printf("%d\n\r", (int16_t)Tmodule);//
open4416 6:fbe401163489 181 //printf("%d\n\r", (int16_t)Vbus);
open4416 0:c4747ebbe0b4 182
open4416 14:bcf5cb4d08a5 183 if (VDU_FLT != 0) { //Check if any error
open4416 6:fbe401163489 184 VDU_STAT = VDU_Fault;
open4416 20:e9daae390513 185 RST_HMI = 0; //Ensure new RST action after error
open4416 7:f674ef860c9c 186 printf("Run 2 Fault\n");
open4416 6:fbe401163489 187 FLT_print = 1;
open4416 6:fbe401163489 188 } else if (RTD_HMI != 1) { //Or command to stop threw can bus
open4416 6:fbe401163489 189 Run2Idle();
open4416 6:fbe401163489 190 VDU_STAT = VDU_Idle;
open4416 6:fbe401163489 191 printf("Run 2 Idle\n");
open4416 6:fbe401163489 192 } //Else keep in state
open4416 6:fbe401163489 193 break;
open4416 6:fbe401163489 194
open4416 6:fbe401163489 195 case VDU_Fault:
open4416 6:fbe401163489 196 /* Controller Fault state
open4416 6:fbe401163489 197 * Description:
open4416 6:fbe401163489 198 * Fault latched state if any faults is detected
open4416 6:fbe401163489 199 * Same to Idle state except keep till RTD_HMI reset
open4416 6:fbe401163489 200 * Do:
open4416 6:fbe401163489 201 * Nothing, like a piece of shit
open4416 6:fbe401163489 202 * Check:
open4416 6:fbe401163489 203 * RUN faults if any
open4416 6:fbe401163489 204 * To VDU_PowerOn:
open4416 6:fbe401163489 205 * Restart VDU
open4416 6:fbe401163489 206 */
open4416 6:fbe401163489 207
open4416 6:fbe401163489 208 RUNT(); //Run test
open4416 6:fbe401163489 209
open4416 6:fbe401163489 210 if (RST_HMI == 1) { //PSU reset to clear error
open4416 18:780366f2534c 211 RST_HMI = 0;
open4416 6:fbe401163489 212 RST_cmd = 1;
open4416 14:bcf5cb4d08a5 213 FLT_print = 0; //Stop error printing
open4416 22:4f764f7cd0b3 214 // FL_online = 5; // 0 indicate detection timeout
open4416 22:4f764f7cd0b3 215 // FR_online = 5; // reset value is 3 to hold for 0.03sec
open4416 22:4f764f7cd0b3 216 // RL_online = 5; // -1 per 100Hz task
open4416 22:4f764f7cd0b3 217 // RR_online = 5;
open4416 22:4f764f7cd0b3 218 // PSU_online = 5;
open4416 22:4f764f7cd0b3 219 VDU_FLT = 0;
open4416 6:fbe401163489 220 VDU_STAT = VDU_PowerOn;
open4416 14:bcf5cb4d08a5 221 printf("VDU rebooting...\n");
open4416 21:e01a019fae2f 222 printf("QDrive rebooting...\n");
open4416 6:fbe401163489 223 } //Else keep in state
open4416 6:fbe401163489 224 break;
open4416 6:fbe401163489 225 }
open4416 6:fbe401163489 226
open4416 7:f674ef860c9c 227 // Shit out torque distribution and special command
open4416 6:fbe401163489 228 if(VDU_STAT == VDU_Run) {
open4416 6:fbe401163489 229 //Allow output torque
open4416 6:fbe401163489 230 Tx_Tcmd_CAN1();
open4416 6:fbe401163489 231 } else if(RST_cmd != 0) {
open4416 7:f674ef860c9c 232 //Send out reset cmd once
open4416 6:fbe401163489 233 Tx_CLRerr_CAN1();
open4416 6:fbe401163489 234 } else {
open4416 6:fbe401163489 235 //Force RTD off when not in VDU_Run
open4416 6:fbe401163489 236 Tx_Estop_CAN1();
open4416 6:fbe401163489 237 }
open4416 6:fbe401163489 238
open4416 14:bcf5cb4d08a5 239 //2019/12/18 Add here to test IMU, newer version use inturrupt get data
open4416 14:bcf5cb4d08a5 240 // pc.printf("%.3f,%.3f,%.3f\n\r", imu.ahrsProcessedData.attitude[0], imu.ahrsProcessedData.attitude[1], imu.ahrsProcessedData.attitude[2]);
open4416 14:bcf5cb4d08a5 241 // pc.printf("%.3f,%.3f,%.3f\n\r", imu.imuProcessedData.rate[0], imu.imuProcessedData.rate[1], imu.imuProcessedData.rate[2]);
open4416 14:bcf5cb4d08a5 242 // pc.printf("%.3f,%.3f,%.3f\n\r", imu.imuProcessedData.accel[0], imu.imuProcessedData.accel[1], imu.imuProcessedData.accel[2]);
open4416 19:d68f21173c23 243 // pc.printf("%.3f,%.3f,%.3f\n\r", YR_imu, Ax_imu, Ay_imu);
open4416 20:e9daae390513 244 // pc.printf("%.3f,%.3f,%.3f\n\r", Roll_imu, Pitch_imu, Yaw_imu);
open4416 22:4f764f7cd0b3 245 // pc.printf("%.3f\n\r", Trq_HMI*100.0f);
open4416 14:bcf5cb4d08a5 246
open4416 6:fbe401163489 247 }
open4416 7:f674ef860c9c 248 // End of high speed loop
open4416 6:fbe401163489 249
open4416 9:c99eeafa6fa3 250 // Do low speed state reporting 10 Hz
open4416 6:fbe401163489 251 if (LST_EXFL == 1) {
open4416 6:fbe401163489 252 LST_EXFL = 0;
open4416 18:780366f2534c 253 //Cooling
open4416 8:f8b1402c8f3c 254 Cooler();
open4416 18:780366f2534c 255
open4416 18:780366f2534c 256 //Print low speed data on CAN
open4416 5:8116016abee0 257 Tx_Qdrv_CAN1();
open4416 6:fbe401163489 258
open4416 18:780366f2534c 259 // Print out error mesagge if exist, 0.5Hz repeative
open4416 6:fbe401163489 260 if(FLT_print != 0) {
open4416 18:780366f2534c 261 //Merge Faults
open4416 22:4f764f7cd0b3 262
open4416 22:4f764f7cd0b3 263 //USE THIS FOR FULL FUNCTION
open4416 22:4f764f7cd0b3 264 // FLT_Post = FL_FLT_Post|FR_FLT_Post|RL_FLT_Post|RR_FLT_Post;
open4416 22:4f764f7cd0b3 265 // FLT_Run = FL_FLT_Run|FR_FLT_Run|RL_FLT_Run|RR_FLT_Run;
open4416 22:4f764f7cd0b3 266
open4416 22:4f764f7cd0b3 267 //ONLY FOR TEST TODO
open4416 22:4f764f7cd0b3 268 FLT_Post = RL_FLT_Post; // Use only for single module test
open4416 22:4f764f7cd0b3 269 FLT_Run = RL_FLT_Run; //2020/3/10
open4416 18:780366f2534c 270
open4416 18:780366f2534c 271 //UART
open4416 7:f674ef860c9c 272 FLT_print_cnt += FLT_print;
open4416 6:fbe401163489 273 if(FLT_print_cnt > 19) {
open4416 18:780366f2534c 274 if(FLT_Post!=0)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 18:780366f2534c 275 if(FLT_Run!=0)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 18:780366f2534c 276 if(VDU_FLT!=0)printf("VDU\n0x%04X\n\n", VDU_FLT);
open4416 6:fbe401163489 277 FLT_print_cnt = 0;
open4416 6:fbe401163489 278 }
open4416 18:780366f2534c 279
open4416 18:780366f2534c 280 //LED
open4416 18:780366f2534c 281 if(Ind_refresh) {
open4416 18:780366f2534c 282 // Refresh data for LED indication after run threw
open4416 18:780366f2534c 283 FLT_Post_ind = FLT_Post;
open4416 18:780366f2534c 284 FLT_Run_ind = FLT_Run;
open4416 18:780366f2534c 285 VDU_FLT_ind = VDU_FLT;
open4416 18:780366f2534c 286 Ind_refresh = 0; // Set after run threw all error bits
open4416 18:780366f2534c 287 }
open4416 18:780366f2534c 288 } else {
open4416 18:780366f2534c 289 // Clear & stop LED when no faults
open4416 18:780366f2534c 290 FLT_Post_ind = 0;
open4416 18:780366f2534c 291 FLT_Run_ind = 0;
open4416 18:780366f2534c 292 VDU_FLT_ind = 0;
open4416 18:780366f2534c 293 Repeat = 0U;
open4416 18:780366f2534c 294 Phase = 0U;
open4416 18:780366f2534c 295 Blk_n = 0U;
open4416 6:fbe401163489 296 }
open4416 14:bcf5cb4d08a5 297
open4416 18:780366f2534c 298 // Blinky output
open4416 18:780366f2534c 299 if (VDU_STAT != VDU_PowerOn) {
open4416 18:780366f2534c 300 // Case after poweron (all module online) or fault(s) occur
open4416 18:780366f2534c 301 Ind_refresh = IndicationKernel(
open4416 18:780366f2534c 302 &Pattern,
open4416 18:780366f2534c 303 &Repeat,
open4416 18:780366f2534c 304 &Phase,
open4416 18:780366f2534c 305 &FLT_Post_ind,
open4416 18:780366f2534c 306 &FLT_Run_ind,
open4416 18:780366f2534c 307 &VDU_FLT_ind);
open4416 18:780366f2534c 308 LED = Indication(Pattern, &Repeat, &Blk_n);
open4416 18:780366f2534c 309 Fault_Ind = LED;
open4416 18:780366f2534c 310 } else {
open4416 18:780366f2534c 311 // Case no fault while waiting for all module online
open4416 18:780366f2534c 312 LED = !LED; //Fast 5Hz blinky indicate the activity
open4416 18:780366f2534c 313 Fault_Ind = LED;
open4416 18:780366f2534c 314 }
open4416 7:f674ef860c9c 315 }
open4416 7:f674ef860c9c 316 // End of low speed state reporting
open4416 6:fbe401163489 317
open4416 7:f674ef860c9c 318 } // end of while
open4416 0:c4747ebbe0b4 319 }
open4416 0:c4747ebbe0b4 320
open4416 6:fbe401163489 321 void Idle2Run(void)
open4416 6:fbe401163489 322 {
open4416 6:fbe401163489 323 RTD_cmd = 1;
open4416 6:fbe401163489 324 }
open4416 6:fbe401163489 325
open4416 6:fbe401163489 326 void Run2Idle(void)
open4416 6:fbe401163489 327 {
open4416 6:fbe401163489 328 RTD_cmd = 0;
open4416 6:fbe401163489 329 }
open4416 6:fbe401163489 330
open4416 6:fbe401163489 331 void POST(void)
open4416 6:fbe401163489 332 {
open4416 8:f8b1402c8f3c 333 //Check IMU status abnormal
open4416 18:780366f2534c 334 if(fabsf(YR_imu) > 250) { //half turn per sec, strange
open4416 6:fbe401163489 335 VDU_FLT |= IMUSTA_VDUFLTCode; //IMU status error
open4416 6:fbe401163489 336 }
open4416 6:fbe401163489 337 }
open4416 6:fbe401163489 338
open4416 6:fbe401163489 339 void RUNT(void)
open4416 6:fbe401163489 340 {
open4416 8:f8b1402c8f3c 341 //POST
open4416 6:fbe401163489 342 POST();
open4416 6:fbe401163489 343
open4416 8:f8b1402c8f3c 344 //Check module response timeout
open4416 6:fbe401163489 345 if((FL_online*FR_online*RL_online*RR_online) == 0) {
open4416 6:fbe401163489 346 VDU_FLT |= MODOFL_VDUFLTCode; //Module timeout
open4416 6:fbe401163489 347 }
open4416 6:fbe401163489 348 if(PSU_online == 0) {
open4416 6:fbe401163489 349 VDU_FLT |= PSUOFL_VDUFLTCode; //PSU timeout
open4416 6:fbe401163489 350 }
open4416 6:fbe401163489 351
open4416 20:e9daae390513 352 //Check ShutDrv voltage when running
open4416 22:4f764f7cd0b3 353 if(VDU_STAT == VDU_Run) {
open4416 8:f8b1402c8f3c 354 if(SDn_voltage < 8.0f) {
open4416 8:f8b1402c8f3c 355 VDU_FLT |= ShDVol_VDUFLTCode; //Shutdown circuit unclosed or uv
open4416 8:f8b1402c8f3c 356 }
open4416 8:f8b1402c8f3c 357 }
open4416 6:fbe401163489 358 }
open4416 6:fbe401163489 359
open4416 6:fbe401163489 360 void Aux_read(void)
open4416 6:fbe401163489 361 {
open4416 19:d68f21173c23 362 //Capture analog in at once imu_driver ver
open4416 13:ac024885d0bf 363 AUX_1_u16 = AUX_1.read_u16() >> 4U;
open4416 13:ac024885d0bf 364 AUX_2_u16 = AUX_2.read_u16() >> 4U;
open4416 13:ac024885d0bf 365 AUX_3_u16 = AUX_3.read_u16() >> 4U;
open4416 13:ac024885d0bf 366 AUX_4_u16 = AUX_4.read_u16() >> 4U;
open4416 8:f8b1402c8f3c 367 SDn_voltage = 18.81f*SDn_sense.read(); //Read in Shutdown circuit voltage in output end
open4416 13:ac024885d0bf 368 Brk_voltage = 5.5f*Brk_sense.read();
open4416 6:fbe401163489 369 }
open4416 6:fbe401163489 370
open4416 19:d68f21173c23 371 #ifdef IMU_direct
open4416 19:d68f21173c23 372 void IMU_isr(void)
open4416 19:d68f21173c23 373 {
open4416 19:d68f21173c23 374 //Start data transfer
open4416 19:d68f21173c23 375 uint8_t data_rx = 0;
open4416 19:d68f21173c23 376 uint16_t reg_VG = Read_VG<<8U;
open4416 19:d68f21173c23 377 cs = 0;
open4416 19:d68f21173c23 378 spi2.write(reg_VG);
open4416 19:d68f21173c23 379 while(data_rx < VG_len) {
open4416 19:d68f21173c23 380 uint16_t spi_data = spi2.write(0x0000);
open4416 19:d68f21173c23 381 switch(data_rx) {
open4416 19:d68f21173c23 382 case 0:
open4416 19:d68f21173c23 383 ahrsdata.status = spi_data;
open4416 19:d68f21173c23 384 break;
open4416 19:d68f21173c23 385 case 1:
open4416 19:d68f21173c23 386 case 2:
open4416 19:d68f21173c23 387 case 3:
open4416 19:d68f21173c23 388 ahrsdata.rate[data_rx - 1] = spi_data;
open4416 19:d68f21173c23 389 break;
open4416 19:d68f21173c23 390 case 4:
open4416 19:d68f21173c23 391 case 5:
open4416 19:d68f21173c23 392 case 6:
open4416 19:d68f21173c23 393 ahrsdata.accel[data_rx - 4] = spi_data;
open4416 19:d68f21173c23 394 break;
open4416 19:d68f21173c23 395 case 7:
open4416 19:d68f21173c23 396 ahrsdata.temperature = spi_data;
open4416 19:d68f21173c23 397 break;
open4416 19:d68f21173c23 398 case 8:
open4416 19:d68f21173c23 399 case 9:
open4416 19:d68f21173c23 400 case 10:
open4416 19:d68f21173c23 401 ahrsdata.attitude[data_rx - 8] = spi_data;
open4416 19:d68f21173c23 402 break;
open4416 19:d68f21173c23 403 default:
open4416 19:d68f21173c23 404 break;
open4416 19:d68f21173c23 405 }
open4416 19:d68f21173c23 406 ++data_rx;
open4416 19:d68f21173c23 407 }
open4416 19:d68f21173c23 408 cs = 1;
open4416 19:d68f21173c23 409 }
open4416 19:d68f21173c23 410 #endif
open4416 19:d68f21173c23 411
open4416 6:fbe401163489 412 void IMU_read(void)
open4416 6:fbe401163489 413 {
open4416 19:d68f21173c23 414 #ifndef IMU_direct
open4416 18:780366f2534c 415 //Get readings threw back ground, direction not checked 2019/12/20
open4416 14:bcf5cb4d08a5 416 YR_imu = imu.imuProcessedData.rate[2];
open4416 14:bcf5cb4d08a5 417 Ax_imu = imu.imuProcessedData.accel[0];
open4416 14:bcf5cb4d08a5 418 Ay_imu = imu.imuProcessedData.accel[1];
open4416 14:bcf5cb4d08a5 419 Roll_imu = imu.ahrsProcessedData.attitude[0];
open4416 14:bcf5cb4d08a5 420 Pitch_imu = imu.ahrsProcessedData.attitude[1];
open4416 19:d68f21173c23 421 Yaw_imu = imu.ahrsProcessedData.attitude[2];
open4416 19:d68f21173c23 422 #else
open4416 19:d68f21173c23 423 YR_imu = ahrsdata.rate[2]/GYRO2F;
open4416 19:d68f21173c23 424 Ax_imu = ahrsdata.accel[0]/ACCL2F;
open4416 19:d68f21173c23 425 Ay_imu = ahrsdata.accel[1]/ACCL2F;
open4416 19:d68f21173c23 426 Roll_imu = ahrsdata.attitude[0]/AHRS2F;
open4416 19:d68f21173c23 427 Pitch_imu = ahrsdata.attitude[1]/AHRS2F;
open4416 19:d68f21173c23 428 #endif
open4416 6:fbe401163489 429 }
open4416 6:fbe401163489 430
open4416 6:fbe401163489 431 void AWD(void)
open4416 6:fbe401163489 432 {
open4416 8:f8b1402c8f3c 433 if(AWD_HMI) {
open4416 8:f8b1402c8f3c 434 // A simple version is put here for reading
open4416 8:f8b1402c8f3c 435 Vb_est = 0.25f * (FL_W_ele + FR_W_ele + RL_W_ele + RR_W_ele);
open4416 8:f8b1402c8f3c 436 YR_ref = Steer_HMI*d2r*Vb_est/(Base+EG*Vb_est*Vb_est);
open4416 8:f8b1402c8f3c 437 Mz_reg = (YR_ref - YR_imu) * K_yaw; //K_yaw unfinished 2019/11/15
open4416 8:f8b1402c8f3c 438 sig = 0.5f - HCG*Trq_HMI/(Base*Rwhl*Mtot*g0);
open4416 8:f8b1402c8f3c 439 FL_Tcmd = (0.5f*Trq_HMI - Mz_reg*Rwhl/Track)*sig;
open4416 8:f8b1402c8f3c 440 FR_Tcmd = (0.5f*Trq_HMI + Mz_reg*Rwhl/Track)*sig;
open4416 8:f8b1402c8f3c 441 RL_Tcmd = (0.5f*Trq_HMI - Mz_reg*Rwhl/Track)*(1.0f-sig);
open4416 8:f8b1402c8f3c 442 RR_Tcmd = (0.5f*Trq_HMI + Mz_reg*Rwhl/Track)*(1.0f-sig);
open4416 8:f8b1402c8f3c 443 } else {
open4416 8:f8b1402c8f3c 444 FL_Tcmd = 0.25f*Trq_HMI;
open4416 8:f8b1402c8f3c 445 FR_Tcmd = 0.25f*Trq_HMI;
open4416 8:f8b1402c8f3c 446 RL_Tcmd = 0.25f*Trq_HMI;
open4416 8:f8b1402c8f3c 447 RR_Tcmd = 0.25f*Trq_HMI;
open4416 8:f8b1402c8f3c 448 }
open4416 8:f8b1402c8f3c 449 }
open4416 6:fbe401163489 450
open4416 8:f8b1402c8f3c 451 void ASL(void)
open4416 8:f8b1402c8f3c 452 {
open4416 8:f8b1402c8f3c 453 //Filter out each motor W_ele and get approximate accel, compare with IMU
open4416 8:f8b1402c8f3c 454 //Policy is set as "degrade only" coefficient exp(K_ASL*delAccel)
open4416 8:f8b1402c8f3c 455 //Fill out if enough time
open4416 6:fbe401163489 456 }
open4416 6:fbe401163489 457
open4416 5:8116016abee0 458 void Rx_CAN1(void)
open4416 5:8116016abee0 459 {
open4416 18:780366f2534c 460 // LED = 1;
open4416 7:f674ef860c9c 461 int16_t tmp;
open4416 7:f674ef860c9c 462
open4416 5:8116016abee0 463 if(can1.read(can_msg_Rx)) {
open4416 7:f674ef860c9c 464 switch(can_msg_Rx.id) { //Filtered input message
open4416 7:f674ef860c9c 465 // Start of 100Hz msg
open4416 8:f8b1402c8f3c 466 case FL_HSB_ID://1
open4416 7:f674ef860c9c 467 //HSB from FL motor drive
open4416 20:e9daae390513 468 FL_DSM = can_msg_Rx.data[6] & 0x03; //Get DSM_STAT
open4416 7:f674ef860c9c 469 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 7:f674ef860c9c 470 FL_W_ele = tmp*1.0f;
open4416 7:f674ef860c9c 471 tmp = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 472 FL_Trq_fil3 = tmp * 0.01f;
open4416 7:f674ef860c9c 473 tmp = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 474 FL_Trq_est = tmp * 0.01f;
open4416 20:e9daae390513 475 FL_online = 5;
open4416 18:780366f2534c 476 //If fault
open4416 18:780366f2534c 477 if(FL_DSM == 3U) {
open4416 22:4f764f7cd0b3 478 // USE THIS FOR FULL FUNCTION
open4416 22:4f764f7cd0b3 479 // VDU_FLT |= DSM_VDUFLTCode; //DSM Fault
open4416 18:780366f2534c 480 }
open4416 7:f674ef860c9c 481 break;
open4416 7:f674ef860c9c 482
open4416 8:f8b1402c8f3c 483 case FR_HSB_ID://2
open4416 7:f674ef860c9c 484 //HSB from FR motor drive
open4416 20:e9daae390513 485 FR_DSM = can_msg_Rx.data[6] & 0x03; //Get DSM_STAT
open4416 7:f674ef860c9c 486 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 7:f674ef860c9c 487 FR_W_ele = tmp*1.0f;
open4416 7:f674ef860c9c 488 tmp = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 489 FR_Trq_fil3 = tmp * 0.01f;
open4416 7:f674ef860c9c 490 tmp = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 491 FR_Trq_est = tmp * 0.01f;
open4416 20:e9daae390513 492 FR_online = 5;
open4416 18:780366f2534c 493 if(FR_DSM == 3U) {
open4416 22:4f764f7cd0b3 494 // USE THIS FOR FULL FUNCTION
open4416 22:4f764f7cd0b3 495 // VDU_FLT |= DSM_VDUFLTCode; //DSM Fault
open4416 18:780366f2534c 496 }
open4416 7:f674ef860c9c 497 break;
open4416 7:f674ef860c9c 498
open4416 8:f8b1402c8f3c 499 case RL_HSB_ID://3
open4416 7:f674ef860c9c 500 //HSB from RL motor drive
open4416 20:e9daae390513 501 RL_DSM = can_msg_Rx.data[6] & 0x03; //Get DSM_STAT
open4416 7:f674ef860c9c 502 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 7:f674ef860c9c 503 RL_W_ele = tmp*1.0f;
open4416 7:f674ef860c9c 504 tmp = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 505 RL_Trq_fil3 = tmp * 0.01f;
open4416 7:f674ef860c9c 506 tmp = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 507 RL_Trq_est = tmp * 0.01f;
open4416 20:e9daae390513 508 RL_online = 5;
open4416 18:780366f2534c 509 if(RL_DSM == 3U) {
open4416 18:780366f2534c 510 VDU_FLT |= DSM_VDUFLTCode; //DSM Fault
open4416 18:780366f2534c 511 }
open4416 7:f674ef860c9c 512 break;
open4416 7:f674ef860c9c 513
open4416 8:f8b1402c8f3c 514 case RR_HSB_ID://4
open4416 7:f674ef860c9c 515 //HSB from RR motor drive
open4416 20:e9daae390513 516 RR_DSM = can_msg_Rx.data[6] & 0x03; //Get DSM_STAT
open4416 7:f674ef860c9c 517 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 7:f674ef860c9c 518 RR_W_ele = tmp*1.0f;
open4416 7:f674ef860c9c 519 tmp = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 520 RR_Trq_fil3 = tmp * 0.01f;
open4416 7:f674ef860c9c 521 tmp = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 522 RR_Trq_est = tmp * 0.01f;
open4416 20:e9daae390513 523 RR_online = 5;
open4416 18:780366f2534c 524 if(RR_DSM == 3U) {
open4416 22:4f764f7cd0b3 525 // USE THIS FOR FULL FUNCTION
open4416 22:4f764f7cd0b3 526 // VDU_FLT |= DSM_VDUFLTCode; //DSM Fault
open4416 18:780366f2534c 527 }
open4416 7:f674ef860c9c 528 break;
open4416 7:f674ef860c9c 529
open4416 8:f8b1402c8f3c 530 case HMI_cmd_ID://5
open4416 7:f674ef860c9c 531 //HMI command from PSU
open4416 7:f674ef860c9c 532 AWD_HMI = can_msg_Rx.data[6] & 0x01; //Get AWD switch
open4416 7:f674ef860c9c 533 RST_HMI = can_msg_Rx.data[5] & 0x01; //Get RST request
open4416 7:f674ef860c9c 534 RTD_HMI = can_msg_Rx.data[4] & 0x01; //Get RTD switch
open4416 7:f674ef860c9c 535 tmp = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 536 Steer_HMI = tmp * 0.01f;
open4416 7:f674ef860c9c 537 tmp = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 538 Trq_HMI = tmp * 0.01f;
open4416 20:e9daae390513 539 PSU_online = 5;
open4416 7:f674ef860c9c 540 break;
open4416 7:f674ef860c9c 541 // end of 100Hz msg
open4416 7:f674ef860c9c 542
open4416 7:f674ef860c9c 543 // Start of 10Hz msg
open4416 8:f8b1402c8f3c 544 case FL_LSB_ID://6
open4416 7:f674ef860c9c 545 //LSB from FL motor drive
open4416 7:f674ef860c9c 546 tmp = can_msg_Rx.data[7] << 8 | can_msg_Rx.data[6];
open4416 7:f674ef860c9c 547 FL_Tmotor = tmp*0.01f;
open4416 7:f674ef860c9c 548 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 7:f674ef860c9c 549 FL_Tmodule = tmp*0.01f;
open4416 7:f674ef860c9c 550 FL_FLT_Run = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 551 FL_FLT_Post = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 552 break;
open4416 7:f674ef860c9c 553
open4416 8:f8b1402c8f3c 554 case FR_LSB_ID://7
open4416 7:f674ef860c9c 555 //LSB from FR motor drive
open4416 7:f674ef860c9c 556 tmp = can_msg_Rx.data[7] << 8 | can_msg_Rx.data[6];
open4416 7:f674ef860c9c 557 FR_Tmotor = tmp*0.01f;
open4416 7:f674ef860c9c 558 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 7:f674ef860c9c 559 FR_Tmodule = tmp*0.01f;
open4416 7:f674ef860c9c 560 FR_FLT_Run = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 561 FR_FLT_Post = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 562 break;
open4416 7:f674ef860c9c 563
open4416 8:f8b1402c8f3c 564 case RL_LSB_ID://8
open4416 7:f674ef860c9c 565 //LSB from RL motor drive
open4416 7:f674ef860c9c 566 tmp = can_msg_Rx.data[7] << 8 | can_msg_Rx.data[6];
open4416 7:f674ef860c9c 567 RL_Tmotor = tmp*0.01f;
open4416 7:f674ef860c9c 568 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 7:f674ef860c9c 569 RL_Tmodule = tmp*0.01f;
open4416 7:f674ef860c9c 570 RL_FLT_Run = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 7:f674ef860c9c 571 RL_FLT_Post = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 572 break;
open4416 7:f674ef860c9c 573
open4416 8:f8b1402c8f3c 574 case RR_LSB_ID://9
open4416 7:f674ef860c9c 575 //LSB from RR motor drive
open4416 7:f674ef860c9c 576 tmp = can_msg_Rx.data[7] << 8 | can_msg_Rx.data[6];
open4416 8:f8b1402c8f3c 577 RR_Tmotor = tmp*0.01f;
open4416 7:f674ef860c9c 578 tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4];
open4416 8:f8b1402c8f3c 579 RR_Tmodule = tmp*0.01f;
open4416 8:f8b1402c8f3c 580 RR_FLT_Run = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2];
open4416 8:f8b1402c8f3c 581 RR_FLT_Post = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0];
open4416 7:f674ef860c9c 582 break;
open4416 7:f674ef860c9c 583 // end of 10Hz msg
open4416 7:f674ef860c9c 584 }
open4416 5:8116016abee0 585 }
open4416 18:780366f2534c 586 // LED = 0;
open4416 5:8116016abee0 587 }
open4416 5:8116016abee0 588
open4416 6:fbe401163489 589 void Tx_CLRerr_CAN1(void)
open4416 2:c7a3a8c1aeed 590 {
open4416 9:c99eeafa6fa3 591 Tx_Estop_CAN1(); //disable as default
open4416 20:e9daae390513 592 RST_cmd = 0; //clear out one shot
open4416 6:fbe401163489 593 }
open4416 6:fbe401163489 594
open4416 6:fbe401163489 595 void Tx_Estop_CAN1(void)
open4416 6:fbe401163489 596 {
open4416 9:c99eeafa6fa3 597 RTD_cmd = 0; //force disable
open4416 6:fbe401163489 598 Tx_Tcmd_CAN1();
open4416 2:c7a3a8c1aeed 599 }
open4416 2:c7a3a8c1aeed 600
open4416 9:c99eeafa6fa3 601 void Tx_Tcmd_CAN1(void) // 100 Hz
open4416 2:c7a3a8c1aeed 602 {
open4416 7:f674ef860c9c 603 int16_t tmp;
open4416 7:f674ef860c9c 604 tmp = (int16_t) (FL_Tcmd * 100.0f);
open4416 7:f674ef860c9c 605 temp_msg[0] = tmp;
open4416 7:f674ef860c9c 606 temp_msg[1] = tmp >> 8U;
open4416 7:f674ef860c9c 607 temp_msg[2] = RTD_cmd;
open4416 21:e01a019fae2f 608 temp_msg[3] = RST_cmd;
open4416 20:e9daae390513 609 // temp_msg[3] = 0U; // 2020/3/4 add to disable HMI reseting Driver
open4416 14:bcf5cb4d08a5 610 temp_msg[4] = 0U;
open4416 14:bcf5cb4d08a5 611 temp_msg[5] = 0U;
open4416 14:bcf5cb4d08a5 612 temp_msg[6] = 0U;
open4416 14:bcf5cb4d08a5 613 temp_msg[7] = 0U;
open4416 8:f8b1402c8f3c 614 can_msg_Tx = CANMessage(FL_CMD_ID,temp_msg,8,CANData,CANStandard);
open4416 6:fbe401163489 615 CANpendTX();
open4416 6:fbe401163489 616 can1.write(can_msg_Tx);
open4416 6:fbe401163489 617
open4416 7:f674ef860c9c 618 tmp = (int16_t) (FR_Tcmd * 100.0f);
open4416 7:f674ef860c9c 619 temp_msg[0] = tmp;
open4416 7:f674ef860c9c 620 temp_msg[1] = tmp >> 8U;
open4416 8:f8b1402c8f3c 621 can_msg_Tx = CANMessage(FR_CMD_ID,temp_msg,8,CANData,CANStandard);
open4416 6:fbe401163489 622 CANpendTX();
open4416 6:fbe401163489 623 can1.write(can_msg_Tx);
open4416 6:fbe401163489 624
open4416 7:f674ef860c9c 625 tmp = (int16_t) (RL_Tcmd * 100.0f);
open4416 7:f674ef860c9c 626 temp_msg[0] = tmp;
open4416 7:f674ef860c9c 627 temp_msg[1] = tmp >> 8U;
open4416 8:f8b1402c8f3c 628 can_msg_Tx = CANMessage(RL_CMD_ID,temp_msg,8,CANData,CANStandard);
open4416 6:fbe401163489 629 CANpendTX();
open4416 6:fbe401163489 630 can1.write(can_msg_Tx);
open4416 6:fbe401163489 631
open4416 7:f674ef860c9c 632 tmp = (int16_t) (RR_Tcmd * 100.0f);
open4416 7:f674ef860c9c 633 temp_msg[0] = tmp;
open4416 7:f674ef860c9c 634 temp_msg[1] = tmp >> 8U;
open4416 8:f8b1402c8f3c 635 can_msg_Tx = CANMessage(RR_CMD_ID,temp_msg,8,CANData,CANStandard);
open4416 6:fbe401163489 636 CANpendTX();
open4416 6:fbe401163489 637 can1.write(can_msg_Tx);
open4416 6:fbe401163489 638
open4416 2:c7a3a8c1aeed 639 }
open4416 2:c7a3a8c1aeed 640
open4416 6:fbe401163489 641 void Tx_Qdrv_CAN1(void) // 10 Hz
open4416 0:c4747ebbe0b4 642 {
open4416 8:f8b1402c8f3c 643 int16_t tmp;
open4416 6:fbe401163489 644 // Auxilary sensor reading shitting out
open4416 7:f674ef860c9c 645 temp_msg[0] = AUX_1_u16;
open4416 7:f674ef860c9c 646 temp_msg[1] = AUX_1_u16 >> 8U;
open4416 7:f674ef860c9c 647 temp_msg[2] = AUX_2_u16;
open4416 7:f674ef860c9c 648 temp_msg[3] = AUX_2_u16 >> 8U;
open4416 7:f674ef860c9c 649 temp_msg[4] = AUX_3_u16;
open4416 7:f674ef860c9c 650 temp_msg[5] = AUX_3_u16 >> 8U;
open4416 7:f674ef860c9c 651 temp_msg[6] = AUX_4_u16;
open4416 7:f674ef860c9c 652 temp_msg[7] = AUX_4_u16 >> 8U;
open4416 6:fbe401163489 653 can_msg_Tx = CANMessage(AUX_sense_ID,temp_msg,8,CANData,CANStandard);
open4416 6:fbe401163489 654 CANpendTX();
open4416 6:fbe401163489 655 can1.write(can_msg_Tx);
open4416 6:fbe401163489 656
open4416 6:fbe401163489 657 // Qdrive internal state shitting out
open4416 8:f8b1402c8f3c 658 temp_msg[0] = VDU_FLT;
open4416 8:f8b1402c8f3c 659 temp_msg[1] = VDU_FLT >> 8U;
open4416 8:f8b1402c8f3c 660 temp_msg[2] = VDU_STAT;
open4416 14:bcf5cb4d08a5 661 temp_msg[3] = (int8_t)(SDn_voltage*10.0f);
open4416 14:bcf5cb4d08a5 662 temp_msg[4] = (int8_t)(Brk_voltage*10.0f);
open4416 14:bcf5cb4d08a5 663 temp_msg[5] = 0U;
open4416 14:bcf5cb4d08a5 664 temp_msg[6] = 0U;
open4416 14:bcf5cb4d08a5 665 temp_msg[7] = 0U;
open4416 6:fbe401163489 666 can_msg_Tx = CANMessage(Qdrv_stat_ID,temp_msg,8,CANData,CANStandard);
open4416 6:fbe401163489 667 CANpendTX();
open4416 6:fbe401163489 668 can1.write(can_msg_Tx);
open4416 2:c7a3a8c1aeed 669
open4416 6:fbe401163489 670 // IMU attitude readings shitting out, 10Hz on CAN but 100Hz for internal use
open4416 8:f8b1402c8f3c 671 tmp = (int16_t) (YR_imu * 10000.0f);
open4416 8:f8b1402c8f3c 672 temp_msg[0] = tmp;
open4416 8:f8b1402c8f3c 673 temp_msg[1] = tmp >> 8U;
open4416 8:f8b1402c8f3c 674 tmp = (int16_t) (Roll_imu * 100.0f);
open4416 8:f8b1402c8f3c 675 temp_msg[2] = tmp;
open4416 8:f8b1402c8f3c 676 temp_msg[3] = tmp >> 8U;
open4416 8:f8b1402c8f3c 677 tmp = (int16_t) (Pitch_imu * 100.0f);
open4416 8:f8b1402c8f3c 678 temp_msg[4] = tmp;
open4416 8:f8b1402c8f3c 679 temp_msg[5] = tmp >> 8U;
open4416 8:f8b1402c8f3c 680 temp_msg[6] = (int8_t)Ax_imu;
open4416 8:f8b1402c8f3c 681 temp_msg[7] = (int8_t)Ay_imu;
open4416 6:fbe401163489 682 can_msg_Tx = CANMessage(IMU_sense_ID,temp_msg,8,CANData,CANStandard);
open4416 6:fbe401163489 683 CANpendTX();
open4416 6:fbe401163489 684 can1.write(can_msg_Tx);
open4416 6:fbe401163489 685 }
open4416 6:fbe401163489 686
open4416 6:fbe401163489 687 void CANpendTX(void)
open4416 6:fbe401163489 688 {
open4416 6:fbe401163489 689 //Pend till TX box has empty slot, timeout will generate error
open4416 6:fbe401163489 690 uint32_t timeout = 0;
open4416 6:fbe401163489 691 while(!(CAN1->TSR & CAN_TSR_TME_Msk)) {
open4416 6:fbe401163489 692 //Wait till non empty
open4416 6:fbe401163489 693 timeout += 1;
open4416 6:fbe401163489 694 if(timeout > 10000) {
open4416 6:fbe401163489 695 // Put some timeout error handler
open4416 6:fbe401163489 696 break;
open4416 6:fbe401163489 697 }
open4416 0:c4747ebbe0b4 698 }
open4416 0:c4747ebbe0b4 699 }
open4416 1:8a9ac822aab7 700
open4416 6:fbe401163489 701 void CAN_init(void)
open4416 6:fbe401163489 702 {
open4416 6:fbe401163489 703 //Set CAN system
open4416 6:fbe401163489 704 SET_BIT(CAN1->MCR, CAN_MCR_ABOM); // Enable auto reboot after bus off
open4416 6:fbe401163489 705 can1.filter(FL_HSB_ID,0xFFFF,CANStandard,0); // ID filter listing mode
open4416 6:fbe401163489 706 can1.filter(FR_HSB_ID,0xFFFF,CANStandard,1);
open4416 6:fbe401163489 707 can1.filter(RL_HSB_ID,0xFFFF,CANStandard,2);
open4416 6:fbe401163489 708 can1.filter(RR_HSB_ID,0xFFFF,CANStandard,3);
open4416 6:fbe401163489 709 can1.filter(FL_LSB_ID,0xFFFF,CANStandard,4);
open4416 6:fbe401163489 710 can1.filter(FR_LSB_ID,0xFFFF,CANStandard,5);
open4416 6:fbe401163489 711 can1.filter(RL_LSB_ID,0xFFFF,CANStandard,6);
open4416 6:fbe401163489 712 can1.filter(RR_LSB_ID,0xFFFF,CANStandard,7);
open4416 8:f8b1402c8f3c 713 can1.filter(HMI_cmd_ID,0xFFFF,CANStandard,8); // PSU online monitoring
open4416 14:bcf5cb4d08a5 714 // can1.mode(CAN::GlobalTest); // Add only for testing 2019/11/13
open4416 8:f8b1402c8f3c 715 can1.attach(&Rx_CAN1, CAN::RxIrq); // CAN1 Recieve Irq
open4416 6:fbe401163489 716 }
open4416 2:c7a3a8c1aeed 717
open4416 6:fbe401163489 718 void Module_WD(void)
open4416 6:fbe401163489 719 {
open4416 9:c99eeafa6fa3 720 //Module online dissipitive indicator
open4416 6:fbe401163489 721 if (FL_online != 0) {
open4416 6:fbe401163489 722 FL_online -= 1;
open4416 6:fbe401163489 723 }
open4416 6:fbe401163489 724 if (FR_online != 0) {
open4416 6:fbe401163489 725 FR_online -= 1;
open4416 6:fbe401163489 726 }
open4416 6:fbe401163489 727 if (RL_online != 0) {
open4416 6:fbe401163489 728 RL_online -= 1;
open4416 6:fbe401163489 729 }
open4416 6:fbe401163489 730 if (RR_online != 0) {
open4416 6:fbe401163489 731 RR_online -= 1;
open4416 6:fbe401163489 732 }
open4416 6:fbe401163489 733 if (PSU_online != 0) {
open4416 6:fbe401163489 734 PSU_online -= 1;
open4416 6:fbe401163489 735 }
open4416 6:fbe401163489 736 }
open4416 18:780366f2534c 737 uint8_t Indication( // Blink indicator in pattern * repeat
open4416 18:780366f2534c 738 uint8_t pattern,
open4416 18:780366f2534c 739 uint16_t*repeat,
open4416 18:780366f2534c 740 uint8_t*blk_n)
open4416 18:780366f2534c 741 {
open4416 18:780366f2534c 742 uint8_t out = 0;
open4416 18:780366f2534c 743 if(*repeat==0) return out; // reject repeat = 0 case, out = 0
open4416 18:780366f2534c 744 out = (pattern>>(*blk_n)) & 1U; // blink from LSB to MSB
open4416 18:780366f2534c 745 if(*blk_n < 7U) {
open4416 18:780366f2534c 746 *blk_n += 1U;
open4416 18:780366f2534c 747 } else { // a full pattern was passed
open4416 18:780366f2534c 748 *blk_n = 0U;
open4416 18:780366f2534c 749 *repeat >>= 1U;
open4416 18:780366f2534c 750 }
open4416 18:780366f2534c 751 return out;
open4416 18:780366f2534c 752 }
open4416 18:780366f2534c 753
open4416 18:780366f2534c 754 uint8_t IndicationKernel( // Generate blink pattern, return 1 if all ind cleared
open4416 18:780366f2534c 755 uint8_t*pattern,
open4416 18:780366f2534c 756 uint16_t*repeat,
open4416 18:780366f2534c 757 uint8_t*phase,
open4416 18:780366f2534c 758 uint16_t*Post_ind,
open4416 18:780366f2534c 759 uint16_t*Run_ind,
open4416 18:780366f2534c 760 uint16_t*VDU_ind)
open4416 18:780366f2534c 761 {
open4416 18:780366f2534c 762 //Blink indicator in pattern
open4416 18:780366f2534c 763 //If FLT_Run = 0b0010-0110, pattern will be --......--...--..
open4416 18:780366f2534c 764 uint8_t refresh = 0;
open4416 18:780366f2534c 765 if(*repeat!=0) return refresh; // skip straight to Indication()
open4416 18:780366f2534c 766
open4416 18:780366f2534c 767 if(*Post_ind != 0) {
open4416 18:780366f2534c 768 switch(*phase) {
open4416 18:780366f2534c 769 case 0U:
open4416 18:780366f2534c 770 *repeat = Post_rep;
open4416 18:780366f2534c 771 *pattern = L_Pulse;
open4416 18:780366f2534c 772 *phase = 1U;
open4416 18:780366f2534c 773 break;
open4416 18:780366f2534c 774
open4416 18:780366f2534c 775 case 1U:
open4416 18:780366f2534c 776 *repeat = (*Post_ind)&(-*Post_ind); // extract LSB bit
open4416 18:780366f2534c 777 *Post_ind &= ~*repeat; // this bit is used out
open4416 18:780366f2534c 778 *pattern = S_Pulse;
open4416 18:780366f2534c 779 *phase = 2U;
open4416 18:780366f2534c 780 break;
open4416 18:780366f2534c 781
open4416 18:780366f2534c 782 case 2U:
open4416 18:780366f2534c 783 *repeat = 1U;
open4416 18:780366f2534c 784 *pattern = N_Pulse;
open4416 18:780366f2534c 785 *phase = 0U;
open4416 18:780366f2534c 786 break;
open4416 18:780366f2534c 787 }
open4416 18:780366f2534c 788 return refresh;
open4416 18:780366f2534c 789 }
open4416 18:780366f2534c 790
open4416 18:780366f2534c 791 if(*Run_ind != 0) {
open4416 18:780366f2534c 792 switch(*phase) {
open4416 18:780366f2534c 793 case 0U:
open4416 18:780366f2534c 794 *repeat = Run_rep;
open4416 18:780366f2534c 795 *pattern = L_Pulse;
open4416 18:780366f2534c 796 *phase = 1U;
open4416 18:780366f2534c 797 break;
open4416 18:780366f2534c 798
open4416 18:780366f2534c 799 case 1U:
open4416 18:780366f2534c 800 *repeat = (*Run_ind)&(-*Run_ind); // extract LSB bit
open4416 18:780366f2534c 801 *Run_ind &= ~*repeat; // this bit is used out
open4416 18:780366f2534c 802 *pattern = S_Pulse;
open4416 18:780366f2534c 803 *phase = 2U;
open4416 18:780366f2534c 804 break;
open4416 18:780366f2534c 805
open4416 18:780366f2534c 806 case 2U:
open4416 18:780366f2534c 807 *repeat = 1U;
open4416 18:780366f2534c 808 *pattern = N_Pulse;
open4416 18:780366f2534c 809 *phase = 0U;
open4416 18:780366f2534c 810 break;
open4416 18:780366f2534c 811 }
open4416 18:780366f2534c 812 return refresh;
open4416 18:780366f2534c 813 }
open4416 18:780366f2534c 814
open4416 18:780366f2534c 815 if(*VDU_ind != 0) {
open4416 18:780366f2534c 816 switch(*phase) {
open4416 18:780366f2534c 817 case 0U:
open4416 18:780366f2534c 818 *repeat = VDU_rep;
open4416 18:780366f2534c 819 *pattern = L_Pulse;
open4416 18:780366f2534c 820 *phase = 1U;
open4416 18:780366f2534c 821 break;
open4416 18:780366f2534c 822
open4416 18:780366f2534c 823 case 1U:
open4416 18:780366f2534c 824 *repeat = (*VDU_ind)&(-*VDU_ind); // extract LSB bit
open4416 18:780366f2534c 825 *VDU_ind &= ~*repeat; // this bit is used out
open4416 18:780366f2534c 826 *pattern = S_Pulse;
open4416 18:780366f2534c 827 *phase = 2U;
open4416 18:780366f2534c 828 break;
open4416 18:780366f2534c 829
open4416 18:780366f2534c 830 case 2U:
open4416 18:780366f2534c 831 *repeat = 1U;
open4416 18:780366f2534c 832 *pattern = N_Pulse;
open4416 18:780366f2534c 833 *phase = 0U;
open4416 18:780366f2534c 834 break;
open4416 18:780366f2534c 835 }
open4416 18:780366f2534c 836 return refresh;
open4416 18:780366f2534c 837 }
open4416 18:780366f2534c 838
open4416 18:780366f2534c 839 // else all XXX_ind is cleared out, set refresh
open4416 18:780366f2534c 840 refresh = 1U;
open4416 18:780366f2534c 841 return refresh;
open4416 18:780366f2534c 842 }
open4416 6:fbe401163489 843
open4416 8:f8b1402c8f3c 844 void Cooler(void)
open4416 8:f8b1402c8f3c 845 {
open4416 8:f8b1402c8f3c 846 //Cooling auto control, unfinish 2019/11/15
open4416 9:c99eeafa6fa3 847 Max_Tmotor = max_fval(FL_Tmotor, FR_Tmotor, RL_Tmotor, RR_Tmotor);
open4416 9:c99eeafa6fa3 848 Max_Tmodule = max_fval(FL_Tmodule, FR_Tmodule, RL_Tmodule, RR_Tmodule);
open4416 8:f8b1402c8f3c 849 if(0) {
open4416 8:f8b1402c8f3c 850 Aux_Rly = 1;
open4416 8:f8b1402c8f3c 851 } else {
open4416 8:f8b1402c8f3c 852 Aux_Rly = 0;
open4416 8:f8b1402c8f3c 853 }
open4416 8:f8b1402c8f3c 854 }
open4416 8:f8b1402c8f3c 855
open4416 9:c99eeafa6fa3 856 int16_t max_uval(int16_t i1, int16_t i2, int16_t i3, int16_t i4)
open4416 2:c7a3a8c1aeed 857 {
open4416 2:c7a3a8c1aeed 858 int16_t max = i1;
open4416 2:c7a3a8c1aeed 859 if(i2 > max) max = i2;
open4416 2:c7a3a8c1aeed 860 if(i3 > max) max = i3;
open4416 6:fbe401163489 861 if(i4 > max) max = i4;
open4416 2:c7a3a8c1aeed 862 return max;
open4416 6:fbe401163489 863 }
open4416 9:c99eeafa6fa3 864
open4416 9:c99eeafa6fa3 865 float max_fval(float i1, float i2, float i3, float i4)
open4416 9:c99eeafa6fa3 866 {
open4416 9:c99eeafa6fa3 867 float max = i1;
open4416 9:c99eeafa6fa3 868 if(i2 > max) max = i2;
open4416 9:c99eeafa6fa3 869 if(i3 > max) max = i3;
open4416 9:c99eeafa6fa3 870 if(i4 > max) max = i4;
open4416 9:c99eeafa6fa3 871 return max;
open4416 9:c99eeafa6fa3 872 }
open4416 8:f8b1402c8f3c 873 // pc.printf("SOC: %.2f\n", Module_Total*0.01f);