From 5e9d7d47763d000f2d9383a4cfd6ca52c209b477 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 16 Feb 2024 18:43:17 -0600 Subject: [PATCH] wip --- common/prefix.py | 1 + common/watcher.py | 33 +++++++++++++++++++++++++++++++++ selfdrive/controls/controlsd.py | 5 +++++ 3 files changed, 39 insertions(+) create mode 100644 common/watcher.py diff --git a/common/prefix.py b/common/prefix.py index c1744e8..8f39141 100644 --- a/common/prefix.py +++ b/common/prefix.py @@ -4,6 +4,7 @@ import uuid from typing import Optional + from openpilot.common.params import Params from openpilot.system.hardware.hw import Paths diff --git a/common/watcher.py b/common/watcher.py new file mode 100644 index 0000000..1a943a6 --- /dev/null +++ b/common/watcher.py @@ -0,0 +1,33 @@ +import socket +import json + +# Initialize the socket without connecting +watcher_sock = None + +class Watcher: + def ensure_socket_connected(): + global watcher_sock + if watcher_sock is None or watcher_sock.fileno() == -1: # Checks if socket is not initialized or closed + try: + # Attempt to initialize and connect the socket + watcher_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + watcher_sock.connect("/tmp/oscar_watcher.sock") + except socket.error: + # If connection fails, set sock to None and do nothing + watcher_sock = None + + def log_watch(var_name, message): + global watcher_sock + ensure_socket_connected() # Ensure the socket is connected before attempting to log + if watcher_sock: # Proceed only if sock is not None (i.e., is connected) + message_json = json.dumps(message) + try: + watcher_sock.sendall(message_json.encode('utf-8') + b'\n') + except socket.error: + # Handle potential error in sending (e.g., if connection was lost) + watcher_sock.close() # Close the current socket to clean up resources + watcher_sock = None # Reset sock to ensure reconnection attempt on next call + +#message = {"variable": "car_stats", "value": {"car_lights_on": [3, 5, 7], "car_state": "on"}} +#log_watch(sock, message) + diff --git a/selfdrive/controls/controlsd.py b/selfdrive/controls/controlsd.py index 8e46743..fcf73c4 100644 --- a/selfdrive/controls/controlsd.py +++ b/selfdrive/controls/controlsd.py @@ -29,6 +29,8 @@ from openpilot.selfdrive.controls.lib.alertmanager import AlertManager, set_offr from openpilot.selfdrive.controls.lib.vehicle_model import VehicleModel from openpilot.system.hardware import HARDWARE +from openpilot.common.watcher import Watcher + SOFT_DISABLE_TIME = 3 # seconds LDW_MIN_SPEED = 31 * CV.MPH_TO_MS LANE_DEPARTURE_THRESHOLD = 0.1 @@ -131,6 +133,9 @@ class Controls: self.CP.alternativeExperience |= ALTERNATIVE_EXPERIENCE.RAISE_LONGITUDINAL_LIMITS_TO_ISO_MAX + # Test + Watcher.log_watch("always_on_lateral", self.always_on_lateral) + # read params self.is_metric = self.params.get_bool("IsMetric") self.is_ldw_enabled = self.params.get_bool("IsLdwEnabled")