Decode the real CAN map: gear, temps, battery, driveline speeds

Ingest HeinrichG-V12/E65_ReverseEngineering (per-ID DBC-style docs with
worked examples) and re-analyse our own 2026-07-15 capture against it.
Resolves both outstanding scaling disputes, closes the engine-state
question, and adds the driveline speeds.

Resolved:
- Gearbox oil temp = raw - 40 (not offset 0, not +40). Source's worked
  example plus our own cross-check: the box read 31 C while coolant was
  at 37 C and climbing; offset 0 would have the transmission warming
  faster than the engine.
- Battery = 12-bit LE * 0.015 (0x3B4 U_BT); the E90 24-bit BE reading is
  wrong for us. The high nibble of byte 1 is ST_BTSW, not voltage.
- Engine was running during the 2026-07-15 capture (ST_ENGINE_RUN = 2 in
  397/397 frames), so its coolant trace is genuine warm-up data and the
  planned cold-start capture is unnecessary.
- Engaged gear = 0x1D2 byte 1, gear = (D1>>4)-4. No drive capture needed.

New signals (0x1A2 turbine, 0x0BA output shaft, 0x0AA RPM/pedal). Turbine
speed physically proves engagement: it free-spins at ~engine speed in P/N
and collapses to ~0 when the box loads up. Output shaft is the only
road-speed source on this bus - there is no DSC module - so it is the
feedback variable any future cruise-control work would need. Scaling is
uncalibrated; it is carried as raw counts with no physical unit.

Corrects three of my own earlier claims:
- 0x1D2 D1 is the engaged gear, not a "drive-gate flag" (it only ever
  showed two values because the car was stationary in 1st).
- 0x1D2 D4 0xF2 is a persistent Manual mode, not a "downshift event" - it
  holds across gears 2-6.
- 0x0BA was dismissed on the strength of D7, which is an alive counter.
  The gear is in D0's low nibble, and it is the only source of engaged
  reverse: 0x1D2 D1 stays 0x0F in R.

