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

Files at this revision

API Documentation at this revision

Comitter:
AliBros
Date:
Mon May 02 04:34:18 2011 +0000
Parent:
0:5b4bcf184488
Commit message:

Changed in this revision

OBDII.cpp Show annotated file Show diff for this revision Revisions of this file
OBDII.h Show annotated file Show diff for this revision Revisions of this file
globals.cpp Show annotated file Show diff for this revision Revisions of this file
globals.h Show annotated file Show diff for this revision Revisions of this file
--- a/OBDII.cpp	Mon May 02 02:46:26 2011 +0000
+++ b/OBDII.cpp	Mon May 02 04:34:18 2011 +0000
@@ -4,94 +4,88 @@
 
 
 // Use a timer to see if things take too long
-Timer CANTimer;  
-namespace mbed { 
-
-
-OBDII::OBDII(int can_speed)
-{
+Timer CANTimer;
+namespace mbed {
+OBDII::OBDII(int can_speed) {
     can2.frequency(can_speed);
 }
 
 #define TIMEOUT 200
-unsigned char OBDII::request(unsigned char pid,  char *buffer)
-{
+//function to request and OBD PID which will be placed in a buffer
+unsigned char OBDII::request(unsigned char pid,  char *buffer) {
     char can_msg[8];
     float engine_data;
-        
-    led1 = 1;
-  
-    can_msg[0] = 0x02;  
+
+
+    //forming the CAN message payload
+    can_msg[0] = 0x02;
     can_msg[1] = 0x01;
-    can_msg[2] = pid;  
+    can_msg[2] = pid;
     can_msg[3] = 0;
-    can_msg[4] = 0;  
+    can_msg[4] = 0;
     can_msg[5] = 0;
-    can_msg[6] = 0;  
+    can_msg[6] = 0;
     can_msg[7] = 0;
 
-   if (can2.write(CANMessage(PID_REQUEST, can_msg, 8))) {
-         
+    if (can2.write(CANMessage(PID_REQUEST, can_msg, 8))) {
+
     }
-   
-   led1 = 0;
-   CANTimer.reset();
-   CANTimer.start();
-   
-   while(CANTimer.read_ms() < TIMEOUT) {
-   
-   if (can2.read(can_MsgRx)) {
-               
-        if((can_MsgRx.id == PID_REPLY) && (can_MsgRx.data[2] == pid))
-        { 
-                        switch(can_MsgRx.data[2])
-                        {   /* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */
-                            case ENGINE_RPM:              //   ((A*256)+B)/4    [RPM]
-                                engine_data =  ((can_MsgRx.data[3]*256) + can_MsgRx.data[4])/4;
-                                sprintf(buffer,"%d",(int) engine_data);
-                                break;
-                            
-                            case ENGINE_COOLANT_TEMP:     //     A-40              [degree C]
-                                engine_data =  can_MsgRx.data[3] - 40;
-                                sprintf(buffer,"%d",(int) engine_data);
-                            
-                            break;
-                            
-                            case VEHICLE_SPEED:         // A                  [km]
-                                engine_data =  can_MsgRx.data[3];
-                                sprintf(buffer,"%d",(int) engine_data);
-                            
-                            break;
+
+    CANTimer.reset();
+    CANTimer.start();
+
+    while (CANTimer.read_ms() < TIMEOUT) {
+
+        if (can2.read(can_MsgRx)) {
+
+            if ((can_MsgRx.id == PID_REPLY) && (can_MsgRx.data[2] == pid)) {
+                switch (can_MsgRx.data[2]) { 
+                    case ENGINE_RPM:              //   ((A*256)+B)/4    [RPM]
+                        engine_data =  ((can_MsgRx.data[3]*256) + can_MsgRx.data[4])/4;
+                        sprintf(buffer,"%d",(int) engine_data);
+                        break;
+
+                    case ENGINE_COOLANT_TEMP:     //     A-40              [degree C]
+                        engine_data =  can_MsgRx.data[3] - 40;
+                        sprintf(buffer,"%d",(int) engine_data);
+
+                        break;
+
+                    case VEHICLE_SPEED:         // A                  [km]
+                        engine_data =  can_MsgRx.data[3];
+                        sprintf(buffer,"%d",(int) engine_data);
+
+                        break;
 
-                            case MAF_SENSOR:               // ((256*A)+B) / 100  [g/s]
-                                engine_data =  ((can_MsgRx.data[3]*256) + can_MsgRx.data[4])/100;
-                                sprintf(buffer,"%d",(int) engine_data);
-                            
-                            break;
+                    case MAF_SENSOR:               // ((256*A)+B) / 100  [g/s]
+                        engine_data =  ((can_MsgRx.data[3]*256) + can_MsgRx.data[4])/100;
+                        sprintf(buffer,"%d",(int) engine_data);
+
+                        break;
+
+                    case O2_VOLTAGE:            // A * 0.005   (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc)
+                        engine_data = can_MsgRx.data[3]*0.005;
+                        sprintf(buffer,"%d",(int) engine_data);
+
+                    case THROTTLE:            //
+                        engine_data = (can_MsgRx.data[3]*100)/255;
+                        sprintf(buffer,"%d",(int) engine_data);
 
-                            case O2_VOLTAGE:            // A * 0.005   (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc)
-                                engine_data = can_MsgRx.data[3]*0.005;
-                                sprintf(buffer,"%d",(int) engine_data);
-     
-                            case THROTTLE:            //
-                                engine_data = (can_MsgRx.data[3]*100)/255;
-                                sprintf(buffer,"%d",(int) engine_data);
-                             
-                            
-                            break;
-                        }
-        
+
+                        break;
+                }
+
                 return 1;
-        
-         }
+
+            }
 
-   }
-   }
+        }
+    }
 
-     return 0;
-  
+    return 0;
+
 
 
 
 }
-} // namespace mbed 
\ No newline at end of file
+}
\ No newline at end of file
--- a/OBDII.h	Mon May 02 02:46:26 2011 +0000
+++ b/OBDII.h	Mon May 02 04:34:18 2011 +0000
@@ -1,11 +1,13 @@
 #ifndef OBDII_H
 #define OBDII_H
+//Can Speed
+#define CANSPEED_125      125000
+#define CANSPEED_250      250000
+#define CANSPEED_500      500000
 
-#define CANSPEED_125      125000        // CAN speed at 125 kbps
-#define CANSPEED_250      250000        // CAN speed at 250 kbps
-#define CANSPEED_500      500000        // CAN speed at 500 kbps
-
- /* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */
+/* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs
+to add more options simply find the required PIDs  from
+http://en.wikipedia.org/wiki/OBD-II_PIDs and define them*/
 #define ENGINE_COOLANT_TEMP 0x05
 #define ENGINE_RPM          0x0C
 #define VEHICLE_SPEED       0x0D
@@ -13,31 +15,23 @@
 #define THROTTLE            0x11
 #define O2_VOLTAGE          0x14
 
+
 #define PID_REQUEST         0x7DF
 #define PID_REPLY           0x7E8
 
-namespace mbed { 
+namespace mbed {
 
-class OBDII{
+class OBDII {
 
 public:
-
     OBDII(int can_speed);
-
     unsigned char request(unsigned char pid,  char *buffer);
 
-private: 
+private:
+    int i;
 
-    int i;
- 
 };
 
-
-
-
-
-    } 
-
-
+}
 
 #endif
\ No newline at end of file
--- a/globals.cpp	Mon May 02 02:46:26 2011 +0000
+++ b/globals.cpp	Mon May 02 04:34:18 2011 +0000
@@ -1,12 +1,5 @@
 #include "globals.h"
-
 Serial pc (USBTX,USBRX);
-
-DigitalOut led1 (LED1);
-DigitalOut led2 (LED2);
-DigitalOut led3 (LED3);
-DigitalOut led4 (LED4);
-
-// We use can on mbed pins 29(CAN_TXD) and 30(CAN_RXD).
+// Using mbed pins 29(CAN_TXD) and 30(CAN_RXD)
 CAN can2(p30, p29);
 CANMessage can_MsgRx;
\ No newline at end of file
--- a/globals.h	Mon May 02 02:46:26 2011 +0000
+++ b/globals.h	Mon May 02 04:34:18 2011 +0000
@@ -4,12 +4,8 @@
 #include "mbed.h"
 
 extern Serial pc;
-extern DigitalOut led1;
-extern DigitalOut led2;
-extern DigitalOut led3;
-extern DigitalOut led4;
 
-// We use can on mbed pins 29(CAN_TXD) and 30(CAN_RXD).
+// Using mbed pins 29(CAN_TXD) and 30(CAN_RXD).
 extern CAN can2;
 extern CANMessage can_MsgRx;
 #endif
\ No newline at end of file