Skip to content

Commit

Permalink
Common interface attribute function (commaai#24731)
Browse files Browse the repository at this point in the history
* replace get_attr_from_cars with get_interface_attr

* and not combining the brands

* explicit check

* minimize diff

* values

(cherry picked from commit 3fbbb7f)
  • Loading branch information
sshane authored and mmmorks committed Jun 15, 2022
1 parent 7c5acb7 commit c3e007b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 58 deletions.
18 changes: 3 additions & 15 deletions selfdrive/car/car_helpers.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import os
from typing import Any, Dict, List
from typing import Dict, List

from cereal import car
from common.params import Params
from common.basedir import BASEDIR
from selfdrive.version import is_comma_remote, is_tested_branch
from selfdrive.car.interfaces import get_interface_attr
from selfdrive.car.fingerprints import eliminate_incompatible_cars, all_legacy_fingerprint_cars
from selfdrive.car.vin import get_vin, VIN_UNKNOWN
from selfdrive.car.fw_versions import get_fw_versions, match_fw_to_car
from selfdrive.swaglog import cloudlog
import cereal.messaging as messaging
from selfdrive.car import gen_empty_fingerprint

from cereal import car
EventName = car.CarEvent.EventName


Expand Down Expand Up @@ -59,19 +60,6 @@ def load_interfaces(brand_names):
return ret


def get_interface_attr(attr: str) -> Dict[str, Any]:
# returns given attribute from each interface
brand_names = {}
for car_folder in sorted([x[0] for x in os.walk(BASEDIR + '/selfdrive/car')]):
try:
brand_name = car_folder.split('/')[-1]
attr_data = getattr(__import__(f'selfdrive.car.{brand_name}.values', fromlist=[attr]), attr, None)
brand_names[brand_name] = attr_data
except (ImportError, OSError):
pass
return brand_names


def _get_interface_names() -> Dict[str, List[str]]:
# returns a dict of brand name and its respective models
brand_names = {}
Expand Down
40 changes: 4 additions & 36 deletions selfdrive/car/fingerprints.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,12 @@
import os
from common.basedir import BASEDIR
from selfdrive.car.interfaces import get_interface_attr


def get_attr_from_cars(attr, result=dict, combine_brands=True):
# read all the folders in selfdrive/car and return a dict where:
# - keys are all the car models
# - values are attr values from all car folders
result = result()

for car_folder in [x[0] for x in os.walk(BASEDIR + '/selfdrive/car')]:
try:
car_name = car_folder.split('/')[-1]
values = __import__(f'selfdrive.car.{car_name}.values', fromlist=[attr])
if hasattr(values, attr):
attr_values = getattr(values, attr)
else:
continue

if isinstance(attr_values, dict):
for f, v in attr_values.items():
if combine_brands:
result[f] = v
else:
if car_name not in result:
result[car_name] = {}
result[car_name][f] = v
elif isinstance(attr_values, list):
result += attr_values

except (ImportError, OSError):
pass

return result


FW_VERSIONS = get_attr_from_cars('FW_VERSIONS')
_FINGERPRINTS = get_attr_from_cars('FINGERPRINTS')
FW_VERSIONS = get_interface_attr('FW_VERSIONS', combine_brands=True, ignore_none=True)
_FINGERPRINTS = get_interface_attr('FINGERPRINTS', combine_brands=True, ignore_none=True)

_DEBUG_ADDRESS = {1880: 8} # reserved for debug purposes


def is_valid_for_fingerprint(msg, car_fingerprint):
adr = msg.address
# ignore addresses that are more than 11 bits
Expand Down
8 changes: 4 additions & 4 deletions selfdrive/car/fw_versions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/env python3
import struct
import traceback
from typing import Any, List
from collections import defaultdict
from dataclasses import dataclass

from typing import Any, List
from tqdm import tqdm

import panda.python.uds as uds
from cereal import car
from selfdrive.car.fingerprints import FW_VERSIONS, get_attr_from_cars
from selfdrive.car.interfaces import get_interface_attr
from selfdrive.car.fingerprints import FW_VERSIONS
from selfdrive.car.isotp_parallel_query import IsoTpParallelQuery
from selfdrive.car.toyota.values import CAR as TOYOTA
from selfdrive.swaglog import cloudlog
Expand Down Expand Up @@ -303,7 +303,7 @@ def get_fw_versions(logcan, sendcan, extra=None, timeout=0.1, debug=False, progr
addrs = []
parallel_addrs = []

versions = get_attr_from_cars('FW_VERSIONS', combine_brands=False)
versions = get_interface_attr('FW_VERSIONS', ignore_none=True)
if extra is not None:
versions.update(extra)

Expand Down
34 changes: 31 additions & 3 deletions selfdrive/car/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import os
import time
from abc import abstractmethod, ABC
from typing import Dict, Tuple, List
from typing import Any, Dict, Tuple, List

from cereal import car
from common.basedir import BASEDIR
from common.conversions import Conversions as CV
from common.kalman.simple_kalman import KF1D
from common.realtime import DT_CTRL
from selfdrive.car import gen_empty_fingerprint
from common.conversions import Conversions as CV
from selfdrive.controls.lib.drive_helpers import V_CRUISE_MAX
from selfdrive.controls.lib.events import Events
from selfdrive.controls.lib.vehicle_model import VehicleModel
Expand All @@ -22,7 +23,6 @@

# generic car and radar interfaces


class CarInterfaceBase(ABC):
def __init__(self, CP, CarController, CarState):
self.CP = CP
Expand Down Expand Up @@ -293,3 +293,31 @@ def get_body_can_parser(CP):
@staticmethod
def get_loopback_can_parser(CP):
return None


# interface-specific helpers

def get_interface_attr(attr: str, combine_brands: bool = False, ignore_none: bool = False) -> Dict[str, Any]:
# read all the folders in selfdrive/car and return a dict where:
# - keys are all the car models or brand names
# - values are attr values from all car folders
result = {}
for car_folder in sorted([x[0] for x in os.walk(BASEDIR + '/selfdrive/car')]):
try:
brand_name = car_folder.split('/')[-1]
brand_values = __import__(f'selfdrive.car.{brand_name}.values', fromlist=[attr])
if hasattr(brand_values, attr) or not ignore_none:
attr_data = getattr(brand_values, attr, None)
else:
continue

if combine_brands:
if isinstance(attr_data, dict):
for f, v in attr_data.items():
result[f] = v
else:
result[brand_name] = attr_data
except (ImportError, OSError):
pass

return result

0 comments on commit c3e007b

Please sign in to comment.