Skip to content

Commit

Permalink
cleanup old sm pm args (commaai#30241)
Browse files Browse the repository at this point in the history
* cleanup sm pm

* fix controlsd

* fix
  • Loading branch information
adeebshihadeh authored Oct 14, 2023
1 parent 9e24a11 commit b68cfbb
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 64 deletions.
36 changes: 15 additions & 21 deletions selfdrive/controls/controlsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,39 +56,33 @@


class Controls:
def __init__(self, sm=None, pm=None, can_sock=None, CI=None):
def __init__(self, CI=None):
config_realtime_process(4, Priority.CTRL_HIGH)

# Ensure the current branch is cached, otherwise the first iteration of controlsd lags
self.branch = get_short_branch("")

# Setup sockets
self.pm = pm
if self.pm is None:
self.pm = messaging.PubMaster(['sendcan', 'controlsState', 'carState',
'carControl', 'carEvents', 'carParams'])
self.pm = messaging.PubMaster(['sendcan', 'controlsState', 'carState',
'carControl', 'carEvents', 'carParams'])

self.sensor_packets = ["accelerometer", "gyroscope"]
self.camera_packets = ["roadCameraState", "driverCameraState", "wideRoadCameraState"]

self.can_sock = can_sock
if can_sock is None:
can_timeout = None if os.environ.get('NO_CAN_TIMEOUT', False) else 20
self.can_sock = messaging.sub_sock('can', timeout=can_timeout)
can_timeout = None if os.environ.get('NO_CAN_TIMEOUT', False) else 20
self.can_sock = messaging.sub_sock('can', timeout=can_timeout)

self.log_sock = messaging.sub_sock('androidLog')

self.params = Params()
self.sm = sm
if self.sm is None:
ignore = self.sensor_packets + ['testJoystick']
if SIMULATION:
ignore += ['driverCameraState', 'managerState']
self.sm = messaging.SubMaster(['deviceState', 'pandaStates', 'peripheralState', 'modelV2', 'liveCalibration',
'driverMonitoringState', 'longitudinalPlan', 'lateralPlan', 'liveLocationKalman',
'managerState', 'liveParameters', 'radarState', 'liveTorqueParameters',
'testJoystick'] + self.camera_packets + self.sensor_packets,
ignore_alive=ignore, ignore_avg_freq=['radarState', 'testJoystick'])
ignore = self.sensor_packets + ['testJoystick']
if SIMULATION:
ignore += ['driverCameraState', 'managerState']
self.sm = messaging.SubMaster(['deviceState', 'pandaStates', 'peripheralState', 'modelV2', 'liveCalibration',
'driverMonitoringState', 'longitudinalPlan', 'lateralPlan', 'liveLocationKalman',
'managerState', 'liveParameters', 'radarState', 'liveTorqueParameters',
'testJoystick'] + self.camera_packets + self.sensor_packets,
ignore_alive=ignore, ignore_avg_freq=['radarState', 'testJoystick'])

if CI is None:
# wait for one pandaState and one CAN packet
Expand Down Expand Up @@ -879,8 +873,8 @@ def controlsd_thread(self):
self.prof.display()


def main(sm=None, pm=None, logcan=None):
controls = Controls(sm, pm, logcan)
def main():
controls = Controls()
controls.controlsd_thread()


Expand Down
15 changes: 6 additions & 9 deletions selfdrive/controls/plannerd.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def publish_ui_plan(sm, pm, lateral_planner, longitudinal_planner):
uiPlan.accel = longitudinal_planner.a_desired_trajectory_full.tolist()
pm.send('uiPlan', ui_send)

def plannerd_thread(sm=None, pm=None):
def plannerd_thread():
config_realtime_process(5, Priority.CTRL_LOW)

cloudlog.info("plannerd is waiting for CarParams")
Expand All @@ -41,12 +41,9 @@ def plannerd_thread(sm=None, pm=None):
longitudinal_planner = LongitudinalPlanner(CP)
lateral_planner = LateralPlanner(CP, debug=debug_mode)

if sm is None:
sm = messaging.SubMaster(['carControl', 'carState', 'controlsState', 'radarState', 'modelV2'],
poll=['radarState', 'modelV2'], ignore_avg_freq=['radarState'])

if pm is None:
pm = messaging.PubMaster(['longitudinalPlan', 'lateralPlan', 'uiPlan'])
pm = messaging.PubMaster(['longitudinalPlan', 'lateralPlan', 'uiPlan'])
sm = messaging.SubMaster(['carControl', 'carState', 'controlsState', 'radarState', 'modelV2'],
poll=['radarState', 'modelV2'], ignore_avg_freq=['radarState'])

while True:
sm.update()
Expand All @@ -58,8 +55,8 @@ def plannerd_thread(sm=None, pm=None):
longitudinal_planner.publish(sm, pm)
publish_ui_plan(sm, pm, lateral_planner, longitudinal_planner)

def main(sm=None, pm=None):
plannerd_thread(sm, pm)
def main():
plannerd_thread()


if __name__ == "__main__":
Expand Down
5 changes: 2 additions & 3 deletions selfdrive/locationd/laikad.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def clear_tmp_cache():
os.mkdir(Paths.download_cache_root())


def main(sm=None, pm=None):
def main():
#clear_tmp_cache()

use_qcom = not Params().get_bool("UbloxAvailable")
Expand All @@ -449,8 +449,7 @@ def main(sm=None, pm=None):
else:
raw_name = "ubloxGnss"
raw_gnss_sock = messaging.sub_sock(raw_name, conflate=False)
if pm is None:
pm = messaging.PubMaster(['gnssMeasurements'])
pm = messaging.PubMaster(['gnssMeasurements'])

# disable until set as main gps source, to better analyze startup time
# TODO ensure low CPU usage before enabling
Expand Down
8 changes: 3 additions & 5 deletions selfdrive/locationd/paramsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,14 @@ def check_valid_with_hysteresis(current_valid: bool, val: float, threshold: floa
return current_valid


def main(sm=None, pm=None):
def main():
config_realtime_process([0, 1, 2, 3], 5)

DEBUG = bool(int(os.getenv("DEBUG", "0")))
REPLAY = bool(int(os.getenv("REPLAY", "0")))

if sm is None:
sm = messaging.SubMaster(['liveLocationKalman', 'carState'], poll=['liveLocationKalman'])
if pm is None:
pm = messaging.PubMaster(['liveParameters'])
pm = messaging.PubMaster(['liveParameters'])
sm = messaging.SubMaster(['liveLocationKalman', 'carState'], poll=['liveLocationKalman'])

params_reader = Params()
# wait for stats about the car to come in from controls
Expand Down
9 changes: 3 additions & 6 deletions selfdrive/locationd/torqued.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,11 @@ def get_msg(self, valid=True, with_points=False):
return msg


def main(sm=None, pm=None):
def main():
config_realtime_process([0, 1, 2, 3], 5)

if sm is None:
sm = messaging.SubMaster(['carControl', 'carState', 'liveLocationKalman'], poll=['liveLocationKalman'])

if pm is None:
pm = messaging.PubMaster(['liveTorqueParameters'])
pm = messaging.PubMaster(['liveTorqueParameters'])
sm = messaging.SubMaster(['carControl', 'carState', 'liveLocationKalman'], poll=['liveLocationKalman'])

params = Params()
with car.CarParams.from_bytes(params.get("CarParams", block=True)) as CP:
Expand Down
13 changes: 5 additions & 8 deletions selfdrive/monitoring/dmonitoringd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@
from openpilot.selfdrive.monitoring.driver_monitor import DriverStatus


def dmonitoringd_thread(sm=None, pm=None):
def dmonitoringd_thread():
gc.disable()
set_realtime_priority(2)

if pm is None:
pm = messaging.PubMaster(['driverMonitoringState'])

if sm is None:
sm = messaging.SubMaster(['driverStateV2', 'liveCalibration', 'carState', 'controlsState', 'modelV2'], poll=['driverStateV2'])
pm = messaging.PubMaster(['driverMonitoringState'])
sm = messaging.SubMaster(['driverStateV2', 'liveCalibration', 'carState', 'controlsState', 'modelV2'], poll=['driverStateV2'])

driver_status = DriverStatus(rhd_saved=Params().get_bool("IsRhdDetected"))

Expand Down Expand Up @@ -89,8 +86,8 @@ def dmonitoringd_thread(sm=None, pm=None):
driver_status.wheel_on_right == (driver_status.wheelpos_learner.filtered_stat.M > driver_status.settings._WHEELPOS_THRESHOLD)):
put_bool_nonblocking("IsRhdDetected", driver_status.wheel_on_right)

def main(sm=None, pm=None):
dmonitoringd_thread(sm, pm)
def main():
dmonitoringd_thread()


if __name__ == '__main__':
Expand Down
8 changes: 3 additions & 5 deletions selfdrive/navd/navd.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,9 @@ def should_recompute(self):
# TODO: Check for going wrong way in segment


def main(sm=None, pm=None):
if sm is None:
sm = messaging.SubMaster(['liveLocationKalman', 'managerState'])
if pm is None:
pm = messaging.PubMaster(['navInstruction', 'navRoute'])
def main():
pm = messaging.PubMaster(['navInstruction', 'navRoute'])
sm = messaging.SubMaster(['liveLocationKalman', 'managerState'])

rk = Ratekeeper(1.0)
route_engine = RouteEngine(sm, pm)
Expand Down
11 changes: 4 additions & 7 deletions system/micd.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def apply_a_weighting(measurements: np.ndarray) -> np.ndarray:


class Mic:
def __init__(self, pm):
self.pm = pm
def __init__(self):
self.rk = Ratekeeper(RATE)
self.pm = messaging.PubMaster(['microphone'])

self.measurements = np.empty(0)

Expand Down Expand Up @@ -93,11 +93,8 @@ def micd_thread(self):
self.update()


def main(pm=None):
if pm is None:
pm = messaging.PubMaster(['microphone'])

mic = Mic(pm)
def main():
mic = Mic()
mic.micd_thread()


Expand Down

0 comments on commit b68cfbb

Please sign in to comment.