Just a regular publish

Dependencies:   mbed imu_driver

Committer:
open4416
Date:
Wed Jan 08 13:10:51 2020 +0000
Revision:
19:d68f21173c23
Parent:
18:780366f2534c
Child:
20:e9daae390513
Add test code for IMU, in case future update

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