A very basic ECU Simulator

A very basic ECU Simulator

Postby MartinViljoen » Tue Nov 10, 2015 8:05 pm

2.png


The Video
https://www.youtube.com/watch?v=Mej-OaSq6Do


//This Arduino UNO Sketch requires the Seeed CAN-BUS Shield Libraries
//https://github.com/yexiaobo-seeedstudio/CAN_BUS_Shield
#include <SPI.h>
#include "mcp_can.h"
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
char rndRPM=10;
char rndSpeed=10;

int ReadButtonPress;
int MenuID=1;
int NewMenuID=1;
int RPMFactor=10;
int CalculateRPM=10;
int SpeedFactor=10;
int CalculateSpeed=10;

INT32U canId = 0x000;

//2024 4 65 12 18 248 185 147


unsigned char len = 0;
unsigned char buf[8];
char str[20];

String BuildMessage="";
int MSGIdentifier=0;

void setup()
{
Serial.begin(38400);
//SETUP LCD
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.setCursor(0,0);
lcd.print("OBD2 Simulator.");
INITScreen();
lcd.setCursor(0,1);
lcd.print("0 ");

delay(2000);


START_INIT:

if(CAN_OK == CAN.begin(CAN_500KBPS))
{
Serial.println("CAN BUS Shield init ok!");
}
else
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(103);
goto START_INIT;
}
}


