Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move car.capnp to opendbc #33656

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
718 changes: 0 additions & 718 deletions cereal/car.capnp

This file was deleted.

1 change: 1 addition & 0 deletions cereal/car.capnp
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
37 changes: 17 additions & 20 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,8 +63,7 @@ def can_send(msgs: list[CanData]) -> None:
class Car:
CI: CarInterfaceBase
RI: RadarInterfaceBase
CP: structs.CarParams
CP_capnp: car.CarParams
CP: car.CarParams

def __init__(self, CI=None, RI=None) -> None:
self.can_sock = messaging.sub_sock('can', timeout=20)
Expand All @@ -78,7 +76,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()
sshane marked this conversation as resolved.
Show resolved Hide resolved

self.params = Params()

Expand All @@ -99,7 +97,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 +121,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()
sshane marked this conversation as resolved.
Show resolved Hide resolved
safety_config.safetyModel = car.CarParams.SafetyModel.noOutput
self.CP.safetyConfigs = [safety_config]

# Write previous route's CarParams
Expand All @@ -134,8 +132,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)
cp_bytes = self.CP_capnp.to_bytes()
cp_bytes = self.CP.to_bytes()
self.params.put("CarParams", cp_bytes)
self.params.put_nonblocking("CarParamsCache", cp_bytes)
self.params.put_nonblocking("CarParamsPersistent", cp_bytes)
Expand All @@ -152,19 +149,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 +181,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,20 +205,20 @@ 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)
if self.sm.frame % int(50. / DT_CTRL) == 0:
cp_send = messaging.new_message('carParams')
cp_send.valid = True
cp_send.carParams = self.CP_capnp
cp_send.carParams = self.CP
self.pm.send('carParams', cp_send)

# 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 +232,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 +248,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 All @@ -269,7 +266,7 @@ def step(self):
initialized = (not any(e.name == EventName.selfdriveInitializing for e in self.sm['onroadEvents']) and
self.sm.seen['onroadEvents'])
if not self.CP.passive and initialized:
self.controls_update(CS, self.sm['carControl'])
self.controls_update(CS, self.sm['carControl'].as_builder())

self.initialized_prev = initialized
self.CS_prev = CS.as_reader()
Expand Down
74 changes: 0 additions & 74 deletions selfdrive/car/helpers.py

This file was deleted.

17 changes: 8 additions & 9 deletions selfdrive/car/tests/test_car_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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 @@ -41,6 +40,7 @@ def test_car_interfaces(self, car_name, data):

car_params = CarInterface.get_params(car_name, args['fingerprints'], args['car_fw'],
experimental_long=args['experimental_long'], docs=False)
car_params = car_params.as_reader()
car_interface = CarInterface(car_params, CarController, CarState)
assert car_params
assert car_interface
Expand Down Expand Up @@ -72,15 +72,15 @@ 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())
CC = 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())
CC = CC.as_reader()
for _ in range(10):
car_interface.update([])
car_interface.apply(CC, now_nanos)
Expand All @@ -89,11 +89,10 @@ 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()
LongControl(car_params_capnp)
if car_params.steerControlType == CarParams.SteerControlType.angle:
LatControlAngle(car_params_capnp, car_interface)
LongControl(car_params)
if car_params.steerControlType == car.CarParams.SteerControlType.angle:
LatControlAngle(car_params, car_interface)
elif car_params.lateralTuning.which() == 'pid':
LatControlPID(car_params_capnp, car_interface)
LatControlPID(car_params, car_interface)
elif car_params.lateralTuning.which() == 'torque':
LatControlTorque(car_params_capnp, car_interface)
LatControlTorque(car_params, car_interface)
Loading
Loading