Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mbed-STM32F103C8T6
Revision 7:467e350d1a10, committed 2020-06-10
- Comitter:
- RogerJKelly
- Date:
- Wed Jun 10 20:15:48 2020 +0000
- Parent:
- 6:41a4ad385daa
- Commit message:
- RIXEN SIM with Maple Mini
Changed in this revision
--- a/TextLCD.lib Tue Dec 30 10:10:02 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/pangsk/code/TextLCD/#ec079a141883
--- a/ecu_simulator.cpp Tue Dec 30 10:10:02 2014 +0000
+++ b/ecu_simulator.cpp Wed Jun 10 20:15:48 2020 +0000
@@ -4,152 +4,398 @@
// Use a timer to see if things take too long
-Timer CANTimer;
-namespace mbed {
+Timer CANTimer;
+
+namespace mbed
+{
ecu_sim::ecu_sim(int can_speed)
{
- can2.frequency(can_speed);
+ if ( can2.frequency(can_speed) )
+ pc.printf("CAN speed set to %d \n\r", can_speed);
+ else
+ pc.printf("CAN speed SET failed\n\r");
}
void ecu_sim::canspeed(int can_speed)
{
- can2.frequency(can_speed);
+ if ( can2.frequency(can_speed) )
+ pc.printf("CAN speed set to %d \n\r", can_speed);
+ else
+ pc.printf("CAN speed SET failed\n\r");
}
-#define TIMEOUT 200
+//#define TIMEOUT 500
unsigned char ecu_sim::request(void)
{
char can_msg[8];
-
+ int done = 0;
+ int cnt = 0;
+ //pc.printf("*");
+
-
-
- if ((can2.read(can_MsgRx)) && (can_MsgRx.id == PID_REQUEST) ){
+ //if ((can2.read(can_MsgRx)) && (can_MsgRx.id == PID_REQUEST) )
+ if (can2.read(can_MsgRx)) //&& (can_MsgRx.id == PID_REQUEST) )
+ {
+ pc.printf(".");
led2 = 1;
- if(can_MsgRx.data[1] == MODE3) // Request trouble codes
+ if (can_MsgRx.id == ESPAR_CMD)
{
- if(ecu.dtc == false){
- can_msg[0] = 0x02;
- can_msg[1] = MODE3_RESPONSE;
- can_msg[2] = 0x00;
- }else{
- can_msg[0] = 0x06;
- can_msg[1] = MODE3_RESPONSE;
- can_msg[2] = 0x02;
- can_msg[3] = 0x01;
- can_msg[4] = 0x00;
- can_msg[5] = 0x02;
- can_msg[6] = 0x00;
- }
- can2.write(CANMessage(PID_REPLY, can_msg, 8));
+ pc.printf("0");
+ // wake command wake[8] = { 0x02, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+ if((can_MsgRx.data[0] == 0x02) && (can_MsgRx.data[1] == 0x3E))// wake request
+ { //expected[3] = {0x02, 0x7e, 0x00};
+ can_msg[0] = 0x02;
+ can_msg[1] = 0x7E;
+ can_msg[2] = 0x00; can_msg[3] = 0x00;
+ can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00;
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ pc.printf("1");
+ }
+ else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x17))
+ { // got get runtime: 7A0 03 22 FD 17 00 00 00 00
+ // send 73C 10 1F 62 FD 17 00 00 00
+ can_msg[0] = 0x10; can_msg[1] = 0x1F; can_msg[2] = 0x62; can_msg[3] = 0xFD;
+ can_msg[4] = 0x17; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00;
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ pc.printf("2");
+ //wait(0.1);
+ done = 0;
+ while ( done == 0 )
+ {
+ //wait for continue msg 7A0 30 00 14 00 00 00 00 00
+ can2.read(can_MsgRx);
+ if ((can_MsgRx.id == ESPAR_CMD) && (can_MsgRx.data[0] == 0x30))
+ {
+ done = 1;
+ pc.printf("a");
+ //73C 2377343693 0008 21 91 00 00 00 00 00 00
+ can_msg[0] = 0x21; can_msg[1] = 0x91; can_msg[2] = 0x00; can_msg[3] = 0x00;
+ can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00;
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ wait(0.2);
+ //73C 2377373697 0008 22 00 00 00 00 00 00 00
+ can_msg[0] = 0x22; can_msg[1] = 0x00; can_msg[2] = 0x00; can_msg[3] = 0x00;
+ can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00;
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ wait(0.2);
+ //73C 2377403705 0008 23 00 00 5F 00 00 00 00
+ can_msg[0] = 0x23; can_msg[1] = 0x00; can_msg[2] = 0x00; can_msg[3] = 0x5F;
+ can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00;
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ wait(0.2);
+ //73C 2377433695 0008 24 00 00 00 00 00 00 00
+ can_msg[0] = 0x24; can_msg[1] = 0x00; can_msg[2] = 0x00; can_msg[3] = 0x00;
+ can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00;
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ wait(0.2);
+ }
+ }
+ } // end of get runtime
+ else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x01))
+ { //get temperatures 7A0 03 22 FD 01 00 00 00 00 -- not sure temperatures???
+ //73C 10 09 62 FD 01 1D E2 1C -- send this
+ can_msg[0] = 0x10; can_msg[1] = 0x09; can_msg[2] = 0x62; can_msg[3] = 0xFD;
+ can_msg[4] = 0x01; can_msg[5] = 0x1D; can_msg[6] = 0xE2; can_msg[7] = 0x1C;
+ pc.printf("3");
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+
+ //wait(0.1);
+ done = 0;
+ while ( done == 0 )
+ {
+ //wait for continue msg 7A0 30 00 14 00 00 00 00 00
+ //***************************************************************
+ if (can2.read(can_MsgRx))
+ {
+ pc.printf("m");
+ //pc.printf("m %x %x %x %x %x %x %x %x %x\n\r",can_MsgRx.id,
+ // can_MsgRx.data[0], can_MsgRx.data[1], can_MsgRx.data[2], can_MsgRx.data[3],
+ // can_MsgRx.data[4], can_MsgRx.data[5], can_MsgRx.data[6], can_MsgRx.data[7]);
+ cnt++;
+ if ( cnt > 10 ) done = 1;
+ }
+ //****************************************************************/
+ //can2.read(can_MsgRx);
+ //pc.printf("x");
+ if ((can_MsgRx.id == ESPAR_CMD) && (can_MsgRx.data[0] == 0x30))
+ {
+ done = 1;
+ pc.printf("b");
+ //73C 2420412954 0008 21 84 1C B6 01 1D E2 1C
+ can_msg[0] = 0x21; can_msg[1] = 0x84; can_msg[2] = 0x1C; can_msg[3] = 0xB6;
+ can_msg[4] = 0x01; can_msg[5] = 0x1D; can_msg[6] = 0xE2; can_msg[7] = 0x1C;
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ wait(0.2);
+ }
+ }
+ } // end of get temperatures
+ else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x0A))
+ { //expected[3] = //73C 05 62 FD 0A 00 00 E2 1C
+ can_msg[0] = 0x05; can_msg[1] = 0x62;
+ can_msg[2] = 0xFD; can_msg[3] = 0x0A;
+ can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0xE2; can_msg[7] = 0x1C;
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ pc.printf("4");
+ }
+ //7A0 03 22 FD 0D 00 00 00 00
+ else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x0D))
+ { //expected[3] = //73C 04 62 FD 0D 00 20 E2 1C
+ can_msg[0] = 0x04; can_msg[1] = 0x62;
+ can_msg[2] = 0xFD; can_msg[3] = 0x0D;
+ can_msg[4] = 0x00; can_msg[5] = 0x20; can_msg[6] = 0xE2; can_msg[7] = 0x1C;
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ pc.printf("5");
+ }
+ //
+ else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x0F))
+ { //get ??? 7A0 03 22 FD 0F 00 00 00 00 -- not sure ???
+ //73C 10 0D 62 FD 0F 00 01 75 -- send this
+ can_msg[0] = 0x10; can_msg[1] = 0x0D; can_msg[2] = 0x62; can_msg[3] = 0xFD;
+ can_msg[4] = 0x0F; can_msg[5] = 0x00; can_msg[6] = 0x01; can_msg[7] = 0x75;
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ pc.printf("6");
+ //wait(0.1);
+ done = 0;
+ while ( done == 0 )
+ {
+ //wait for continue msg 7A0 30 00 14 00 00 00 00 00
+ can2.read(can_MsgRx);
+ if ((can_MsgRx.id == ESPAR_CMD) && (can_MsgRx.data[0] == 0x30))
+ {
+ done = 1;
+ pc.printf("d");
+ //73C 21 30 75 44 7F FC 7F FC
+ can_msg[0] = 0x21; can_msg[1] = 0x30; can_msg[2] = 0x75; can_msg[3] = 0x44;
+ can_msg[4] = 0x7F; can_msg[5] = 0xFC; can_msg[6] = 0x7F; can_msg[7] = 0xFC;
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ wait(0.1);
+ }
+ }
+ } // end of ??
+ else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x22))
+ { // got get 7a0 3 22 fd 22 0 0 0 0 - get water pump setting
+ //send 0x05, 0x62, 0xFD, 0x24, 0x00, 0x00, 0x00, 0x00 will keep it happy
+ can_msg[0] = 0x05; can_msg[1] = 0x62; can_msg[2] = 0xFD; can_msg[3] = 0x24;
+ can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00;
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ pc.printf("7");
+ }
+ else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x24))
+ { // got get 7a0 3 22 fd 24 0 0 0 0 - get heat time limit
+ //send 0x05, 0x62, 0xFD, 0x24, 0x00, 0x00, 0x00, 0x00 will keep it happy
+ can_msg[0] = 0x05; can_msg[1] = 0x62; can_msg[2] = 0xFD; can_msg[3] = 0x24;
+ can_msg[4] = 0x00; can_msg[5] = 0x00; can_msg[6] = 0x00; can_msg[7] = 0x00;
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ pc.printf("8");
+ }
+ else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x36))
+ { // got 7a0 3 22 fd 36 0 0 0 0 - get altitude.
+ //send Response is 0x05, 0x62, 0xFD, 0x04, [0x27, 0x10], 0xB0, 0x1C
+ can_msg[0] = 0x05; can_msg[1] = 0x62; can_msg[2] = 0xFD; can_msg[3] = 0x04;
+ can_msg[4] = 0x27; can_msg[5] = 0x10; can_msg[6] = 0xB0; can_msg[7] = 0x1C;
+ if ( got_alt == true )
+ {
+ can_msg[4] = alt[0];
+ can_msg[5] = alt[1];
+ }
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ pc.printf("9");
+ }
+ else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x22) && (can_MsgRx.data[2] == 0xFD) && (can_MsgRx.data[3] == 0x04))
+ { // battery send: { 0x03, 0x22, 0xFD, 0x04, 0x00, 0x00, 0x00, 0x00 };
+ // Response is 0x05, 0x62, 0xFD, 0x04, MSB, LSB, 0x00, 0x00
+ can_msg[0] = 0x05; can_msg[1] = 0x62; can_msg[2] = 0xFD; can_msg[3] = 0x04;
+ can_msg[4] = 0x00; can_msg[5] = 0x7A; can_msg[6] = 0x00; can_msg[7] = 0x00;
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ pc.printf("V");
+ }
+ else if ((can_MsgRx.data[0] == 0x03) && (can_MsgRx.data[1] == 0x19) && (can_MsgRx.data[2] == 0x02) && (can_MsgRx.data[3] == 0x08))
+ { // get single fault: { 0x03, 0x19, 0x02, 0x08, 0x00, 0x00, 0x00, 0x00 };
+ // Response with 1 fault 073C: 07 59 02 7B 00 02 11 2A
+ // Response with No fault 073C:03 59 02 7B 05 48 C3 01
+ if ( ecu.active_fault == true )
+ {
+ can_msg[0] = 0x07; can_msg[1] = 0x59; can_msg[2] = 0x02; can_msg[3] = 0x7B;
+ can_msg[4] = 0x00; can_msg[5] = 0x02; can_msg[6] = 0x11; can_msg[7] = 0x2B;
+ }
+ else // stored fault
+ {
+ // these 2 lines reset to NO Faults.
+ can_msg[0] = 0x03; can_msg[1] = 0x59; can_msg[2] = 0x02; can_msg[3] = 0x7B;
+ can_msg[4] = 0x05; can_msg[5] = 0x48; can_msg[6] = 0xC3; can_msg[7] = 0x01;
+ // these 2 lines reset to a non-active fault
+ //can_msg[0] = 0x07; can_msg[1] = 0x59; can_msg[2] = 0x02; can_msg[3] = 0x7B;
+ //can_msg[4] = 0x00; can_msg[5] = 0x02; can_msg[6] = 0x11; can_msg[7] = 0x2A;
+ }
+ can2.write(CANMessage(ESPAR_RES , can_msg, 8));
+ pc.printf("F");
+ }
+ else
+ {
+ pc.printf("\n\r%x %x %x %x %x %x %x %x %x\n\r",can_MsgRx.id,
+ can_MsgRx.data[0], can_MsgRx.data[1], can_MsgRx.data[2], can_MsgRx.data[3],
+ can_MsgRx.data[4], can_MsgRx.data[5], can_MsgRx.data[6], can_MsgRx.data[7]);
+ }
+ } // end of if ESPAR_CMD
+ //"Set" altitude command:
+ //addresses0x0055 and 0x009D: { 0x4c, 0x1d, 0, 0, 0, 0, 0, 0 };
+ //
+ //LSB, MSB altitude x 10 in packet. 1D4C = 7500 = 750hpA, 2710 = 10000 = 1000hPa.
+ else if ((can_MsgRx.id == 0x0055) || (can_MsgRx.id == 0x009D))
+ {
+ alt[0] = can_MsgRx.data[1];
+ alt[1] = can_MsgRx.data[0];
+ got_alt = true;
+ pc.printf("A");
+ }
+ else if (can_MsgRx.id == PID_REQUEST)
+ {
+ pc.printf("-");
+ if(can_MsgRx.data[1] == MODE3) // Request trouble codes
+ {
+ if(ecu.dtc == false)
+ {
+ can_msg[0] = 0x02;
+ can_msg[1] = MODE3_RESPONSE;
+ can_msg[2] = 0x00;
+ }
+ else
+ {
+ can_msg[0] = 0x06;
+ can_msg[1] = MODE3_RESPONSE;
+ can_msg[2] = 0x02;
+ can_msg[3] = 0x01;
+ can_msg[4] = 0x00;
+ can_msg[5] = 0x02;
+ can_msg[6] = 0x00;
+ }
+ can2.write(CANMessage(PID_REPLY, can_msg, 8));
+ }
+
+ if(can_MsgRx.data[1] == MODE4) // Clear trouble codes, clear Check engine light
+ {
+ ecu.dtc = false;
+ led4 = 0;
+
+ can_msg[0] = 0x00;
+ can_msg[1] = MODE4_RESPONSE;
+ can2.write(CANMessage(PID_REPLY, can_msg, 8));
+ }
+
+ if(can_MsgRx.data[1] == MODE1)
+ {
+ can_msg[1] = MODE1_RESPONSE;
+ switch(can_MsgRx.data[2])
+ {
+ /* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */
+ case PID_SUPPORTED:
+ can_msg[0] = 0x06;
+ can_msg[2] = PID_SUPPORTED;
+ can_msg[3] = 0xE8;
+ can_msg[4] = 0x19;
+ can_msg[5] = 0x30;
+ can_msg[6] = 0x12;
+ can_msg[5] = 0x00;
+ can2.write(CANMessage(PID_REPLY, can_msg, 8));
+
+ break;
+
+ case MONITOR_STATUS:
+ can_msg[0] = 0x05;
+ can_msg[2] = MONITOR_STATUS;
+
+ if(ecu.dtc == true) can_msg[3] = 0x82;
+ else can_msg[3] = 0x00;
+
+ can_msg[4] = 0x07;
+ can_msg[5] = 0xFF;
+ can2.write(CANMessage(PID_REPLY, can_msg, 8));
+ break;
+
+ case ENGINE_RPM: // ((A*256)+B)/4 [RPM]
+ can_msg[0] = 0x04;
+ can_msg[2] = ENGINE_RPM;
+ can_msg[3] = (ecu.engine_rpm & 0xff00) >> 8;
+ can_msg[4] = ecu.engine_rpm & 0x00ff;
+ can2.write(CANMessage(PID_REPLY, can_msg, 8));
+ break;
+
+ case ENGINE_COOLANT_TEMP: // A-40 [degree C]
+ can_msg[0] = 0x03;
+ can_msg[2] = ENGINE_COOLANT_TEMP;
+ can_msg[3] = ecu.coolant_temp;
+ can2.write(CANMessage(PID_REPLY, can_msg, 8));
+ break;
+
+ case VEHICLE_SPEED: // A [km]
+ can_msg[0] = 0x03;
+ can_msg[2] = VEHICLE_SPEED;
+ can_msg[3] = ecu.vehicle_speed;
+ can2.write(CANMessage(PID_REPLY, can_msg, 8));
+ break;
+
+ case MAF_SENSOR: // ((256*A)+B) / 100 [g/s]
+ can_msg[0] = 0x04;
+ can_msg[2] = MAF_SENSOR;
+ can_msg[3] = (ecu.maf_airflow & 0xff00) >> 8;
+ can_msg[4] = ecu.maf_airflow & 0x00ff;
+ can2.write(CANMessage(PID_REPLY, can_msg, 8));
+ break;
+
+ case O2_VOLTAGE: // A * 0.005 (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc)
+ can_msg[0] = 0x04;
+ can_msg[2] = O2_VOLTAGE;
+ can_msg[3] = ecu.o2_voltage & 0x00ff;
+ can_msg[4] = (ecu.o2_voltage & 0xff00) >> 8;
+ can2.write(CANMessage(PID_REPLY, can_msg, 8));
+ break;;
+
+ case THROTTLE: //
+ can_msg[0] = 0x03;
+ can_msg[2] = THROTTLE;
+ can_msg[3] = ecu.throttle_position;
+ can2.write(CANMessage(PID_REPLY, can_msg, 8));
+ break;
+ }//switch
+ }
}
- if(can_MsgRx.data[1] == MODE4) // Clear trouble codes, clear Check engine light
- {
- ecu.dtc = false;
- led4 = 0;
-
- can_msg[0] = 0x00;
- can_msg[1] = MODE4_RESPONSE;
- can2.write(CANMessage(PID_REPLY, can_msg, 8));
+ /*****************************************************************
+ pc.printf("\n\r%x %x %x %x %x %x %x %x %x",can_MsgRx.id,can_MsgRx.data[0],
+ can_MsgRx.data[1],
+ can_MsgRx.data[2],
+ can_MsgRx.data[3],
+ can_MsgRx.data[4],
+ can_MsgRx.data[5],
+ can_MsgRx.data[6],
+ can_MsgRx.data[7]);
+ ****************************************************************/
+ led2 = 0;
+ }
+ else
+ {
+ //pc.printf("\n\rNo CAN Message\n\r");
+ int rxerrors = can2.rderror();
+ if ( rxerrors != 0 )
+ {
+ pc.printf("\n\rCAN Rx Errors: %d\n\r", rxerrors);
+ can2.reset();
}
-
- if(can_MsgRx.data[1] == MODE1)
+ /*************************
+ else
{
- can_msg[1] = MODE1_RESPONSE;
- switch(can_MsgRx.data[2])
- { /* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */
- case PID_SUPPORTED:
- can_msg[0] = 0x06;
- can_msg[2] = PID_SUPPORTED;
- can_msg[3] = 0xE8;
- can_msg[4] = 0x19;
- can_msg[5] = 0x30;
- can_msg[6] = 0x12;
- can_msg[5] = 0x00;
- can2.write(CANMessage(PID_REPLY, can_msg, 8));
-
- break;
-
- case MONITOR_STATUS:
- can_msg[0] = 0x05;
- can_msg[2] = MONITOR_STATUS;
-
- if(ecu.dtc == true) can_msg[3] = 0x82;
- else can_msg[3] = 0x00;
-
- can_msg[4] = 0x07;
- can_msg[5] = 0xFF;
- can2.write(CANMessage(PID_REPLY, can_msg, 8));
- break;
-
- case ENGINE_RPM: // ((A*256)+B)/4 [RPM]
- can_msg[0] = 0x04;
- can_msg[2] = ENGINE_RPM;
- can_msg[3] = (ecu.engine_rpm & 0xff00) >> 8;
- can_msg[4] = ecu.engine_rpm & 0x00ff;
- can2.write(CANMessage(PID_REPLY, can_msg, 8));
- break;
-
- case ENGINE_COOLANT_TEMP: // A-40 [degree C]
- can_msg[0] = 0x03;
- can_msg[2] = ENGINE_COOLANT_TEMP;
- can_msg[3] = ecu.coolant_temp;
- can2.write(CANMessage(PID_REPLY, can_msg, 8));
- break;
-
- case VEHICLE_SPEED: // A [km]
- can_msg[0] = 0x03;
- can_msg[2] = VEHICLE_SPEED;
- can_msg[3] = ecu.vehicle_speed;
- can2.write(CANMessage(PID_REPLY, can_msg, 8));
- break;
-
- case MAF_SENSOR: // ((256*A)+B) / 100 [g/s]
- can_msg[0] = 0x04;
- can_msg[2] = MAF_SENSOR;
- can_msg[3] = (ecu.maf_airflow & 0xff00) >> 8;
- can_msg[4] = ecu.maf_airflow & 0x00ff;
- can2.write(CANMessage(PID_REPLY, can_msg, 8));
- break;
-
- case O2_VOLTAGE: // A * 0.005 (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc)
- can_msg[0] = 0x04;
- can_msg[2] = O2_VOLTAGE;
- can_msg[3] = ecu.o2_voltage & 0x00ff;
- can_msg[4] = (ecu.o2_voltage & 0xff00) >> 8;
- can2.write(CANMessage(PID_REPLY, can_msg, 8));
- break;;
-
- case THROTTLE: //
- can_msg[0] = 0x03;
- can_msg[2] = THROTTLE;
- can_msg[3] = ecu.throttle_position;
- can2.write(CANMessage(PID_REPLY, can_msg, 8));
- break;
- }//switch
+ pc.printf(".");
}
-
-
-
- pc.printf("\n\r%x %x %x %x %x %x %x %x %x",can_MsgRx.id,can_MsgRx.data[0],
- can_MsgRx.data[1],
- can_MsgRx.data[2],
- can_MsgRx.data[3],
- can_MsgRx.data[4],
- can_MsgRx.data[5],
- can_MsgRx.data[6],
- can_MsgRx.data[7]);
- led2 = 0;
- }
-
+ **************************/
+ }
+
return 0;
}
-
-} // namespace mbed
\ No newline at end of file
+
+} // namespace mbed
\ No newline at end of file
--- a/ecu_simulator.h Tue Dec 30 10:10:02 2014 +0000
+++ b/ecu_simulator.h Wed Jun 10 20:15:48 2020 +0000
@@ -1,8 +1,8 @@
#ifndef ECU_SIMULATOR_H
#define ECU_SIMULATOR_H
-#define CANSPEED_125 125000 // CAN speed at 125 kbps
-#define CANSPEED_250 250000 // CAN speed at 250 kbps
+//#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 */
@@ -26,8 +26,10 @@
#define PID_REQUEST 0x7DF
#define PID_REPLY 0x7E8
-typedef struct{
-
+#define ESPAR_CMD 0x7A0 // COMMAND ID
+#define ESPAR_RES 0x73C // response id
+
+typedef struct{
unsigned char coolant_temp;
unsigned int engine_rpm;
unsigned char throttle_position;
@@ -35,7 +37,7 @@
unsigned int maf_airflow;
unsigned int o2_voltage;
unsigned char dtc;
-
+ unsigned char active_fault;
}ecu_t;
extern ecu_t ecu;
--- a/globals.cpp Tue Dec 30 10:10:02 2014 +0000 +++ b/globals.cpp Wed Jun 10 20:15:48 2020 +0000 @@ -2,12 +2,21 @@ -DigitalOut led1 (LED1); -DigitalOut led2 (LED2); -DigitalOut led3 (LED3); -DigitalOut led4 (LED4); +DigitalOut led1 (PA_4); // not present on Maple Mini +DigitalOut led2 (PA_5); // not present on Maple Mini +DigitalOut led3 (PA_6); // not present on Maple Mini +DigitalOut led4 (PA_7); // not present on Maple Mini // We use can on mbed pins 29(CAN_TXD) and 30(CAN_RXD). -CAN can2(p30, p29); +//CAN can2(p30, p29); // for LPC1768 +CAN can2(PA_11, PA_12); // for Blue Pill 64K Maple Mini +// OR can on mbed pins 10(CAN_TXD) and 9(CAN_RXD). +//CAN can1(p9, p10); CANMessage can_MsgRx; -Serial pc(USBTX, USBRX); \ No newline at end of file + +int got_alt = false; + +unsigned char alt[3]; + +//Serial pc(USBTX, USBRX); -- for LPC1768 +Serial pc(PA_2, PA_3); // for Maple Mini \ No newline at end of file
--- a/globals.h Tue Dec 30 10:10:02 2014 +0000 +++ b/globals.h Wed Jun 10 20:15:48 2020 +0000 @@ -9,6 +9,12 @@ extern DigitalOut led4; extern CAN can2; +//extern CAN can1; extern CANMessage can_MsgRx; + +extern int got_alt; + +extern unsigned char alt[3]; + extern Serial pc; #endif \ No newline at end of file
--- a/main.cpp Tue Dec 30 10:10:02 2014 +0000
+++ b/main.cpp Wed Jun 10 20:15:48 2020 +0000
@@ -13,56 +13,49 @@
********************************************************************************
*/
-
+//#include "MapleMini.h"
+#include "stm32f103c8t6.h"
#include "mbed.h"
#include "ecu_simulator.h"
#include "globals.h"
-#include "TextLCD.h"
-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);
+// pinout for Maple Mini
+DigitalIn click(PA_1); // Button inputs
+// pinout for lpc1768
+//DigitalIn click(p21); // Joystick inputs
+//DigitalIn right(p22);
+//DigitalIn down(p23);
+//DigitalIn left(p24);
+//DigitalIn up(p25);
ecu_sim sim(CANSPEED_500); //Create object and set CAN speed
ecu_t ecu;
-void update_menu(void);
-void update_param(unsigned char dir);
+Ticker timer;
+
+void attime()
+{
+ //second_tick = true;
+ //myled = !myled;
+ led1 = !led1;
+}
// 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
-#define INC 0
-#define DEC 1
-
-#define CAN250 0
-#define CAN500 1
-unsigned char menu_state;
-unsigned char canspeed;
+//unsigned char menu_state;
+//unsigned char canspeed;
int main()
{
+ confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock)
pc.baud(115200);
-
+
//Enable Pullup
click.mode(PullUp);
- right.mode(PullUp);
- down.mode(PullUp);
- left.mode(PullUp);
- up.mode(PullUp);
- printf("\n\nECU Simulator v1.0 \n");
+
+ pc.printf("\n\nRIXEN Simulator v1.1 \r\n");
led1 = 1;
wait(0.1);
@@ -74,213 +67,40 @@
wait(0.2);
led1 = 0; led2 = 0; led3 = 0; led4 = 0;
- lcd.cls();
- lcd.locate(0,0); // Set LCD cursor position
- lcd.printf("ECU Simulator v1.0");
- lcd.locate(0,1);
- lcd.printf("www.skpang.co.uk");
wait(1);
- lcd.cls();
- lcd.printf("<- Params ->");
- lcd.locate(0,1);
- lcd.printf("500kb/s");
+ //canspeed = CAN500;
+ ecu.dtc = false;
+ ecu.active_fault = false;
- canspeed = CAN500;
- menu_state = L0_MAIN_MENU;
- ecu.dtc = false;
+ sim.canspeed(CANSPEED_500);
wait(0.2);
led1 = 1;
- while(1) { // Main CAN loop
-
+ timer.attach(&attime, 1);
+
+ while(1) // Main CAN loop
+ {
sim.request();
- if(!down){
- update_param(DEC);
- }
-
- if(!up){
- update_param(INC);
+ if(!click)
+ {
+ //update_param(DEC);
+ if ( ecu.active_fault == true )
+ {
+ ecu.active_fault = false;
+ led4 = 0;
+ } else {
+ ecu.active_fault = true;
+ led4 = 1;
+ }
+ wait(0.1); //Delay for auto repeat
}
-
- if(!left){
- if(menu_state != L0_MAIN_MENU) menu_state--;
- update_menu();
- }
-
- if(!right){
- if(menu_state != L0_DTC) menu_state++;
- update_menu();
- }
-
+ //wait(0.1); //Delay cause can bus problems
+ //led1 = 1;
+ //wait(0.1);
+ //led1 = 0;
}
}
-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--;
-
- 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);
-
-}
-
-
-
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-STM32F103C8T6.lib Wed Jun 10 20:15:48 2020 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/hudakz/code/mbed-STM32F103C8T6/#727468adfd1d
--- a/mbed.bld Tue Dec 30 10:10:02 2014 +0000 +++ b/mbed.bld Wed Jun 10 20:15:48 2020 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/9114680c05da \ No newline at end of file +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file