Introduce config.h and provide separate configs for gen1 and gen2 86's

Remove MCP filtering for now, I don't think it's needed anymore.
This commit is contained in:
Timur Iskhodzhanov 2022-06-05 14:46:22 -07:00
parent 6f83d559ed
commit 8d9b5ea9b9
4 changed files with 118 additions and 69 deletions

View File

@ -1,7 +1,7 @@
#include <CAN.h>
#include <RaceChrono.h>
#define DEVICE_NAME "BLE CAN device demo"
#include "config.h"
// Connections:
// MCP | BOARD
@ -22,12 +22,6 @@ const long BAUD_RATE = 500 * 1E3; // 500k.
bool isCanBusReaderActive = false;
long lastCanMessageReceivedMs;
// We use RaceChronoPidMap to keep track of stuff for each PID.
// In this implementation, we're going to ignore "update intervals" requested by
// RaceChrono, and instead send every Nth CAN message we receive, per PID, where
// N is different for different PIDs.
const uint8_t DEFAULT_UPDATE_RATE_DIVIDER = 10;
struct PidExtra {
// Only send one out of |updateRateDivider| packets per PID.
uint8_t updateRateDivider = DEFAULT_UPDATE_RATE_DIVIDER;
@ -127,44 +121,7 @@ public:
if (entry != nullptr) {
PidExtra *pidExtra = pidMap.getExtra(entry);
pidExtra->skippedUpdates = 0;
// Customizations for Subaru BRZ / Toyota 86 / Scion FR-S:
switch (pid) {
// These are sent over the CAN bus 100 times per second, we want 25.
case 0x18:
case 0x140:
case 0x141:
case 0x142:
pidExtra->updateRateDivider = 4;
break;
// This is sent over the CAN bus 50 times per second and includes brake
// system pressure. It's useful to have this at the highest update rate
// possible, as braking can be very short, e.g. at autocross.
case 0xD1:
pidExtra->updateRateDivider = 1;
break;
// These are sent over the CAN bus 50 times per second, we want 25.
case 0xD0:
case 0xD2:
case 0xD3:
case 0xD4:
case 0x144:
case 0x152:
case 0x156:
case 0x280:
pidExtra->updateRateDivider = 2;
break;
// 0x360 is sent over the CAN bus 20 times per second, we want 1.
case 0x360:
pidExtra->updateRateDivider = 20;
break;
default:
pidExtra->updateRateDivider = DEFAULT_UPDATE_RATE_DIVIDER;
}
pidExtra->updateRateDivider = getUpdateRateDivider(pid);
}
dumpMapToSerial();
@ -220,7 +177,7 @@ bool startCanBusReader() {
CAN.setSPIFrequency(SPI_FREQUENCY);
CAN.setPins(CS_PIN, IRQ_PIN);
boolean result = CAN.begin(BAUD_RATE, /* stayInConfigurationMode= */ true);
boolean result = CAN.begin(BAUD_RATE);
if (!result) {
Serial.println("ERROR: Unable to start the CAN bus listener.");
return false;
@ -228,29 +185,6 @@ bool startCanBusReader() {
Serial.println("Success!");
// These values are customized for Subaru BRZ / Toyota 86 / Scion FR-S.
// TODO: generalize this? Figure out a good way to build this from the list of
// requested PIDs? Or perhaps filtering is no longer needed after the recent
// stability improvements?
if (!CAN.setFilterRegisters(
/* mask0= */ 0b11111111111 /* full match only */,
/* filter0= */ 0xD1,
/* filter1= */ 0x140,
/* mask1= */ 0b11111111111 /* full match only */,
/* filter2= */ 0xD0,
/* filter3= */ 0xD4,
/* filter4= */ 0x360,
/* filter5= */ 0x360, // Repeated filters don't matter.
/* allowRollover= */ false)) {
Serial.println("WARNING: Unable to set filter registers.");
Serial.println("Trying to continue without filtering...");
if (!CAN.switchToNormalMode()) {
Serial.println("WARNING: Unable to switch to normal mode!");
return false;
}
}
isCanBusReaderActive = true;
return true;
}

34
config.h Normal file
View File

@ -0,0 +1,34 @@
#if !defined(RACECHRONO_BIY_BLE_DEVICE_CONFIG_H)
#define RACECHRONO_BIY_BLE_DEVICE_CONFIG_H
// Change the value
#define DEVICE_NAME "BLE CAN device demo"
// We use RaceChronoPidMap to keep track of stuff for each CAN ID.
// In this implementation, we're going to ignore "update intervals" requested by
// RaceChrono, and instead send every Nth CAN message we receive, per CAN ID, where
// N is different for different PIDs.
const uint8_t DEFAULT_UPDATE_RATE_DIVIDER = 10;
// You need to pick one of the provided configurations below, or define your own
// configuration. Your configuration should define the following two functions:
// Returns an "update rate divider for a given CAN ID.
// If the value is N, only every Nth message received from the CAN bus will be
// communicated to RaceChrono via BLE.
uint8_t getUpdateRateDivider(uint32_t can_id);
// Here are some configurations you can pick from by uncommenting one of the
// following lines:
//#include "configs/ft86_gen1.h"
//#include "configs/ft86_gen2.h"
// Once you read all the comments and address what they asked of you, please
// uncomment the nest line:
//#define I_READ_THE_COMMENTS
#if !defined(I_READ_THE_COMMENTS)
#error Please open config.h and read all the comments!
#endif // I_READ_THE_COMMENTS
#endif // RACECHRONO_BIY_BLE_DEVICE_CONFIG_H

42
configs/ft86_gen1.h Normal file
View File

@ -0,0 +1,42 @@
// Customizations for 1st generation Subaru BRZ / Toyota 86 / Scion FR-S
// (2013-2020 model years).
#if !defined(RACECHRONO_BIY_BLE_DEVICE_FT86_GEN1_H)
#define RACECHRONO_BIY_BLE_DEVICE_FT86_GEN1_H)
uint8_t getUpdateRateDivider(uint32_t can_id) {
switch (can_id) {
// This is sent over the CAN bus 50 times per second and includes brake
// system pressure. It's useful to have this at the highest update rate
// possible, as braking can be very short, e.g. at autocross.
case 0xD1:
return 1;
// These are sent over the CAN bus 100 times per second, we want 25.
case 0x18:
case 0x140:
case 0x141:
case 0x142:
return 4;
// These are sent over the CAN bus 50 times per second, we want 25.
case 0xD0:
case 0xD2:
case 0xD3:
case 0xD4:
case 0x144:
case 0x152:
case 0x156:
case 0x280:
return 2;
// 0x360 is sent over the CAN bus 20 times per second, we want 1.
case 0x360:
return 20;
default:
return DEFAULT_UPDATE_RATE_DIVIDER;
}
}
#endif // RACECHRONO_BIY_BLE_DEVICE_FT86_GEN1_H

39
configs/ft86_gen2.h Normal file
View File

@ -0,0 +1,39 @@
// Customizations for 2nd generation Subaru BRZ / Toyota GR86
// (2022+ model years).
#if !defined(RACECHRONO_BIY_BLE_DEVICE_FT86_GEN2_H)
#define RACECHRONO_BIY_BLE_DEVICE_FT86_GEN2_H)
uint8_t getUpdateRateDivider(uint32_t can_id) {
// This is sent over the CAN bus 50 times per second and includes brake
// system pressure. It's useful to have this at the highest update rate
// possible, as braking can be very short, e.g. at autocross.
if (can_id == 313) {
return 1;
}
// This is sent over the CAN bus 10 times per second, we want 1.
if (can_id == 837) {
return 10;
}
// These are sent over the CAN bus 100 times per second, we want 25.
if (can_id < 0x100) {
return 4;
}
// These are sent over the CAN bus 50 times per second, we want 25.
if (can_id < 0x200) {
return 2;
}
// OBD responses should be rare, don't limit them at all if we're listening to
// them.
if (can_id > 0x700) {
return 1;
}
return DEFAULT_UPDATE_RATE_DIVIDER;
}
#endif // RACECHRONO_BIY_BLE_DEVICE_FT86_GEN1_H