CAN-Bus ECU simulator. Only part of the SAE J1979 are implemented. Uses CAN-Bus demo board as hardware platform. http://skpang.co.uk/catalog/canbus-ecu-simulator-with-lpc1768-module-p-1400.html Useful for testing diagnostic tools.
Fork of ecu_reader by
Diff: main.cpp
- Revision:
- 6:41a4ad385daa
- Parent:
- 3:05bb8f0bd7a4
--- a/main.cpp Tue Aug 28 13:16:46 2012 +0000 +++ b/main.cpp Tue Dec 30 10:10:02 2014 +0000 @@ -1,12 +1,8 @@ /* -mbed Can-Bus demo - -This program is to demonstrate the CAN-bus capability of the mbed module. +mbed Can-Bus ECU simulator -http://www.skpang.co.uk/catalog/product_info.php?products_id=741 - -v1.0 July 2010 +v1.0 December 2014 ******************************************************************************** @@ -16,161 +12,275 @@ ******************************************************************************** - */ #include "mbed.h" -#include "ecu_reader.h" +#include "ecu_simulator.h" #include "globals.h" #include "TextLCD.h" -#include "GPS.h" -#include "SDFileSystem.h" -GPS gps(p28, p27); 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_sim sim(CANSPEED_500); //Create object and set CAN speed +ecu_t ecu; + +void update_menu(void); +void update_param(unsigned char dir); -ecu_reader obdii(CANSPEED_500); //Create object and set CAN speed -void gps_demo(void); -void sd_demo(void); +// Menu defines +#define L0_MAIN_MENU 0 +#define L0_RPM 1 +#define L0_THROTTLE 2 +#define L0_SPEED 3 +#define L0_COOLANT 4 +#define L0_MAF 5 +#define L0_O2 6 +#define L0_DTC 7 -int main() { +#define INC 0 +#define DEC 1 + +#define CAN250 0 +#define CAN500 1 +unsigned char menu_state; +unsigned char canspeed; + +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); - - printf("ECU Reader \n"); + + printf("\n\nECU Simulator v1.0 \n"); + + led1 = 1; + wait(0.1); + led2 = 1; + wait(0.1); + led3 = 1; + wait(0.1); + led4 = 1; + wait(0.2); + led1 = 0; led2 = 0; led3 = 0; led4 = 0; + + lcd.cls(); lcd.locate(0,0); // Set LCD cursor position - lcd.printf("CAN-Bus demo"); - + lcd.printf("ECU Simulator v1.0"); lcd.locate(0,1); lcd.printf("www.skpang.co.uk"); - - pc.printf("\n\rCAN-bus demo..."); - wait(3); + wait(1); lcd.cls(); - lcd.printf("Use joystick"); - + + lcd.printf("<- Params ->"); lcd.locate(0,1); - lcd.printf("U-CAN:D-GPS:L-SD"); + lcd.printf("500kb/s"); - pc.printf("\nU-CAN:D-GPS:L-SD"); + canspeed = CAN500; + menu_state = L0_MAIN_MENU; + ecu.dtc = false; + + wait(0.2); + led1 = 1; + + while(1) { // Main CAN loop - while(1) // Wait until option is selected by the joystick - { - - if(down == 0) gps_demo(); - if(left == 0) sd_demo(); - - if(up == 0) break; + sim.request(); - } - lcd.cls(); - - while(1) { // Main CAN loop - led2 = 1; - wait(0.1); - led2 = 0; - wait(0.1); + if(!down){ + update_param(DEC); + } + + if(!up){ + update_param(INC); + } - 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(!left){ + if(menu_state != L0_MAIN_MENU) menu_state--; + update_menu(); } - if(obdii.request(VEHICLE_SPEED,buffer) == 1) - { - lcd.locate(0,1); - lcd.printf(buffer); + if(!right){ + if(menu_state != L0_DTC) menu_state++; + update_menu(); } + + } +} +void update_param(unsigned char dir) +{ + char buffer[20]; + lcd.locate(0,1); + switch(menu_state) + { + case L0_MAIN_MENU: + if(dir == INC){ + sim.canspeed(CANSPEED_500); + lcd.printf("500kb/s"); + canspeed = CAN500; + }else { + sim.canspeed(CANSPEED_250); + lcd.printf("250kb/s"); + canspeed = CAN250; + } + + break; + + case L0_RPM: + if(dir == INC){ + ecu.engine_rpm = ecu.engine_rpm +10; + } else ecu.engine_rpm = ecu.engine_rpm - 10; + + sprintf(buffer,"%d RPM ", (int)((((ecu.engine_rpm & 0xff00) >> 8) * 256) + (ecu.engine_rpm & 0x00ff)) / 4); + lcd.printf(buffer); + break; + + case L0_THROTTLE: + if(dir == INC){ + ecu.throttle_position++; + } else ecu.throttle_position--; + + sprintf(buffer,"%d %% ", (int)ecu.throttle_position); + lcd.printf(buffer); + break; + + case L0_SPEED: + if(dir == INC){ + ecu.vehicle_speed++; + } else ecu.vehicle_speed--; + + sprintf(buffer,"%d km/h ",(int) ecu.vehicle_speed); + lcd.printf(buffer); + break; + + case L0_COOLANT: + if(dir == INC){ + ecu.coolant_temp++; + } else ecu.coolant_temp--; - if(obdii.request(THROTTLE,buffer) ==1 ) - { - lcd.locate(9,1); - lcd.printf(buffer); - } - + sprintf(buffer,"%d C ",(int) ecu.coolant_temp - 40); + lcd.printf(buffer); + break; + + case L0_MAF: + if(dir == INC){ + ecu.maf_airflow = ecu.maf_airflow +10; + } else ecu.maf_airflow = ecu.maf_airflow - 10; + + sprintf(buffer,"%d g/s ", (int)((((ecu.maf_airflow & 0xff00) >> 8) * 256) + (ecu.maf_airflow & 0x00ff)) / 100); + lcd.printf(buffer); + break; + + case L0_O2: + if(dir == INC){ + ecu.o2_voltage = ecu.o2_voltage +10; + } else ecu.o2_voltage = ecu.o2_voltage - 10; + + sprintf(buffer,"%d ", (int)ecu.o2_voltage); + lcd.printf(buffer); + break; + + case L0_DTC: + if(dir == INC){ + ecu.dtc = true; + led4 = 1; + } else { + ecu.dtc = false; + led4 = 0; + } + + sprintf(buffer,"%d ", (int)ecu.dtc); + lcd.printf(buffer); + break; + } + + wait(0.1); //Delay for auto repeat + + +} +void update_menu(void) +{ + char buffer[20]; + lcd.cls(); + lcd.locate(0,0); + + switch(menu_state) + { + case L0_MAIN_MENU: + lcd.printf("CAN speed"); + lcd.locate(0,1); + if(canspeed == CAN500) lcd.printf("500kb/s"); + else lcd.printf("250kb/s"); + break; + + case L0_RPM: + lcd.printf("Engine RPM"); + lcd.locate(0,1); + sprintf(buffer,"%d RPM ", (int) ((((ecu.engine_rpm & 0xff00) >> 8) * 256) + (ecu.engine_rpm & 0x00ff)) / 4); + lcd.printf(buffer); + break; + + case L0_THROTTLE: + lcd.printf("Throttle pos"); + lcd.locate(0,1); + sprintf(buffer,"%d % ", ecu.throttle_position); + lcd.printf(buffer); + break; + + case L0_SPEED: + lcd.printf("Vehicle speed"); + lcd.locate(0,1); + sprintf(buffer,"%d kph ", ecu.vehicle_speed); + lcd.printf(buffer); + break; + + case L0_COOLANT: + lcd.printf("Coolant temp"); + lcd.locate(0,1); + sprintf(buffer,"%d C ",(int) ecu.coolant_temp - 40); + lcd.printf(buffer); + break; + + case L0_MAF: + lcd.printf("MAF air flow "); + lcd.locate(0,1); + sprintf(buffer,"%d g/s ", ecu.maf_airflow); + lcd.printf(buffer); + break; + + case L0_O2: + lcd.printf("Oxygen sensor v."); + lcd.locate(0,1); + sprintf(buffer,"%d ", ecu.o2_voltage); + lcd.printf(buffer); + break; + + case L0_DTC: + lcd.printf("DTC"); + lcd.locate(0,1); + sprintf(buffer,"%d ", ecu.dtc); + lcd.printf(buffer); + break; + + } + + while(!left); //Wait for key to be released + while(!right); + wait(0.05); + } -void gps_demo(void) -{ - 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) -{ - 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) - { - led2 = 1; - wait(0.1); - led2 = 0; - wait(0.1); - - } - -} +