0bd8f5c14d
First capture with the vehicle actually moving (raw/log_20260717_145930.txt, 1857 frames, filtered to 0x0BA + 0x1D2). Verified, closing yesterday's inference: ST_GR_GRB values 6/7/8/9/A observed directly = 2nd..6th. So 0x0BA byte 0's low nibble is a COMPLETE gear source on its own - P/R/N plus 1st..6th, reverse included. 0x1D2 is now needed only for the lever and the D/S/M mode, which 0x0BA does not carry. Value 4 is still unaccounted for. Also caught ST_CCLT (torque converter lockup clutch) live - 422 frames "controlling", clustered in the higher gears. This matters beyond curiosity: a closed clutch bolts the turbine to the impeller, so turbine == engine exactly. Any turbine/engine slip ratio read at road speed must check CCLT first or it will report "not engaged" for a perfectly healthy locked-up box. Decoded into driveline_t with that warning attached. Output shaft: identification now verified (rises monotonically 1139->16702 across gears 1->6, zero whenever the output is held). Scaling narrowed to x0.125 but NOT adopted - it implies a coherent 901->1872 rpm upshift schedule across five independent 6HP26X ratios, where raw-as-rpm would imply 7,200-15,000 rpm against a ~4,500 redline. Still rests on the unproven assumption that each gear's max output is its upshift point, so the field stays in raw counts. One capture including 0x1A2 settles it: turbine/output gives each ratio directly. Gearbox confirmed as ZF 6HP26X (user). Same gearset as the 6HP26, so the ratio table holds; the X is the xDrive output variant, which dates the donor to E60/E61 rather than E65 - the 7-series never took M57N with xDrive. That partly answers a standing open question, and raises a new one: the X box measures its output shaft upstream of the transfer case, so if a Patrol transfer case sits in the path its ratio enters the km/h conversion and changes with high/low range. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
164 lines
5.5 KiB
C++
164 lines
5.5 KiB
C++
/**
|
|
* can_driver.h
|
|
*
|
|
* CAN Bus driver for ESP32-S3-Touch-LCD-5B using TWAI (Two-Wire Automotive Interface)
|
|
* Reads BMW E60/E65 PT-CAN messages at 500 Kbps.
|
|
*
|
|
* Hardware: GPIO 15 (TX), GPIO 16 (RX)
|
|
* Mode: TWAI_MODE_NORMAL — the board ACKs the bus (LISTEN_ONLY can't ACK, which
|
|
* makes transmitters flood it with retries). We never call twai_transmit(),
|
|
* so behaviour is read-only in practice, but note this is a policy, not a
|
|
* hardware guarantee: nothing but discipline prevents a write.
|
|
* Baud: 500 Kbps
|
|
*/
|
|
|
|
#ifndef _CAN_DRIVER_H
|
|
#define _CAN_DRIVER_H
|
|
|
|
#include <Arduino.h>
|
|
#include <lvgl.h>
|
|
#include "driver/twai.h"
|
|
#include "protocol/automotive_data.h"
|
|
|
|
// ============================================================================
|
|
// HARDWARE CONFIGURATION
|
|
// ============================================================================
|
|
|
|
#define CAN_TX_PIN 15
|
|
#define CAN_RX_PIN 16
|
|
#define CAN_BAUD_RATE TWAI_TIMING_CONFIG_500KBITS()
|
|
|
|
// ============================================================================
|
|
// TASK CONFIGURATION
|
|
// ============================================================================
|
|
|
|
#define CAN_TASK_STACK_SIZE 4096
|
|
#define CAN_TASK_PRIORITY 1
|
|
#define CAN_TASK_CORE 0 // Run on Core 0 with UART task
|
|
|
|
// ============================================================================
|
|
// CAN EVENT TYPES
|
|
// ============================================================================
|
|
|
|
typedef enum {
|
|
CAN_EVENT_START,
|
|
CAN_EVENT_STOP
|
|
} can_event_type_t;
|
|
|
|
typedef struct {
|
|
can_event_type_t type;
|
|
} can_queue_event_t;
|
|
|
|
// ============================================================================
|
|
// CAN STATISTICS
|
|
// ============================================================================
|
|
|
|
typedef struct {
|
|
uint32_t messages_received;
|
|
uint32_t messages_parsed;
|
|
uint32_t parse_errors;
|
|
uint32_t last_receive_time;
|
|
} can_stats_t;
|
|
|
|
// ============================================================================
|
|
// DRIVELINE STATE
|
|
// ============================================================================
|
|
|
|
/**
|
|
* Decoded driveline speeds. Deliberately NOT part of automotive_data_t: that
|
|
* struct is the wire format for the UART link to Controller B, and widening it
|
|
* would break the receiver. These live here until there's a reason to spend a
|
|
* protocol version bump on them.
|
|
*
|
|
* See wiki/pages/driveline-speed-signals.md for the physics and the calibration
|
|
* that output_shaft_raw still needs.
|
|
*/
|
|
typedef struct {
|
|
uint16_t engine_rpm; // 0x0AA RPM_ENG (verified)
|
|
uint16_t turbine_rpm; // 0x1A2 RPM_GRB_TURB (verified). ~engine speed in
|
|
// P/N; collapses toward 0 when the box engages.
|
|
uint16_t output_shaft_raw; // 0x0BA bytes 3-4, RAW COUNTS — scaling unknown.
|
|
// Zero when the output is held, non-zero when the
|
|
// wheels turn. The only road-speed signal we have.
|
|
uint8_t pedal_percent; // 0x0AA ANG_ACPD
|
|
uint8_t engine_run_state; // 0x1D0 ST_ENGINE_RUN (ENGINE_RUN_* constants)
|
|
uint8_t gearbox_cooling; // 0x0B5 ST_OTMP_GRB (GEARBOX_COOL_* constants)
|
|
uint8_t converter_clutch; // 0x0BA ST_CCLT (CCLT_* constants). When CLOSED,
|
|
// turbine_rpm == engine_rpm by design — read this
|
|
// before interpreting the two as a slip ratio.
|
|
bool gear_mismatch; // lever (0x1D2) vs gearbox (0x0BA) disagree,
|
|
// sustained past the debounce window
|
|
} driveline_t;
|
|
|
|
// ============================================================================
|
|
// PUBLIC API
|
|
// ============================================================================
|
|
|
|
namespace CANDriver {
|
|
/**
|
|
* Initialize CAN driver and create FreeRTOS task
|
|
* @param auto_start If true, CAN bus starts listening immediately
|
|
*/
|
|
void init(bool auto_start = false);
|
|
|
|
/**
|
|
* Start CAN bus reception
|
|
*/
|
|
void start();
|
|
|
|
/**
|
|
* Stop CAN bus reception
|
|
*/
|
|
void stop();
|
|
|
|
/**
|
|
* Get current CAN statistics
|
|
* @return Statistics structure
|
|
*/
|
|
can_stats_t getStats();
|
|
|
|
/**
|
|
* Get the decoded driveline speeds (engine / turbine / output shaft).
|
|
* Not carried over the UART link — see driveline_t.
|
|
* @return Snapshot of the current driveline state
|
|
*/
|
|
driveline_t getDriveline();
|
|
|
|
/**
|
|
* Check if CAN is currently active
|
|
* @return true if CAN is receiving messages
|
|
*/
|
|
bool isActive();
|
|
|
|
/**
|
|
* Enable/disable raw frame logging (candump-style capture of EVERY frame).
|
|
* When enabled, each received frame is printed to Serial as a compact,
|
|
* greppable, re-parseable line prefixed "CANRAW," (see emitRawFrame()).
|
|
* Intended for CAN ID discovery: capture a drive, save the serial stream
|
|
* to a file, and analyze offline.
|
|
* @param enabled true to log every frame
|
|
*/
|
|
void setRawLogging(bool enabled);
|
|
|
|
/**
|
|
* @return true if raw frame logging is currently enabled
|
|
*/
|
|
bool isRawLogging();
|
|
|
|
/**
|
|
* Enable/disable verbose per-frame DECODED logging (human-prose ESP_LOG
|
|
* lines for known/unknown IDs). Noisy; off by default so it doesn't drown
|
|
* the raw capture stream. Independent of setRawLogging().
|
|
* @param enabled true to log decoded values per frame
|
|
*/
|
|
void setVerboseDecode(bool enabled);
|
|
|
|
/**
|
|
* UI event handler for CAN toggle switch
|
|
* @param e LVGL event
|
|
*/
|
|
void onCANToggle(lv_event_t *e);
|
|
}
|
|
|
|
#endif // _CAN_DRIVER_H
|