From 894855a4e52f1586c66720cedfa013e9a2ed8d0b Mon Sep 17 00:00:00 2001 From: brendan gecse Date: Sun, 1 Dec 2019 04:32:26 +1300 Subject: [PATCH] Simulates Mode 01 over CANBus. --- CAN.ino | 25 ++++++++ HTSCANSIM.ino | 49 ++++++++++++++++ MODE1.ino | 96 ++++++++++++++++++++++++++++++ MODE3.ino | 9 +++ SERIAL.ino | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 336 insertions(+) create mode 100644 CAN.ino create mode 100644 HTSCANSIM.ino create mode 100644 MODE1.ino create mode 100644 MODE3.ino create mode 100644 SERIAL.ino diff --git a/CAN.ino b/CAN.ino new file mode 100644 index 0000000..e45b984 --- /dev/null +++ b/CAN.ino @@ -0,0 +1,25 @@ +void DOCAN() +{ + if (!digitalRead(CAN0_INT)) + { + CAN.readMsgBuf(&canId, &len, buf); + MODE = buf[1]; + PID = buf[2]; + if (MODE == 1) + { + ProcessMode1(PID); + } + else if (MODE == 3) + { + ProcessMode3(); + } + else if (MODE == 4) + { + //ProcessMode4(); + } + else if (MODE == 9) + { + //ProcessMode9(); + } + } +} diff --git a/HTSCANSIM.ino b/HTSCANSIM.ino new file mode 100644 index 0000000..3116ebb --- /dev/null +++ b/HTSCANSIM.ino @@ -0,0 +1,49 @@ +#include +#include +#define CAN0_INT 2 +MCP_CAN CAN(10); +INT32U canId = 0x000; + + +unsigned char len = 0; +unsigned char OBDIImsg[8] = {4, 65, 0, 0, 0, 0, 0, 0}; +byte buf[8]; +String CMD = ""; +char msgString[64]; +byte PID = 0; +byte MODE = 0; +bool IsSweep = true; +byte sweep = 0; + +//Adjustable values +byte Load = 10; +byte TPS = 15; +int TA = 10; +float O2 = 1; +int ECT = 80; +int IAT = 30; +byte MAP = 99; +int RPM = 2000; +byte VSS = 50; +byte FP = 10; + +void setup() { + Serial.begin(115200); + Serial.setTimeout(50); + Serial.println("HTS"); + if (CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) + { + Serial.println("HTS CAN SIM 01"); + CAN.setMode(MCP_NORMAL); + } + else + { + Serial.println("Failed To setup HTS CAN SIM 01"); + } + pinMode(CAN0_INT, INPUT); +} + +void loop() { + DOCAN(); + DOSERIAL(); +} diff --git a/MODE1.ino b/MODE1.ino new file mode 100644 index 0000000..fb300c9 --- /dev/null +++ b/MODE1.ino @@ -0,0 +1,96 @@ +void ProcessMode1(byte PID) +{ + OBDIImsg[0] = 4; + OBDIImsg[1] = 65; + if (IsSweep) + { + Load = 0; + TPS = 0; + TA = 0; + O2 = 0; + ECT = 0; + IAT = 0; + MAP = 0; + RPM = 0; + VSS = 0; + FP = 0; + sweep++; + } + else + { + sweep = 0; + } + if (PID == 0x00) //PID list 1 + { + OBDIImsg[0] = 6; + OBDIImsg[3] = 0xFF; + OBDIImsg[4] = 0xFF; + OBDIImsg[5] = 0xFF; + OBDIImsg[6] = 0xFF; + } + if (PID == 0x03) //Fuel system status + { + OBDIImsg[2] = PID; + OBDIImsg[3] = 2; + } + if (PID == 0x04) //Engine Load + { + OBDIImsg[2] = PID; + OBDIImsg[3] = (((Load + sweep) * 255) / 100); + } + if (PID == 0x05) //ECT + { + OBDIImsg[2] = PID; + OBDIImsg[3] = ((ECT + sweep) + 40); + } + if (PID == 0x0A) //Fuel Pressure + { + OBDIImsg[2] = PID; + OBDIImsg[3] = ((FP + sweep) * 3); + } + if (PID == 0x0B) //MAP + { + OBDIImsg[2] = PID; + OBDIImsg[3] = MAP + sweep; + } + if (PID == 0x0C) //RPM + { + int TRPM = RPM + (sweep * 20); + TRPM = TRPM * 4; + OBDIImsg[2] = PID; + OBDIImsg[3] = TRPM / 256; + OBDIImsg[4] = TRPM & 0xFF; + } + if (PID == 0x0D) //Speed + { + OBDIImsg[2] = PID; + OBDIImsg[3] = VSS + sweep; + } + if (PID == 0x0E) //Timing Advance + { + OBDIImsg[2] = PID; + OBDIImsg[3] = (((TA + sweep) * 2) + 128); + } + if (PID == 0x0F) //IAT + { + OBDIImsg[2] = PID; + OBDIImsg[3] = (IAT + sweep) + 40; + } + if (PID == 0x11) //TPS + { + TPS = (TPS + sweep); + OBDIImsg[2] = PID; + OBDIImsg[3] = TPS; + } + if (PID == 0x14) //O2 + { + OBDIImsg[2] = PID; + if (IsSweep) + { + O2 = random(80, 120); + O2 = O2 / 100; + } + OBDIImsg[3] = O2 * 200; + } + CAN.sendMsgBuf(0x7E8, 0, 8, OBDIImsg); +} diff --git a/MODE3.ino b/MODE3.ino new file mode 100644 index 0000000..d80c487 --- /dev/null +++ b/MODE3.ino @@ -0,0 +1,9 @@ +void ProcessMode3() +{ + OBDIImsg[0] = 6; + OBDIImsg[1] = 67; + OBDIImsg[2] = 1; + OBDIImsg[3] = 2; + OBDIImsg[4] = 23; + CAN.sendMsgBuf(0x7E8, 0, 8, OBDIImsg); +} diff --git a/SERIAL.ino b/SERIAL.ino new file mode 100644 index 0000000..ad5e105 --- /dev/null +++ b/SERIAL.ino @@ -0,0 +1,157 @@ +void DOSERIAL() +{ + if (GETCMD()) + { + int tempvalue = getValue(CMD, ':', 1).toInt(); + + if (CMD.indexOf("SW") == 0) + { + if (tempvalue == 1) + { + IsSweep = true; + } + else + { + IsSweep = false; + } + Serial.println(String("Sweep " + String(IsSweep))); + } + + + if (CMD.indexOf("RPM") == 0) + { + if (tempvalue <= 10000 && tempvalue > 0) + { + RPM = tempvalue; + Serial.println("S"); + } else { + Serial.println(String(RPM)); + } + } + + if (CMD.indexOf("FP") == 0) + { + if (tempvalue <= 255 && tempvalue > 0) + { + FP = tempvalue; + Serial.println("S"); + } else { + Serial.println(String(FP)); + } + } + + if (CMD.indexOf("VSS") == 0) + { + if (tempvalue <= 255 && tempvalue > 0) + { + VSS = tempvalue; + Serial.println("S"); + } else { + Serial.println(String(VSS)); + } + } + + if (CMD.indexOf("EL") == 0) + { + if (tempvalue <= 100 && tempvalue > 0) + { + Load = tempvalue; + Serial.println("S"); + } else { + Serial.println(String(Load)); + } + } + if (CMD.indexOf("TPS") == 0) + { + if (tempvalue <= 255 && tempvalue > 0) + { + TPS = tempvalue; + Serial.println("S"); + } else { + Serial.println(String(TPS)); + } + } + + if (CMD.indexOf("TA") == 0) + { + if (tempvalue <= 63 && tempvalue > -64) + { + TA = tempvalue; + Serial.println("S"); + } else { + Serial.println(String(TA)); + } + } + + if (CMD.indexOf("O2") == 0) + { + if (getValue(CMD, ':', 1).toFloat() <= 5 && getValue(CMD, ':', 1).toFloat() > 0) + { + O2 = getValue(CMD, ':', 1).toFloat(); + Serial.println("S"); + } else { + Serial.println(String(O2)); + } + } + if (CMD.indexOf("ECT") == 0) + { + if (tempvalue <= 215 && tempvalue > -40) + { + ECT = tempvalue; + Serial.println("S"); + } else { + Serial.println(String(ECT)); + } + } + if (CMD.indexOf("IAT") == 0) + { + if (tempvalue <= 215 && tempvalue > -40) + { + IAT = tempvalue; + Serial.println("S"); + } else { + Serial.println(String(IAT)); + } + } + if (CMD.indexOf("MAP") == 0) + { + if (tempvalue <= 255 && tempvalue > 0) + { + MAP = tempvalue; + Serial.println("S"); + } else { + Serial.println(String(MAP)); + } + } + + } + +} + +bool GETCMD() { + if (Serial.available() > 0) { + CMD = Serial.readString(); + CMD.trim(); + return true; + } + else + { + return false; + } +} + +String getValue(String data, char separator, int index) +{ + int found = 0; + int strIndex[] = { 0, -1 }; + int maxIndex = data.length() - 1; + + for (int i = 0; i <= maxIndex && found <= index; i++) { + if (data.charAt(i) == separator || i == maxIndex) { + found++; + strIndex[0] = strIndex[1] + 1; + strIndex[1] = (i == maxIndex) ? i + 1 : i; + } + } + return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; +}