OBDII library, based on SK Pang\\\'s ecu reader. more details to be added shortly.

Committer:
AliBros
Date:
Mon May 02 02:46:26 2011 +0000
Revision:
0:5b4bcf184488
Child:
1:4b7c280d433d

        

Who changed what in which revision?

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