trying to get GPS speed into CAN-BUS to create a gauge on my ECU
basically, i am trying to use a GPS module attached to Serial1 which pushes CAN data over the Seeed CANBUS and is picked up by a generic sensor on the ECU.
i am able to send random values and the gauge updates, but i cannot figure out how to obtain GPS MPH speed and throw it into the CAN
this is mostly copy&paste from other projects, it does kinda what i need it to do.
ive never done C++ in my life and i'm learning as i go. so don't lose your mind over the "cleanliness" of my code, but any critique is welcomed.
//GPS
#include <TinyGPSPlus.h>
// CAN receiving
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg;
MCP2515 mcp2515(9); // SPI CS Pin 10
// The TinyGPSPlus object
TinyGPSPlus gps;
//int cantxValue = 0;
// megasquirt variables
int MAP, RPM, TPS, CLT, AFR, BATT, BARO, MAT, EGO, EGOC, FUELP, OILP, OILT, KNOCK, ETH, VVT;
// can variables
unsigned long previousMillis = 0;
int delayPeriod = 1000;
void setup() {
// Serial INIT
Serial.begin(115200);
Serial1.begin(115200);
Serial2.begin(115200);
//CAN INIT
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS);
mcp2515.setNormalMode();
//Create CAN Message
uint8_t data[8];
float speed = gps.speed.mph();
uint32_t gpsSpeed = *(uint32_t*)(&speed);
//CAN Messages for Sensor 4,5 and 6
canMsg.can_id = 0x01;
canMsg.can_dlc = 8;
canMsg.data[0] = (gps.speed.mph());
//canMsg.data[1] = &sats;
}
void loop() {
//
mcp2515.sendMessage(&canMsg); //Send Data to CAN Bus
while (Serial2.available() > 0) // Read GPS ninfo
gps.encode(Serial2.read());
if (gps.speed.isUpdated())
{
Serial.print(F(" MPH="));
Serial.println(gps.speed.mph());
}
{
switch (canMsg.can_id) {
case 1520: // group 0 - RPM
RPM = (float)(word(canMsg.data[6], canMsg.data[7]));
break;
case (1520 + 2): // BARO, MAT, CLT,
BARO = (float)(word(canMsg.data[0], canMsg.data[1]));
BARO = (BARO / 10);
MAP = (float)(word(canMsg.data[2], canMsg.data[3]));
MAP = (MAP / 10 );
MAT = (float)(word(canMsg.data[4], canMsg.data[5]));
MAT = (MAT / 10);
CLT = (float)(word(canMsg.data[6], canMsg.data[7]));
CLT = (CLT / 10);
break;
case (1520 + 3): // TPS, BATT, EGO
TPS = (float)(word(canMsg.data[0], canMsg.data[1]));
TPS = (TPS / 10);
BATT = (float)(word(canMsg.data[2], canMsg.data[3]));
BATT = (BATT / 10);
EGO = (float)(word(canMsg.data[4], canMsg.data[5]));
EGO = (EGO / 10);
break;
case (1520 + 4): // EGO Correction / Knock Sensor
EGOC = (float)(word(canMsg.data[2], canMsg.data[3]));
EGOC = (EGOC / 10);
KNOCK = (float)(word(canMsg.data[5], canMsg.data[6]));
KNOCK = (KNOCK / 10);
break;
case (1520 + 13): // Fuel Pressure, Oil Pressure, Oil Temperature - Generic Sensors 1,2,3
FUELP = (float)(word(canMsg.data[0], canMsg.data[1]));
FUELP = (FUELP / 10);
OILP = (float)(word(canMsg.data[2], canMsg.data[3]));
OILP = (OILP / 10);
OILT = (float)(word(canMsg.data[4], canMsg.data[5]));
OILT = (OILT / 10);
case (1520 + 44): // VVT Angle 1
VVT = (float)(word(canMsg.data[0], canMsg.data[1]));
VVT = (VVT / 10);
break;
case (1520 + 47): // Ethanol Content %
ETH = (float)(word(canMsg.data[0], canMsg.data[1]));
ETH = (ETH / 10);
}
previousMillis = 0; // reset no data timer
}
{ // no CAN bus data coming in
unsigned long currentMillis = millis();
if (previousMillis == 0)
{
previousMillis = currentMillis; // entered no data timer
} else if (currentMillis - previousMillis > delayPeriod)
{ // no data timer expired
previousMillis = currentMillis;
MAP = -999; //Fake data to test gauge even if data is being received
RPM = -999; //Fake data to test gauge even if data is being received
TPS = -999; //Fake data to test gauge even if data is being received
CLT = -999; //Fake data to test gauge even if data is being received
AFR = -999; //Fake data to test gauge even if data is being received
BATT = -999; //Fake data to test gauge even if data is being received
BARO = -999; //Fake data to test gauge even if data is being received
MAT = -999; //Fake data to test gauge even if data is being received
EGO = -999; //Fake data to test gauge even if data is being received
EGOC = -999; //Fake data to test gauge even if data is being received
FUELP = -999; //Fake data to test gauge even if data is being received
OILP = -999; //Fake data to test gauge even if data is being received
OILT = -999; //Fake data to test gauge even if data is being received
KNOCK = -999; //Fake data to test gauge even if data is being received
ETH = -999; //Fake data to test gauge even if data is being received
VVT = -999; //Fake data to test gauge even if data is being received
}
}
//Serial1.print(F("page: main."));
Serial1.print(F("rpm.txt=\""));
Serial1.print(RPM);
Serial1.print(F("\""));
Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);
Serial1.print(F("mat.txt=\""));
Serial1.print(MAT);
Serial1.print(F("\""));
Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);
Serial1.print(F("map.txt=\""));
Serial1.print(MAP);
Serial1.print(F("\""));
Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);
Serial1.print(F("tps.val="));
Serial1.print(TPS);
Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);
Serial1.print(F("clt.txt=\""));
Serial1.print(CLT);
Serial1.print(F("\""));
Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);
Serial1.print(F("afr.txt=\""));
Serial1.print(EGO);
Serial1.print(F("\""));
Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);
Serial1.print(F("fuelp.txt=\""));
Serial1.print(FUELP);
Serial1.print(F("\""));
Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);
Serial1.print(F("oilp.txt=\""));
Serial1.print(OILP);
Serial1.print(F("\""));
Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);
Serial1.print(F("oilt.txt=\""));
Serial1.print(OILT);
Serial1.print(F("\""));
Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);
Serial1.print(F("batt.txt=\""));
Serial1.print(BATT);
Serial1.print(F("\""));
Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);
Serial1.print(F("knock.txt=\""));
Serial1.print(KNOCK);
Serial1.print(F("\""));
Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);
Serial1.print(F("eth.txt=\""));
Serial1.print(ETH);
Serial1.print(F("\""));
Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);
Serial1.print(F("vvt.txt=\""));
Serial1.print(VVT);
Serial1.print(F("\""));
Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);
Serial1.print(F("mph.txt=\""));
Serial1.println(gps.speed.mph());
Serial1.print(F("\""));
Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);
}