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 mbed-STM32F103C8T6
ecu_simulator.cpp@7:467e350d1a10, 2020-06-10 (annotated)
- Committer:
- RogerJKelly
- Date:
- Wed Jun 10 20:15:48 2020 +0000
- Revision:
- 7:467e350d1a10
- Parent:
- 6:41a4ad385daa
RIXEN SIM with Maple Mini
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| pangsk | 0:908be729d27c | 1 | #include "mbed.h" |
| pangsk | 6:41a4ad385daa | 2 | #include "ecu_simulator.h" |
| pangsk | 0:908be729d27c | 3 | #include "globals.h" |
| pangsk | 0:908be729d27c | 4 | |
| pangsk | 0:908be729d27c | 5 | |
| pangsk | 0:908be729d27c | 6 | // Use a timer to see if things take too long |
| RogerJKelly | 7:467e350d1a10 | 7 | Timer CANTimer; |
| RogerJKelly | 7:467e350d1a10 | 8 | |
| RogerJKelly | 7:467e350d1a10 | 9 | namespace mbed |
| RogerJKelly | 7:467e350d1a10 | 10 | { |
| pangsk | 0:908be729d27c | 11 | |
| pangsk | 0:908be729d27c | 12 | |
| pangsk | 6:41a4ad385daa | 13 | ecu_sim::ecu_sim(int can_speed) |
| pangsk | 6:41a4ad385daa | 14 | { |
| RogerJKelly | 7:467e350d1a10 | 15 | if ( can2.frequency(can_speed) ) |
| RogerJKelly | 7:467e350d1a10 | 16 | pc.printf("CAN speed set to %d \n\r", can_speed); |
| RogerJKelly | 7:467e350d1a10 | 17 | else |
| RogerJKelly | 7:467e350d1a10 | 18 | pc.printf("CAN speed SET failed\n\r"); |
| pangsk | 6:41a4ad385daa | 19 | } |
| pangsk | 6:41a4ad385daa | 20 | |
| pangsk | 6:41a4ad385daa | 21 | void ecu_sim::canspeed(int can_speed) |
| pangsk | 0:908be729d27c | 22 | { |
| RogerJKelly | 7:467e350d1a10 | 23 | if ( can2.frequency(can_speed) ) |
| RogerJKelly | 7:467e350d1a10 | 24 | pc.printf("CAN speed set to %d \n\r", can_speed); |
| RogerJKelly | 7:467e350d1a10 | 25 | else |
| RogerJKelly | 7:467e350d1a10 | 26 | pc.printf("CAN speed SET failed\n\r"); |
| pangsk | 0:908be729d27c | 27 | } |
| pangsk | 0:908be729d27c | 28 | |
| pangsk | 3:05bb8f0bd7a4 | 29 | |
| pangsk | 6:41a4ad385daa | 30 | |
| RogerJKelly | 7:467e350d1a10 | 31 | //#define TIMEOUT 500 |
| pangsk | 6:41a4ad385daa | 32 | unsigned char ecu_sim::request(void) |
| pangsk | 0:908be729d27c | 33 | { |
| pangsk | 0:908be729d27c | 34 | char can_msg[8]; |
| RogerJKelly | 7:467e350d1a10 | 35 | int done = 0; |
| RogerJKelly | 7:467e350d1a10 | 36 | int cnt = 0; |
| RogerJKelly | 7:467e350d1a10 | 37 | //pc.printf("*"); |
| RogerJKelly | 7:467e350d1a10 | 38 | |
| pangsk | 6:41a4ad385daa | 39 | |
| RogerJKelly | 7:467e350d1a10 | 40 | //if ((can2.read(can_MsgRx)) && (can_MsgRx.id == PID_REQUEST) ) |
| RogerJKelly | 7:467e350d1a10 | 41 | if (can2.read(can_MsgRx)) //&& (can_MsgRx.id == PID_REQUEST) ) |
| RogerJKelly | 7:467e350d1a10 | 42 | { |
| RogerJKelly | 7:467e350d1a10 | 43 | pc.printf("."); |
| pangsk | 6:41a4ad385daa | 44 | led2 = 1; |
| RogerJKelly | 7:467e350d1a10 | 45 | if (can_MsgRx.id == ESPAR_CMD) |
| pangsk | 6:41a4ad385daa | 46 | { |
| RogerJKelly | 7:467e350d1a10 | 47 | pc.printf("0"); |
| RogerJKelly | 7:467e350d1a10 | 48 | // wake command wake[8] = { 0x02, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| RogerJKelly | 7:467e350d1a10 | 49 | if((can_MsgRx.data[0] == 0x02) && (can_MsgRx.data[1] == 0x3E))// wake request |
| RogerJKelly | 7:467e350d1a10 | 50 | { //expected[3] = {0x02, 0x7e, 0x00}; |
| RogerJKelly | 7:467e350d1a10 | 51 | can_msg[0] = 0x02; |
| RogerJKelly | 7:467e350d1a10 | 52 | can_msg[1] = 0x7E; |
| RogerJKelly | 7:467e350d1a10 | 53 | can_msg[2] = 0x00; can_msg[3] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 54 | can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 55 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 56 | pc.printf("1"); |
| RogerJKelly | 7:467e350d1a10 | 57 | } |
| RogerJKelly | 7:467e350d1a10 | 58 | else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x17)) |
| RogerJKelly | 7:467e350d1a10 | 59 | { // got get runtime: 7A0 03 22 FD 17 00 00 00 00 |
| RogerJKelly | 7:467e350d1a10 | 60 | // send 73C 10 1F 62 FD 17 00 00 00 |
| RogerJKelly | 7:467e350d1a10 | 61 | can_msg[0] = 0x10; can_msg[1] = 0x1F; can_msg[2] = 0x62; can_msg[3] = 0xFD; |
| RogerJKelly | 7:467e350d1a10 | 62 | can_msg[4] = 0x17; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 63 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 64 | pc.printf("2"); |
| RogerJKelly | 7:467e350d1a10 | 65 | //wait(0.1); |
| RogerJKelly | 7:467e350d1a10 | 66 | done = 0; |
| RogerJKelly | 7:467e350d1a10 | 67 | while ( done == 0 ) |
| RogerJKelly | 7:467e350d1a10 | 68 | { |
| RogerJKelly | 7:467e350d1a10 | 69 | //wait for continue msg 7A0 30 00 14 00 00 00 00 00 |
| RogerJKelly | 7:467e350d1a10 | 70 | can2.read(can_MsgRx); |
| RogerJKelly | 7:467e350d1a10 | 71 | if ((can_MsgRx.id == ESPAR_CMD) && (can_MsgRx.data[0] == 0x30)) |
| RogerJKelly | 7:467e350d1a10 | 72 | { |
| RogerJKelly | 7:467e350d1a10 | 73 | done = 1; |
| RogerJKelly | 7:467e350d1a10 | 74 | pc.printf("a"); |
| RogerJKelly | 7:467e350d1a10 | 75 | //73C 2377343693 0008 21 91 00 00 00 00 00 00 |
| RogerJKelly | 7:467e350d1a10 | 76 | can_msg[0] = 0x21; can_msg[1] = 0x91; can_msg[2] = 0x00; can_msg[3] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 77 | can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 78 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 79 | wait(0.2); |
| RogerJKelly | 7:467e350d1a10 | 80 | //73C 2377373697 0008 22 00 00 00 00 00 00 00 |
| RogerJKelly | 7:467e350d1a10 | 81 | can_msg[0] = 0x22; can_msg[1] = 0x00; can_msg[2] = 0x00; can_msg[3] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 82 | can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 83 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 84 | wait(0.2); |
| RogerJKelly | 7:467e350d1a10 | 85 | //73C 2377403705 0008 23 00 00 5F 00 00 00 00 |
| RogerJKelly | 7:467e350d1a10 | 86 | can_msg[0] = 0x23; can_msg[1] = 0x00; can_msg[2] = 0x00; can_msg[3] = 0x5F; |
| RogerJKelly | 7:467e350d1a10 | 87 | can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 88 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 89 | wait(0.2); |
| RogerJKelly | 7:467e350d1a10 | 90 | //73C 2377433695 0008 24 00 00 00 00 00 00 00 |
| RogerJKelly | 7:467e350d1a10 | 91 | can_msg[0] = 0x24; can_msg[1] = 0x00; can_msg[2] = 0x00; can_msg[3] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 92 | can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 93 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 94 | wait(0.2); |
| RogerJKelly | 7:467e350d1a10 | 95 | } |
| RogerJKelly | 7:467e350d1a10 | 96 | } |
| RogerJKelly | 7:467e350d1a10 | 97 | } // end of get runtime |
| RogerJKelly | 7:467e350d1a10 | 98 | else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x01)) |
| RogerJKelly | 7:467e350d1a10 | 99 | { //get temperatures 7A0 03 22 FD 01 00 00 00 00 -- not sure temperatures??? |
| RogerJKelly | 7:467e350d1a10 | 100 | //73C 10 09 62 FD 01 1D E2 1C -- send this |
| RogerJKelly | 7:467e350d1a10 | 101 | can_msg[0] = 0x10; can_msg[1] = 0x09; can_msg[2] = 0x62; can_msg[3] = 0xFD; |
| RogerJKelly | 7:467e350d1a10 | 102 | can_msg[4] = 0x01; can_msg[5] = 0x1D; can_msg[6] = 0xE2; can_msg[7] = 0x1C; |
| RogerJKelly | 7:467e350d1a10 | 103 | pc.printf("3"); |
| RogerJKelly | 7:467e350d1a10 | 104 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 105 | |
| RogerJKelly | 7:467e350d1a10 | 106 | //wait(0.1); |
| RogerJKelly | 7:467e350d1a10 | 107 | done = 0; |
| RogerJKelly | 7:467e350d1a10 | 108 | while ( done == 0 ) |
| RogerJKelly | 7:467e350d1a10 | 109 | { |
| RogerJKelly | 7:467e350d1a10 | 110 | //wait for continue msg 7A0 30 00 14 00 00 00 00 00 |
| RogerJKelly | 7:467e350d1a10 | 111 | //*************************************************************** |
| RogerJKelly | 7:467e350d1a10 | 112 | if (can2.read(can_MsgRx)) |
| RogerJKelly | 7:467e350d1a10 | 113 | { |
| RogerJKelly | 7:467e350d1a10 | 114 | pc.printf("m"); |
| RogerJKelly | 7:467e350d1a10 | 115 | //pc.printf("m %x %x %x %x %x %x %x %x %x\n\r",can_MsgRx.id, |
| RogerJKelly | 7:467e350d1a10 | 116 | // can_MsgRx.data[0], can_MsgRx.data[1], can_MsgRx.data[2], can_MsgRx.data[3], |
| RogerJKelly | 7:467e350d1a10 | 117 | // can_MsgRx.data[4], can_MsgRx.data[5], can_MsgRx.data[6], can_MsgRx.data[7]); |
| RogerJKelly | 7:467e350d1a10 | 118 | cnt++; |
| RogerJKelly | 7:467e350d1a10 | 119 | if ( cnt > 10 ) done = 1; |
| RogerJKelly | 7:467e350d1a10 | 120 | } |
| RogerJKelly | 7:467e350d1a10 | 121 | //****************************************************************/ |
| RogerJKelly | 7:467e350d1a10 | 122 | //can2.read(can_MsgRx); |
| RogerJKelly | 7:467e350d1a10 | 123 | //pc.printf("x"); |
| RogerJKelly | 7:467e350d1a10 | 124 | if ((can_MsgRx.id == ESPAR_CMD) && (can_MsgRx.data[0] == 0x30)) |
| RogerJKelly | 7:467e350d1a10 | 125 | { |
| RogerJKelly | 7:467e350d1a10 | 126 | done = 1; |
| RogerJKelly | 7:467e350d1a10 | 127 | pc.printf("b"); |
| RogerJKelly | 7:467e350d1a10 | 128 | //73C 2420412954 0008 21 84 1C B6 01 1D E2 1C |
| RogerJKelly | 7:467e350d1a10 | 129 | can_msg[0] = 0x21; can_msg[1] = 0x84; can_msg[2] = 0x1C; can_msg[3] = 0xB6; |
| RogerJKelly | 7:467e350d1a10 | 130 | can_msg[4] = 0x01; can_msg[5] = 0x1D; can_msg[6] = 0xE2; can_msg[7] = 0x1C; |
| RogerJKelly | 7:467e350d1a10 | 131 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 132 | wait(0.2); |
| RogerJKelly | 7:467e350d1a10 | 133 | } |
| RogerJKelly | 7:467e350d1a10 | 134 | } |
| RogerJKelly | 7:467e350d1a10 | 135 | } // end of get temperatures |
| RogerJKelly | 7:467e350d1a10 | 136 | else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x0A)) |
| RogerJKelly | 7:467e350d1a10 | 137 | { //expected[3] = //73C 05 62 FD 0A 00 00 E2 1C |
| RogerJKelly | 7:467e350d1a10 | 138 | can_msg[0] = 0x05; can_msg[1] = 0x62; |
| RogerJKelly | 7:467e350d1a10 | 139 | can_msg[2] = 0xFD; can_msg[3] = 0x0A; |
| RogerJKelly | 7:467e350d1a10 | 140 | can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0xE2; can_msg[7] = 0x1C; |
| RogerJKelly | 7:467e350d1a10 | 141 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 142 | pc.printf("4"); |
| RogerJKelly | 7:467e350d1a10 | 143 | } |
| RogerJKelly | 7:467e350d1a10 | 144 | //7A0 03 22 FD 0D 00 00 00 00 |
| RogerJKelly | 7:467e350d1a10 | 145 | else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x0D)) |
| RogerJKelly | 7:467e350d1a10 | 146 | { //expected[3] = //73C 04 62 FD 0D 00 20 E2 1C |
| RogerJKelly | 7:467e350d1a10 | 147 | can_msg[0] = 0x04; can_msg[1] = 0x62; |
| RogerJKelly | 7:467e350d1a10 | 148 | can_msg[2] = 0xFD; can_msg[3] = 0x0D; |
| RogerJKelly | 7:467e350d1a10 | 149 | can_msg[4] = 0x00; can_msg[5] = 0x20; can_msg[6] = 0xE2; can_msg[7] = 0x1C; |
| RogerJKelly | 7:467e350d1a10 | 150 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 151 | pc.printf("5"); |
| RogerJKelly | 7:467e350d1a10 | 152 | } |
| RogerJKelly | 7:467e350d1a10 | 153 | // |
| RogerJKelly | 7:467e350d1a10 | 154 | else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x0F)) |
| RogerJKelly | 7:467e350d1a10 | 155 | { //get ??? 7A0 03 22 FD 0F 00 00 00 00 -- not sure ??? |
| RogerJKelly | 7:467e350d1a10 | 156 | //73C 10 0D 62 FD 0F 00 01 75 -- send this |
| RogerJKelly | 7:467e350d1a10 | 157 | can_msg[0] = 0x10; can_msg[1] = 0x0D; can_msg[2] = 0x62; can_msg[3] = 0xFD; |
| RogerJKelly | 7:467e350d1a10 | 158 | can_msg[4] = 0x0F; can_msg[5] = 0x00; can_msg[6] = 0x01; can_msg[7] = 0x75; |
| RogerJKelly | 7:467e350d1a10 | 159 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 160 | pc.printf("6"); |
| RogerJKelly | 7:467e350d1a10 | 161 | //wait(0.1); |
| RogerJKelly | 7:467e350d1a10 | 162 | done = 0; |
| RogerJKelly | 7:467e350d1a10 | 163 | while ( done == 0 ) |
| RogerJKelly | 7:467e350d1a10 | 164 | { |
| RogerJKelly | 7:467e350d1a10 | 165 | //wait for continue msg 7A0 30 00 14 00 00 00 00 00 |
| RogerJKelly | 7:467e350d1a10 | 166 | can2.read(can_MsgRx); |
| RogerJKelly | 7:467e350d1a10 | 167 | if ((can_MsgRx.id == ESPAR_CMD) && (can_MsgRx.data[0] == 0x30)) |
| RogerJKelly | 7:467e350d1a10 | 168 | { |
| RogerJKelly | 7:467e350d1a10 | 169 | done = 1; |
| RogerJKelly | 7:467e350d1a10 | 170 | pc.printf("d"); |
| RogerJKelly | 7:467e350d1a10 | 171 | //73C 21 30 75 44 7F FC 7F FC |
| RogerJKelly | 7:467e350d1a10 | 172 | can_msg[0] = 0x21; can_msg[1] = 0x30; can_msg[2] = 0x75; can_msg[3] = 0x44; |
| RogerJKelly | 7:467e350d1a10 | 173 | can_msg[4] = 0x7F; can_msg[5] = 0xFC; can_msg[6] = 0x7F; can_msg[7] = 0xFC; |
| RogerJKelly | 7:467e350d1a10 | 174 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 175 | wait(0.1); |
| RogerJKelly | 7:467e350d1a10 | 176 | } |
| RogerJKelly | 7:467e350d1a10 | 177 | } |
| RogerJKelly | 7:467e350d1a10 | 178 | } // end of ?? |
| RogerJKelly | 7:467e350d1a10 | 179 | else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x22)) |
| RogerJKelly | 7:467e350d1a10 | 180 | { // got get 7a0 3 22 fd 22 0 0 0 0 - get water pump setting |
| RogerJKelly | 7:467e350d1a10 | 181 | //send 0x05, 0x62, 0xFD, 0x24, 0x00, 0x00, 0x00, 0x00 will keep it happy |
| RogerJKelly | 7:467e350d1a10 | 182 | can_msg[0] = 0x05; can_msg[1] = 0x62; can_msg[2] = 0xFD; can_msg[3] = 0x24; |
| RogerJKelly | 7:467e350d1a10 | 183 | can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 184 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 185 | pc.printf("7"); |
| RogerJKelly | 7:467e350d1a10 | 186 | } |
| RogerJKelly | 7:467e350d1a10 | 187 | else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x24)) |
| RogerJKelly | 7:467e350d1a10 | 188 | { // got get 7a0 3 22 fd 24 0 0 0 0 - get heat time limit |
| RogerJKelly | 7:467e350d1a10 | 189 | //send 0x05, 0x62, 0xFD, 0x24, 0x00, 0x00, 0x00, 0x00 will keep it happy |
| RogerJKelly | 7:467e350d1a10 | 190 | can_msg[0] = 0x05; can_msg[1] = 0x62; can_msg[2] = 0xFD; can_msg[3] = 0x24; |
| RogerJKelly | 7:467e350d1a10 | 191 | can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 192 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 193 | pc.printf("8"); |
| RogerJKelly | 7:467e350d1a10 | 194 | } |
| RogerJKelly | 7:467e350d1a10 | 195 | else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x36)) |
| RogerJKelly | 7:467e350d1a10 | 196 | { // got 7a0 3 22 fd 36 0 0 0 0 - get altitude. |
| RogerJKelly | 7:467e350d1a10 | 197 | //send Response is 0x05, 0x62, 0xFD, 0x04, [0x27, 0x10], 0xB0, 0x1C |
| RogerJKelly | 7:467e350d1a10 | 198 | can_msg[0] = 0x05; can_msg[1] = 0x62; can_msg[2] = 0xFD; can_msg[3] = 0x04; |
| RogerJKelly | 7:467e350d1a10 | 199 | can_msg[4] = 0x27; can_msg[5] = 0x10; can_msg[6] = 0xB0; can_msg[7] = 0x1C; |
| RogerJKelly | 7:467e350d1a10 | 200 | if ( got_alt == true ) |
| RogerJKelly | 7:467e350d1a10 | 201 | { |
| RogerJKelly | 7:467e350d1a10 | 202 | can_msg[4] = alt[0]; |
| RogerJKelly | 7:467e350d1a10 | 203 | can_msg[5] = alt[1]; |
| RogerJKelly | 7:467e350d1a10 | 204 | } |
| RogerJKelly | 7:467e350d1a10 | 205 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 206 | pc.printf("9"); |
| RogerJKelly | 7:467e350d1a10 | 207 | } |
| RogerJKelly | 7:467e350d1a10 | 208 | else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x04)) |
| RogerJKelly | 7:467e350d1a10 | 209 | { // battery send: { 0x03, 0x22, 0xFD, 0x04, 0x00, 0x00, 0x00, 0x00 }; |
| RogerJKelly | 7:467e350d1a10 | 210 | // Response is 0x05, 0x62, 0xFD, 0x04, MSB, LSB, 0x00, 0x00 |
| RogerJKelly | 7:467e350d1a10 | 211 | can_msg[0] = 0x05; can_msg[1] = 0x62; can_msg[2] = 0xFD; can_msg[3] = 0x04; |
| RogerJKelly | 7:467e350d1a10 | 212 | can_msg[4] = 0x00; can_msg[5] = 0x7A; can_msg[6] = 0x00; can_msg[7] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 213 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 214 | pc.printf("V"); |
| RogerJKelly | 7:467e350d1a10 | 215 | } |
| RogerJKelly | 7:467e350d1a10 | 216 | else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x19) && (can_MsgRx.data[2] == 0x02) && (can_MsgRx.data[3] == 0x08)) |
| RogerJKelly | 7:467e350d1a10 | 217 | { // get single fault: { 0x03, 0x19, 0x02, 0x08, 0x00, 0x00, 0x00, 0x00 }; |
| RogerJKelly | 7:467e350d1a10 | 218 | // Response with 1 fault 073C: 07 59 02 7B 00 02 11 2A |
| RogerJKelly | 7:467e350d1a10 | 219 | // Response with No fault 073C:03 59 02 7B 05 48 C3 01 |
| RogerJKelly | 7:467e350d1a10 | 220 | if ( ecu.active_fault == true ) |
| RogerJKelly | 7:467e350d1a10 | 221 | { |
| RogerJKelly | 7:467e350d1a10 | 222 | can_msg[0] = 0x07; can_msg[1] = 0x59; can_msg[2] = 0x02; can_msg[3] = 0x7B; |
| RogerJKelly | 7:467e350d1a10 | 223 | can_msg[4] = 0x00; can_msg[5] = 0x02; can_msg[6] = 0x11; can_msg[7] = 0x2B; |
| RogerJKelly | 7:467e350d1a10 | 224 | } |
| RogerJKelly | 7:467e350d1a10 | 225 | else // stored fault |
| RogerJKelly | 7:467e350d1a10 | 226 | { |
| RogerJKelly | 7:467e350d1a10 | 227 | // these 2 lines reset to NO Faults. |
| RogerJKelly | 7:467e350d1a10 | 228 | can_msg[0] = 0x03; can_msg[1] = 0x59; can_msg[2] = 0x02; can_msg[3] = 0x7B; |
| RogerJKelly | 7:467e350d1a10 | 229 | can_msg[4] = 0x05; can_msg[5] = 0x48; can_msg[6] = 0xC3; can_msg[7] = 0x01; |
| RogerJKelly | 7:467e350d1a10 | 230 | // these 2 lines reset to a non-active fault |
| RogerJKelly | 7:467e350d1a10 | 231 | //can_msg[0] = 0x07; can_msg[1] = 0x59; can_msg[2] = 0x02; can_msg[3] = 0x7B; |
| RogerJKelly | 7:467e350d1a10 | 232 | //can_msg[4] = 0x00; can_msg[5] = 0x02; can_msg[6] = 0x11; can_msg[7] = 0x2A; |
| RogerJKelly | 7:467e350d1a10 | 233 | } |
| RogerJKelly | 7:467e350d1a10 | 234 | can2.write(CANMessage(ESPAR_RES , can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 235 | pc.printf("F"); |
| RogerJKelly | 7:467e350d1a10 | 236 | } |
| RogerJKelly | 7:467e350d1a10 | 237 | else |
| RogerJKelly | 7:467e350d1a10 | 238 | { |
| RogerJKelly | 7:467e350d1a10 | 239 | pc.printf("\n\r%x %x %x %x %x %x %x %x %x\n\r",can_MsgRx.id, |
| RogerJKelly | 7:467e350d1a10 | 240 | can_MsgRx.data[0], can_MsgRx.data[1], can_MsgRx.data[2], can_MsgRx.data[3], |
| RogerJKelly | 7:467e350d1a10 | 241 | can_MsgRx.data[4], can_MsgRx.data[5], can_MsgRx.data[6], can_MsgRx.data[7]); |
| RogerJKelly | 7:467e350d1a10 | 242 | } |
| RogerJKelly | 7:467e350d1a10 | 243 | } // end of if ESPAR_CMD |
| RogerJKelly | 7:467e350d1a10 | 244 | //"Set" altitude command: |
| RogerJKelly | 7:467e350d1a10 | 245 | //addresses0x0055 and 0x009D: { 0x4c, 0x1d, 0, 0, 0, 0, 0, 0 }; |
| RogerJKelly | 7:467e350d1a10 | 246 | // |
| RogerJKelly | 7:467e350d1a10 | 247 | //LSB, MSB altitude x 10 in packet. 1D4C = 7500 = 750hpA, 2710 = 10000 = 1000hPa. |
| RogerJKelly | 7:467e350d1a10 | 248 | else if ((can_MsgRx.id == 0x0055) || (can_MsgRx.id == 0x009D)) |
| RogerJKelly | 7:467e350d1a10 | 249 | { |
| RogerJKelly | 7:467e350d1a10 | 250 | alt[0] = can_MsgRx.data[1]; |
| RogerJKelly | 7:467e350d1a10 | 251 | alt[1] = can_MsgRx.data[0]; |
| RogerJKelly | 7:467e350d1a10 | 252 | got_alt = true; |
| RogerJKelly | 7:467e350d1a10 | 253 | pc.printf("A"); |
| RogerJKelly | 7:467e350d1a10 | 254 | } |
| RogerJKelly | 7:467e350d1a10 | 255 | else if (can_MsgRx.id == PID_REQUEST) |
| RogerJKelly | 7:467e350d1a10 | 256 | { |
| RogerJKelly | 7:467e350d1a10 | 257 | pc.printf("-"); |
| RogerJKelly | 7:467e350d1a10 | 258 | if(can_MsgRx.data[1] == MODE3) // Request trouble codes |
| RogerJKelly | 7:467e350d1a10 | 259 | { |
| RogerJKelly | 7:467e350d1a10 | 260 | if(ecu.dtc == false) |
| RogerJKelly | 7:467e350d1a10 | 261 | { |
| RogerJKelly | 7:467e350d1a10 | 262 | can_msg[0] = 0x02; |
| RogerJKelly | 7:467e350d1a10 | 263 | can_msg[1] = MODE3_RESPONSE; |
| RogerJKelly | 7:467e350d1a10 | 264 | can_msg[2] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 265 | } |
| RogerJKelly | 7:467e350d1a10 | 266 | else |
| RogerJKelly | 7:467e350d1a10 | 267 | { |
| RogerJKelly | 7:467e350d1a10 | 268 | can_msg[0] = 0x06; |
| RogerJKelly | 7:467e350d1a10 | 269 | can_msg[1] = MODE3_RESPONSE; |
| RogerJKelly | 7:467e350d1a10 | 270 | can_msg[2] = 0x02; |
| RogerJKelly | 7:467e350d1a10 | 271 | can_msg[3] = 0x01; |
| RogerJKelly | 7:467e350d1a10 | 272 | can_msg[4] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 273 | can_msg[5] = 0x02; |
| RogerJKelly | 7:467e350d1a10 | 274 | can_msg[6] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 275 | } |
| RogerJKelly | 7:467e350d1a10 | 276 | can2.write(CANMessage(PID_REPLY, can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 277 | } |
| RogerJKelly | 7:467e350d1a10 | 278 | |
| RogerJKelly | 7:467e350d1a10 | 279 | if(can_MsgRx.data[1] == MODE4) // Clear trouble codes, clear Check engine light |
| RogerJKelly | 7:467e350d1a10 | 280 | { |
| RogerJKelly | 7:467e350d1a10 | 281 | ecu.dtc = false; |
| RogerJKelly | 7:467e350d1a10 | 282 | led4 = 0; |
| RogerJKelly | 7:467e350d1a10 | 283 | |
| RogerJKelly | 7:467e350d1a10 | 284 | can_msg[0] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 285 | can_msg[1] = MODE4_RESPONSE; |
| RogerJKelly | 7:467e350d1a10 | 286 | can2.write(CANMessage(PID_REPLY, can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 287 | } |
| RogerJKelly | 7:467e350d1a10 | 288 | |
| RogerJKelly | 7:467e350d1a10 | 289 | if(can_MsgRx.data[1] == MODE1) |
| RogerJKelly | 7:467e350d1a10 | 290 | { |
| RogerJKelly | 7:467e350d1a10 | 291 | can_msg[1] = MODE1_RESPONSE; |
| RogerJKelly | 7:467e350d1a10 | 292 | switch(can_MsgRx.data[2]) |
| RogerJKelly | 7:467e350d1a10 | 293 | { |
| RogerJKelly | 7:467e350d1a10 | 294 | /* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */ |
| RogerJKelly | 7:467e350d1a10 | 295 | case PID_SUPPORTED: |
| RogerJKelly | 7:467e350d1a10 | 296 | can_msg[0] = 0x06; |
| RogerJKelly | 7:467e350d1a10 | 297 | can_msg[2] = PID_SUPPORTED; |
| RogerJKelly | 7:467e350d1a10 | 298 | can_msg[3] = 0xE8; |
| RogerJKelly | 7:467e350d1a10 | 299 | can_msg[4] = 0x19; |
| RogerJKelly | 7:467e350d1a10 | 300 | can_msg[5] = 0x30; |
| RogerJKelly | 7:467e350d1a10 | 301 | can_msg[6] = 0x12; |
| RogerJKelly | 7:467e350d1a10 | 302 | can_msg[5] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 303 | can2.write(CANMessage(PID_REPLY, can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 304 | |
| RogerJKelly | 7:467e350d1a10 | 305 | break; |
| RogerJKelly | 7:467e350d1a10 | 306 | |
| RogerJKelly | 7:467e350d1a10 | 307 | case MONITOR_STATUS: |
| RogerJKelly | 7:467e350d1a10 | 308 | can_msg[0] = 0x05; |
| RogerJKelly | 7:467e350d1a10 | 309 | can_msg[2] = MONITOR_STATUS; |
| RogerJKelly | 7:467e350d1a10 | 310 | |
| RogerJKelly | 7:467e350d1a10 | 311 | if(ecu.dtc == true) can_msg[3] = 0x82; |
| RogerJKelly | 7:467e350d1a10 | 312 | else can_msg[3] = 0x00; |
| RogerJKelly | 7:467e350d1a10 | 313 | |
| RogerJKelly | 7:467e350d1a10 | 314 | can_msg[4] = 0x07; |
| RogerJKelly | 7:467e350d1a10 | 315 | can_msg[5] = 0xFF; |
| RogerJKelly | 7:467e350d1a10 | 316 | can2.write(CANMessage(PID_REPLY, can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 317 | break; |
| RogerJKelly | 7:467e350d1a10 | 318 | |
| RogerJKelly | 7:467e350d1a10 | 319 | case ENGINE_RPM: // ((A*256)+B)/4 [RPM] |
| RogerJKelly | 7:467e350d1a10 | 320 | can_msg[0] = 0x04; |
| RogerJKelly | 7:467e350d1a10 | 321 | can_msg[2] = ENGINE_RPM; |
| RogerJKelly | 7:467e350d1a10 | 322 | can_msg[3] = (ecu.engine_rpm & 0xff00) >> 8; |
| RogerJKelly | 7:467e350d1a10 | 323 | can_msg[4] = ecu.engine_rpm & 0x00ff; |
| RogerJKelly | 7:467e350d1a10 | 324 | can2.write(CANMessage(PID_REPLY, can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 325 | break; |
| RogerJKelly | 7:467e350d1a10 | 326 | |
| RogerJKelly | 7:467e350d1a10 | 327 | case ENGINE_COOLANT_TEMP: // A-40 [degree C] |
| RogerJKelly | 7:467e350d1a10 | 328 | can_msg[0] = 0x03; |
| RogerJKelly | 7:467e350d1a10 | 329 | can_msg[2] = ENGINE_COOLANT_TEMP; |
| RogerJKelly | 7:467e350d1a10 | 330 | can_msg[3] = ecu.coolant_temp; |
| RogerJKelly | 7:467e350d1a10 | 331 | can2.write(CANMessage(PID_REPLY, can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 332 | break; |
| RogerJKelly | 7:467e350d1a10 | 333 | |
| RogerJKelly | 7:467e350d1a10 | 334 | case VEHICLE_SPEED: // A [km] |
| RogerJKelly | 7:467e350d1a10 | 335 | can_msg[0] = 0x03; |
| RogerJKelly | 7:467e350d1a10 | 336 | can_msg[2] = VEHICLE_SPEED; |
| RogerJKelly | 7:467e350d1a10 | 337 | can_msg[3] = ecu.vehicle_speed; |
| RogerJKelly | 7:467e350d1a10 | 338 | can2.write(CANMessage(PID_REPLY, can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 339 | break; |
| RogerJKelly | 7:467e350d1a10 | 340 | |
| RogerJKelly | 7:467e350d1a10 | 341 | case MAF_SENSOR: // ((256*A)+B) / 100 [g/s] |
| RogerJKelly | 7:467e350d1a10 | 342 | can_msg[0] = 0x04; |
| RogerJKelly | 7:467e350d1a10 | 343 | can_msg[2] = MAF_SENSOR; |
| RogerJKelly | 7:467e350d1a10 | 344 | can_msg[3] = (ecu.maf_airflow & 0xff00) >> 8; |
| RogerJKelly | 7:467e350d1a10 | 345 | can_msg[4] = ecu.maf_airflow & 0x00ff; |
| RogerJKelly | 7:467e350d1a10 | 346 | can2.write(CANMessage(PID_REPLY, can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 347 | break; |
| RogerJKelly | 7:467e350d1a10 | 348 | |
| RogerJKelly | 7:467e350d1a10 | 349 | case O2_VOLTAGE: // A * 0.005 (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc) |
| RogerJKelly | 7:467e350d1a10 | 350 | can_msg[0] = 0x04; |
| RogerJKelly | 7:467e350d1a10 | 351 | can_msg[2] = O2_VOLTAGE; |
| RogerJKelly | 7:467e350d1a10 | 352 | can_msg[3] = ecu.o2_voltage & 0x00ff; |
| RogerJKelly | 7:467e350d1a10 | 353 | can_msg[4] = (ecu.o2_voltage & 0xff00) >> 8; |
| RogerJKelly | 7:467e350d1a10 | 354 | can2.write(CANMessage(PID_REPLY, can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 355 | break;; |
| RogerJKelly | 7:467e350d1a10 | 356 | |
| RogerJKelly | 7:467e350d1a10 | 357 | case THROTTLE: // |
| RogerJKelly | 7:467e350d1a10 | 358 | can_msg[0] = 0x03; |
| RogerJKelly | 7:467e350d1a10 | 359 | can_msg[2] = THROTTLE; |
| RogerJKelly | 7:467e350d1a10 | 360 | can_msg[3] = ecu.throttle_position; |
| RogerJKelly | 7:467e350d1a10 | 361 | can2.write(CANMessage(PID_REPLY, can_msg, 8)); |
| RogerJKelly | 7:467e350d1a10 | 362 | break; |
| RogerJKelly | 7:467e350d1a10 | 363 | }//switch |
| RogerJKelly | 7:467e350d1a10 | 364 | } |
| pangsk | 6:41a4ad385daa | 365 | } |
| pangsk | 0:908be729d27c | 366 | |
| RogerJKelly | 7:467e350d1a10 | 367 | /***************************************************************** |
| RogerJKelly | 7:467e350d1a10 | 368 | pc.printf("\n\r%x %x %x %x %x %x %x %x %x",can_MsgRx.id,can_MsgRx.data[0], |
| RogerJKelly | 7:467e350d1a10 | 369 | can_MsgRx.data[1], |
| RogerJKelly | 7:467e350d1a10 | 370 | can_MsgRx.data[2], |
| RogerJKelly | 7:467e350d1a10 | 371 | can_MsgRx.data[3], |
| RogerJKelly | 7:467e350d1a10 | 372 | can_MsgRx.data[4], |
| RogerJKelly | 7:467e350d1a10 | 373 | can_MsgRx.data[5], |
| RogerJKelly | 7:467e350d1a10 | 374 | can_MsgRx.data[6], |
| RogerJKelly | 7:467e350d1a10 | 375 | can_MsgRx.data[7]); |
| RogerJKelly | 7:467e350d1a10 | 376 | ****************************************************************/ |
| RogerJKelly | 7:467e350d1a10 | 377 | led2 = 0; |
| RogerJKelly | 7:467e350d1a10 | 378 | } |
| RogerJKelly | 7:467e350d1a10 | 379 | else |
| RogerJKelly | 7:467e350d1a10 | 380 | { |
| RogerJKelly | 7:467e350d1a10 | 381 | //pc.printf("\n\rNo CAN Message\n\r"); |
| RogerJKelly | 7:467e350d1a10 | 382 | int rxerrors = can2.rderror(); |
| RogerJKelly | 7:467e350d1a10 | 383 | if ( rxerrors != 0 ) |
| RogerJKelly | 7:467e350d1a10 | 384 | { |
| RogerJKelly | 7:467e350d1a10 | 385 | pc.printf("\n\rCAN Rx Errors: %d\n\r", rxerrors); |
| RogerJKelly | 7:467e350d1a10 | 386 | can2.reset(); |
| pangsk | 6:41a4ad385daa | 387 | } |
| RogerJKelly | 7:467e350d1a10 | 388 | /************************* |
| RogerJKelly | 7:467e350d1a10 | 389 | else |
| pangsk | 6:41a4ad385daa | 390 | { |
| RogerJKelly | 7:467e350d1a10 | 391 | pc.printf("."); |
| pangsk | 6:41a4ad385daa | 392 | } |
| RogerJKelly | 7:467e350d1a10 | 393 | **************************/ |
| RogerJKelly | 7:467e350d1a10 | 394 | } |
| RogerJKelly | 7:467e350d1a10 | 395 | |
| pangsk | 6:41a4ad385daa | 396 | return 0; |
| pangsk | 0:908be729d27c | 397 | |
| pangsk | 0:908be729d27c | 398 | } |
| pangsk | 6:41a4ad385daa | 399 | |
| RogerJKelly | 7:467e350d1a10 | 400 | |
| RogerJKelly | 7:467e350d1a10 | 401 | } // namespace mbed |