Skip to content
Permalink
Browse files

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)
  • Loading branch information...
adhintz authored and rbiasini committed May 2, 2019
1 parent 2f92d57 commit 9dae0bfac4e54ec2b2e488d2b4ead1495c8f56d8
Showing with 114 additions and 116 deletions.
  1. +2 −2 common/dbc.py
  2. +1 −1 common/fingerprints.py
  3. +2 −2 common/sympy_helpers.py
  4. +4 −4 common/transformations/camera.py
  5. +3 −3 common/transformations/model.py
  6. +5 −5 selfdrive/athena/athenad.py
  7. +4 −7 selfdrive/boardd/boardd.py
  8. +1 −1 selfdrive/can/packer.py
  9. +1 −1 selfdrive/can/parser.py
  10. +2 −2 selfdrive/can/process_dbc.py
  11. +3 −3 selfdrive/car/chrysler/chryslercan.py
  12. +1 −1 selfdrive/car/chrysler/chryslercan_test.py
  13. +6 −6 selfdrive/car/chrysler/radar_interface.py
  14. +4 −4 selfdrive/car/ford/radar_interface.py
  15. +4 −4 selfdrive/car/gm/carcontroller.py
  16. +7 −7 selfdrive/car/gm/interface.py
  17. +2 −2 selfdrive/car/gm/radar_interface.py
  18. +3 −3 selfdrive/car/honda/carcontroller.py
  19. +3 −3 selfdrive/car/honda/radar_interface.py
  20. +1 −1 selfdrive/car/subaru/subarucan.py
  21. +4 −4 selfdrive/car/toyota/carcontroller.py
  22. +6 −5 selfdrive/car/toyota/radar_interface.py
  23. +1 −1 selfdrive/controls/lib/latcontrol_helpers.py
  24. +4 −4 selfdrive/controls/lib/pathplanner.py
  25. +1 −1 selfdrive/controls/lib/planner.py
  26. +1 −1 selfdrive/controls/radard.py
  27. +5 −5 selfdrive/locationd/calibrationd.py
  28. +3 −3 selfdrive/locationd/kalman/loc_local_kf.py
  29. +2 −2 selfdrive/locationd/kalman/loc_local_model.py
  30. +2 −2 selfdrive/locationd/test/ci_test.py
  31. +2 −2 selfdrive/locationd/test/ublox.py
  32. +2 −2 selfdrive/locationd/test/ubloxd.py
  33. +2 −2 selfdrive/locationd/test/ubloxd_easy.py
  34. +2 −2 selfdrive/locationd/test/ubloxd_py_test.py
  35. +1 −1 selfdrive/manager.py
  36. +1 −1 selfdrive/mapd/default_speeds_generator.py
  37. +5 −5 selfdrive/mapd/mapd.py
  38. +2 −2 selfdrive/mapd/mapd_helpers.py
  39. +2 −2 selfdrive/test/plant/maneuver.py
  40. +1 −1 selfdrive/test/plant/plant.py
  41. +3 −3 selfdrive/test/plant/plant_ui.py
  42. +3 −3 selfdrive/updated.py
@@ -98,7 +98,7 @@ def __init__(self, fn):
sgname = dat.group(2)
defvals = dat.group(3)

defvals = defvals.replace("?","\?") #escape sequence in C++
defvals = defvals.replace("?",r"\?") #escape sequence in C++
defvals = defvals.split('"')[:-1]

defs = defvals[1::2]
@@ -112,7 +112,7 @@ def __init__(self, fn):

self.def_vals[ids].append((sgname, defvals))

for msg in self.msgs.viewvalues():
for msg in self.msgs.values():
msg[1].sort(key=lambda x: x.start_bit)

self.msg_name_to_address = {}
@@ -61,4 +61,4 @@ def eliminate_incompatible_cars(msg, candidate_cars):

def all_known_cars():
"""Returns a list of all known car strings."""
return _FINGERPRINTS.keys()
return list(_FINGERPRINTS.keys())
@@ -75,7 +75,7 @@ def sympy_into_c(sympy_functions):
routines.append(r)

