Count the number of CAN bus failures, report them in 0x777

This commit is contained in:
Timur Iskhodzhanov 2020-09-11 00:52:13 -07:00
parent a136fc14a2
commit 350425c6a8
1 changed files with 24 additions and 3 deletions

View File

@ -37,11 +37,15 @@ RaceChronoPidMap<PidExtra> pidMap;
uint32_t loop_iteration = 0; uint32_t loop_iteration = 0;
uint32_t last_time_num_can_bus_timeouts_sent_ms = 0;
uint16_t num_can_bus_timeouts = 0;
// Forward declarations to help put code in a natural reading order. // Forward declarations to help put code in a natural reading order.
void waitForConnection(); void waitForConnection();
void bufferNewPacket(uint32_t pid, uint8_t *data, uint8_t data_length); void bufferNewPacket(uint32_t pid, uint8_t *data, uint8_t data_length);
void handleOneBufferedPacket(); void handleOneBufferedPacket();
void flushBufferedPackets(); void flushBufferedPackets();
void sendNumCanBusTimeouts();
void resetSkippedUpdatesCounters(); void resetSkippedUpdatesCounters();
void dumpMapToSerial() { void dumpMapToSerial() {
@ -268,6 +272,7 @@ void loop() {
Serial.println("Waiting for a new connection."); Serial.println("Waiting for a new connection.");
waitForConnection(); waitForConnection();
sendNumCanBusTimeouts();
} }
while (!isCanBusReaderActive) { while (!isCanBusReaderActive) {
@ -283,10 +288,12 @@ void loop() {
uint32_t timeNowMs = millis(); uint32_t timeNowMs = millis();
// I've observed that MCP2515 has a tendency to just stop responding for some // I've observed that MCP2515 has a tendency to just stop responding for some
// weird reason. Just kill it if we don't have any data for 50 ms. The timeout // weird reason. Just kill it if we don't have any data for 100 ms. The
// might need to be tweaked for some cars. // timeout might need to be tweaked for some cars.
if (timeNowMs - lastCanMessageReceivedMs > 50) { if (timeNowMs - lastCanMessageReceivedMs > 100) {
Serial.println("ERROR: CAN bus timeout, aborting."); Serial.println("ERROR: CAN bus timeout, aborting.");
num_can_bus_timeouts++;
sendNumCanBusTimeouts();
stopCanBusReader(); stopCanBusReader();
return; return;
} }
@ -330,6 +337,10 @@ void loop() {
} }
handleOneBufferedPacket(); handleOneBufferedPacket();
if (millis() - last_time_num_can_bus_timeouts_sent_ms > 2000) {
sendNumCanBusTimeouts();
}
} }
struct BufferedMessage { struct BufferedMessage {
@ -396,6 +407,16 @@ void flushBufferedPackets() {
bufferToReadFrom = 0; bufferToReadFrom = 0;
} }
void sendNumCanBusTimeouts() {
// Send the count of timeouts to RaceChrono in a "fake" PID=0x777 message.
uint8_t data[2];
data[0] = num_can_bus_timeouts & 0xff;
data[1] = num_can_bus_timeouts >> 8;
RaceChronoBle.sendCanData(0x777, data, 2);
last_time_num_can_bus_timeouts_sent_ms = millis();
}
void resetSkippedUpdatesCounters() { void resetSkippedUpdatesCounters() {
struct { struct {
void operator() (void *entry) { void operator() (void *entry) {