Files
dragonpilot/common/fingerprints.py

65 lines
1.9 KiB
Python
Raw Normal View History

2018-06-16 20:59:34 -07:00
import os
from common.basedir import BASEDIR
def get_fingerprint_list():
# read all the folders in selfdrive/car and return a dict where:
# - keys are all the car models for which we have a fingerprint
2019-06-28 21:11:30 +00:00
# - values are lists dicts of messages that constitute the unique
2018-06-16 20:59:34 -07:00
# CAN fingerprint of each car model and all its variants
fingerprints = {}
for car_folder in [x[0] for x in os.walk(BASEDIR + '/selfdrive/car')]:
try:
car_name = car_folder.split('/')[-1]
values = __import__('selfdrive.car.%s.values' % car_name, fromlist=['FINGERPRINTS'])
if hasattr(values, 'FINGERPRINTS'):
car_fingerprints = values.FINGERPRINTS
else:
continue
2018-07-12 18:52:06 -07:00
for f, v in car_fingerprints.items():
2018-06-16 20:59:34 -07:00
fingerprints[f] = v
except (ImportError, IOError):
pass
return fingerprints
_FINGERPRINTS = get_fingerprint_list()
2018-04-14 06:10:58 +00:00
2018-03-17 00:01:50 -07:00
_DEBUG_ADDRESS = {1880: 8} # reserved for debug purposes
2017-05-11 12:41:17 -07:00
2018-01-30 12:58:14 -08:00
def is_valid_for_fingerprint(msg, car_fingerprint):
adr = msg.address
2018-06-16 20:59:34 -07:00
bus = msg.src
# ignore addresses that are more than 11 bits
return (adr in car_fingerprint and car_fingerprint[adr] == len(msg.dat)) or \
bus != 0 or adr >= 0x800
2018-01-30 12:58:14 -08:00
2017-05-11 12:41:17 -07:00
def eliminate_incompatible_cars(msg, candidate_cars):
"""Removes cars that could not have sent msg.
Inputs:
msg: A cereal/log CanData message from the car.
candidate_cars: A list of cars to consider.
Returns:
A list containing the subset of candidate_cars that could have sent msg.
"""
compatible_cars = []
2018-06-16 20:59:34 -07:00
2017-05-11 12:41:17 -07:00
for car_name in candidate_cars:
2018-01-30 12:58:14 -08:00
car_fingerprints = _FINGERPRINTS[car_name]
for fingerprint in car_fingerprints:
2018-03-17 00:01:50 -07:00
fingerprint.update(_DEBUG_ADDRESS) # add alien debug address
2018-01-30 12:58:14 -08:00
if is_valid_for_fingerprint(msg, fingerprint):
compatible_cars.append(car_name)
break
2017-05-11 12:41:17 -07:00
return compatible_cars
2018-01-30 12:58:14 -08:00
2017-05-11 12:41:17 -07:00
def all_known_cars():
"""Returns a list of all known car strings."""
getting ready for Python 3 (#619) * tabs to spaces python 2 to 3: https://portingguide.readthedocs.io/en/latest/syntax.html#tabs-and-spaces * use the new except syntax python 2 to 3: https://portingguide.readthedocs.io/en/latest/exceptions.html#the-new-except-syntax * make relative imports absolute python 2 to 3: https://portingguide.readthedocs.io/en/latest/imports.html#absolute-imports * Queue renamed to queue in python 3 Use the six compatibility library to support both python 2 and 3: https://portingguide.readthedocs.io/en/latest/stdlib-reorg.html#renamed-modules * replace dict.has_key() with in python 2 to 3: https://portingguide.readthedocs.io/en/latest/dicts.html#removed-dict-has-key * make dict views compatible with python 3 python 2 to 3: https://portingguide.readthedocs.io/en/latest/dicts.html#dict-views-and-iterators Where needed, wrapping things that will be a view in python 3 with a list(). For example, if it's accessed with [] Python 3 has no iter*() methods, so just using the values() instead of itervalues() as long as it's not too performance intensive. Note that any minor performance hit of using a list instead of a view will go away when switching to python 3. If it is intensive, we could use the six version. * Explicitly use truncating division python 2 to 3: https://portingguide.readthedocs.io/en/latest/numbers.html#division python 3 treats / as float division. When we want the result to be an integer, use // * replace map() with list comprehension where a list result is needed. In python 3, map() returns an iterator. python 2 to 3: https://portingguide.readthedocs.io/en/latest/iterators.html#new-behavior-of-map-and-filter * replace filter() with list comprehension In python 3, filter() returns an interatoooooooooooor. python 2 to 3: https://portingguide.readthedocs.io/en/latest/iterators.html#new-behavior-of-map-and-filter * wrap zip() in list() where we need the result to be a list python 2 to 3: https://portingguide.readthedocs.io/en/latest/iterators.html#new-behavior-of-zip * clean out some lint Removes these pylint warnings: ************* Module selfdrive.car.chrysler.chryslercan W: 15, 0: Unnecessary semicolon (unnecessary-semicolon) W: 16, 0: Unnecessary semicolon (unnecessary-semicolon) W: 25, 0: Unnecessary semicolon (unnecessary-semicolon) ************* Module common.dbc W:101, 0: Anomalous backslash in string: '\?'. String constant might be missing an r prefix. (anomalous-backslash-in-string) ************* Module selfdrive.car.gm.interface R:102, 6: Redefinition of ret.minEnableSpeed type from float to int (redefined-variable-type) R:103, 6: Redefinition of ret.mass type from int to float (redefined-variable-type) ************* Module selfdrive.updated R: 20, 6: Redefinition of r type from int to str (redefined-variable-type)
2019-05-02 11:08:59 -07:00
return list(_FINGERPRINTS.keys())