from cereal import car
VisualAlert = car . CarControl . HUDControl . VisualAlert
AudibleAlert = car . CarControl . HUDControl . AudibleAlert
def calc_checksum ( data ) :
""" This function does not want the checksum byte in the input data.
jeep chrysler canbus checksum from http : / / illmatics . com / Remote % 20 Car % 20 Hacking . pdf
"""
end_index = len ( data )
index = 0
checksum = 0xFF
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
temp_chk = 0
bit_sum = 0
if ( end_index < = index ) :
return False
for index in range ( 0 , end_index ) :
shift = 0x80
curr = data [ index ]
iterate = 8
while ( iterate > 0 ) :
iterate - = 1
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
bit_sum = curr & shift
temp_chk = checksum & 0x80
if ( bit_sum != 0 ) :
bit_sum = 0x1C
if ( temp_chk != 0 ) :
bit_sum = 1
checksum = checksum << 1
temp_chk = checksum | 1
bit_sum ^ = temp_chk
else :
if ( temp_chk != 0 ) :
bit_sum = 0x1D
checksum = checksum << 1
bit_sum ^ = checksum
checksum = bit_sum
shift = shift >> 1
return ~ checksum & 0xFF
def make_can_msg ( addr , dat ) :
return [ addr , 0 , dat , 0 ]
def create_lkas_hud ( packer , gear , lkas_active , hud_alert , hud_count , lkas_car_model ) :
# LKAS_HUD 0x2a6 (678) Controls what lane-keeping icon is displayed.
if hud_alert == VisualAlert . steerRequired :
msg = ' 0000000300000000 ' . decode ( ' hex ' )
return make_can_msg ( 0x2a6 , msg )
color = 1 # default values are for park or neutral in 2017 are 0 0, but trying 1 1 for 2019
lines = 1
alerts = 0
if hud_count < ( 1 * 4 ) : # first 3 seconds, 4Hz
alerts = 1
# CAR.PACIFICA_2018_HYBRID and CAR.PACIFICA_2019_HYBRID
# had color = 1 and lines = 1 but trying 2017 hybrid style for now.
if gear in ( ' drive ' , ' reverse ' , ' low ' ) :
if lkas_active :
color = 2 # control active, display green.
lines = 6
else :
color = 1 # control off, display white.
lines = 1
values = {
" LKAS_ICON_COLOR " : color , # byte 0, last 2 bits
" CAR_MODEL " : lkas_car_model , # byte 1
" LKAS_LANE_LINES " : lines , # byte 2, last 4 bits
" LKAS_ALERTS " : alerts , # byte 3, last 4 bits
}
return packer . make_can_msg ( " LKAS_HUD " , 0 , values ) # 0x2a6
def create_lkas_command ( packer , apply_steer , moving_fast , frame ) :
# LKAS_COMMAND 0x292 (658) Lane-keeping signal to turn the wheel.
values = {
" LKAS_STEERING_TORQUE " : apply_steer ,
" LKAS_HIGH_TORQUE " : int ( moving_fast ) ,
" COUNTER " : frame % 0x10 ,
}
dat = packer . make_can_msg ( " LKAS_COMMAND " , 0 , values ) [ 2 ]
dat = [ ord ( i ) for i in dat ] [ : - 1 ]
checksum = calc_checksum ( dat )
values [ " CHECKSUM " ] = checksum
return packer . make_can_msg ( " LKAS_COMMAND " , 0 , values )
def create_chimes ( audible_alert ) :
# '0050' nothing, chime '4f55'
if audible_alert == AudibleAlert . none :
msg = ' 0050 ' . decode ( ' hex ' )
else :
msg = ' 4f55 ' . decode ( ' hex ' )
return make_can_msg ( 0x339 , msg )
def create_wheel_buttons ( frame ) :
# WHEEL_BUTTONS (571) Message sent to cancel ACC.
start = [ 0x01 ] # acc cancel set
counter = ( frame % 10 ) << 4
dat = start + [ counter ]
dat = dat + [ calc_checksum ( dat ) ]
return make_can_msg ( 0x23b , str ( bytearray ( dat ) ) )