From 1fd0382b9804e2cd55d646031d3d1d8c3d44ca69 Mon Sep 17 00:00:00 2001 From: deanlee Date: Mon, 9 Dec 2024 03:24:12 +0800 Subject: [PATCH] generic --- common/parameter_updater.py | 53 ++++++++++++++++++++++++++++++ selfdrive/car/card.py | 7 ++-- selfdrive/car/parameter_updater.py | 33 ------------------- 3 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 common/parameter_updater.py delete mode 100644 selfdrive/car/parameter_updater.py diff --git a/common/parameter_updater.py b/common/parameter_updater.py new file mode 100644 index 00000000000000..b38635874d6243 --- /dev/null +++ b/common/parameter_updater.py @@ -0,0 +1,53 @@ +import threading +import time +from typing import Dict, Union +from openpilot.common.params import Params + + +class ParameterUpdater: + def __init__(self, params_to_update: Dict[str, str]): + """ + params_to_update: A dictionary where keys are parameter names, and values are their types ('bool' or 'str'). + Example: {"IsMetric": "bool", "LongitudinalPersonality": "str"} + """ + self.params = Params() + self.params_to_update = params_to_update + self.param_values: Dict[str, Union[bool, str]] = {param: None for param in params_to_update} + + self._update() # Initial update + + self.mutex = threading.Lock() + self.stop_event = threading.Event() + self.update_thread = None + + def start(self) -> None: + if self.update_thread is None or not self.update_thread.is_alive(): + self.update_thread = threading.Thread(target=self._update_periodically, daemon=True) + self.update_thread.start() + + def stop(self) -> None: + if self.update_thread and self.update_thread.is_alive(): + self.stop_event.set() + self.update_thread.join() + + def get_param_value(self, param: str) -> Union[bool, str, None]: + with self.mutex: + return self.param_values.get(param) + + def _update(self) -> None: + new_values: Dict[str, Union[bool, str]] = {} + for param, param_type in self.params_to_update.items(): + if param_type == "bool": + new_values[param] = self.params.get_bool(param) + elif param_type == "str": + new_values[param] = self.params.get(param) + else: + raise ValueError(f"Unsupported type {param_type} for parameter {param}") + + with self.mutex: + self.param_values = new_values + + def _update_periodically(self) -> None: + while not self.stop_event.is_set(): + self._update() + time.sleep(0.1) diff --git a/selfdrive/car/card.py b/selfdrive/car/card.py index 4b2a9b8b58e37c..030b8650b14875 100755 --- a/selfdrive/car/card.py +++ b/selfdrive/car/card.py @@ -8,6 +8,7 @@ from panda import ALTERNATIVE_EXPERIENCE +from openpilot.common.parameter_updater import ParameterUpdater from openpilot.common.params import Params from openpilot.common.realtime import config_realtime_process, Priority, Ratekeeper from openpilot.common.swaglog import cloudlog, ForwardingHandler @@ -20,7 +21,6 @@ from openpilot.selfdrive.pandad import can_capnp_to_list, can_list_to_can_capnp from openpilot.selfdrive.car.cruise import VCruiseHelper from openpilot.selfdrive.car.car_specific import MockCarState -from openpilot.selfdrive.car.parameter_updater import ParameterUpdater REPLAY = "REPLAY" in os.environ @@ -254,9 +254,8 @@ def card_thread(self): parameter_updater = ParameterUpdater() try: while True: - with parameter_updater.mutex: - self.is_metric = parameter_updater.is_metric - self.experimental_mode = parameter_updater.experimental_mode + self.is_metric = parameter_updater.get_param_value('is_metric') + self.experimental_mode = parameter_updater.get_param_value('experimental_mode') self.step() self.rk.monitor_time() diff --git a/selfdrive/car/parameter_updater.py b/selfdrive/car/parameter_updater.py deleted file mode 100644 index 7f4df28adec9dd..00000000000000 --- a/selfdrive/car/parameter_updater.py +++ /dev/null @@ -1,33 +0,0 @@ -import threading -import time - -from openpilot.common.params import Params - - -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)