Skip to content

Commit

Permalink
car: CarController and CarState are always present (commaai#31925)
Browse files Browse the repository at this point in the history
* always set

* add mock

* little more

* fix

* fix
  • Loading branch information
adeebshihadeh authored Mar 19, 2024
1 parent 3a7582d commit afc9697
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
14 changes: 2 additions & 12 deletions selfdrive/car/car_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from cereal import car
from openpilot.common.params import Params
from openpilot.common.basedir import BASEDIR
from openpilot.system.version import is_comma_remote, is_tested_branch
from openpilot.selfdrive.car.interfaces import get_interface_attr
from openpilot.selfdrive.car.fingerprints import eliminate_incompatible_cars, all_legacy_fingerprint_cars
Expand Down Expand Up @@ -48,17 +47,8 @@ def load_interfaces(brand_names):
for brand_name in brand_names:
path = f'openpilot.selfdrive.car.{brand_name}'
CarInterface = __import__(path + '.interface', fromlist=['CarInterface']).CarInterface

if os.path.exists(BASEDIR + '/' + path.replace('.', '/') + '/carstate.py'):
CarState = __import__(path + '.carstate', fromlist=['CarState']).CarState
else:
CarState = None

if os.path.exists(BASEDIR + '/' + path.replace('.', '/') + '/carcontroller.py'):
CarController = __import__(path + '.carcontroller', fromlist=['CarController']).CarController
else:
CarController = None

CarState = __import__(path + '.carstate', fromlist=['CarState']).CarState
CarController = __import__(path + '.carcontroller', fromlist=['CarController']).CarController
for model_name in brand_names[brand_name]:
ret[model_name] = (CarInterface, CarController, CarState)
return ret
Expand Down
32 changes: 17 additions & 15 deletions selfdrive/car/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,16 @@ def __init__(self, CP, CarController, CarState):
self.silent_steer_warning = True
self.v_ego_cluster_seen = False

self.CS = None
self.can_parsers = []
if CarState is not None:
self.CS = CarState(CP)

self.cp = self.CS.get_can_parser(CP)
self.cp_cam = self.CS.get_cam_can_parser(CP)
self.cp_adas = self.CS.get_adas_can_parser(CP)
self.cp_body = self.CS.get_body_can_parser(CP)
self.cp_loopback = self.CS.get_loopback_can_parser(CP)
self.can_parsers = [self.cp, self.cp_cam, self.cp_adas, self.cp_body, self.cp_loopback]

self.CC: CarControllerBase = None
if CarController is not None:
self.CC = CarController(self.cp.dbc_name, CP, self.VM)
self.CS = CarState(CP)
self.cp = self.CS.get_can_parser(CP)
self.cp_cam = self.CS.get_cam_can_parser(CP)
self.cp_adas = self.CS.get_adas_can_parser(CP)
self.cp_body = self.CS.get_body_can_parser(CP)
self.cp_loopback = self.CS.get_loopback_can_parser(CP)
self.can_parsers = [self.cp, self.cp_cam, self.cp_adas, self.cp_body, self.cp_loopback]

dbc_name = "" if self.cp is None else self.cp.dbc_name
self.CC: CarControllerBase = CarController(dbc_name, CP, self.VM)

def apply(self, c: car.CarControl, now_nanos: int) -> tuple[car.CarControl.Actuators, list[tuple[int, int, bytes, int]]]:
return self.CC.update(c, self.CS, now_nanos)
Expand Down Expand Up @@ -438,6 +433,10 @@ def parse_gear_shifter(gear: str | None) -> car.CarState.GearShifter:
}
return d.get(gear.upper(), GearShifter.unknown)

@staticmethod
def get_can_parser(CP):
return None

@staticmethod
def get_cam_can_parser(CP):
return None
Expand All @@ -459,6 +458,9 @@ def get_loopback_can_parser(CP):


class CarControllerBase(ABC):
def __init__(self, dbc_name: str, CP, VM):
pass

@abstractmethod
def update(self, CC: car.CarControl.Actuators, CS: car.CarState, now_nanos: int) -> tuple[car.CarControl.Actuators, list[SendCan]]:
pass
Expand Down
5 changes: 5 additions & 0 deletions selfdrive/car/mock/carcontroller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from openpilot.selfdrive.car.interfaces import CarControllerBase

class CarController(CarControllerBase):
def update(self, CC, CS, now_nanos):
return CC.actuators.copy(), []
4 changes: 4 additions & 0 deletions selfdrive/car/mock/carstate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from openpilot.selfdrive.car.interfaces import CarStateBase

class CarState(CarStateBase):
pass

0 comments on commit afc9697

Please sign in to comment.