Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp@0:b2e8c7b0bad9, 2019-06-24 (annotated)
- Committer:
- chd21
- Date:
- Mon Jun 24 14:22:46 2019 +0000
- Revision:
- 0:b2e8c7b0bad9
Shutdown Board Monitoring code
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| chd21 | 0:b2e8c7b0bad9 | 1 | #include "mbed.h" |
| chd21 | 0:b2e8c7b0bad9 | 2 | #include <cmath> |
| chd21 | 0:b2e8c7b0bad9 | 3 | |
| chd21 | 0:b2e8c7b0bad9 | 4 | DigitalOut myled(LED1); |
| chd21 | 0:b2e8c7b0bad9 | 5 | |
| chd21 | 0:b2e8c7b0bad9 | 6 | // 18 Digitial In pins for monitoring system |
| chd21 | 0:b2e8c7b0bad9 | 7 | // 16 are low when ok, high when fault |
| chd21 | 0:b2e8c7b0bad9 | 8 | // 2 that aren't are brake and current, low when fault, high when ok |
| chd21 | 0:b2e8c7b0bad9 | 9 | DigitalIn interlock_in(p5); // LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 10 | DigitalIn tsms_in(p6); // LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 11 | DigitalIn inertia_in(p7); // LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 12 | DigitalIn bspd_latch_in(p8); // LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 13 | DigitalIn pcb_in(p11); // LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 14 | DigitalIn hvd_in(p12); // LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 15 | DigitalIn e_stp_roll_in(p13);// LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 16 | DigitalIn e_stp_dash_in(p14);// LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 17 | DigitalIn ecu_in(p15); // LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 18 | DigitalIn imd_latch_in(p16); // LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 19 | DigitalIn bspd_in(p18); // LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 20 | DigitalIn bms_in(p19); // LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 21 | DigitalIn fuse_in(p20); // LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 22 | DigitalIn imd_in(p21); // LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 23 | DigitalIn bms_latch_in(p23); // LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 24 | DigitalIn bots_in(p24); // LOW OK, HIGH FAULT |
| chd21 | 0:b2e8c7b0bad9 | 25 | |
| chd21 | 0:b2e8c7b0bad9 | 26 | DigitalIn current_in(p17); // HIGH OK, LOW FAULT |
| chd21 | 0:b2e8c7b0bad9 | 27 | DigitalIn brake_in(p22); // HIGH OK, LOW FAULT |
| chd21 | 0:b2e8c7b0bad9 | 28 | |
| chd21 | 0:b2e8c7b0bad9 | 29 | CAN can1(p9,p10); // CAN bus |
| chd21 | 0:b2e8c7b0bad9 | 30 | CANMessage shutdown_msg; |
| chd21 | 0:b2e8c7b0bad9 | 31 | |
| chd21 | 0:b2e8c7b0bad9 | 32 | int main() { |
| chd21 | 0:b2e8c7b0bad9 | 33 | while(1) { |
| chd21 | 0:b2e8c7b0bad9 | 34 | int car_shutdown = 0; |
| chd21 | 0:b2e8c7b0bad9 | 35 | int latched_shutdown = 0; |
| chd21 | 0:b2e8c7b0bad9 | 36 | int any_fault = 0; |
| chd21 | 0:b2e8c7b0bad9 | 37 | |
| chd21 | 0:b2e8c7b0bad9 | 38 | // fault array |
| chd21 | 0:b2e8c7b0bad9 | 39 | int fault_array1[8] = {car_shutdown, latched_shutdown, any_fault, fuse_in, pcb_in, bots_in, e_stp_dash_in, inertia_in}; |
| chd21 | 0:b2e8c7b0bad9 | 40 | //printf("Fault array: %d %d %d %d %d %d %d %d\r\n",fault_array1[0],fault_array1[1],fault_array1[2],fault_array1[3],fault_array1[4],fault_array1[5],fault_array1[6],fault_array1[7]); |
| chd21 | 0:b2e8c7b0bad9 | 41 | int fault_array2[8] = {e_stp_roll_in, tsms_in, hvd_in, interlock_in, ecu_in, bms_in, imd_in, !brake_in}; |
| chd21 | 0:b2e8c7b0bad9 | 42 | int fault_array3[5] = {!current_in, bspd_in, bms_latch_in, imd_latch_in, bspd_latch_in}; |
| chd21 | 0:b2e8c7b0bad9 | 43 | |
| chd21 | 0:b2e8c7b0bad9 | 44 | // find the sum of the faults, find if a shutdown fault has gone, find if a latch has gone |
| chd21 | 0:b2e8c7b0bad9 | 45 | int fault_latch[3] = {bms_latch_in, imd_latch_in, bspd_latch_in}; |
| chd21 | 0:b2e8c7b0bad9 | 46 | int fault_shutdown[13] = {fuse_in, pcb_in, bots_in, e_stp_dash_in, inertia_in, e_stp_roll_in, tsms_in, hvd_in, interlock_in, ecu_in, bms_latch_in, imd_latch_in, bspd_latch_in}; |
| chd21 | 0:b2e8c7b0bad9 | 47 | |
| chd21 | 0:b2e8c7b0bad9 | 48 | // if any latched fault is detected then set flag high |
| chd21 | 0:b2e8c7b0bad9 | 49 | for(int i=0;i<3;i++) { |
| chd21 | 0:b2e8c7b0bad9 | 50 | latched_shutdown = latched_shutdown + fault_latch[i]; |
| chd21 | 0:b2e8c7b0bad9 | 51 | if (latched_shutdown>0) { |
| chd21 | 0:b2e8c7b0bad9 | 52 | latched_shutdown = 1; |
| chd21 | 0:b2e8c7b0bad9 | 53 | fault_array1[1] = latched_shutdown; |
| chd21 | 0:b2e8c7b0bad9 | 54 | } |
| chd21 | 0:b2e8c7b0bad9 | 55 | } |
| chd21 | 0:b2e8c7b0bad9 | 56 | // if any shutdown fault is detected then set flag high |
| chd21 | 0:b2e8c7b0bad9 | 57 | for(int j=0;j<5;j++) { |
| chd21 | 0:b2e8c7b0bad9 | 58 | car_shutdown = car_shutdown + fault_shutdown[j]; |
| chd21 | 0:b2e8c7b0bad9 | 59 | printf("car shutdown = %d\r\n",car_shutdown); |
| chd21 | 0:b2e8c7b0bad9 | 60 | if (car_shutdown>0) { |
| chd21 | 0:b2e8c7b0bad9 | 61 | car_shutdown = 1; |
| chd21 | 0:b2e8c7b0bad9 | 62 | fault_array1[0] = car_shutdown; |
| chd21 | 0:b2e8c7b0bad9 | 63 | break; |
| chd21 | 0:b2e8c7b0bad9 | 64 | } |
| chd21 | 0:b2e8c7b0bad9 | 65 | } |
| chd21 | 0:b2e8c7b0bad9 | 66 | printf("CAR SHUTDOWN %d\r\n",car_shutdown); |
| chd21 | 0:b2e8c7b0bad9 | 67 | // if any fault is deteched set flag high |
| chd21 | 0:b2e8c7b0bad9 | 68 | for (int l=0;l<3;l++) { |
| chd21 | 0:b2e8c7b0bad9 | 69 | for (int k=0;k<8;k++) { |
| chd21 | 0:b2e8c7b0bad9 | 70 | any_fault = any_fault + fault_array1[k]; |
| chd21 | 0:b2e8c7b0bad9 | 71 | if (any_fault>0) { |
| chd21 | 0:b2e8c7b0bad9 | 72 | any_fault = 1; |
| chd21 | 0:b2e8c7b0bad9 | 73 | fault_array1[2] = any_fault; |
| chd21 | 0:b2e8c7b0bad9 | 74 | break; |
| chd21 | 0:b2e8c7b0bad9 | 75 | } |
| chd21 | 0:b2e8c7b0bad9 | 76 | } |
| chd21 | 0:b2e8c7b0bad9 | 77 | for (int m=0;m<8;m++) { |
| chd21 | 0:b2e8c7b0bad9 | 78 | any_fault = any_fault + fault_array2[m]; |
| chd21 | 0:b2e8c7b0bad9 | 79 | if (any_fault>0) { |
| chd21 | 0:b2e8c7b0bad9 | 80 | any_fault = 1; |
| chd21 | 0:b2e8c7b0bad9 | 81 | break; |
| chd21 | 0:b2e8c7b0bad9 | 82 | } |
| chd21 | 0:b2e8c7b0bad9 | 83 | } |
| chd21 | 0:b2e8c7b0bad9 | 84 | for (int n=0;n<5;n++) { |
| chd21 | 0:b2e8c7b0bad9 | 85 | any_fault = any_fault + fault_array3[n]; |
| chd21 | 0:b2e8c7b0bad9 | 86 | if (any_fault>0) { |
| chd21 | 0:b2e8c7b0bad9 | 87 | any_fault = 1; |
| chd21 | 0:b2e8c7b0bad9 | 88 | break; |
| chd21 | 0:b2e8c7b0bad9 | 89 | } |
| chd21 | 0:b2e8c7b0bad9 | 90 | } |
| chd21 | 0:b2e8c7b0bad9 | 91 | } |
| chd21 | 0:b2e8c7b0bad9 | 92 | |
| chd21 | 0:b2e8c7b0bad9 | 93 | for (int k=3;k<8;k++) { |
| chd21 | 0:b2e8c7b0bad9 | 94 | any_fault = any_fault + fault_array1[k]; |
| chd21 | 0:b2e8c7b0bad9 | 95 | if (any_fault>0) { |
| chd21 | 0:b2e8c7b0bad9 | 96 | any_fault = 1; |
| chd21 | 0:b2e8c7b0bad9 | 97 | fault_array1[2] = any_fault; |
| chd21 | 0:b2e8c7b0bad9 | 98 | break; |
| chd21 | 0:b2e8c7b0bad9 | 99 | } |
| chd21 | 0:b2e8c7b0bad9 | 100 | } |
| chd21 | 0:b2e8c7b0bad9 | 101 | printf("ANY FAULT %d\r\n",any_fault); |
| chd21 | 0:b2e8c7b0bad9 | 102 | |
| chd21 | 0:b2e8c7b0bad9 | 103 | int x = 2; |
| chd21 | 0:b2e8c7b0bad9 | 104 | int byte1 = 0; |
| chd21 | 0:b2e8c7b0bad9 | 105 | int byte2 = 0; |
| chd21 | 0:b2e8c7b0bad9 | 106 | int byte3 = 0; |
| chd21 | 0:b2e8c7b0bad9 | 107 | // Generate the CAN message |
| chd21 | 0:b2e8c7b0bad9 | 108 | for (int b=0;b<8;b++) { |
| chd21 | 0:b2e8c7b0bad9 | 109 | int power = pow((double)x,b); |
| chd21 | 0:b2e8c7b0bad9 | 110 | //printf("Current val = %d, b = %d \r\n",fault_array1[b], power); |
| chd21 | 0:b2e8c7b0bad9 | 111 | byte1 = byte1 + (fault_array1[b]*pow((double)x,b)); |
| chd21 | 0:b2e8c7b0bad9 | 112 | //printf("Byte 1 = %d\r\n",byte1); |
| chd21 | 0:b2e8c7b0bad9 | 113 | byte2 = byte2 + (fault_array2[b]*pow((double)x,b)); |
| chd21 | 0:b2e8c7b0bad9 | 114 | byte3 = byte3 + (fault_array3[b]*pow((double)x,b)); |
| chd21 | 0:b2e8c7b0bad9 | 115 | } |
| chd21 | 0:b2e8c7b0bad9 | 116 | |
| chd21 | 0:b2e8c7b0bad9 | 117 | shutdown_msg.id = 0x705; |
| chd21 | 0:b2e8c7b0bad9 | 118 | shutdown_msg.data[0] = byte1; |
| chd21 | 0:b2e8c7b0bad9 | 119 | shutdown_msg.data[1] = byte2; |
| chd21 | 0:b2e8c7b0bad9 | 120 | shutdown_msg.data[2] = byte3; |
| chd21 | 0:b2e8c7b0bad9 | 121 | |
| chd21 | 0:b2e8c7b0bad9 | 122 | printf("CAN Message, %X %X %X %X \r\n", shutdown_msg.id,shutdown_msg.data[0],shutdown_msg.data[1],shutdown_msg.data[2]); |
| chd21 | 0:b2e8c7b0bad9 | 123 | //printf("CAN Message, %X %X \r\n", shutdown_msg.id,shutdown_msg.data[0]); |
| chd21 | 0:b2e8c7b0bad9 | 124 | |
| chd21 | 0:b2e8c7b0bad9 | 125 | wait(1); |
| chd21 | 0:b2e8c7b0bad9 | 126 | } |
| chd21 | 0:b2e8c7b0bad9 | 127 | } |