Skip to content

Fault Injection

farzad edited this page Oct 7, 2026 · 2 revisions

Fault Injection

simulator/faults.py defines named fault scenarios that put the simulated vehicle into a specific fault state on demand. They're for demos, and for testing whether an AI client diagnoses correctly. Without a fault active, the simulated vehicle is healthy: no crash, doors closed, airbag system OK.

Presets

Preset What happens OBD-II / UDS DTC J1939 DTC
overheat Coolant temperature at its maximum; airbag SYSTEM_STATUS = FAULT_PRESENT P0217 SPN 110 FMI 0
abs_fault All four wheel-speed sensors read zero; SYSTEM_STATUS = FAULT_PRESENT C0035 SPN 84 FMI 5
low_fuel Fuel level critically low (no stored code, as on a real car) SPN 96 FMI 18
crash Airbags deployed (CRASH_DETECTED), engine stalled and vehicle stopped, doors unlocked, driver door open, hazard lights on B0001
door_ajar Rear-right door reported open while driving
misfire Engine speed rough and erratic, check-engine lamp on P0300 SPN 1322 FMI 31 + SPN 651 FMI 7 (sent via BAM)
battery_low Charging failure: about 11.3 V with the engine running (normal ≈ 14.2 V) P0562 SPN 168 FMI 18

peykan fault list is the source of truth if this table ever drifts.

Using them

peykan fault misfire        # activate
peykan fault clear          # deactivate
peykan fault list           # presets with descriptions and codes

or activate_fault_scenario(preset) / activate_fault_scenario() to clear, or the fault_drill prompt, which injects a fault, has the model diagnose it from the evidence alone, compares, and clears it.

Activation is a round trip over the bus: the client sends a control frame on 0x7F0 (byte 0 = the preset's position, 0 = clear), FaultListenerThread in the simulator applies it and acknowledges on 0x7F1. It therefore needs a simulator on the same bus (peykan demo), and counts as a write under the transmit policy.

How a fault reaches every protocol

A preset can do two things:

  • overrides: force DBC signals to values (e.g. CRASH_DETECTED: 1). Checked first for every signal the 11-bit simulator encodes.
  • state_effect: change the underlying driving state (e.g. speed 0 and engine off for a crash, ±250 rpm jitter for a misfire, 11.3 V for battery_low). Applied to a copy of the state before any protocol derives values from it: DBC signals, J1939 PGNs, OBD-II PIDs and UDS live DIDs. A crash therefore reads 0 km/h everywhere, not just in one message.

DTCs appear in OBD Mode 03, UDS 0x19 (where they stay stored as confirmed after the fault clears, until cleared with 0x14, see UDS over ISO-TP), and for engine faults in J1939 DM1.

Adding a preset

Append to PRESETS in faults.py. The control frame identifies presets by position, so only ever append.

def _coolant_leak(state: DrivingState) -> None:
    state.engine_temp_c = min(state.engine_temp_c + 10, ENGINE_TEMP_MAX_C)

"coolant_leak": FaultPreset(
    name="coolant_leak",
    description="One-line description shown by `peykan fault list`.",
    overrides={},                    # DBC signal -> forced value
    dtcs=["P0128"],                  # OBD-II / UDS codes, optional
    state_effect=_coolant_leak,      # optional
),

Then:

  • Add its J1939 codes to J1939_FAULT_DTCS in simulator/j1939_runner.py, or list it in PRESETS_WITHOUT_J1939_DTCS. A test fails if you do neither.
  • Check override values are encodable: a signal's declared DBC range isn't always reachable by its bit width (ENGINE_TEMP declares 127.5 but 8 bits at scale 0.5 / offset -40 top out at 87.5). Values are clamped rather than crashing, but a silently clamped value is confusing.
  • Override names must be real DBC signals; tests/test_faults.py checks every preset.

Clone this wiki locally