Dependencies:   FatFileSystem mbed GPS TextLCD

Files at this revision

API Documentation at this revision

Comitter:
pangsk
Date:
Thu Jul 08 21:05:29 2010 +0000
Child:
1:3552a3289608
Commit message:

Changed in this revision

ecu_reader.cpp Show annotated file Show diff for this revision Revisions of this file
ecu_reader.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
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ecu_reader.cpp	Thu Jul 08 21:05:29 2010 +0000
@@ -0,0 +1,97 @@
+#include "mbed.h"
+#include "ecu_reader.h"
+#include "globals.h"
+
+
+// Use a timer to see if things take too long
+Timer CANTimer;  
+namespace mbed { 
+
+
+ecu_reader::ecu_reader(int can_speed)
+{
+    can2.frequency(can_speed);
+}
+
+#define TIMEOUT 200
+unsigned char ecu_reader::request(unsigned char pid,  char *buffer)
+{
+    char can_msg[8];
+    float engine_data;
+        
+    led1 = 1;
+  
+    can_msg[0] = 0x02;  
+    can_msg[1] = 0x01;
+    can_msg[2] = pid;  
+    can_msg[3] = 0;
+    can_msg[4] = 0;  
+    can_msg[5] = 0;
+    can_msg[6] = 0;  
+    can_msg[7] = 0;
+
+   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 rpm ",(int) engine_data);
+                                break;
+                            
+                            case ENGINE_COOLANT_TEMP:     //     A-40              [degree C]
+                                engine_data =  can_MsgRx.data[3] - 40;
+                                sprintf(buffer,"%d degC ",(int) engine_data);
+                            
+                            break;
+                            
+                            case VEHICLE_SPEED:         // A                  [km]
+                                engine_data =  can_MsgRx.data[3];
+                                sprintf(buffer,"%d km ",(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 g/s",(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 v ",(int) engine_data);
+     
+                            case THROTTLE:            //
+                                engine_data = (can_MsgRx.data[3]*100)/255;
+                                sprintf(buffer,"%d %% ",(int) engine_data);
+                             
+                            
+                            break;
+                        }
+        
+                return 1;
+        
+         }
+
+   }
+   }
+
+     return 0;
+  
+
+
+
+}
+} // namespace mbed 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ecu_reader.h	Thu Jul 08 21:05:29 2010 +0000
@@ -0,0 +1,43 @@
+#ifndef ECU_READER_H
+#define ECU_READER_H
+
+#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 */
+#define ENGINE_COOLANT_TEMP 0x05
+#define ENGINE_RPM          0x0C
+#define VEHICLE_SPEED       0x0D
+#define MAF_SENSOR          0x10
+#define THROTTLE            0x11
+#define O2_VOLTAGE          0x14
+
+#define PID_REQUEST         0x7DF
+#define PID_REPLY           0x7E8
+
+namespace mbed { 
+
+class ecu_reader{
+
+public:
+
+    ecu_reader(int can_speed);
+
+    unsigned char request(unsigned char pid,  char *buffer);
+
+private: 
+
+    int i;
+ 
+};
+
+
+
+
+
+    } 
+
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/globals.cpp	Thu Jul 08 21:05:29 2010 +0000
@@ -0,0 +1,12 @@
+#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).
+CAN can2(p30, p29);
+CANMessage can_MsgRx;
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/globals.h	Thu Jul 08 21:05:29 2010 +0000
@@ -0,0 +1,15 @@
+#ifndef GLOBALS_H
+#define GLOBALS_H
+
+#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).
+extern CAN can2;
+extern CANMessage can_MsgRx;
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jul 08 21:05:29 2010 +0000
@@ -0,0 +1,152 @@
+/*
+
+mbed Can-Bus demo
+
+This program is to demonstrate the CAN-bus capability of the mbed module.
+
+www.skpang.co.uk/catalog
+
+v1.0 July 2010
+
+********************************************************************************
+
+WARNING: Use at your own risk, sadly this software comes with no guarantees.
+This software is provided 'free' and in good faith, but the author does not
+accept liability for any damage arising from its use.
+
+********************************************************************************
+
+
+*/
+
+#include "mbed.h"
+#include "ecu_reader.h"
+#include "globals.h"
+#include "TextLCD.h"
+#include "GPS.h"
+
+GPS gps(p28, p27);
+TextLCD lcd(p18, p19, p20, p17, p16, p15, p14); // rs, rw, e, d0, d1, d2, d3
+
+DigitalIn click(p21);   // Joystick inputs
+DigitalIn right(p22);
+DigitalIn down(p23);
+DigitalIn left(p24);
+DigitalIn up(p25);
+
+ecu_reader obdii(CANSPEED_500);     //Create object and set CAN speed
+void gps_demo(void);
+void sd_demo(void);
+
+int main() {
+    
+    char buffer[20];
+ 
+    lcd.locate(0,0);
+    lcd.printf("CAN-Bus demo");
+    
+    lcd.locate(0,1);
+    lcd.printf("www.skpang.co.uk");
+       
+    pc.printf("\n\rCAN-bus demo...");
+    
+    wait(3);
+    lcd.cls();
+    lcd.printf("Use joystick");
+
+    lcd.locate(0,1);
+    lcd.printf("U-CAN:D-GPS:L-SD");
+
+    while(1)    // Wait until option is selected by the joystick
+    {
+   
+        if(down == 0) gps_demo();
+        if(left == 0) sd_demo();
+               
+        if(up == 0) break;
+        
+    }
+    lcd.cls();
+
+    while(1) {  // Main CAN loop
+        led2 = 1;
+        wait(0.1);
+        led2 = 0;
+        wait(0.1);
+        
+        if(obdii.request(ENGINE_RPM,buffer) == 1)   // Get engine rpm and display on LCD
+        {
+            lcd.locate(0,0);
+            lcd.printf(buffer);
+        }   
+         
+        if(obdii.request(ENGINE_COOLANT_TEMP,buffer) == 1)
+        {
+            lcd.locate(9,0);
+            lcd.printf(buffer);
+        }
+        
+        if(obdii.request(VEHICLE_SPEED,buffer) == 1)
+        {
+            lcd.locate(0,1);
+            lcd.printf(buffer);
+        }
+     
+        if(obdii.request(THROTTLE,buffer) ==1 )
+        {
+            lcd.locate(9,1);
+            lcd.printf(buffer);          
+        }   
+       
+    }
+}
+
+void gps_demo(void)
+{
+    pc.printf("\rGPS demo");
+    lcd.cls();
+    lcd.printf("GPS demo");
+    lcd.locate(0,1);
+    lcd.printf("Waiting for lock");
+ 
+    wait(3);    
+    lcd.cls();
+    
+    
+    while(1)
+    {
+      if(gps.sample()) {
+         lcd.cls();
+        lcd.printf("Long:%f", gps.longitude);
+           lcd.locate(0,1);
+        lcd.printf("Lat:%f", gps.latitude);
+            pc.printf("I'm at %f, %f\n", gps.longitude, gps.latitude);
+        } else {
+            pc.printf("Oh Dear! No lock :(\n");
+            lcd.cls();
+            lcd.printf("Waiting for lock");
+     
+ 
+    
+        }
+    
+    
+    }
+ 
+}
+
+void sd_demo(void)
+{
+    pc.printf("\rSD demo");
+    
+    lcd.cls();
+    lcd.printf("Use joystick");
+    wait(3);      
+    while(1)
+    {
+    
+    
+    
+    }
+ 
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Jul 08 21:05:29 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/3944f1e2fa4f