I'm testing my sketch in 3 different cars :
- Seat Toledo 2015
- Mazda 3 2015
- Hyundai elantra 2017
And I have consistent data in the Seat toledo (meaning that the data I ask for, the data I got from the car - RPM, Speed, Module Voltage-), but when I test the same sketch on the Mazda and Toledo, most of the time the cars doesn't give me the data I asked (few times the car gives me the module voltage, or the speed, and ALWAYS the RPMS).
I changed the timing between the send and receive function, and the behaviour is almost the same in the Mazda and Hyundai.
My sketch is :
SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
#include <carloop.h>
Carloop<CarloopRevision2> carloop;
void sendObdRequest();
void waitForObdResponse();
int maxUnreadMessage = 5;
int currentPid;
int currentPidIndex = 0;
const auto OBD_REQUEST_ID = 0x7E0;
const auto OBD_REPLY_ID = 0x7E8;
const auto OBD_PID_SERVICE = 0x01;
const auto PID_ENGINE_RPM = 0x0C ;
const auto PID_VEHICLE_SPEED = 0x0D ;
const auto PID_CONTROL_MODULE_VOLTAGE = 0x42;
const byte PIDS[] = {
PID_ENGINE_RPM,
PID_VEHICLE_SPEED,
PID_DISTANCE_TRAVELED_SINCE_CODES_CLEARED,
PID_CONTROL_MODULE_VOLTAGE
};
float ENGINE_RPM = 0;
int VEHICLE_SPEED = 0;
float CONTROL_MODULE_VOLTAGE = 0;
void setup() {
Serial.begin(9600);
carloop.begin();
maxUnreadMessage = sizeof(PIDS);
parseConfig();
}
void loop() {
carloop.update();
sendObdRequest();
delay(100);
waitForObdResponse();
}
void sendObdRequest() {
if(currentPidIndex == sizeof(PIDS)) {
currentPidIndex = 0;
}
currentPid = PIDS[currentPidIndex];
currentPidIndex++;
CANMessage message;
message.id = OBD_REQUEST_ID;
message.len = 8;
message.data[0] = 0x02;
message.data[1] = OBD_PID_SERVICE;
message.data[2] = currentPid;//This is the data we want to ask to the car
carloop.can().transmit(message);
}
void waitForObdResponse() {
bool responseReceived = false;
CANMessage message;
while (carloop.can().receive(message)) {
if(message.id == OBD_REPLY_ID && message.data[2] == currentPid) {
parseMessage(message.data);
break;
}
}
}
void parseMessage(byte data[]) {
if (data[2] == 12) {
ENGINE_RPM = ((data[3] * 256) + data[4]) / 4;
}
if (data[2] == 13) {
VEHICLE_SPEED =(int) data[3];
}
if (data[2] == 66) {
CONTROL_MODULE_VOLTAGE = (256 * data[3] + data[4])/1000;
}
}
Any ideas ? I'm changed the delay(100) to delay(1000) (and some values in between) and I got better results, but not consistent results yet.
Is there any issue I have to consider in terms of timing between send/receive responses?
Thanks