Dependencies:   FatFileSystem mbed GPS TextLCD

Committer:
pangsk
Date:
Tue Aug 28 13:16:46 2012 +0000
Revision:
5:46a6a3e36e05
Parent:
3:05bb8f0bd7a4
Published Textlcd and gps files

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