This is Ben and I's final project for Embedded Systems. Our goal was to create a car diagnostic system that interfaces with the CAN bus in all US sold cars newer than 2008. We aimed to request data such as RPM, Vehicle speed, engine temp, etc and then display our findings. This software was succussful in communicating with the car and reading and writing, however it fails to accomplish it's task when dealing with certain makes of cars, specifically Honda. Included are various PID codes, functions, and files that interface with a car's CAN bus system.

Dependencies:   UniGraphic mbed

Committer:
Nrode17
Date:
Wed Jan 27 17:40:51 2016 +0000
Revision:
0:bb9076ae4c4a
Final Update for our final project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nrode17 0:bb9076ae4c4a 1 #include "ODBII.h"
Nrode17 0:bb9076ae4c4a 2 #include "globals.h"
Nrode17 0:bb9076ae4c4a 3
Nrode17 0:bb9076ae4c4a 4 DigitalOut myled(LED1);//diagnostic LED, used to see if data is being transmitted
Nrode17 0:bb9076ae4c4a 5 Timer CANTimer;//This is used to determine whether or not the car has taken too long to respond
Nrode17 0:bb9076ae4c4a 6
Nrode17 0:bb9076ae4c4a 7 void set_frequency(int amount)
Nrode17 0:bb9076ae4c4a 8 {
Nrode17 0:bb9076ae4c4a 9 can2.frequency(amount);//Different ECU's operate at different frequencies, 125kbs, 25kbs, 500kbs
Nrode17 0:bb9076ae4c4a 10 }
Nrode17 0:bb9076ae4c4a 11
Nrode17 0:bb9076ae4c4a 12 float request(char pid)
Nrode17 0:bb9076ae4c4a 13 {
Nrode17 0:bb9076ae4c4a 14 char can_msg[8];//message to be send to CAN
Nrode17 0:bb9076ae4c4a 15 float engine_data = 0;// this is what we get back
Nrode17 0:bb9076ae4c4a 16
Nrode17 0:bb9076ae4c4a 17
Nrode17 0:bb9076ae4c4a 18 //forming the CAN message payload, uses 8 bytes, first is number of extra bytes, second is mode, third is PID, others are misc
Nrode17 0:bb9076ae4c4a 19 can_msg[0] = 0x02;//# of extra bytes
Nrode17 0:bb9076ae4c4a 20 can_msg[1] = 0x01;//mode
Nrode17 0:bb9076ae4c4a 21 can_msg[2] = pid;
Nrode17 0:bb9076ae4c4a 22 can_msg[3] = 0;
Nrode17 0:bb9076ae4c4a 23 can_msg[4] = 0;
Nrode17 0:bb9076ae4c4a 24 can_msg[5] = 0;
Nrode17 0:bb9076ae4c4a 25 can_msg[6] = 0;
Nrode17 0:bb9076ae4c4a 26 can_msg[7] = 0;
Nrode17 0:bb9076ae4c4a 27
Nrode17 0:bb9076ae4c4a 28 if (can2.write(CANMessage(PID_REQUEST, can_msg, 8)))
Nrode17 0:bb9076ae4c4a 29 {
Nrode17 0:bb9076ae4c4a 30 myled = !myled;//diagnostic LED
Nrode17 0:bb9076ae4c4a 31 }
Nrode17 0:bb9076ae4c4a 32 CANTimer.reset();
Nrode17 0:bb9076ae4c4a 33 CANTimer.start();
Nrode17 0:bb9076ae4c4a 34
Nrode17 0:bb9076ae4c4a 35 while(CANTimer.read_us() < 200)//give it some time to respond
Nrode17 0:bb9076ae4c4a 36 {
Nrode17 0:bb9076ae4c4a 37 if (can2.read(can_MsgRx)) {//if you can read back, then go into it
Nrode17 0:bb9076ae4c4a 38 pc.printf("Message read\n\r");
Nrode17 0:bb9076ae4c4a 39
Nrode17 0:bb9076ae4c4a 40
Nrode17 0:bb9076ae4c4a 41 for (int i = 0; i < (int)can_MsgRx.len; i++) {
Nrode17 0:bb9076ae4c4a 42 pc.printf("can_MsgRx.data[%d]: %x\n\r", i, can_MsgRx.data[i]);//this just prints the contents of the data back
Nrode17 0:bb9076ae4c4a 43 }
Nrode17 0:bb9076ae4c4a 44
Nrode17 0:bb9076ae4c4a 45
Nrode17 0:bb9076ae4c4a 46
Nrode17 0:bb9076ae4c4a 47 if ((can_MsgRx.id == PID_REPLY) && (can_MsgRx.data[2] == pid)) //if the PID match, then we know we have valid data
Nrode17 0:bb9076ae4c4a 48 {
Nrode17 0:bb9076ae4c4a 49 pc.printf("We've made it this far\n\r");
Nrode17 0:bb9076ae4c4a 50 switch (can_MsgRx.data[2]) //the PID is in [2], so we use that in this switch
Nrode17 0:bb9076ae4c4a 51 {
Nrode17 0:bb9076ae4c4a 52 case ENGINE_RPM: // ((A*256)+B)/4 is the formula for rpm
Nrode17 0:bb9076ae4c4a 53 engine_data = ((can_MsgRx.data[3]*256) + can_MsgRx.data[4])/4;
Nrode17 0:bb9076ae4c4a 54 break;
Nrode17 0:bb9076ae4c4a 55
Nrode17 0:bb9076ae4c4a 56 case ENGINE_COOLANT_TEMP: //C, this is the formula for temp
Nrode17 0:bb9076ae4c4a 57 engine_data = can_MsgRx.data[3] - 40;
Nrode17 0:bb9076ae4c4a 58 break;
Nrode17 0:bb9076ae4c4a 59
Nrode17 0:bb9076ae4c4a 60 case VEHICLE_SPEED: //[km], this is the formula for kmph
Nrode17 0:bb9076ae4c4a 61 engine_data = can_MsgRx.data[3];
Nrode17 0:bb9076ae4c4a 62 break;
Nrode17 0:bb9076ae4c4a 63 }
Nrode17 0:bb9076ae4c4a 64
Nrode17 0:bb9076ae4c4a 65 return engine_data;//whatever we get we return back to the main.cpp
Nrode17 0:bb9076ae4c4a 66
Nrode17 0:bb9076ae4c4a 67 }
Nrode17 0:bb9076ae4c4a 68
Nrode17 0:bb9076ae4c4a 69 }
Nrode17 0:bb9076ae4c4a 70 }
Nrode17 0:bb9076ae4c4a 71
Nrode17 0:bb9076ae4c4a 72 return 0;
Nrode17 0:bb9076ae4c4a 73 }