Firmware: real IDs replace the placeholder map (all 11-bit standard now,
so the extended-ID assumption is gone), full gear decode incl. Sport vs
Manual, and a debounced lever-vs-gearbox mismatch fault - the "lever says
R but the box never took it" case. Engine oil temp reads 0xFF here (sensor
not populated), so its valid flag is left clear rather than publishing
-207 C. Driveline speeds are kept out of automotive_data_t deliberately:
that struct is the UART wire format and widening it would break
Controller B.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-17 12:33:31 +02:00
parent 0a58afca9a
commit bb87b3d6da
13 changed files with 428331 additions and 132 deletions
+157 -39
View File
@@ -24,6 +24,12 @@ namespace CANDriver {
// CAN timeout threshold - stop UART if no messages received for this duration
#define CAN_TIMEOUT_MS 2000 // 2 seconds (for 5Hz expected rate, this is 10 missed messages)
// How long the lever (0x1D2) and the gearbox (0x0BA) must disagree before we call
// it a fault. They legitimately differ for a moment during every shift — 0x1D2 is
// a 200ms message and 0x0BA a 20ms one, so they are never perfectly in step.
// Without this window the flag would strobe on every gear change.
#define GEAR_MISMATCH_DEBOUNCE_MS 500
// ============================================================================
// MODULE STATE
// ============================================================================
@@ -41,10 +47,60 @@ static bool g_raw_logging = true;
// (and would drown) the raw capture stream. Enable only for quick eyeballing.
static bool g_verbose_decode = false;
// Driveline speeds — decoded but NOT sent over UART (see driveline_t in the header).
// Guarded by g_data_mutex along with g_automotive_data.
static driveline_t g_driveline = {0};
// Last gear LEVER code seen on 0x1D2, for the lever-vs-gearbox cross-check.
static gear_position_t g_last_lever = GEAR_UNKNOWN;
// When the current disagreement started (0 = currently in agreement).
static uint32_t g_mismatch_since = 0;
// ============================================================================
// PRIVATE FUNCTIONS
// ============================================================================
/**
* @brief Cross-check the driver's lever selection against the gearbox's own
* target gear and raise a fault if they disagree for long enough.
*
* This is the "lever says R but the car isn't moving" detector: if the box never
* took the request, 0x0BA reports a different gear than the lever shows.
*
* Note this compares INTENT, not mechanical engagement — 0x0BA is a target gear.
* Proving the box physically caught requires the turbine/engine speed ratio, which
* needs output-shaft calibration to gate correctly (it reads "not engaged" whenever
* the wheels are free, e.g. on a lift). Not attempted here on purpose.
* See wiki/pages/driveline-speed-signals.md
*
* Caller must hold g_data_mutex.
* @param target Gear the gearbox reports as its target
*/
static void updateGearMismatch(gear_position_t target) {
// Ignore frames we can't interpret rather than guessing a mismatch from them.
if (target == GEAR_UNKNOWN || g_last_lever == GEAR_UNKNOWN) return;
// The lever's D detent covers D/S/M; can_to_target_gear() maps every forward
// gear to GEAR_DRIVE, so both sides are directly comparable.
bool disagrees = (target != g_last_lever);
if (!disagrees) {
g_mismatch_since = 0;
g_driveline.gear_mismatch = false;
g_automotive_data.fault_codes &= ~FAULT_GEAR_MISMATCH;
return;
}
uint32_t now = millis();
if (g_mismatch_since == 0) {
g_mismatch_since = now; // start the clock, don't fault yet
} else if ((now - g_mismatch_since) >= GEAR_MISMATCH_DEBOUNCE_MS) {
g_driveline.gear_mismatch = true;
g_automotive_data.fault_codes |= FAULT_GEAR_MISMATCH;
g_automotive_data.valid_flags |= VALID_FLAG_FAULT_CODES;
}
}
/**
* @brief Parse BMW-specific CAN message and update automotive data
* @param message Received TWAI message
@@ -61,59 +117,101 @@ static bool parseCANMessage(const twai_message_t& message) {
return false;
}
uint8_t dlc = message.data_length_code;
switch (id) {
case CAN_ID_COOLANT_TEMP:
// Engine coolant temperature (byte 0)
if (message.data_length_code > CAN_BYTE_COOLANT_TEMP) {
g_automotive_data.coolant_temp = can_to_temperature(data[CAN_BYTE_COOLANT_TEMP]);
case CAN_ID_ENGINE_DATA:
// 0x1D0: coolant (byte 0), engine oil (byte 1), engine run state (byte 2).
if (dlc > CAN_BYTE_COOLANT_TEMP) {
g_automotive_data.coolant_temp =
can_to_temperature(data[CAN_BYTE_COOLANT_TEMP], ENGINE_TEMP_OFFSET);
g_automotive_data.valid_flags |= VALID_FLAG_COOLANT_TEMP;
parsed = true;
}
break;
case CAN_ID_ENGINE_OIL_TEMP:
// Engine oil temperature (byte 7)
if (message.data_length_code > CAN_BYTE_ENGINE_OIL_TEMP) {
g_automotive_data.engine_oil_temp = can_to_temperature(data[CAN_BYTE_ENGINE_OIL_TEMP]);
// Oil temp reads 0xFF on this M57N — the sensor isn't populated. Leave
// the valid flag clear rather than publishing -207 C as if it were real.
if (dlc > CAN_BYTE_ENGINE_OIL_TEMP && data[CAN_BYTE_ENGINE_OIL_TEMP] != 0xFF) {
g_automotive_data.engine_oil_temp =
can_to_temperature(data[CAN_BYTE_ENGINE_OIL_TEMP], ENGINE_TEMP_OFFSET);
g_automotive_data.valid_flags |= VALID_FLAG_ENGINE_TEMP;
parsed = true;
}
if (dlc > CAN_BYTE_ENGINE_STATUS) {
g_driveline.engine_run_state = (data[CAN_BYTE_ENGINE_STATUS] >> 4) & 0x03;
}
break;
case CAN_ID_GEARBOX_TEMP:
// Gearbox oil temperature (byte 3)
if (message.data_length_code > CAN_BYTE_GEARBOX_TEMP) {
g_automotive_data.gearbox_oil_temp = can_to_temperature(data[CAN_BYTE_GEARBOX_TEMP]);
case CAN_ID_TORQUE_EGS:
// 0x0B5: gearbox oil temp (byte 7) + the box's own cooling demand.
if (dlc > CAN_BYTE_GEARBOX_TEMP) {
g_automotive_data.gearbox_oil_temp =
can_to_temperature(data[CAN_BYTE_GEARBOX_TEMP], GEARBOX_TEMP_OFFSET);
g_automotive_data.valid_flags |= VALID_FLAG_GEARBOX_TEMP;
parsed = true;
}
if (dlc > CAN_BYTE_GEARBOX_COOL) {
// ST_OTMP_GRB = bits 38-39 = byte 4, bits 6-7
g_driveline.gearbox_cooling = (data[CAN_BYTE_GEARBOX_COOL] >> 6) & 0x03;
}
break;
case CAN_ID_GEAR_LEVER:
// Gear lever position from the EGS (0x1D2) — verified on the swap bus
// 2026-07-16. Byte 0 is a whole-byte lever code (E1=P, D2=R, B4=N, 78=D/S-M).
// Byte 4 (CAN_BYTE_GEAR_MODE) splits D vs S/M and carries manual +/- shifts;
// not surfaced yet (no enum slot; the engaged gear number needs a drive capture).
if (message.data_length_code > CAN_BYTE_GEAR_LEVER) {
gear_position_t g = can_to_gear_lever(data[CAN_BYTE_GEAR_LEVER]);
if (g != GEAR_UNKNOWN) {
g_automotive_data.gear_position = (uint8_t)g;
g_automotive_data.valid_flags |= VALID_FLAG_GEAR_POS;
parsed = true;
}
case CAN_ID_GEAR_DISPLAY: {
// 0x1D2: the EGS publishing its whole state — byte 0 is what the driver
// selected, byte 1 is what's actually engaged, byte 4 is the mode.
gear_position_t g = decode_gear(data, dlc);
if (g != GEAR_UNKNOWN) {
g_automotive_data.gear_position = (uint8_t)g;
g_automotive_data.valid_flags |= VALID_FLAG_GEAR_POS;
parsed = true;
}
// Keep the bare lever (not the resolved display value) for the
// cross-check below: 0x0BA reports a gear, never a mode.
if (dlc > CAN_BYTE_GEAR_LEVER) {
g_last_lever = can_to_gear_lever(data[CAN_BYTE_GEAR_LEVER]);
}
break;
}
case CAN_ID_GEARBOX_STATE: {
// 0x0BA: the gearbox's own target gear — the ONLY source of engaged
// reverse (0x1D2 byte 1 stays 0x0F in R) — plus the output shaft.
if (dlc > CAN_BYTE_GEARBOX_TARGET) {
gear_position_t target =
can_to_target_gear(data[CAN_BYTE_GEARBOX_TARGET] & 0x0F);
updateGearMismatch(target);
parsed = true;
}
if (dlc > CAN_BYTE_OUTPUT_SHAFT_H) {
// Raw counts only — scaling uncalibrated, so no physical unit.
g_driveline.output_shaft_raw =
(uint16_t)((data[CAN_BYTE_OUTPUT_SHAFT_H] << 8) | data[CAN_BYTE_OUTPUT_SHAFT_L]);
}
break;
}
case CAN_ID_TURBINE_SPEED:
// 0x1A2: gearbox input speed — the physical engagement signal.
if (dlc > CAN_BYTE_TURBINE_H) {
g_driveline.turbine_rpm = can_to_turbine_rpm(data);
parsed = true;
}
break;
case CAN_ID_TORQUE_3:
// 0x0AA: engine RPM + accelerator pedal.
if (dlc > CAN_BYTE_RPM_H) {
g_driveline.engine_rpm = can_to_rpm(data);
parsed = true;
}
if (dlc > CAN_BYTE_PEDAL_POS) {
// ANG_ACPD: % = raw * 0.39063 (i.e. raw * 100 / 256)
g_driveline.pedal_percent = (uint8_t)((data[CAN_BYTE_PEDAL_POS] * 100) / 256);
}
break;
case CAN_ID_BATTERY_VOLTAGE:
// Battery voltage (bytes 0-1, 16-bit value)
if (message.data_length_code > CAN_BYTE_BATTERY_VOLT_L) {
// Combine high and low bytes (assuming big-endian)
uint16_t raw_voltage = (data[CAN_BYTE_BATTERY_VOLT_H] << 8) | data[CAN_BYTE_BATTERY_VOLT_L];
// Convert to internal format (0.01V units)
// Assuming CAN sends in 0.1V units, multiply by 10
g_automotive_data.battery_voltage = raw_voltage * 10;
// 0x3B4: U_BT, 12-bit little-endian x 0.015 V.
if (dlc > CAN_BYTE_BATTERY_VOLT_H) {
g_automotive_data.battery_voltage = can_to_voltage(data);
g_automotive_data.valid_flags |= VALID_FLAG_BATTERY_VOLT;
parsed = true;
}
@@ -193,13 +291,16 @@ static void handleRxMessage(twai_message_t& message) {
// Per-frame decoded prose — off by default (redundant with raw capture)
if (g_verbose_decode && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(10)) == pdTRUE) {
ESP_LOGI("CANDrv", "✓ RX CAN ID=0x%X → Coolant:%d.%d°C, Oil:%d.%d°C, Gearbox:%d.%d°C, Batt:%d.%02dV, Gear:%d [%d/%d]",
ESP_LOGI("CANDrv", "✓ RX 0x%X → Gear:%s%s Coolant:%d.%d°C Gearbox:%d.%d°C Batt:%d.%02dV | RPM:%u Turb:%u Shaft:%u [%d/%d]",
message.identifier,
gear_to_string((gear_position_t)g_automotive_data.gear_position),
g_driveline.gear_mismatch ? " !MISMATCH" : "",
g_automotive_data.coolant_temp / 10, abs(g_automotive_data.coolant_temp % 10),
g_automotive_data.engine_oil_temp / 10, abs(g_automotive_data.engine_oil_temp % 10),
g_automotive_data.gearbox_oil_temp / 10, abs(g_automotive_data.gearbox_oil_temp % 10),
g_automotive_data.battery_voltage / 100, g_automotive_data.battery_voltage % 100,
g_automotive_data.gear_position,
g_driveline.engine_rpm,
g_driveline.turbine_rpm,
g_driveline.output_shaft_raw,
g_can_stats.messages_parsed,
g_can_stats.messages_received);
xSemaphoreGive(g_data_mutex);
@@ -295,6 +396,10 @@ static void canTask(void* parameter) {
if (initTWAI()) {
g_can_active = true;
memset(&g_can_stats, 0, sizeof(g_can_stats));
// Drop stale driveline/gear state from a previous session
memset(&g_driveline, 0, sizeof(g_driveline));
g_last_lever = GEAR_UNKNOWN;
g_mismatch_since = 0;
ESP_LOGI("CANTask", "CAN reception active");
// Auto-start UART transmission (CAN drives UART)
@@ -437,6 +542,19 @@ can_stats_t getStats() {
return g_can_stats;
}
driveline_t getDriveline() {
driveline_t snapshot;
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(10)) == pdTRUE) {
snapshot = g_driveline;
xSemaphoreGive(g_data_mutex);
} else {
// Never hand back a torn read; an all-zero snapshot is honest about
// having no fresh data (engine_run_state 0 == ENGINE_RUN_OFF).
memset(&snapshot, 0, sizeof(snapshot));
}
return snapshot;
}
bool isActive() {
return g_can_active;
}