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

Committer:
AliBros
Date:
Mon May 02 04:34:18 2011 +0000
Revision:
1:4b7c280d433d
Parent:
0:5b4bcf184488

        

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 1:4b7c280d433d 7 Timer CANTimer;
AliBros 1:4b7c280d433d 8 namespace mbed {
AliBros 1:4b7c280d433d 9 OBDII::OBDII(int can_speed) {
AliBros 0:5b4bcf184488 10 can2.frequency(can_speed);
AliBros 0:5b4bcf184488 11 }
AliBros 0:5b4bcf184488 12
AliBros 0:5b4bcf184488 13 #define TIMEOUT 200
AliBros 1:4b7c280d433d 14 //function to request and OBD PID which will be placed in a buffer
AliBros 1:4b7c280d433d 15 unsigned char OBDII::request(unsigned char pid, char *buffer) {
AliBros 0:5b4bcf184488 16 char can_msg[8];
AliBros 0:5b4bcf184488 17 float engine_data;
AliBros 1:4b7c280d433d 18
AliBros 1:4b7c280d433d 19
AliBros 1:4b7c280d433d 20 //forming the CAN message payload
AliBros 1:4b7c280d433d 21 can_msg[0] = 0x02;
AliBros 0:5b4bcf184488 22 can_msg[1] = 0x01;
AliBros 1:4b7c280d433d 23 can_msg[2] = pid;
AliBros 0:5b4bcf184488 24 can_msg[3] = 0;
AliBros 1:4b7c280d433d 25 can_msg[4] = 0;
AliBros 0:5b4bcf184488 26 can_msg[5] = 0;
AliBros 1:4b7c280d433d 27 can_msg[6] = 0;
AliBros 0:5b4bcf184488 28 can_msg[7] = 0;
AliBros 0:5b4bcf184488 29
AliBros 1:4b7c280d433d 30 if (can2.write(CANMessage(PID_REQUEST, can_msg, 8))) {
AliBros 1:4b7c280d433d 31
AliBros 0:5b4bcf184488 32 }
AliBros 1:4b7c280d433d 33
AliBros 1:4b7c280d433d 34 CANTimer.reset();
AliBros 1:4b7c280d433d 35 CANTimer.start();
AliBros 1:4b7c280d433d 36
AliBros 1:4b7c280d433d 37 while (CANTimer.read_ms() < TIMEOUT) {
AliBros 1:4b7c280d433d 38
AliBros 1:4b7c280d433d 39 if (can2.read(can_MsgRx)) {
AliBros 1:4b7c280d433d 40
AliBros 1:4b7c280d433d 41 if ((can_MsgRx.id == PID_REPLY) && (can_MsgRx.data[2] == pid)) {
AliBros 1:4b7c280d433d 42 switch (can_MsgRx.data[2]) {
AliBros 1:4b7c280d433d 43 case ENGINE_RPM: // ((A*256)+B)/4 [RPM]
AliBros 1:4b7c280d433d 44 engine_data = ((can_MsgRx.data[3]*256) + can_MsgRx.data[4])/4;
AliBros 1:4b7c280d433d 45 sprintf(buffer,"%d",(int) engine_data);
AliBros 1:4b7c280d433d 46 break;
AliBros 1:4b7c280d433d 47
AliBros 1:4b7c280d433d 48 case ENGINE_COOLANT_TEMP: // A-40 [degree C]
AliBros 1:4b7c280d433d 49 engine_data = can_MsgRx.data[3] - 40;
AliBros 1:4b7c280d433d 50 sprintf(buffer,"%d",(int) engine_data);
AliBros 1:4b7c280d433d 51
AliBros 1:4b7c280d433d 52 break;
AliBros 1:4b7c280d433d 53
AliBros 1:4b7c280d433d 54 case VEHICLE_SPEED: // A [km]
AliBros 1:4b7c280d433d 55 engine_data = can_MsgRx.data[3];
AliBros 1:4b7c280d433d 56 sprintf(buffer,"%d",(int) engine_data);
AliBros 1:4b7c280d433d 57
AliBros 1:4b7c280d433d 58 break;
AliBros 0:5b4bcf184488 59
AliBros 1:4b7c280d433d 60 case MAF_SENSOR: // ((256*A)+B) / 100 [g/s]
AliBros 1:4b7c280d433d 61 engine_data = ((can_MsgRx.data[3]*256) + can_MsgRx.data[4])/100;
AliBros 1:4b7c280d433d 62 sprintf(buffer,"%d",(int) engine_data);
AliBros 1:4b7c280d433d 63
AliBros 1:4b7c280d433d 64 break;
AliBros 1:4b7c280d433d 65
AliBros 1:4b7c280d433d 66 case O2_VOLTAGE: // A * 0.005 (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc)
AliBros 1:4b7c280d433d 67 engine_data = can_MsgRx.data[3]*0.005;
AliBros 1:4b7c280d433d 68 sprintf(buffer,"%d",(int) engine_data);
AliBros 1:4b7c280d433d 69
AliBros 1:4b7c280d433d 70 case THROTTLE: //
AliBros 1:4b7c280d433d 71 engine_data = (can_MsgRx.data[3]*100)/255;
AliBros 1:4b7c280d433d 72 sprintf(buffer,"%d",(int) engine_data);
AliBros 0:5b4bcf184488 73
AliBros 1:4b7c280d433d 74
AliBros 1:4b7c280d433d 75 break;
AliBros 1:4b7c280d433d 76 }
AliBros 1:4b7c280d433d 77
AliBros 0:5b4bcf184488 78 return 1;
AliBros 1:4b7c280d433d 79
AliBros 1:4b7c280d433d 80 }
AliBros 0:5b4bcf184488 81
AliBros 1:4b7c280d433d 82 }
AliBros 1:4b7c280d433d 83 }
AliBros 0:5b4bcf184488 84
AliBros 1:4b7c280d433d 85 return 0;
AliBros 1:4b7c280d433d 86
AliBros 0:5b4bcf184488 87
AliBros 0:5b4bcf184488 88
AliBros 0:5b4bcf184488 89
AliBros 0:5b4bcf184488 90 }
AliBros 1:4b7c280d433d 91 }