Skip to content

Commit

Permalink
remove helpers.py
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Sep 26, 2024
1 parent 4f2ff43 commit 3790e4b
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 136 deletions.
2 changes: 1 addition & 1 deletion opendbc_repo
Submodule opendbc_repo updated 85 files
+10 −9 opendbc/car/__init__.py
+1 −2 opendbc/car/body/carcontroller.py
+4 −4 opendbc/car/body/carstate.py
+1 −1 opendbc/car/body/fingerprints.py
+4 −4 opendbc/car/body/interface.py
+1 −2 opendbc/car/body/values.py
+10 −0 opendbc/car/capnp.py
+3 −4 opendbc/car/car_helpers.py
+1 −2 opendbc/car/chrysler/carcontroller.py
+4 −4 opendbc/car/chrysler/carstate.py
+3 −3 opendbc/car/chrysler/chryslercan.py
+1 −1 opendbc/car/chrysler/fingerprints.py
+3 −3 opendbc/car/chrysler/interface.py
+3 −3 opendbc/car/chrysler/radar_interface.py
+1 −2 opendbc/car/chrysler/values.py
+1 −2 opendbc/car/docs.py
+3 −3 opendbc/car/docs_definitions.py
+4 −5 opendbc/car/ford/carcontroller.py
+6 −6 opendbc/car/ford/carstate.py
+1 −1 opendbc/car/ford/fingerprints.py
+2 −2 opendbc/car/ford/fordcan.py
+6 −6 opendbc/car/ford/interface.py
+4 −4 opendbc/car/ford/radar_interface.py
+1 −1 opendbc/car/ford/tests/print_platform_codes.py
+2 −2 opendbc/car/ford/tests/test_ford.py
+1 −2 opendbc/car/ford/values.py
+1 −1 opendbc/car/fw_query_definitions.py
+2 −3 opendbc/car/fw_versions.py
+5 −6 opendbc/car/gm/carcontroller.py
+6 −6 opendbc/car/gm/carstate.py
+8 −8 opendbc/car/gm/interface.py
+3 −3 opendbc/car/gm/radar_interface.py
+1 −2 opendbc/car/gm/values.py
+7 −8 opendbc/car/honda/carcontroller.py
+6 −6 opendbc/car/honda/carstate.py
+1 −1 opendbc/car/honda/fingerprints.py
+5 −5 opendbc/car/honda/interface.py
+3 −3 opendbc/car/honda/radar_interface.py
+4 −4 opendbc/car/honda/values.py
+5 −6 opendbc/car/hyundai/carcontroller.py
+6 −6 opendbc/car/hyundai/carstate.py
+1 −1 opendbc/car/hyundai/fingerprints.py
+7 −7 opendbc/car/hyundai/interface.py
+3 −3 opendbc/car/hyundai/radar_interface.py
+1 −1 opendbc/car/hyundai/tests/print_platform_codes.py
+1 −2 opendbc/car/hyundai/tests/test_hyundai.py
+1 −2 opendbc/car/hyundai/values.py
+30 −30 opendbc/car/interfaces.py
+3 −4 opendbc/car/mazda/carcontroller.py
+4 −4 opendbc/car/mazda/carstate.py
+1 −1 opendbc/car/mazda/fingerprints.py
+3 −3 opendbc/car/mazda/interface.py
+1 −2 opendbc/car/mazda/values.py
+3 −3 opendbc/car/mock/carstate.py
+2 −2 opendbc/car/mock/interface.py
+3 −4 opendbc/car/nissan/carcontroller.py
+4 −4 opendbc/car/nissan/carstate.py
+1 −1 opendbc/car/nissan/fingerprints.py
+4 −4 opendbc/car/nissan/interface.py
+1 −2 opendbc/car/nissan/values.py
+4 −4 opendbc/car/panda_runner.py
+0 −501 opendbc/car/structs.py
+1 −2 opendbc/car/subaru/carcontroller.py
+3 −3 opendbc/car/subaru/carstate.py
+1 −1 opendbc/car/subaru/fingerprints.py
+5 −5 opendbc/car/subaru/interface.py
+2 −2 opendbc/car/subaru/subarucan.py
+1 −2 opendbc/car/subaru/values.py
+5 −5 opendbc/car/tests/test_car_interfaces.py
+4 −4 opendbc/car/tests/test_fw_fingerprint.py
+5 −6 opendbc/car/toyota/carcontroller.py
+5 −5 opendbc/car/toyota/carstate.py
+1 −1 opendbc/car/toyota/fingerprints.py
+4 −4 opendbc/car/toyota/interface.py
+3 −3 opendbc/car/toyota/radar_interface.py
+1 −1 opendbc/car/toyota/tests/test_toyota.py
+1 −1 opendbc/car/toyota/toyotacan.py
+1 −2 opendbc/car/toyota/values.py
+5 −6 opendbc/car/volkswagen/carcontroller.py
+6 −6 opendbc/car/volkswagen/carstate.py
+1 −1 opendbc/car/volkswagen/fingerprints.py
+4 −4 opendbc/car/volkswagen/interface.py
+1 −1 opendbc/car/volkswagen/tests/test_volkswagen.py
+18 −18 opendbc/car/volkswagen/values.py
+1 −0 pyproject.toml
12 changes: 6 additions & 6 deletions selfdrive/car/car_specific.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from cereal import car
import cereal.messaging as messaging
from opendbc.car import DT_CTRL, structs
from opendbc.car import DT_CTRL
from opendbc.car.interfaces import MAX_CTRL_SPEED, CarStateBase, CarControllerBase
from opendbc.car.volkswagen.values import CarControllerParams as VWCarControllerParams
from opendbc.car.hyundai.interface import ENABLE_BUTTONS as HYUNDAI_ENABLE_BUTTONS

