CAN-Bus ECU simulator. Only part of the SAE J1979 are implemented. Uses CAN-Bus demo board as hardware platform. http://skpang.co.uk/catalog/canbus-ecu-simulator-with-lpc1768-module-p-1400.html Useful for testing diagnostic tools.

Dependencies:   TextLCD mbed

Fork of ecu_reader by Sukkin Pang

Committer:
pangsk
Date:
Thu Jul 08 21:05:29 2010 +0000
Revision:
0:908be729d27c
Child:
3:05bb8f0bd7a4

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pangsk 0:908be729d27c 1 #include "mbed.h"
pangsk 0:908be729d27c 2 #include "ecu_reader.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
pangsk 0:908be729d27c 7 Timer CANTimer;
pangsk 0:908be729d27c 8 namespace mbed {
pangsk 0:908be729d27c 9
pangsk 0:908be729d27c 10
pangsk 0:908be729d27c 11 ecu_reader::ecu_reader(int can_speed)
pangsk 0:908be729d27c 12 {
pangsk 0:908be729d27c 13 can2.frequency(can_speed);
pangsk 0:908be729d27c 14 }
pangsk 0:908be729d27c 15
pangsk 0:908be729d27c 16 #define TIMEOUT 200
pangsk 0:908be729d27c 17 unsigned char ecu_reader::request(unsigned char pid, char *buffer)
pangsk 0:908be729d27c 18 {
pangsk 0:908be729d27c 19 char can_msg[8];
pangsk 0:908be729d27c 20 float engine_data;
pangsk 0:908be729d27c 21
pangsk 0:908be729d27c 22 led1 = 1;
pangsk 0:908be729d27c 23
pangsk 0:908be729d27c 24 can_msg[0] = 0x02;
pangsk 0:908be729d27c 25 can_msg[1] = 0x01;
pangsk 0:908be729d27c 26 can_msg[2] = pid;
pangsk 0:908be729d27c 27 can_msg[3] = 0;
pangsk 0:908be729d27c 28 can_msg[4] = 0;
pangsk 0:908be729d27c 29 can_msg[5] = 0;
pangsk 0:908be729d27c 30 can_msg[6] = 0;
pangsk 0:908be729d27c 31 can_msg[7] = 0;
pangsk 0:908be729d27c 32
pangsk 0:908be729d27c 33 if (can2.write(CANMessage(PID_REQUEST, can_msg, 8))) {
pangsk 0:908be729d27c 34
pangsk 0:908be729d27c 35 }
pangsk 0:908be729d27c 36
pangsk 0:908be729d27c 37 led1 = 0;
pangsk 0:908be729d27c 38 CANTimer.reset();
pangsk 0:908be729d27c 39 CANTimer.start();
pangsk 0:908be729d27c 40
pangsk 0:908be729d27c 41 while(CANTimer.read_ms() < TIMEOUT) {
pangsk 0:908be729d27c 42
pangsk 0:908be729d27c 43 if (can2.read(can_MsgRx)) {
pangsk 0:908be729d27c 44
pangsk 0:908be729d27c 45 if((can_MsgRx.id == PID_REPLY) && (can_MsgRx.data[2] == pid))
pangsk 0:908be729d27c 46 {
pangsk 0:908be729d27c 47 switch(can_MsgRx.data[2])
pangsk 0:908be729d27c 48 { /* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */
pangsk 0:908be729d27c 49 case ENGINE_RPM: // ((A*256)+B)/4 [RPM]
pangsk 0:908be729d27c 50 engine_data = ((can_MsgRx.data[3]*256) + can_MsgRx.data[4])/4;
pangsk 0:908be729d27c 51 sprintf(buffer,"%d rpm ",(int) engine_data);
pangsk 0:908be729d27c 52 break;
pangsk 0:908be729d27c 53
pangsk 0:908be729d27c 54 case ENGINE_COOLANT_TEMP: // A-40 [degree C]
pangsk 0:908be729d27c 55 engine_data = can_MsgRx.data[3] - 40;
pangsk 0:908be729d27c 56 sprintf(buffer,"%d degC ",(int) engine_data);
pangsk 0:908be729d27c 57
pangsk 0:908be729d27c 58 break;
pangsk 0:908be729d27c 59
pangsk 0:908be729d27c 60 case VEHICLE_SPEED: // A [km]
pangsk 0:908be729d27c 61 engine_data = can_MsgRx.data[3];
pangsk 0:908be729d27c 62 sprintf(buffer,"%d km ",(int) engine_data);
pangsk 0:908be729d27c 63
pangsk 0:908be729d27c 64 break;
pangsk 0:908be729d27c 65
pangsk 0:908be729d27c 66 case MAF_SENSOR: // ((256*A)+B) / 100 [g/s]
pangsk 0:908be729d27c 67 engine_data = ((can_MsgRx.data[3]*256) + can_MsgRx.data[4])/100;
pangsk 0:908be729d27c 68 sprintf(buffer,"%d g/s",(int) engine_data);
pangsk 0:908be729d27c 69
pangsk 0:908be729d27c 70 break;
pangsk 0:908be729d27c 71
pangsk 0:908be729d27c 72 case O2_VOLTAGE: // A * 0.005 (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc)
pangsk 0:908be729d27c 73 engine_data = can_MsgRx.data[3]*0.005;
pangsk 0:908be729d27c 74 sprintf(buffer,"%d v ",(int) engine_data);
pangsk 0:908be729d27c 75
pangsk 0:908be729d27c 76 case THROTTLE: //
pangsk 0:908be729d27c 77 engine_data = (can_MsgRx.data[3]*100)/255;
pangsk 0:908be729d27c 78 sprintf(buffer,"%d %% ",(int) engine_data);
pangsk 0:908be729d27c 79
pangsk 0:908be729d27c 80
pangsk 0:908be729d27c 81 break;
pangsk 0:908be729d27c 82 }
pangsk 0:908be729d27c 83
pangsk 0:908be729d27c 84 return 1;
pangsk 0:908be729d27c 85
pangsk 0:908be729d27c 86 }
pangsk 0:908be729d27c 87
pangsk 0:908be729d27c 88 }
pangsk 0:908be729d27c 89 }
pangsk 0:908be729d27c 90
pangsk 0:908be729d27c 91 return 0;
pangsk 0:908be729d27c 92
pangsk 0:908be729d27c 93
pangsk 0:908be729d27c 94
pangsk 0:908be729d27c 95
pangsk 0:908be729d27c 96 }
pangsk 0:908be729d27c 97 } // namespace mbed