[(c_name, c_code), (h_name, c_header)] = codegen.get_code_generator('C', 'ekf', 'C99').write(routines, "ekf")
c_code = '\n'.join(filter(lambda x: len(x) > 0 and x[0] != '#', c_code.split("\n")))
c_header = '\n'.join(filter(lambda x: len(x) > 0 and x[0] != '#', c_header.split("\n")))
c_code = '\n'.join(x for x in c_code.split("\n") if len(x) > 0 and x[0] != '#')
c_header = '\n'.join(x for x in c_header.split("\n") if len(x) > 0 and x[0] != '#')

return c_header, c_code
@@ -15,13 +15,13 @@


leon_dcam_intrinsics = np.array([
[650, 0, 816/2],
[ 0, 650, 612/2],
[650, 0, 816//2],
[ 0, 650, 612//2],
[ 0, 0, 1]])

eon_dcam_intrinsics = np.array([
[860, 0, 1152/2],
[ 0, 860, 864/2],
[860, 0, 1152//2],
[ 0, 860, 864//2],
[ 0, 0, 1]])

# aka 'K_inv' aka view_frame_from_camera_frame
@@ -1,9 +1,9 @@
import numpy as np

from common.transformations.camera import eon_focal_length, \
vp_from_ke, \
get_view_frame_from_road_frame, \
FULL_FRAME_SIZE
vp_from_ke, \
get_view_frame_from_road_frame, \
FULL_FRAME_SIZE

# segnet

@@ -6,7 +6,7 @@
import threading
import traceback
import zmq
import Queue
import six.moves.queue
from jsonrpc import JSONRPCResponseManager, dispatcher
from websocket import create_connection, WebSocketTimeoutException

@@ -21,8 +21,8 @@
HANDLER_THREADS = os.getenv('HANDLER_THREADS', 4)

dispatcher["echo"] = lambda s: s
payload_queue = Queue.Queue()
response_queue = Queue.Queue()
payload_queue = six.moves.queue.Queue()
response_queue = six.moves.queue.Queue()

def handle_long_poll(ws):
end_event = threading.Event()
@@ -52,7 +52,7 @@ def jsonrpc_handler(end_event):
data = payload_queue.get(timeout=1)
response = JSONRPCResponseManager.handle(data, dispatcher)
response_queue.put_nowait(response)
except Queue.Empty:
except six.moves.queue.Empty:
pass
except Exception as e:
cloudlog.exception("athena jsonrpc handler failed")
@@ -87,7 +87,7 @@ def ws_send(ws, end_event):
try:
response = response_queue.get(timeout=1)
ws.send(response.json)
except Queue.Empty:
except six.moves.queue.Empty:
pass
except Exception:
traceback.print_exc()
@@ -127,16 +127,13 @@ def boardd_mock_loop():

while 1:
tsc = messaging.drain_sock(logcan, wait_for_one=True)
snds = map(lambda x: can_capnp_to_can_list(x.can), tsc)
snd = []
for s in snds:
snd += s
snd = filter(lambda x: x[-1] <= 1, snd)
can_send_many(snd)
snds = [can_capnp_to_can_list(x.can) for x in tsc]
snds = [x for x in snds if x[-1] <= 1]
can_send_many(snds)

# recv @ 100hz
can_msgs = can_recv()
print("sent %d got %d" % (len(snd), len(can_msgs)))
print("sent %d got %d" % (len(snds), len(can_msgs)))
m = can_list_to_can_capnp(can_msgs)
sendcan.send(m.to_bytes())

@@ -20,7 +20,7 @@ def __init__(self, dbc_name):

def pack(self, addr, values, counter):
values_thing = []
for name, value in values.iteritems():
for name, value in values.items():
if name not in self.sig_names:
self.sig_names[name] = ffi.new("char[]", name)

@@ -58,7 +58,7 @@ def __init__(self, dbc_name, signals, checks=None, bus=0, sendcan=False, tcp_add
{
'address': msg_address,
'check_frequency': freq,
} for msg_address, freq in message_options.iteritems()])
} for msg_address, freq in message_options.items()])

self.can = libdbc.can_init(bus, dbc_name, len(message_options_c), message_options_c,
len(signal_options_c), signal_options_c, sendcan, tcp_addr)
@@ -39,10 +39,10 @@ def main():
continue #skip output is newer than template and dbc

