Back to Repositories

Testing Radar Fault Detection System in openpilot

This test suite validates radar fault handling in the openpilot system, specifically focusing on scenarios where radar-related CAN traffic is absent. It ensures proper error handling and system response in degraded radar conditions.

Test Coverage Overview

The test suite covers critical radar fault detection scenarios in the openpilot system.

Key areas tested include:
  • Radar communication failure detection
  • System response to missing CAN traffic
  • Validation of liveTracks message handling
  • Error state verification for Toyota Corolla TSS2

Implementation Analysis

The testing approach utilizes process replay functionality to simulate radar fault conditions. It implements message generation and verification patterns using the cereal messaging system.

Notable implementation features:
  • Custom message generation for CAN, carState, and modelV2
  • Iterative message processing simulation
  • State validation through liveTracks message analysis

Technical Details

Testing components and configuration:
  • cereal messaging framework for communication
  • OpenDBC car values integration
  • Process replay testing utilities
  • Custom message generation functions
  • Toyota-specific fingerprint configuration

Best Practices Demonstrated

The test implementation showcases several testing best practices for automotive systems.

Notable practices include:
  • Isolation of radar subsystem testing
  • Comprehensive error state validation
  • Simulation of degraded system conditions
  • Clear test case organization and documentation

commaai/openpilot

selfdrive/controls/tests/test_leads.py

            
import cereal.messaging as messaging

from opendbc.car.toyota.values import CAR as TOYOTA
from openpilot.selfdrive.test.process_replay import replay_process_with_name


class TestLeads:
  def test_radar_fault(self):
    # if there's no radar-related can traffic, radard should either not respond or respond with an error
    # this is tightly coupled with underlying car radar_interface implementation, but it's a good sanity check
    def single_iter_pkg():
      # single iter package, with meaningless cans and empty carState/modelV2
      msgs = []
      for _ in range(500):
        can = messaging.new_message("can", 1)
        cs = messaging.new_message("carState")
        cp = messaging.new_message("carParams")
        msgs.append(can.as_reader())
        msgs.append(cs.as_reader())
        msgs.append(cp.as_reader())
      model = messaging.new_message("modelV2")
      msgs.append(model.as_reader())

      return msgs

    msgs = [m for _ in range(3) for m in single_iter_pkg()]
    out = replay_process_with_name("card", msgs, fingerprint=TOYOTA.TOYOTA_COROLLA_TSS2)
    states = [m for m in out if m.which() == "liveTracks"]
    failures = [not state.valid and len(state.liveTracks.errors) for state in states]

    assert len(states) == 0 or all(failures)