|
|
|
#!/usr/bin/env python
|
|
|
|
import os
|
|
|
|
import zmq
|
|
|
|
import time
|
|
|
|
from cereal import car
|
|
|
|
from selfdrive.can.parser import CANParser
|
|
|
|
from common.realtime import sec_since_boot
|
|
|
|
from selfdrive.services import service_list
|
|
|
|
import selfdrive.messaging as messaging
|
|
|
|
|
|
|
|
|
|
|
|
def _create_nidec_can_parser():
|
|
|
|
dbc_f = 'acura_ilx_2016_nidec.dbc'
|
|
|
|
radar_messages = [0x400] + range(0x430, 0x43A) + range(0x440, 0x446)
|
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)
6 years ago
|
|
|
signals = list(zip(['RADAR_STATE'] +
|
|
|
|
['LONG_DIST'] * 16 + ['NEW_TRACK'] * 16 + ['LAT_DIST'] * 16 +
|
|
|
|
['REL_SPEED'] * 16,
|
|
|
|
[0x400] + radar_messages[1:] * 4,
|
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)
6 years ago
|
|
|
[0] + [255] * 16 + [1] * 16 + [0] * 16 + [0] * 16))
|
|
|
|
checks = list(zip([0x445], [20]))
|
|
|
|
|
|
|
|
return CANParser(os.path.splitext(dbc_f)[0], signals, checks, 1)
|
|
|
|
|
|
|
|
|
|
|
|
class RadarInterface(object):
|
|
|
|
def __init__(self, CP):
|
|
|
|
# radar
|
|
|
|
self.pts = {}
|
|
|
|
self.track_id = 0
|
|
|
|
self.radar_fault = False
|
|
|
|
self.radar_wrong_config = False
|
|
|
|
self.radar_off_can = CP.radarOffCan
|
|
|
|
|
|
|
|
self.delay = 0.1 # Delay of radar
|
|
|
|
|
|
|
|
# Nidec
|
|
|
|
self.rcp = _create_nidec_can_parser()
|
|
|
|
|
|
|
|
context = zmq.Context()
|
|
|
|
self.logcan = messaging.sub_sock(context, service_list['can'].port)
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
canMonoTimes = []
|
|
|
|
|
|
|
|
updated_messages = set()
|
|
|
|
ret = car.RadarData.new_message()
|
|
|
|
|
|
|
|
# in Bosch radar and we are only steering for now, so sleep 0.05s to keep
|
|
|
|
# radard at 20Hz and return no points
|
|
|
|
if self.radar_off_can:
|
|
|
|
time.sleep(0.05)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
while 1:
|
|
|
|
tm = int(sec_since_boot() * 1e9)
|
|
|
|
_, vls = self.rcp.update(tm, True)
|
|
|
|
updated_messages.update(vls)
|
|
|
|
if 0x445 in updated_messages:
|
|
|
|
break
|
|
|
|
|
|
|
|
for ii in updated_messages:
|
|
|
|
cpt = self.rcp.vl[ii]
|
|
|
|
if ii == 0x400:
|
|
|
|
# check for radar faults
|
|
|
|
self.radar_fault = cpt['RADAR_STATE'] != 0x79
|
|
|
|
self.radar_wrong_config = cpt['RADAR_STATE'] == 0x69
|
|
|
|
elif cpt['LONG_DIST'] < 255:
|
|
|
|
if ii not in self.pts or cpt['NEW_TRACK']:
|
|
|
|
self.pts[ii] = car.RadarData.RadarPoint.new_message()
|
|
|
|
self.pts[ii].trackId = self.track_id
|
|
|
|
self.track_id += 1
|
|
|
|
self.pts[ii].dRel = cpt['LONG_DIST'] # from front of car
|
|
|
|
self.pts[ii].yRel = -cpt['LAT_DIST'] # in car frame's y axis, left is positive
|
|
|
|
self.pts[ii].vRel = cpt['REL_SPEED']
|
|
|
|
self.pts[ii].aRel = float('nan')
|
|
|
|
self.pts[ii].yvRel = float('nan')
|
|
|
|
self.pts[ii].measured = True
|
|
|
|
else:
|
|
|
|
if ii in self.pts:
|
|
|
|
del self.pts[ii]
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
if not self.rcp.can_valid:
|
|
|
|
errors.append("commIssue")
|
|
|
|
if self.radar_fault:
|
|
|
|
errors.append("fault")
|
|
|
|
if self.radar_wrong_config:
|
|
|
|
errors.append("wrongConfig")
|
|
|
|
ret.errors = errors
|
|
|
|
ret.canMonoTimes = canMonoTimes
|
|
|
|
|
|
|
|
ret.points = self.pts.values()
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
class CarParams:
|
|
|
|
radarOffCan = False
|
|
|
|
|
|
|
|
RI = RadarInterface(CarParams)
|
|
|
|
while 1:
|
|
|
|
ret = RI.update()
|
|
|
|
print(chr(27) + "[2J")
|
|
|
|
print(ret)
|