Skip to content

Commit

Permalink
use ParameterUpdater in selfdrived
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Dec 8, 2024
1 parent 1fd0382 commit ff659a1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
24 changes: 12 additions & 12 deletions common/parameter_updater.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
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]):
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.param_values = {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
self.update_thread: threading.Thread | None = None

def start(self) -> None:
if self.update_thread is None or not self.update_thread.is_alive():
def start(self):
if self.update_thread is None:
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():
def stop(self):
if self.update_thread:
self.stop_event.set()
self.update_thread.join()

def get_param_value(self, param: str) -> Union[bool, str, None]:
def get_param_value(self, param: str):
with self.mutex:
return self.param_values.get(param)

def _update(self) -> None:
new_values: Dict[str, Union[bool, str]] = {}
def _update(self):
new_values: dict[str, bool | str | None] = {}
for param, param_type in self.params_to_update.items():
if param_type == "bool":
new_values[param] = self.params.get_bool(param)
Expand All @@ -47,7 +47,7 @@ def _update(self) -> None:
with self.mutex:
self.param_values = new_values

def _update_periodically(self) -> None:
def _update_periodically(self):
while not self.stop_event.is_set():
self._update()
time.sleep(0.1)
16 changes: 12 additions & 4 deletions selfdrive/car/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,21 @@ def step(self):

self.initialized_prev = initialized

def read_parameters(self, parameter_updater: ParameterUpdater) -> None:
self.is_metric = parameter_updater.get_param_value('is_metric')
self.experimental_mode = parameter_updater.get_param_value('experimental_mode')

def card_thread(self):
parameter_updater = ParameterUpdater()
params_to_update = {
'IsMetric': 'bool',
'ExperimentalMode': 'bool',
}
parameter_updater = ParameterUpdater(params_to_update)
parameter_updater.start()

try:
while True:
self.is_metric = parameter_updater.get_param_value('is_metric')
self.experimental_mode = parameter_updater.get_param_value('experimental_mode')

self.read_parameters(parameter_updater)
self.step()
self.rk.monitor_time()
finally:
Expand Down
37 changes: 18 additions & 19 deletions selfdrive/selfdrived/selfdrived.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env python3
import os
import time
import threading

import cereal.messaging as messaging

Expand All @@ -10,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, DT_CTRL
from openpilot.common.swaglog import cloudlog
Expand Down Expand Up @@ -84,7 +83,7 @@ def __init__(self, CP=None):
ignore_valid=ignore, frequency=int(1/DT_CTRL))

# read params
self.is_metric = self.params.get_bool("IsMetric")
self.is_metric = False
self.is_ldw_enabled = self.params.get_bool("IsLdwEnabled")

car_recognized = self.CP.carName != 'mock'
Expand All @@ -111,7 +110,7 @@ def __init__(self, CP=None):
self.logged_comm_issue = None
self.not_running_prev = None
self.experimental_mode = False
self.personality = self.read_personality_param()
self.personality = log.LongitudinalPersonality.standard
self.recalibrating_seen = False
self.state_machine = StateMachine()
self.rk = Ratekeeper(100, print_delay_threshold=None)
Expand Down Expand Up @@ -462,30 +461,30 @@ def step(self):

self.CS_prev = CS

def read_personality_param(self):
def read_parameters(self, parameter_updater: ParameterUpdater) -> None:
self.is_metric = parameter_updater.get_param_value("IsMetric")
self.experimental_mode = parameter_updater.get_param_value("ExperimentalMode") and self.CP.openpilotLongitudinalControl
try:
return int(self.params.get('LongitudinalPersonality'))
self.personality = int(parameter_updater.get_param_value('LongitudinalPersonality'))
except (ValueError, TypeError):
return log.LongitudinalPersonality.standard

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
self.personality = self.read_personality_param()
time.sleep(0.1)
self.personality = log.LongitudinalPersonality.standard

def run(self):
e = threading.Event()
t = threading.Thread(target=self.params_thread, args=(e, ))
params_to_update = {
'IsMetric': 'bool',
'ExperimentalMode': 'bool',
'LongitudinalPersonality': 'str'
}
parameter_updater = ParameterUpdater(params_to_update)
parameter_updater.start()

try:
t.start()
while True:
self.read_parameters(parameter_updater)
self.step()
self.rk.monitor_time()
finally:
e.set()
t.join()
parameter_updater.stop()


def main():
Expand Down

0 comments on commit ff659a1

Please sign in to comment.