Skip to content

Commit

Permalink
improve thread-safety with ParameterUpdater class
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Dec 8, 2024
1 parent 7558108 commit 4a6a539
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions selfdrive/car/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,35 @@ def can_send(msgs: list[CanData]) -> None:
return can_recv, can_send


class ParameterUpdater:
def __init__(self):
self.params = Params()
self.mutex = threading.Lock()
self.is_metric = False
self.experimental_mode = False

self._update() # Initial update
self.stop_event = threading.Event()
self.update_thread = threading.Thread(target=self._update_periodically)
self.update_thread.start()

def stop(self):
self.stop_event.set()
self.update_thread.join()

def _update(self):
is_metric = self.params.get_bool("IsMetric")
experimental_mode = self.params.get_bool("ExperimentalMode")
with self.mutex:
self.is_metric = is_metric
self.experimental_mode = experimental_mode

def _update_periodically(self):
while not self.stop_event.is_set():
self._update()
time.sleep(0.1)


class Car:
CI: CarInterfaceBase
RI: RadarInterfaceBase
Expand Down Expand Up @@ -149,8 +178,8 @@ def __init__(self, CI=None, RI=None) -> None:
self.mock_carstate = MockCarState()
self.v_cruise_helper = VCruiseHelper(self.CP)

self.is_metric = self.params.get_bool("IsMetric")
self.experimental_mode = self.params.get_bool("ExperimentalMode")
self.is_metric = False
self.experimental_mode = False

# card is driven by can recv, expected at 100Hz
self.rk = Ratekeeper(100, print_delay_threshold=None)
Expand Down Expand Up @@ -250,23 +279,18 @@ def step(self):

self.initialized_prev = initialized

def params_thread(self, evt):
while not evt.is_set():
self.is_metric = self.params.get_bool("IsMetric")
self.experimental_mode = self.params.get_bool("ExperimentalMode") and self.CP.openpilotLongitudinalControl
time.sleep(0.1)

def card_thread(self):
e = threading.Event()
t = threading.Thread(target=self.params_thread, args=(e, ))
parameter_updater = ParameterUpdater()
try:
t.start()
while True:
with parameter_updater.mutex:
self.is_metric = parameter_updater.is_metric
self.experimental_mode = parameter_updater.experimental_mode

self.step()
self.rk.monitor_time()
finally:
e.set()
t.join()
parameter_updater.stop()


def main():
Expand Down

0 comments on commit 4a6a539

Please sign in to comment.