Simple CAN test based on SKpang's work

Dependencies:   CANInterface mbed

Fork of ecu_reader by Nicholas Herriot

Revision:
9:4fbd2f50cf78
Parent:
7:61b712aa2771
--- a/main.cpp	Tue Feb 25 14:09:09 2014 +0000
+++ b/main.cpp	Wed Apr 30 13:51:32 2014 +0000
@@ -1,215 +1,55 @@
-/*
-
-mbed Can-Bus demo
-
-This program is to demonstrate the CAN-bus capability of the mbed module.
-
-http://www.skpang.co.uk/catalog/product_info.php?products_id=741
-
-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 "CANInterface.h"
 
-*/
-
-#include "mbed.h"
-#include "ecu_reader.h"
-#include "globals.h"
-#include "TextLCD.h"
-#include "GPS.h"
-#include "SDFileSystem.h"
-
-GPS gps(p28, p27);
-TextLCD lcd2(p30, p19, p20, p17, p16, p15, p14);
-TextLCD lcd(p18, p19, p20, p17, p16, p15, p14); // rs, rw, e, d0, d1, d2, d3
-SDFileSystem sd(p5, p6, p7, p13, "sd");
-
-DigitalIn click(p21);   // Joystick inputs
-DigitalIn right(p22);
-DigitalIn down(p23);
-DigitalIn left(p24);
-DigitalIn up(p25);
 Serial pc(USBTX, USBRX);
 
-
-
-ecu_reader obdii(CANSPEED_500);     //Create object and set CAN speed
-void can_demo(void);
-void gps_demo(void);
-void sd_demo(void);
-void home(void);                    // Function to set the screen on a home position
-
-int main() 
-{
-    pc.baud(115200);
-    char buffer[20];
-    
-    //Enable Pullup 
-    click.mode(PullUp);
-    right.mode(PullUp);
-    down.mode(PullUp);
-    left.mode(PullUp);
-    up.mode(PullUp);
-    
-    // Display splash screen
-    printf("Vodafone UBI Prototype \n"); 
-    lcd.locate(0,0);                // Set LCD cursor position
-    lcd.printf("Voda UBI Proto");    
-    lcd.locate(0,1);
-    lcd.printf("www.vodafone.com");      
-    // Wait and clear screen
-    wait(3);
-    lcd.cls();
-
-    // Call up the home menu
-    home();
-}
-
+#define CAN_RD p30
+#define CAN_TD p29
 
-void can_demo(void)
-{
-    char buffer[20];
-    lcd.cls();
-    pc.printf("\nVodafone CAN selelected:");
-    
-    lcd.locate(0,0);                // Set LCD cursor position
-    lcd.printf("Voda Car Connect");
-    lcd.locate(0,1);
-    lcd.printf("CAN bus selected");  
-
-    while(1) 
-    {  // Main CAN loop
-        if(click == 0) home();
-        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);
-              pc.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);          
-        }   
-       
-    }
-
-}
-
-
+#define ENGINE_COOLANT_TEMP 0x05
+#define ENGINE_RPM          0x0C
+#define VEHICLE_SPEED       0x0D
+#define MAF_SENSOR          0x10
+#define THROTTLE            0x11
+#define O2_VOLTAGE          0x14
 
-void home(void)
-{
-    pc.printf("\nVodafone Home Menu");
-    lcd.cls();
-    lcd.locate(0,0);
-    lcd.printf("Home - menu");
-    lcd.locate(0,1);
-    lcd.printf("select option...");
-    wait(2);
-    lcd.cls();
-    lcd.locate(0,0);
-    lcd.printf("Use joystick");
-    lcd.locate(0,1);
-    lcd.printf("U-CAN:D-GPS:P-Ho");   
-    pc.printf("\nUse the joystick to select an option - hold down the joystick to get back to home");
-    pc.printf("\nUp = CAN buss demo:Down = GPS demo:Left = SD demo: Press = Home");
- 
-    while(1)    // Wait until option is selected by the joystick
-    {
-        
-        if(down == 0) gps_demo();
-        if(left == 0) sd_demo();
-        if(up == 0) can_demo();       
-    }
-  
-}
-
-
-void gps_demo(void)
-{
+int main() {
+    pc.baud(115200);
+    pc.printf("Begin.\r\n");
+    uint8_t outBuf[2];
+    int outLen;
     
-    lcd.cls();
-    lcd.printf("GPS demo");
-    lcd.locate(0,1);
-    lcd.printf("Waiting for lock");
-    pc.printf("\nVodafone Ublox GPS initiated");
-    pc.printf("\nGPS Sampling starting....\n");   
-
-    while(1)
-    {
-      if(click == 0) home();  
-      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 Latitude: %f, Longitude: %f\n", gps.latitude, gps.longitude);
-      } 
-      else 
-      {
-        pc.printf("Oh Dear! No lock :(\n");
-        lcd.cls();
-        lcd.printf("Waiting for lock");
-      }
+    CANInterface *can = new CANInterface(CAN_RD,CAN_TD);
+    while(1) {
+       // RPM
+       can->makeRequest(ENGINE_RPM,(uint8_t*)&outBuf,&outLen);
+       int rpm = ((outBuf[0]*256) + outBuf[1])/4;
+       pc.printf("RPM: %d\r\n",rpm);
+       
+       // coolant temperature
+       can->makeRequest(ENGINE_COOLANT_TEMP,(uint8_t*)&outBuf,&outLen);
+       int coolantTemp = (int)outBuf[0]-40;
+       pc.printf("Coolant temperature: %d\r\n",coolantTemp);
+       
+       // vehicle speed
+       can->makeRequest(VEHICLE_SPEED,(uint8_t*)&outBuf,&outLen);
+       pc.printf("Vehicle speed: %d\r\n",(int)outBuf[0]);
+       
+       // maf sensor
+       can->makeRequest(MAF_SENSOR,(uint8_t*)&outBuf,&outLen);
+       int mafSensor = ((outBuf[0]*256) + outBuf[1])/100;
+       pc.printf("MAF sensor: %d\r\n",mafSensor);
+       
+       // O2 voltage
+       can->makeRequest(THROTTLE,(uint8_t*)&outBuf,&outLen);
+       int o2Voltage = outBuf[0]*0.005;
+       pc.printf("O2 voltage: %d\r\n",o2Voltage);
+       
+       // throttle
+       can->makeRequest(O2_VOLTAGE,(uint8_t*)&outBuf,&outLen);
+       int throttlePos = (outBuf[0]*100)/255;
+       pc.printf("Throttle pos: %d\r\n",throttlePos);
+       
+       wait(0.5);
     }
-
-} 
-
-void sd_demo(void)
-{
-    lcd.cls();
-     printf("\nSD demo");
-    lcd.printf("SD demo");
-    wait(2);      
-    lcd.cls();
-    
-    FILE *fp = fopen("/sd/sdtest2.txt", "w");
-    if(fp == NULL) {
-        lcd.cls();
-        lcd.printf("Could not open file for write\n");
-         pc.printf("\nCould not open file for write");
-    }
-    fprintf(fp, "Hello fun SD Card World! testing 1234");
-    fclose(fp); 
-    lcd.locate(0,1);
-    lcd.printf("Writtern to SD card");
-    pc.printf("\nWrittern to SD card");
-        
-    while(1)
-    {
-        if(click == 0) home();
-        led2 = 1;
-        wait(0.1);
-        led2 = 0;
-        wait(0.1);
-   
-    }
- 
-}
+}
\ No newline at end of file