diff --git a/openpilot/selfdrive/car/card.py b/openpilot/selfdrive/car/card.py index 59151d8e02..f8942ce1a8 100644 --- a/openpilot/selfdrive/car/card.py +++ b/openpilot/selfdrive/car/card.py @@ -4,6 +4,7 @@ import os import time import threading import uuid +from importlib import import_module from typing import cast import openpilot.cereal.messaging as messaging @@ -401,12 +402,14 @@ class Car: self.learning_send_mono_ns = self.learning_send_committed_mono_ns = 0 self.learning_car_state_source_ns = 0 self.learning_applied_evidence = self.learning_previous_applied_evidence = None + self.learning_feedback_factory = None try: from openpilot.starpilot.lateral.learning_preferences import read_preference preference = read_preference(self.params, self.CP) if preference.valid and preference.enabled: from opendbc.car.steering_projection import install_observer self.learning_stamp_observer = self.record_learning_send_stamp if install_observer(self.CI) else None + self._prepare_feedback_capture() except Exception: pass # Optional learning setup cannot stop native startup. self.aol_transport_feedback = TransportPauseFeedback(self.CP) if self.aol_replay else None @@ -1162,10 +1165,9 @@ class Car: publication = getattr(self, 'learning_feedback_publication', None) if publication is not None: try: - from openpilot.starpilot.flm.feedback_wire import write_publication feedback_companion = (companion.as_reader().as_builder() if companion is not None else messaging.new_message('starpilotCarState', valid=bool(cs_send.valid), logMonoTime=int(cs_send.logMonoTime))) - write_publication(feedback_companion.starpilotCarState, publication) + self.learning_feedback_writer(feedback_companion.starpilotCarState, publication) companion = feedback_companion except Exception: self.learning_feedback_publication = None @@ -1485,15 +1487,26 @@ class Car: (not ps.safetyRxChecksInvalid or inactive_keepalive and self.gm_startup_keepalive()) for ps, cfg in zip(pandas, self.CP.safetyConfigs, strict=True))) + def _prepare_feedback_capture(self): + from openpilot.starpilot.flm.feedback_observation import card_capture_for + from openpilot.starpilot.flm.feedback_wire import write_publication + from openpilot.starpilot.flm.runtime_adapter_registry import car_params_digest + + # The first calibrated receipt can arrive after lateral is active. + import_module('openpilot.starpilot.flm.learning_wire') + import_module('openpilot.starpilot.flm.physical_input_observation') + self._feedback_cp_sha256 = car_params_digest(self.CP) + self.learning_feedback_writer = write_publication + self.learning_feedback_factory = card_capture_for + def _feedback_capture(self, now_ns): - # Default OFF performs no feedback-module import, digest or witness allocation. try: source = self.sm['autoSteeringLearning'].learning.sourceReceipt - if not source.enabled or source.manual: + factory = self.learning_feedback_factory + if factory is None or not source.enabled or source.manual: self.learning_feedback_capture = None return None - from openpilot.starpilot.flm.feedback_observation import card_capture_for - return card_capture_for(self, now_ns) + return factory(self, now_ns) except Exception: self.learning_feedback_capture = None return None diff --git a/openpilot/selfdrive/car/tests/test_feedback_startup.py b/openpilot/selfdrive/car/tests/test_feedback_startup.py new file mode 100644 index 0000000000..f6e7484f0c --- /dev/null +++ b/openpilot/selfdrive/car/tests/test_feedback_startup.py @@ -0,0 +1,35 @@ +from unittest.mock import patch + +from opendbc.car import structs +from openpilot.cereal import messaging +from openpilot.selfdrive.car.card import Car +from openpilot.starpilot.flm.learning_wire import write_source_receipt +from openpilot.starpilot.flm.runtime_adapter_registry import SourceState + + +def test_first_calibrated_feedback_uses_startup_preparation(): + card = Car.__new__(Car) + card.CP = structs.CarParams.new_message(brand='gm', carFingerprint='CHEVROLET_BOLT_ACC_2022_2023') + card.learning_feedback_factory = None + card._prepare_feedback_capture() + + stamp = 10_000_000_000 + service = 'autoSteeringLearning' + packet = messaging.new_message(service, valid=True, logMonoTime=stamp) + source = SourceState(True, False, 'boot', 'a' * 64, stamp, stamp + 500_000_000, + card._feedback_cp_sha256, (0., 0., 0.)) + write_source_receipt(packet, source) + sm = messaging.SubMaster([service]) + sm.update_msgs(stamp / 1e9, [packet.as_reader()]) + sm.alive[service] = sm.freq_ok[service] = True + card.sm = sm + + with patch('openpilot.starpilot.flm.runtime_adapter_registry.car_params_digest', side_effect=AssertionError): + capture = card._feedback_capture(stamp) + assert capture is not None + output = messaging.new_message('carOutput', valid=True, logMonoTime=stamp) + companion = messaging.new_message('starpilotCarState', valid=True, logMonoTime=stamp) + card.learning_feedback_writer(companion.starpilotCarState, capture.publication(output)) + assert companion.starpilotCarState.feedbackPublication.version == 1 + assert not companion.starpilotCarState.feedbackPublication.outputKnown + assert card._feedback_capture(stamp + 500_000_001) is None diff --git a/prebuilt.json b/prebuilt.json index 3b1ba875b2..5f64db3128 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "8500c69206622a351e909625283a52d919cc3bc1720212da0553d8da789bf8a0", "schema_version": 1} +{"protected_inventory_sha256": "5b2460ad19d9585f718ed0ea9fd51bb99c82df4d86f446356f9d6b4d3206706c", "schema_version": 1}