From cb61d0045cb38f7fed362a848dcf3646b130d951 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Tue, 13 Aug 2024 22:48:55 -0700 Subject: [PATCH] card: set mock carState fields (#33294) * mock carstate inside card * not used --- selfdrive/car/car_specific.py | 16 ++++++++++++++++ selfdrive/car/card.py | 6 +++++- selfdrive/car/mock/carstate.py | 4 ++-- selfdrive/car/mock/interface.py | 16 +--------------- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/selfdrive/car/car_specific.py b/selfdrive/car/car_specific.py index a4500b6f1..98ae3e0b8 100644 --- a/selfdrive/car/car_specific.py +++ b/selfdrive/car/car_specific.py @@ -1,4 +1,5 @@ from cereal import car +import cereal.messaging as messaging from openpilot.selfdrive.car import DT_CTRL from openpilot.selfdrive.car.interfaces import MAX_CTRL_SPEED from openpilot.selfdrive.car.volkswagen.values import CarControllerParams as VWCarControllerParams @@ -12,6 +13,21 @@ EventName = car.CarEvent.EventName NetworkLocation = car.CarParams.NetworkLocation +# TODO: the goal is to abstract this file into the CarState struct and make events generic +class MockCarState: + def __init__(self): + self.sm = messaging.SubMaster(['gpsLocation', 'gpsLocationExternal']) + + def update(self, CS: car.CarState): + self.sm.update(0) + gps_sock = 'gpsLocationExternal' if self.sm.recv_frame['gpsLocationExternal'] > 1 else 'gpsLocation' + + CS.vEgo = self.sm[gps_sock].speed + CS.vEgoRaw = self.sm[gps_sock].speed + + return CS + + class CarSpecificEvents: def __init__(self, CP: car.CarParams): self.CP = CP diff --git a/selfdrive/car/card.py b/selfdrive/car/card.py index 78b95163e..a7cd2f5a4 100755 --- a/selfdrive/car/card.py +++ b/selfdrive/car/card.py @@ -15,7 +15,7 @@ from openpilot.common.swaglog import cloudlog, ForwardingHandler from openpilot.selfdrive.pandad import can_capnp_to_list, can_list_to_can_capnp from openpilot.selfdrive.car import DT_CTRL, carlog from openpilot.selfdrive.car.can_definitions import CanData, CanRecvCallable, CanSendCallable -from openpilot.selfdrive.car.car_specific import CarSpecificEvents +from openpilot.selfdrive.car.car_specific import CarSpecificEvents, MockCarState from openpilot.selfdrive.car.fw_versions import ObdCallback from openpilot.selfdrive.car.car_helpers import get_car from openpilot.selfdrive.car.interfaces import CarInterfaceBase @@ -133,6 +133,7 @@ class Car: self.events = Events() self.car_events = CarSpecificEvents(self.CP) + self.mock_carstate = MockCarState() # card is driven by can recv, expected at 100Hz self.rk = Ratekeeper(100, print_delay_threshold=None) @@ -144,6 +145,9 @@ class Car: can_strs = messaging.drain_sock_raw(self.can_sock, wait_for_one=True) CS = self.CI.update(can_capnp_to_list(can_strs)) + if self.CP.carName == 'mock': + CS = self.mock_carstate.update(CS) + self.sm.update(0) can_rcv_valid = len(can_strs) > 0 diff --git a/selfdrive/car/mock/carstate.py b/selfdrive/car/mock/carstate.py index 41a298e24..3937e8d75 100644 --- a/selfdrive/car/mock/carstate.py +++ b/selfdrive/car/mock/carstate.py @@ -3,5 +3,5 @@ from openpilot.selfdrive.car.interfaces import CarStateBase class CarState(CarStateBase): - def update(self, *args) -> car.CarState: - pass + def update(self, *_) -> car.CarState: + return car.CarState.new_message() diff --git a/selfdrive/car/mock/interface.py b/selfdrive/car/mock/interface.py index cfd11d1cf..44cd40d28 100755 --- a/selfdrive/car/mock/interface.py +++ b/selfdrive/car/mock/interface.py @@ -1,16 +1,9 @@ #!/usr/bin/env python3 -from cereal import car -import cereal.messaging as messaging from openpilot.selfdrive.car.interfaces import CarInterfaceBase # mocked car interface for dashcam mode class CarInterface(CarInterfaceBase): - def __init__(self, CP, CarController, CarState): - super().__init__(CP, CarController, CarState) - - self.speed = 0. - self.sm = messaging.SubMaster(['gpsLocation', 'gpsLocationExternal']) @staticmethod def _get_params(ret, candidate, fingerprint, car_fw, experimental_long, docs): @@ -23,11 +16,4 @@ class CarInterface(CarInterfaceBase): return ret def _update(self): - self.sm.update(0) - gps_sock = 'gpsLocationExternal' if self.sm.recv_frame['gpsLocationExternal'] > 1 else 'gpsLocation' - - ret = car.CarState.new_message() - ret.vEgo = self.sm[gps_sock].speed - ret.vEgoRaw = self.sm[gps_sock].speed - - return ret + return self.CS.update()