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