Whitworth-EN173-2016 / Mbed 2 deprecated CAN Diagnostics

Dependencies:   UniGraphic mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ODBII.cpp Source File

ODBII.cpp

00001 #include "ODBII.h"
00002 #include "globals.h"
00003 
00004 DigitalOut myled(LED1);//diagnostic LED, used to see if data is being transmitted
00005 Timer CANTimer;//This is used to determine whether or not the car has taken too long to respond
00006 
00007 void set_frequency(int amount)
00008 {
00009     can2.frequency(amount);//Different ECU's operate at different frequencies, 125kbs, 25kbs, 500kbs
00010 }
00011 
00012 float request(char pid)
00013 {
00014     char can_msg[8];//message to be send to CAN 
00015     float engine_data = 0;// this is what we get back
00016 
00017 
00018     //forming the CAN message payload, uses 8 bytes, first is number of extra bytes, second is mode, third is PID, others are misc
00019     can_msg[0] = 0x02;//# of extra bytes
00020     can_msg[1] = 0x01;//mode
00021     can_msg[2] = pid;
00022     can_msg[3] = 0;
00023     can_msg[4] = 0;
00024     can_msg[5] = 0;
00025     can_msg[6] = 0;
00026     can_msg[7] = 0;
00027     
00028     if (can2.write(CANMessage(PID_REQUEST, can_msg, 8))) 
00029     {
00030         myled = !myled;//diagnostic LED
00031     }
00032     CANTimer.reset();
00033     CANTimer.start();
00034 
00035     while(CANTimer.read_us() < 200)//give it some time to respond
00036     {
00037         if (can2.read(can_MsgRx)) {//if you can read back, then go into it
00038             pc.printf("Message read\n\r");
00039             
00040  
00041             for (int i = 0; i < (int)can_MsgRx.len; i++) {
00042                 pc.printf("can_MsgRx.data[%d]: %x\n\r", i, can_MsgRx.data[i]);//this just prints the contents of the data back
00043             }
00044 
00045 
00046 
00047             if ((can_MsgRx.id == PID_REPLY) && (can_MsgRx.data[2] == pid)) //if the PID match, then we know we have valid data
00048             {
00049                 pc.printf("We've made it this far\n\r");
00050                 switch (can_MsgRx.data[2]) //the PID is in [2], so we use that in this switch
00051                 { 
00052                     case ENGINE_RPM:              //   ((A*256)+B)/4 is the formula for rpm
00053                         engine_data =  ((can_MsgRx.data[3]*256) + can_MsgRx.data[4])/4;
00054                         break;
00055 
00056                     case ENGINE_COOLANT_TEMP: //C, this is the formula for temp
00057                         engine_data =  can_MsgRx.data[3] - 40;
00058                         break;
00059 
00060                     case VEHICLE_SPEED:   //[km], this is the formula for kmph
00061                         engine_data =  can_MsgRx.data[3];
00062                         break;
00063                 }
00064 
00065                 return engine_data;//whatever we get we return back to the main.cpp
00066 
00067             }
00068 
00069         }
00070     }
00071 
00072     return 0;
00073 }