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.
Fork of ecu_reader by
ecu_simulator.cpp
00001 #include "mbed.h" 00002 #include "ecu_simulator.h" 00003 #include "globals.h" 00004 00005 00006 // Use a timer to see if things take too long 00007 Timer CANTimer; 00008 namespace mbed { 00009 00010 00011 ecu_sim::ecu_sim(int can_speed) 00012 { 00013 can2.frequency(can_speed); 00014 } 00015 00016 void ecu_sim::canspeed(int can_speed) 00017 { 00018 can2.frequency(can_speed); 00019 } 00020 00021 00022 00023 #define TIMEOUT 200 00024 unsigned char ecu_sim::request(void) 00025 { 00026 char can_msg[8]; 00027 00028 00029 00030 00031 if ((can2.read(can_MsgRx)) && (can_MsgRx.id == PID_REQUEST) ){ 00032 led2 = 1; 00033 if(can_MsgRx.data[1] == MODE3) // Request trouble codes 00034 { 00035 if(ecu.dtc == false){ 00036 can_msg[0] = 0x02; 00037 can_msg[1] = MODE3_RESPONSE; 00038 can_msg[2] = 0x00; 00039 }else{ 00040 can_msg[0] = 0x06; 00041 can_msg[1] = MODE3_RESPONSE; 00042 can_msg[2] = 0x02; 00043 can_msg[3] = 0x01; 00044 can_msg[4] = 0x00; 00045 can_msg[5] = 0x02; 00046 can_msg[6] = 0x00; 00047 } 00048 can2.write(CANMessage(PID_REPLY, can_msg, 8)); 00049 } 00050 00051 if(can_MsgRx.data[1] == MODE4) // Clear trouble codes, clear Check engine light 00052 { 00053 ecu.dtc = false; 00054 led4 = 0; 00055 00056 can_msg[0] = 0x00; 00057 can_msg[1] = MODE4_RESPONSE; 00058 can2.write(CANMessage(PID_REPLY, can_msg, 8)); 00059 } 00060 00061 if(can_MsgRx.data[1] == MODE1) 00062 { 00063 can_msg[1] = MODE1_RESPONSE; 00064 switch(can_MsgRx.data[2]) 00065 { /* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */ 00066 case PID_SUPPORTED: 00067 can_msg[0] = 0x06; 00068 can_msg[2] = PID_SUPPORTED; 00069 can_msg[3] = 0xE8; 00070 can_msg[4] = 0x19; 00071 can_msg[5] = 0x30; 00072 can_msg[6] = 0x12; 00073 can_msg[5] = 0x00; 00074 can2.write(CANMessage(PID_REPLY, can_msg, 8)); 00075 00076 break; 00077 00078 case MONITOR_STATUS: 00079 can_msg[0] = 0x05; 00080 can_msg[2] = MONITOR_STATUS; 00081 00082 if(ecu.dtc == true) can_msg[3] = 0x82; 00083 else can_msg[3] = 0x00; 00084 00085 can_msg[4] = 0x07; 00086 can_msg[5] = 0xFF; 00087 can2.write(CANMessage(PID_REPLY, can_msg, 8)); 00088 break; 00089 00090 case ENGINE_RPM: // ((A*256)+B)/4 [RPM] 00091 can_msg[0] = 0x04; 00092 can_msg[2] = ENGINE_RPM; 00093 can_msg[3] = (ecu.engine_rpm & 0xff00) >> 8; 00094 can_msg[4] = ecu.engine_rpm & 0x00ff; 00095 can2.write(CANMessage(PID_REPLY, can_msg, 8)); 00096 break; 00097 00098 case ENGINE_COOLANT_TEMP: // A-40 [degree C] 00099 can_msg[0] = 0x03; 00100 can_msg[2] = ENGINE_COOLANT_TEMP; 00101 can_msg[3] = ecu.coolant_temp; 00102 can2.write(CANMessage(PID_REPLY, can_msg, 8)); 00103 break; 00104 00105 case VEHICLE_SPEED: // A [km] 00106 can_msg[0] = 0x03; 00107 can_msg[2] = VEHICLE_SPEED; 00108 can_msg[3] = ecu.vehicle_speed; 00109 can2.write(CANMessage(PID_REPLY, can_msg, 8)); 00110 break; 00111 00112 case MAF_SENSOR: // ((256*A)+B) / 100 [g/s] 00113 can_msg[0] = 0x04; 00114 can_msg[2] = MAF_SENSOR; 00115 can_msg[3] = (ecu.maf_airflow & 0xff00) >> 8; 00116 can_msg[4] = ecu.maf_airflow & 0x00ff; 00117 can2.write(CANMessage(PID_REPLY, can_msg, 8)); 00118 break; 00119 00120 case O2_VOLTAGE: // A * 0.005 (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc) 00121 can_msg[0] = 0x04; 00122 can_msg[2] = O2_VOLTAGE; 00123 can_msg[3] = ecu.o2_voltage & 0x00ff; 00124 can_msg[4] = (ecu.o2_voltage & 0xff00) >> 8; 00125 can2.write(CANMessage(PID_REPLY, can_msg, 8)); 00126 break;; 00127 00128 case THROTTLE: // 00129 can_msg[0] = 0x03; 00130 can_msg[2] = THROTTLE; 00131 can_msg[3] = ecu.throttle_position; 00132 can2.write(CANMessage(PID_REPLY, can_msg, 8)); 00133 break; 00134 }//switch 00135 } 00136 00137 00138 00139 pc.printf("\n\r%x %x %x %x %x %x %x %x %x",can_MsgRx.id,can_MsgRx.data[0], 00140 can_MsgRx.data[1], 00141 can_MsgRx.data[2], 00142 can_MsgRx.data[3], 00143 can_MsgRx.data[4], 00144 can_MsgRx.data[5], 00145 can_MsgRx.data[6], 00146 can_MsgRx.data[7]); 00147 led2 = 0; 00148 } 00149 00150 return 0; 00151 00152 } 00153 00154 00155 } // namespace mbed
Generated on Wed Jul 13 2022 23:36:48 by
 1.7.2
 1.7.2 
    