msgs = [(address, msg_name, msg_size, sorted(msg_sigs, key=lambda s: s.name not in ("COUNTER", "CHECKSUM"))) # process counter and checksums first
for address, ((msg_name, msg_size), msg_sigs) in sorted(can_dbc.msgs.iteritems()) if msg_sigs]
for address, ((msg_name, msg_size), msg_sigs) in sorted(can_dbc.msgs.items()) if msg_sigs]

def_vals = {a: set(b) for a,b in can_dbc.def_vals.items()} #remove duplicates
def_vals = [(address, sig) for address, sig in sorted(def_vals.iteritems())]
def_vals = [(address, sig) for address, sig in sorted(def_vals.items())]

if can_dbc.name.startswith("honda") or can_dbc.name.startswith("acura"):
checksum_type = "honda"
@@ -12,8 +12,8 @@ def calc_checksum(data):
end_index = len(data)
index = 0
checksum = 0xFF
temp_chk = 0;
bit_sum = 0;
temp_chk = 0
bit_sum = 0
if(end_index <= index):
return False
for index in range(0, end_index):
@@ -22,7 +22,7 @@ def calc_checksum(data):
iterate = 8
while(iterate > 0):
iterate -= 1
bit_sum = curr & shift;
bit_sum = curr & shift
temp_chk = checksum & 0x80
if (bit_sum != 0):
bit_sum = 0x1C
@@ -1,4 +1,4 @@
import chryslercan
from selfdrive.car.chrysler import chryslercan
from selfdrive.can.packer import CANPacker

from cereal import car
@@ -25,29 +25,29 @@ def _create_radard_can_parser():

# The factor and offset are applied by the dbc parsing library, so the
# default values should be after the factor/offset are applied.
signals = zip(['LONG_DIST'] * msg_n +
signals = list(zip(['LONG_DIST'] * msg_n +
['LAT_DIST'] * msg_n +
['REL_SPEED'] * msg_n,
RADAR_MSGS_C * 2 + # LONG_DIST, LAT_DIST
RADAR_MSGS_D, # REL_SPEED
[0] * msg_n + # LONG_DIST
[-1000] * msg_n + # LAT_DIST
[-146.278] * msg_n) # REL_SPEED set to 0, factor/offset to this
[-146.278] * msg_n)) # REL_SPEED set to 0, factor/offset to this
# TODO what are the checks actually used for?
# honda only checks the last message,
# toyota checks all the messages. Which do we want?
checks = zip(RADAR_MSGS_C +
checks = list(zip(RADAR_MSGS_C +
RADAR_MSGS_D,
[20]*msg_n + # 20Hz (0.05s)
[20]*msg_n) # 20Hz (0.05s)
[20]*msg_n)) # 20Hz (0.05s)

return CANParser(os.path.splitext(dbc_f)[0], signals, checks, 1)

def _address_to_track(address):
if address in RADAR_MSGS_C:
return (address - RADAR_MSGS_C[0]) / 2
return (address - RADAR_MSGS_C[0]) // 2
if address in RADAR_MSGS_D:
return (address - RADAR_MSGS_D[0]) / 2
return (address - RADAR_MSGS_D[0]) // 2
raise ValueError("radar received unexpected address %d" % address)

class RadarInterface(object):
@@ -14,10 +14,10 @@
def _create_radard_can_parser():
dbc_f = 'ford_fusion_2018_adas.dbc'
msg_n = len(RADAR_MSGS)
signals = zip(['X_Rel'] * msg_n + ['Angle'] * msg_n + ['V_Rel'] * msg_n,
RADAR_MSGS * 3,
[0] * msg_n + [0] * msg_n + [0] * msg_n)
checks = zip(RADAR_MSGS, [20]*msg_n)
signals = list(zip(['X_Rel'] * msg_n + ['Angle'] * msg_n + ['V_Rel'] * msg_n,
RADAR_MSGS * 3,
[0] * msg_n + [0] * msg_n + [0] * msg_n))
checks = list(zip(RADAR_MSGS, [20]*msg_n))

return CANParser(os.path.splitext(dbc_f)[0], signals, checks, 1)