from openpilot.selfdrive.selfdrived.events import Events

ButtonType = structs.CarState.ButtonEvent.Type
GearShifter = structs.CarState.GearShifter
ButtonType = car.CarState.ButtonEvent.Type
GearShifter = car.CarState.GearShifter
EventName = car.OnroadEvent.EventName
NetworkLocation = structs.CarParams.NetworkLocation
NetworkLocation = car.CarParams.NetworkLocation


# TODO: the goal is to abstract this file into the CarState struct and make events generic
Expand All @@ -29,7 +29,7 @@ def update(self, CS: car.CarState):


class CarSpecificEvents:
def __init__(self, CP: structs.CarParams):
def __init__(self, CP: car.CarParams):
self.CP = CP

self.steering_unpressed = 0
Expand Down Expand Up @@ -172,7 +172,7 @@ def update(self, CS: CarStateBase, CS_prev: car.CarState, CC: CarControllerBase,

return events

def create_common_events(self, CS: structs.CarState, CS_prev: car.CarState, extra_gears=None, pcm_enable=True,
def create_common_events(self, CS: car.CarState, CS_prev: car.CarState, extra_gears=None, pcm_enable=True,
allow_enable=True, enable_buttons=(ButtonType.accelCruise, ButtonType.decelCruise)):
events = Events()

Expand Down
31 changes: 15 additions & 16 deletions selfdrive/car/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
from openpilot.common.realtime import config_realtime_process, Priority, Ratekeeper
from openpilot.common.swaglog import cloudlog, ForwardingHandler

from opendbc.car import DT_CTRL, carlog, structs
from opendbc.car import DT_CTRL, carlog
from opendbc.car.can_definitions import CanData, CanRecvCallable, CanSendCallable
from opendbc.car.fw_versions import ObdCallback
from opendbc.car.car_helpers import get_car, get_radar_interface
from opendbc.car.interfaces import CarInterfaceBase, RadarInterfaceBase
from openpilot.selfdrive.pandad import can_capnp_to_list, can_list_to_can_capnp
from openpilot.selfdrive.car.cruise import VCruiseHelper
from openpilot.selfdrive.car.car_specific import CarSpecificEvents, MockCarState
from openpilot.selfdrive.car.helpers import convert_carControl, convert_to_capnp
from openpilot.selfdrive.selfdrived.events import Events, ET

REPLAY = "REPLAY" in os.environ
Expand Down Expand Up @@ -64,7 +63,7 @@ def can_send(msgs: list[CanData]) -> None:
class Car:
CI: CarInterfaceBase
RI: RadarInterfaceBase
CP: structs.CarParams
CP: car.CarParams
CP_capnp: car.CarParams

def __init__(self, CI=None, RI=None) -> None:
Expand All @@ -78,7 +77,7 @@ def __init__(self, CI=None, RI=None) -> None:
self.CS_prev = car.CarState.new_message()
self.initialized_prev = False

self.last_actuators_output = structs.CarControl.Actuators()
self.last_actuators_output = car.CarControl.Actuators.new_message()

self.params = Params()

Expand All @@ -99,7 +98,7 @@ def __init__(self, CI=None, RI=None) -> None:
cached_params_raw = self.params.get("CarParamsCache")
if cached_params_raw is not None:
with car.CarParams.from_bytes(cached_params_raw) as _cached_params:
cached_params = structs.CarParams(carName=_cached_params.carName, carFw=_cached_params.carFw, carVin=_cached_params.carVin)
cached_params = car.CarParams(carName=_cached_params.carName, carFw=_cached_params.carFw, carVin=_cached_params.carVin)

self.CI = get_car(*self.can_callbacks, obd_callback(self.params), experimental_long_allowed, num_pandas, cached_params)
self.RI = get_radar_interface(self.CI.CP)
Expand All @@ -123,8 +122,8 @@ def __init__(self, CI=None, RI=None) -> None:

self.CP.passive = not controller_available or self.CP.dashcamOnly
if self.CP.passive:
safety_config = structs.CarParams.SafetyConfig()
safety_config.safetyModel = structs.CarParams.SafetyModel.noOutput
safety_config = car.CarParams.SafetyConfig.new_message()
safety_config.safetyModel = car.CarParams.SafetyModel.noOutput
self.CP.safetyConfigs = [safety_config]

# Write previous route's CarParams
Expand All @@ -134,7 +133,7 @@ def __init__(self, CI=None, RI=None) -> None:

# Write CarParams for controls and radard
# convert to pycapnp representation for caching and logging
self.CP_capnp = convert_to_capnp(self.CP)
self.CP_capnp = self.CP
cp_bytes = self.CP_capnp.to_bytes()
self.params.put("CarParams", cp_bytes)
self.params.put_nonblocking("CarParamsCache", cp_bytes)
Expand All @@ -152,19 +151,19 @@ def __init__(self, CI=None, RI=None) -> None:
# card is driven by can recv, expected at 100Hz
self.rk = Ratekeeper(100, print_delay_threshold=None)

def state_update(self) -> tuple[car.CarState, structs.RadarData | None]:
def state_update(self) -> tuple[car.CarState, car.RadarData]:
"""carState update loop, driven by can"""

can_strs = messaging.drain_sock_raw(self.can_sock, wait_for_one=True)
can_list = can_capnp_to_list(can_strs)

# Update carState from CAN
CS = convert_to_capnp(self.CI.update(can_list))
CS = self.CI.update(can_list)
if self.CP.carName == 'mock':
CS = self.mock_carstate.update(CS)

# Update radar tracks from CAN
RD: structs.RadarData | None = self.RI.update(can_list)
RD: car.RadarData = self.RI.update(can_list)

self.sm.update(0)

Expand All @@ -184,7 +183,7 @@ def state_update(self) -> tuple[car.CarState, structs.RadarData | None]:

return CS, RD

def update_events(self, CS: car.CarState, RD: structs.RadarData | None):
def update_events(self, CS: car.CarState, RD: car.RadarData):
self.events.clear()

CS.events = self.car_events.update(self.CI.CS, self.CS_prev, self.CI.CC, self.CC_prev).to_msg()
Expand All @@ -208,7 +207,7 @@ def update_events(self, CS: car.CarState, RD: structs.RadarData | None):

CS.events = self.events.to_msg()

def state_publish(self, CS: car.CarState, RD: structs.RadarData | None):
def state_publish(self, CS: car.CarState, RD: car.RadarData):
"""carState and carParams publish loop"""

# carParams - logged every 50 seconds (> 1 per segment)
Expand All @@ -221,7 +220,7 @@ def state_publish(self, CS: car.CarState, RD: structs.RadarData | None):
# publish new carOutput
co_send = messaging.new_message('carOutput')
co_send.valid = self.sm.all_checks(['carControl'])
co_send.carOutput.actuatorsOutput = convert_to_capnp(self.last_actuators_output)
co_send.carOutput.actuatorsOutput = self.last_actuators_output
self.pm.send('carOutput', co_send)

# kick off controlsd step while we actuate the latest carControl packet
Expand All @@ -235,7 +234,7 @@ def state_publish(self, CS: car.CarState, RD: structs.RadarData | None):
if RD is not None:
tracks_msg = messaging.new_message('liveTracks')
tracks_msg.valid = len(RD.errors) == 0
tracks_msg.liveTracks = convert_to_capnp(RD)
tracks_msg.liveTracks = RD
self.pm.send('liveTracks', tracks_msg)

def controls_update(self, CS: car.CarState, CC: car.CarControl):
Expand All @@ -251,7 +250,7 @@ def controls_update(self, CS: car.CarState, CC: car.CarControl):
if self.sm.all_alive(['carControl']):
# send car controls over can
now_nanos = self.can_log_mono_time if REPLAY else int(time.monotonic() * 1e9)
self.last_actuators_output, can_sends = self.CI.apply(convert_carControl(CC), now_nanos)
self.last_actuators_output, can_sends = self.CI.apply(CC, now_nanos)
self.pm.send('sendcan', can_list_to_can_capnp(can_sends, msgtype='sendcan', valid=CS.canValid))

self.CC_prev = CC
Expand Down
74 changes: 0 additions & 74 deletions selfdrive/car/helpers.py

This file was deleted.

8 changes: 2 additions & 6 deletions selfdrive/car/tests/test_car_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
from parameterized import parameterized

from cereal import car
from opendbc.car import DT_CTRL
from opendbc.car import DT_CTRL, CarParams
from opendbc.car.car_helpers import interfaces
from opendbc.car.structs import CarParams
from opendbc.car.tests.test_car_interfaces import get_fuzzy_car_interface_args
from opendbc.car.fingerprints import all_known_cars
from opendbc.car.fw_versions import FW_VERSIONS, FW_QUERY_CONFIGS
from opendbc.car.mock.values import CAR as MOCK
from openpilot.selfdrive.car.card import convert_carControl, convert_to_capnp
from openpilot.selfdrive.controls.lib.latcontrol_angle import LatControlAngle
from openpilot.selfdrive.controls.lib.latcontrol_pid import LatControlPID
from openpilot.selfdrive.controls.lib.latcontrol_torque import LatControlTorque
Expand Down Expand Up @@ -72,15 +70,13 @@ def test_car_interfaces(self, car_name, data):
# Run car interface
now_nanos = 0
CC = car.CarControl.new_message(**cc_msg)
CC = convert_carControl(CC.as_reader())
for _ in range(10):
car_interface.update([])
car_interface.apply(CC, now_nanos)
now_nanos += DT_CTRL * 1e9 # 10 ms

CC = car.CarControl.new_message(**cc_msg)
CC.enabled = True
CC = convert_carControl(CC.as_reader())
for _ in range(10):
car_interface.update([])
car_interface.apply(CC, now_nanos)
Expand All @@ -89,7 +85,7 @@ def test_car_interfaces(self, car_name, data):
# Test controller initialization
# TODO: wait until card refactor is merged to run controller a few times,
# hypothesis also slows down significantly with just one more message draw
car_params_capnp = convert_to_capnp(car_params).as_reader()
car_params_capnp = car_params.as_reader()
LongControl(car_params_capnp)
if car_params.steerControlType == CarParams.SteerControlType.angle:
LatControlAngle(car_params_capnp, car_interface)
Expand Down
26 changes: 12 additions & 14 deletions selfdrive/car/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import capnp
import copy
import dataclasses
import os
import pytest
import random
Expand All @@ -14,13 +12,13 @@
from cereal import messaging, log, car
from openpilot.common.basedir import BASEDIR
from openpilot.common.params import Params
from opendbc.car import DT_CTRL, gen_empty_fingerprint, structs
from opendbc.car import DT_CTRL, gen_empty_fingerprint
from opendbc.car.fingerprints import all_known_cars, MIGRATION
from opendbc.car.car_helpers import FRAME_FINGERPRINT, interfaces
from opendbc.car.honda.values import CAR as HONDA, HondaFlags
from opendbc.car.values import Platform
from opendbc.car.tests.routes import non_tested_cars, routes, CarTestRoute
from openpilot.selfdrive.car.card import Car, convert_to_capnp
from openpilot.selfdrive.car.card import Car
from openpilot.selfdrive.pandad import can_capnp_to_list
from openpilot.selfdrive.test.helpers import read_segment_list
from openpilot.system.hardware.hw import DEFAULT_DOWNLOAD_CACHE_ROOT
Expand Down Expand Up @@ -185,15 +183,15 @@ def tearDownClass(cls):
del cls.can_msgs

def setUp(self):
self.CI = self.CarInterface(copy.deepcopy(self.CP), self.CarController, self.CarState)
self.CI = self.CarInterface(self.CP.copy(), self.CarController, self.CarState)
assert self.CI

Params().put_bool("OpenpilotEnabledToggle", self.openpilot_enabled)

# TODO: check safetyModel is in release panda build
self.safety = libpanda_py.libpanda

cfg = car.CarParams.SafetyConfig(**dataclasses.asdict(self.CP.safetyConfigs[-1]))
cfg = self.CP.safetyConfigs[-1]
set_status = self.safety.set_safety_hooks(cfg.safetyModel.raw, cfg.safetyParam)
self.assertEqual(0, set_status, f"failed to set safetyModel {cfg}")
self.safety.init_tests()
Expand All @@ -205,7 +203,7 @@ def test_car_params(self):
# make sure car params are within a valid range
self.assertGreater(self.CP.mass, 1)

if self.CP.steerControlType != structs.CarParams.SteerControlType.angle:
if self.CP.steerControlType != car.CarParams.SteerControlType.angle:
tuning = self.CP.lateralTuning.which()
if tuning == 'pid':
self.assertTrue(len(self.CP.lateralTuning.pid.kpV))
Expand All @@ -218,7 +216,7 @@ def test_car_interface(self):
# TODO: also check for checksum violations from can parser
can_invalid_cnt = 0
can_valid = False
CC = structs.CarControl()
CC = car.CarControl.new_message()

for i, msg in enumerate(self.can_msgs):
CS = self.CI.update(can_capnp_to_list((msg.as_builder().to_bytes(),)))
Expand All @@ -241,9 +239,9 @@ def test_radar_interface(self):
# start parsing CAN messages after we've left ELM mode and can expect CAN traffic
error_cnt = 0
for i, msg in enumerate(self.can_msgs[self.elm_frame:]):
rr: structs.RadarData | None = RI.update(can_capnp_to_list((msg.as_builder().to_bytes(),)))
rr: car.RadarData | None = RI.update(can_capnp_to_list((msg.as_builder().to_bytes(),)))
if rr is not None and i > 50:
error_cnt += structs.RadarData.Error.canError in rr.errors
error_cnt += car.RadarData.Error.canError in rr.errors
self.assertEqual(error_cnt, 0)

def test_panda_safety_rx_checks(self):
Expand Down Expand Up @@ -309,17 +307,17 @@ def test_car_controller(car_control):
self.assertGreater(msgs_sent, 50)

# Make sure we can send all messages while inactive
CC = structs.CarControl()
CC = car.CarControl.new_message()
test_car_controller(CC)

# Test cancel + general messages (controls_allowed=False & cruise_engaged=True)
self.safety.set_cruise_engaged_prev(True)
CC = structs.CarControl(cruiseControl=structs.CarControl.CruiseControl(cancel=True))
CC = car.CarControl.new_message(cruiseControl={'cancel': True})
test_car_controller(CC)

# Test resume + general messages (controls_allowed=True & cruise_engaged=True)
self.safety.set_controls_allowed(True)
CC = structs.CarControl(cruiseControl=structs.CarControl.CruiseControl(resume=True))
CC = car.CarControl.new_message(cruiseControl={'resume': True})
test_car_controller(CC)

# Skip stdout/stderr capture with pytest, causes elevated memory usage
Expand Down Expand Up @@ -406,7 +404,7 @@ def test_panda_safety_carstate(self):
checks = defaultdict(int)
card = Car(CI=self.CI)
for idx, can in enumerate(self.can_msgs):
CS = convert_to_capnp(self.CI.update(can_capnp_to_list((can.as_builder().to_bytes(), ))))
CS = self.CI.update(can_capnp_to_list((can.as_builder().to_bytes(), )))
for msg in filter(lambda m: m.src in range(64), can.can):
to_send = libpanda_py.make_CANPacket(msg.address, msg.src % 4, msg.dat)
ret = self.safety.safety_rx_hook(to_send)
Expand Down
Loading

0 comments on commit 3790e4b

Please sign in to comment.