Skip to content

Commit

Permalink
generic
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Dec 8, 2024
1 parent 7aa447d commit 1fd0382
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
53 changes: 53 additions & 0 deletions common/parameter_updater.py
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 3 additions & 4 deletions selfdrive/car/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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()
Expand Down
33 changes: 0 additions & 33 deletions selfdrive/car/parameter_updater.py

This file was deleted.

0 comments on commit 1fd0382

Please sign in to comment.