void loop()
{

if(MenuID!=NewMenuID){lcd.setCursor(0,1);lcd.print("Press Up or Down to begin");NewMenuID=MenuID;}


//MenuNavigation
ReadButtonPress = analogRead (0);
if (ReadButtonPress > 800 and ReadButtonPress < 820){}//Select
if (ReadButtonPress > 620 and ReadButtonPress < 630){if(MenuID>=2){MenuID=MenuID-1;}}//Left


if (ReadButtonPress > -10 and ReadButtonPress < 10){if(MenuID<=1){MenuID=MenuID+1;}}//Right


//When Down Button Was pressed Decrease Factors
if (ReadButtonPress > 400 and ReadButtonPress < 415)
{
if(MenuID==1)//RPM
{
CalculateRPM=(RPMFactor*256)/4;
if(RPMFactor>=10){RPMFactor=RPMFactor-1;}lcd.setCursor(0,1);lcd.print(CalculateRPM-64);lcd.print(" ");
}
if(MenuID==2)//SPEED
{
CalculateSpeed=SpeedFactor;
if(SpeedFactor>10){SpeedFactor=SpeedFactor-1;}lcd.setCursor(0,1);lcd.print(CalculateSpeed-1);lcd.print(" ");
}
}
//When UP Button Was pressed Decrease Factors
if (ReadButtonPress > 190 and ReadButtonPress < 215)
{
if(MenuID==1)
{
CalculateRPM=(RPMFactor*256)/4;
if(RPMFactor<=230){RPMFactor=RPMFactor+1;}lcd.setCursor(0,1);lcd.print(CalculateRPM+64);lcd.print(" ");
}
if(MenuID==2)//SPEED
{
CalculateSpeed=SpeedFactor;
if(SpeedFactor<230){SpeedFactor=SpeedFactor+1;}lcd.setCursor(0,1);lcd.print(CalculateSpeed+1);lcd.print(" ");
}
}

if(MenuID==1){lcd.setCursor(0,0);lcd.print ("RPM Output ");}
if(MenuID==2){lcd.setCursor(0,0);lcd.print ("Speed Output ");}







char rndCoolantTemp=random(1,200);

/*
if(rndRPM<=100)
{
rndRPM++;
}
else
{
rndRPM=13;
}
*/


rndRPM=RPMFactor;
rndSpeed=SpeedFactor;
char rndIAT=random(0,255);
char rndMAF=random(0,255);
char rndAmbientAirTemp=random(0,200);
char rndCAT1Temp=random(100,110);
char rndEGT1 = 103;

//GENERAL ROUTINE
unsigned char SupportedPID[8] = {1,2,3,4,5,6,7,8};
unsigned char MilCleared[7] = {4, 65, 63, 34, 224, 185, 147};

//SENSORS
unsigned char CoolantTemp[7] = {4, 65, 5, rndCoolantTemp, 0, 185, 147};
unsigned char rpm[7] = {4, 65, 12, rndRPM, 0, 185, 147};
unsigned char vspeed[7] = {4, 65, 13, rndSpeed, 224, 185, 147};
unsigned char IATSensor[7] = {4, 65, 15, rndIAT, 0, 185, 147};
unsigned char MAFSensor[7] = {4, 65, 16, rndMAF, 0, 185, 147};
unsigned char AmbientAirTemp[7] = {4, 65, 70, rndAmbientAirTemp, 0, 185, 147};
unsigned char CAT1Temp[7] = {4, 65, 60, rndCAT1Temp, 224, 185, 147};
unsigned char CAT2Temp[7] = {4, 65, 61, rndCAT1Temp, 224, 185, 147};
unsigned char CAT3Temp[7] = {4, 65, 62, rndCAT1Temp, 224, 185, 147};
unsigned char CAT4Temp[7] = {4, 65, 63, rndCAT1Temp, 224, 185, 147};
unsigned char EGT1[7] = {4, 65, 120, 01, 19, 20, 0};

if(CAN_MSGAVAIL == CAN.checkReceive())
{

CAN.readMsgBuf(&len, buf);
canId = CAN.getCanId();
Serial.print("<");Serial.print(canId);Serial.print(",");

for(int i = 0; i<len; i++)
{
BuildMessage = BuildMessage + buf[i] + ",";
}
Serial.println( BuildMessage);

//Check wich message was received.
if(BuildMessage=="2,1,0,0,0,0,0,0,") {CAN.sendMsgBuf(0x7E8, 0, 8, SupportedPID);}
if(BuildMessage=="2,1,1,0,0,0,0,0,") {CAN.sendMsgBuf(0x7E8, 0, 7, MilCleared);}

//SEND SENSOR STATUSES
if(BuildMessage=="2,1,5,0,0,0,0,0,") {CAN.sendMsgBuf(0x7E8, 0, 7, CoolantTemp);}
if(BuildMessage=="2,1,12,0,0,0,0,0,"){CAN.sendMsgBuf(0x7E8, 0, 7, rpm);}
if(BuildMessage=="2,1,13,0,0,0,0,0,"){CAN.sendMsgBuf(0x7E8, 0, 7, vspeed);}
if(BuildMessage=="2,1,15,0,0,0,0,0,"){CAN.sendMsgBuf(0x7E8, 0, 7, IATSensor);}
if(BuildMessage=="2,1,16,0,0,0,0,0,"){CAN.sendMsgBuf(0x7E8, 0, 7, MAFSensor);}
if(BuildMessage=="2,1,70,0,0,0,0,0,"){CAN.sendMsgBuf(0x7E8, 0, 7, AmbientAirTemp);}
if(BuildMessage=="2,1,60,0,0,0,0,0,"){CAN.sendMsgBuf(0x7E8, 0, 7, CAT1Temp);}
if(BuildMessage=="2,1,61,0,0,0,0,0,"){CAN.sendMsgBuf(0x7E8, 0, 7, CAT2Temp);}
if(BuildMessage=="2,1,62,0,0,0,0,0,"){CAN.sendMsgBuf(0x7E8, 0, 7, CAT3Temp);}
if(BuildMessage=="2,1,63,0,0,0,0,0,"){CAN.sendMsgBuf(0x7E8, 0, 7, CAT4Temp);}
if(BuildMessage=="2,1,120,0,0,0,0,0,"){CAN.sendMsgBuf(0x7E8, 0, 7,EGT1); }

BuildMessage="";

}

}

void INITScreen()
{
lcd.setCursor(0,0);
lcd.print("OBD2 Simulator.");
delay(500);
lcd.setCursor(0,0);
lcd.print("OBD2 Simulator..");
delay(500);
lcd.setCursor(0,0);
lcd.print("OBD2 Simulator...");
delay(500);
lcd.setCursor(0,0);
lcd.print("OBD2 Simulator....");
delay(500);
lcd.setCursor(0,0);
lcd.print("OBD2 Simulator.....");
delay(500);
lcd.setCursor(0,0);
lcd.print("OBD2 Simulator......");
delay(1000);
lcd.setCursor(0,0);
lcd.print("Welcome! ");
delay(1000);

}
You do not have the required permissions to view the files attached to this post.
Image
User avatar
MartinViljoen
 
Posts: 109
Joined: Mon Oct 06, 2014 5:23 pm
Location: Centurion South Africa

Return to OBD2 & CAN-BUS Projects

Who is online

Users browsing this forum: No registered users and 1 guest

cron