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
+188 -32
View File
@@ -86,39 +86,114 @@ typedef struct __attribute__((packed)) {
#define VALID_FLAG_BATTERY_VOLT (1 << 4)
#define VALID_FLAG_FAULT_CODES (1 << 5)
// Fault code bitmap (fault_codes field). NOTE: these are OUR derived faults, not
// DDE DTCs — real fault codes are not broadcast on this bus and would require
// diagnostic request/response (see wiki/pages/can-id-candidates.md).
#define FAULT_GEAR_MISMATCH (1 << 0) // lever (0x1D2) and gearbox (0x0BA) disagree
// ============================================================================
// CAN BUS ID MAPPINGS (BMW-Specific)
// ============================================================================
// BMW CAN message IDs and byte positions
#define CAN_ID_COOLANT_TEMP 0x4F // Engine coolant temperature
#define CAN_BYTE_COOLANT_TEMP 0 // Byte 0
// BMW E60/E65 PT-CAN, 500 kbps. Every ID below is an 11-bit STANDARD frame and
// has been seen live on the actual swap bus. Decodes cross-checked against
// HeinrichG-V12/E65_ReverseEngineering (per-ID DBC docs) AND our own captures.
// Full evidence + confidence labels: wiki/pages/can-id-candidates.md
//
// The original map (0x4F/0x55/0x201/0x1001/0x101F) was placeholder junk — those
// IDs were verified ABSENT from the bus 2026-07-15. Do not resurrect them.
#define CAN_ID_ENGINE_OIL_TEMP 0x55 // Engine oil temperature
#define CAN_BYTE_ENGINE_OIL_TEMP 7 // Byte 7
// --- 0x1D0 "Engine Data" (DME/DDE, DLC 8, 100 ms) --------------------------
#define CAN_ID_ENGINE_DATA 0x1D0
#define CAN_BYTE_COOLANT_TEMP 0 // TEMP_ENG: °C = raw - 48 (verified)
#define CAN_BYTE_ENGINE_OIL_TEMP 1 // TEMP_EOI: °C = raw - 48 (reads 0xFF here — sensor not populated on this M57N)
#define CAN_BYTE_ENGINE_STATUS 2 // bits 4-5 = ST_ENGINE_RUN
#define ENGINE_TEMP_OFFSET 48 // subtract from raw
#define CAN_ID_GEARBOX_TEMP 0x201 // Gearbox oil temperature
#define CAN_BYTE_GEARBOX_TEMP 3 // Byte 3
// ST_ENGINE_RUN (byte 2, bits 4-5)
#define ENGINE_RUN_OFF 0
#define ENGINE_RUN_STARTING 1
#define ENGINE_RUN_RUNNING 2
#define ENGINE_RUN_INVALID 3
// Gear lever position — EGS "TransmissionDataDisplay", DLC 6.
// VERIFIED on the swap bus 2026-07-16 (see wiki/sources/gear-box-can-revealings.md).
#define CAN_ID_GEAR_LEVER 0x1D2 // Gear lever position message
#define CAN_BYTE_GEAR_LEVER 0 // Byte 0: lever code (see can_to_gear_lever)
#define CAN_BYTE_GEAR_MODE 4 // Byte 4: 0xF0=Drive(auto), 0xF1=Sport/Manual, 0xF2=downshift(-)
// --- 0x0B5 "Torque Request EGS" (EGS, DLC 8, 10 ms) ------------------------
#define CAN_ID_TORQUE_EGS 0x0B5
#define CAN_BYTE_GEARBOX_TEMP 7 // TEMP_GRB: °C = raw - 40 (verified two ways)
#define GEARBOX_TEMP_OFFSET 40 // NOT 0, NOT +40 — see wiki; a 40 °C error either way
#define CAN_BYTE_GEARBOX_COOL 4 // bits 38-39 = ST_OTMP_GRB (cooling demand)
// Raw byte-0 lever codes (whole-byte encoding, all verified against the live bus)
// ST_OTMP_GRB — the box's own overheat opinion (better than thresholding temp)
#define GEARBOX_COOL_NONE 0
#define GEARBOX_COOL_STAGE1 1
#define GEARBOX_COOL_STAGE2 2
#define GEARBOX_COOL_INVALID 3
// --- 0x1D2 "Display Gearbox Data" (EGS, DLC 6, 200 ms) ---------------------
// Our decode is AHEAD of the published source here: the E65 repo lists this ID
// with an empty signal table. Byte 1 and byte 4 are our own work.
#define CAN_ID_GEAR_DISPLAY 0x1D2
#define CAN_BYTE_GEAR_LEVER 0 // whole-byte lever code
#define CAN_BYTE_GEAR_ENGAGED 1 // (raw >> 4) - 4 = gear 1..6; 0x0F = none
#define CAN_BYTE_GEAR_MODE 4 // 0xF0 auto / 0xF1 Sport / 0xF2 Manual
// Byte 0 lever codes (verified — every code matches the documented table)
#define GEAR_CODE_PARK 0xE1
#define GEAR_CODE_REVERSE 0xD2
#define GEAR_CODE_NEUTRAL 0xB4
#define GEAR_CODE_DRIVE 0x78 // also S/M side-gate — split via CAN_BYTE_GEAR_MODE
#define GEAR_MODE_DRIVE_AUTO 0xF0 // byte 4 value when in plain Drive (else Sport/Manual)
#define GEAR_CODE_DRIVE 0x78 // D *and* S/M — the side-gate is physically the same detent
#define CAN_ID_BATTERY_VOLTAGE 0x101F // Battery voltage (16-bit)
#define CAN_BYTE_BATTERY_VOLT_H 0 // Byte 0 (high byte)
#define CAN_BYTE_BATTERY_VOLT_L 1 // Byte 1 (low byte)
// Byte 4 mode codes. NOTE: 0xF2 is a PERSISTENT mode, not a downshift event —
// it holds across gears 2..6. The +/- nudges have no distinct code; the first
// tap simply promotes Sport -> Manual.
#define GEAR_MODE_DRIVE_AUTO 0xF0
#define GEAR_MODE_SPORT 0xF1
#define GEAR_MODE_MANUAL 0xF2
// CAN ID type: Mix of standard (0x4F, 0x55, 0x201) and extended (0x1001, 0x101F)
#define USE_EXTENDED_CAN_IDS true // Using 29-bit extended IDs for some messages
#define GEAR_ENGAGED_NONE 0x0F // byte 1 value in P/R/N — and in R even when engaged
// --- 0x0BA gearbox state (EGS, DLC 8, 20 ms) -------------------------------
// The ONLY source of engaged reverse: 0x1D2 byte 1 stays 0x0F in R.
#define CAN_ID_GEARBOX_STATE 0x0BA
#define CAN_BYTE_GEARBOX_TARGET 0 // low nibble = ST_GR_GRB
// ST_GR_GRB (byte 0, low nibble). 1/2/3/5 verified live; 6..0xA inferred from
// the identical ladder in 0x1D2 byte 1. Value 4 unaccounted for.
#define GRB_TARGET_NEUTRAL 1
#define GRB_TARGET_REVERSE 2
#define GRB_TARGET_PARK 3
#define GRB_TARGET_GEAR1 5 // ..0xA = 6th
#define GRB_TARGET_INVALID 0xF
// Bytes 3-4: output shaft speed. UNDOCUMENTED in every source; zero in every
// stationary frame (including engaged-in-D), non-zero once the wheels turn.
// This is the project's only road-speed signal — there is no DSC/wheel-speed
// module on this bus. SCALING IS NOT CALIBRATED — do not convert to a physical
// unit yet. See wiki/pages/driveline-speed-signals.md
#define CAN_BYTE_OUTPUT_SHAFT_L 3
#define CAN_BYTE_OUTPUT_SHAFT_H 4
// --- 0x1A2 turbine speed (EGS, DLC 4, 20 ms) -------------------------------
// Gearbox INPUT speed. Physically proves engagement: free-spins at ~engine
// speed in P/N, collapses to ~0 when the box engages against a held output.
#define CAN_ID_TURBINE_SPEED 0x1A2
#define CAN_BYTE_TURBINE_L 0
#define CAN_BYTE_TURBINE_H 1
// --- 0x0AA "Torque 3" (DME/DDE, DLC 8, 10 ms) ------------------------------
#define CAN_ID_TORQUE_3 0x0AA
#define CAN_BYTE_PEDAL_POS 3 // ANG_ACPD: % = raw * 0.39063
#define CAN_BYTE_RPM_L 4 // RPM_ENG: 1/min = raw16 * 0.25
#define CAN_BYTE_RPM_H 5
#define CAN_BYTE_CRUISE_STATE 6 // low nibble = ST_CLCTR_V
// --- 0x3B4 "Power Management Battery Voltage" (DLC 8, 5 s) -----------------
#define CAN_ID_BATTERY_VOLTAGE 0x3B4
#define CAN_BYTE_BATTERY_VOLT_L 0 // U_BT: 12-bit little-endian, V = raw * 0.015
#define CAN_BYTE_BATTERY_VOLT_H 1 // low nibble only (high nibble = ST_BTSW)
// All IDs are 11-bit standard frames — the extended-ID assumption came from the
// bogus 0x1001/0x101F placeholders and is dead.
#define USE_EXTENDED_CAN_IDS false
// ============================================================================
// PACKET STRUCTURE FOR UART TRANSMISSION
@@ -172,40 +247,121 @@ static inline bool validate_packet(const uart_packet_t* packet) {
/**
* Convert temperature from CAN format to internal format
* @param can_value Raw CAN bus value
* @param offset Temperature offset (usually -40 for automotive)
* @param offset Value to SUBTRACT from raw (48 for 0x1D0 engine temps, 40 for 0x0B5)
* @return Temperature in 0.1°C units
*/
static inline int16_t can_to_temperature(uint8_t can_value, int16_t offset = -40) {
return (int16_t)((can_value + offset) * 10); // Convert to 0.1°C units
static inline int16_t can_to_temperature(uint8_t can_value, int16_t offset) {
return (int16_t)(((int16_t)can_value - offset) * 10);
}
/**
* Convert voltage from CAN format to internal format
* @param can_value Raw CAN bus value (typically in 0.1V)
* Decode battery voltage from a 0x3B4 frame.
* U_BT is 12-bit little-endian, factor 0.015 V — i.e. 0.01 V units = raw * 1.5.
* (Verified: raw 0x3D9 = 985 -> 14.775 V.) The high nibble of byte 1 is ST_BTSW,
* NOT voltage — mask it off or you'll read ~4x too high.
* @param d Payload of a 0x3B4 frame (needs DLC >= 2)
* @return Voltage in 0.01V units
*/
static inline uint16_t can_to_voltage(uint16_t can_value) {
return can_value * 10; // Convert 0.1V to 0.01V units
static inline uint16_t can_to_voltage(const uint8_t* d) {
uint16_t raw12 = (uint16_t)(((d[CAN_BYTE_BATTERY_VOLT_H] & 0x0F) << 8) | d[CAN_BYTE_BATTERY_VOLT_L]);
return (uint16_t)((raw12 * 3) / 2); // raw * 0.015 V -> 0.01 V units
}
/**
* Decode engine RPM from a 0x0AA frame. RPM_ENG = raw16 (Intel) * 0.25.
* @param d Payload of a 0x0AA frame (needs DLC >= 6)
*/
static inline uint16_t can_to_rpm(const uint8_t* d) {
uint16_t raw = (uint16_t)((d[CAN_BYTE_RPM_H] << 8) | d[CAN_BYTE_RPM_L]);
return (uint16_t)(raw / 4);
}
/**
* Decode gearbox turbine (input) speed from a 0x1A2 frame.
* RPM_GRB_TURB = raw16 (Intel) * 0.125 + 6.
* @param d Payload of a 0x1A2 frame (needs DLC >= 2)
*/
static inline uint16_t can_to_turbine_rpm(const uint8_t* d) {
uint16_t raw = (uint16_t)((d[CAN_BYTE_TURBINE_H] << 8) | d[CAN_BYTE_TURBINE_L]);
return (uint16_t)(raw / 8 + 6);
}
/**
* Map a raw 0x1D2 byte-0 lever code to a gear_position_t.
* D and S/M share code 0x78 — they're distinguished by byte 4 (CAN_BYTE_GEAR_MODE),
* not surfaced here yet (no enum slot; the engaged manual gear number needs a drive
* capture — see wiki/pages/open-questions.md).
* D and S/M share code 0x78 — use decode_gear() to resolve them.
* @param d0 Byte 0 of a 0x1D2 frame
* @return Gear position, or GEAR_UNKNOWN if the code isn't recognized
* @return Lever position, or GEAR_UNKNOWN if the code isn't recognized
*/
static inline gear_position_t can_to_gear_lever(uint8_t d0) {
switch (d0) {
case GEAR_CODE_PARK: return GEAR_PARK;
case GEAR_CODE_REVERSE: return GEAR_REVERSE;
case GEAR_CODE_NEUTRAL: return GEAR_NEUTRAL;
case GEAR_CODE_DRIVE: return GEAR_DRIVE; // D or S/M
case GEAR_CODE_DRIVE: return GEAR_DRIVE; // D or S/M — see decode_gear()
default: return GEAR_UNKNOWN;
}
}
/**
* Extract the engaged gear number from 0x1D2 byte 1.
* High nibble is a +16 ladder: 0x5F=1st .. 0xAF=6th. 0x0F = no numbered gear.
* @param d1 Byte 1 of a 0x1D2 frame
* @return 1..6, or 0 if no gear is engaged / value out of range.
* Always 0 in REVERSE — reverse is unnumbered here; use 0x0BA instead.
*/
static inline uint8_t can_to_engaged_gear(uint8_t d1) {
if (d1 == GEAR_ENGAGED_NONE) return 0;
uint8_t n = (uint8_t)((d1 >> 4) - 4);
return (n >= 1 && n <= 6) ? n : 0;
}
/**
* Resolve the full 0x1D2 frame into a single displayable gear_position_t.
*
* P/R/N come straight from the lever. In the D detent, byte 4 splits plain Drive
* from the Sport/Manual side-gate; when in S/M we surface the engaged gear number
* (GEAR_MANUAL_1..6) because that's what a driver in the side-gate wants to read.
*
* @param d Payload of a 0x1D2 frame
* @param dlc Frame DLC (must be >= 5 to reach the mode byte)
* @return Gear to display, or GEAR_UNKNOWN if the lever code isn't recognized
*/
static inline gear_position_t decode_gear(const uint8_t* d, uint8_t dlc) {
if (dlc <= CAN_BYTE_GEAR_LEVER) return GEAR_UNKNOWN;
gear_position_t lever = can_to_gear_lever(d[CAN_BYTE_GEAR_LEVER]);
if (lever != GEAR_DRIVE) return lever; // P/R/N (or UNKNOWN) need no further work
// In the D detent. Without the mode byte we can't tell D from S/M — report D.
if (dlc <= CAN_BYTE_GEAR_MODE) return GEAR_DRIVE;
if (d[CAN_BYTE_GEAR_MODE] == GEAR_MODE_DRIVE_AUTO) return GEAR_DRIVE;
// Sport or Manual: show the engaged gear if the box has told us one.
if (dlc > CAN_BYTE_GEAR_ENGAGED) {
uint8_t n = can_to_engaged_gear(d[CAN_BYTE_GEAR_ENGAGED]);
if (n >= 1 && n <= 6) return (gear_position_t)(GEAR_MANUAL_1 + (n - 1));
}
return GEAR_DRIVE;
}
/**
* Map a 0x0BA ST_GR_GRB nibble (the gearbox's own target gear) to a
* gear_position_t, for cross-checking against the lever.
* @param nibble Low nibble of 0x0BA byte 0
* @return Target gear, or GEAR_UNKNOWN if invalid/unrecognized
*/
static inline gear_position_t can_to_target_gear(uint8_t nibble) {
switch (nibble) {
case GRB_TARGET_PARK: return GEAR_PARK;
case GRB_TARGET_REVERSE: return GEAR_REVERSE;
case GRB_TARGET_NEUTRAL: return GEAR_NEUTRAL;
default: break;
}
// 5..0xA = forward gears 1..6 (same ladder as 0x1D2 byte 1)
if (nibble >= GRB_TARGET_GEAR1 && nibble <= 0xA) return GEAR_DRIVE;
return GEAR_UNKNOWN;
}
/**
* Get human-readable gear position string
* @param gear Gear position enum value