@@ -104,7 +104,7 @@ def update(self, sendcan, enabled, CS, frame, actuators, \
apply_steer = 0

self.apply_steer_last = apply_steer
idx = (frame / P.STEER_STEP) % 4
idx = (frame // P.STEER_STEP) % 4

if self.car_fingerprint in SUPERCRUISE_CARS:
can_sends += gmcan.create_steering_control_ct6(self.packer_pt,
@@ -134,7 +134,7 @@ def update(self, sendcan, enabled, CS, frame, actuators, \

# Gas/regen and brakes - all at 25Hz
if (frame % 4) == 0:
idx = (frame / 4) % 4
idx = (frame // 4) % 4

at_full_stop = enabled and CS.standstill
near_stop = enabled and (CS.v_ego < P.NEAR_STOP_BRAKE_PHASE)
@@ -153,13 +153,13 @@ def update(self, sendcan, enabled, CS, frame, actuators, \
tt = sec_since_boot()

if frame % time_and_headlights_step == 0:
idx = (frame / time_and_headlights_step) % 4
idx = (frame // time_and_headlights_step) % 4
can_sends.append(gmcan.create_adas_time_status(canbus.obstacle, int((tt - self.start_time) * 60), idx))
can_sends.append(gmcan.create_adas_headlights_status(canbus.obstacle))

speed_and_accelerometer_step = 2
if frame % speed_and_accelerometer_step == 0:
idx = (frame / speed_and_accelerometer_step) % 4
idx = (frame // speed_and_accelerometer_step) % 4
can_sends.append(gmcan.create_adas_steering_status(canbus.obstacle, idx))
can_sends.append(gmcan.create_adas_accelerometer_speed_status(canbus.obstacle, CS.v_ego, idx))

@@ -70,7 +70,7 @@ def get_params(candidate, fingerprint):
# supports stop and go, but initial engage must be above 18mph (which include conservatism)
ret.minEnableSpeed = 18 * CV.MPH_TO_MS
# kg of standard extra cargo to count for driver, gas, etc...
ret.mass = 1607 + std_cargo
ret.mass = 1607. + std_cargo
ret.safetyModel = car.CarParams.SafetyModels.gm
ret.wheelbase = 2.69
ret.steerRatio = 15.7
@@ -80,7 +80,7 @@ def get_params(candidate, fingerprint):
elif candidate == CAR.MALIBU:
# supports stop and go, but initial engage must be above 18mph (which include conservatism)
ret.minEnableSpeed = 18 * CV.MPH_TO_MS
ret.mass = 1496 + std_cargo
ret.mass = 1496. + std_cargo
ret.safetyModel = car.CarParams.SafetyModels.gm
ret.wheelbase = 2.83
ret.steerRatio = 15.8
@@ -89,7 +89,7 @@ def get_params(candidate, fingerprint):

elif candidate == CAR.HOLDEN_ASTRA:
# kg of standard extra cargo to count for driver, gas, etc...
ret.mass = 1363 + std_cargo
ret.mass = 1363. + std_cargo
ret.wheelbase = 2.662
# Remaining parameters copied from Volt for now
ret.centerToFront = ret.wheelbase * 0.4
@@ -99,7 +99,7 @@ def get_params(candidate, fingerprint):
ret.steerRatioRear = 0.

elif candidate == CAR.ACADIA:
ret.minEnableSpeed = -1 # engage speed is decided by pcm
ret.minEnableSpeed = -1. # engage speed is decided by pcm
ret.mass = 4353. * CV.LB_TO_KG + std_cargo
ret.safetyModel = car.CarParams.SafetyModels.gm
ret.wheelbase = 2.86
@@ -118,7 +118,7 @@ def get_params(candidate, fingerprint):

elif candidate == CAR.CADILLAC_ATS:
ret.minEnableSpeed = 18 * CV.MPH_TO_MS
ret.mass = 1601 + std_cargo
ret.mass = 1601. + std_cargo
ret.safetyModel = car.CarParams.SafetyModels.gm
ret.wheelbase = 2.78
ret.steerRatio = 15.3
@@ -127,7 +127,7 @@ def get_params(candidate, fingerprint):

elif candidate == CAR.CADILLAC_CT6:
# engage speed is decided by pcm
ret.minEnableSpeed = -1
ret.minEnableSpeed = -1.
# kg of standard extra cargo to count for driver, gas, etc...
ret.mass = 4016. * CV.LB_TO_KG + std_cargo
ret.safetyModel = car.CarParams.SafetyModels.cadillac
@@ -218,7 +218,7 @@ def update(self, c):
ret.gasPressed = self.CS.user_gas_pressed

# brake pedal
ret.brake = self.CS.user_brake / 0xd0
ret.brake = self.CS.user_brake // 0xd0
ret.brakePressed = self.CS.brake_pressed

# steering wheel
@@ -25,7 +25,7 @@ def create_radard_can_parser(canbus, car_fingerprint):
if car_fingerprint in (CAR.VOLT, CAR.MALIBU, CAR.HOLDEN_ASTRA, CAR.ACADIA, CAR.CADILLAC_ATS):
# C1A-ARS3-A by Continental
radar_targets = range(SLOT_1_MSG, SLOT_1_MSG + NUM_SLOTS)
signals = zip(['FLRRNumValidTargets',
signals = list(zip(['FLRRNumValidTargets',
'FLRRSnsrBlckd', 'FLRRYawRtPlsblityFlt',
'FLRRHWFltPrsntInt', 'FLRRAntTngFltPrsnt',
'FLRRAlgnFltPrsnt', 'FLRRSnstvFltPrsntInt'] +
@@ -36,7 +36,7 @@ def create_radard_can_parser(canbus, car_fingerprint):
[0] * 7 +
[0.0] * NUM_SLOTS + [0.0] * NUM_SLOTS +
[0.0] * NUM_SLOTS + [0.0] * NUM_SLOTS +
[0.0] * NUM_SLOTS + [0] * NUM_SLOTS)
[0.0] * NUM_SLOTS + [0] * NUM_SLOTS))

checks = []

@@ -133,7 +133,7 @@ def update(self, sendcan, enabled, CS, frame, actuators, \
# **** process the car messages ****

# *** compute control surfaces ***
BRAKE_MAX = 1024/4
BRAKE_MAX = 1024//4
if CS.CP.carFingerprint in (CAR.ACURA_ILX):
STEER_MAX = 0xF00
elif CS.CP.carFingerprint in (CAR.CRV, CAR.ACURA_RDX):
@@ -160,7 +160,7 @@ def update(self, sendcan, enabled, CS, frame, actuators, \

# Send dashboard UI commands.
if (frame % 10) == 0:
idx = (frame/10) % 4
idx = (frame//10) % 4
can_sends.extend(hondacan.create_ui_commands(self.packer, pcm_speed, hud, CS.CP.carFingerprint, idx))

if CS.CP.radarOffCan:
@@ -173,7 +173,7 @@ def update(self, sendcan, enabled, CS, frame, actuators, \
else:
# Send gas and brake commands.
if (frame % 2) == 0:
idx = frame / 2
idx = frame // 2
pump_on, self.last_pump_ts = brake_pump_hysteresys(apply_brake, self.apply_brake_last, self.last_pump_ts)
can_sends.append(hondacan.create_brake_command(self.packer, apply_brake, pump_on,
pcm_override, pcm_cancel_cmd, hud.chime, hud.fcw, idx))
@@ -12,12 +12,12 @@
def _create_nidec_can_parser():
dbc_f = 'acura_ilx_2016_nidec.dbc'
radar_messages = [0x400] + range(0x430, 0x43A) + range(0x440, 0x446)
signals = zip(['RADAR_STATE'] +
signals = list(zip(['RADAR_STATE'] +
['LONG_DIST'] * 16 + ['NEW_TRACK'] * 16 + ['LAT_DIST'] * 16 +
['REL_SPEED'] * 16,
[0x400] + radar_messages[1:] * 4,
[0] + [255] * 16 + [1] * 16 + [0] * 16 + [0] * 16)
checks = zip([0x445], [20])
[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)

@@ -13,7 +13,7 @@ def create_steering_control(packer, car_fingerprint, apply_steer, frame, steer_s

if car_fingerprint == CAR.IMPREZA:
#counts from 0 to 15 then back to 0 + 16 for enable bit
idx = ((frame / steer_step) % 16)
idx = ((frame // steer_step) % 16)

values = {
"Counter": idx,
Oops, something went wrong.

0 comments on commit 9dae0bf

Please sign in to comment.
You can’t perform that action at this time.