OBD reader

Dependencies:   mbed

Committer:
rtgree01
Date:
Mon Jan 31 05:18:42 2011 +0000
Revision:
0:60212cabf694

        

Who changed what in which revision?

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