From 70ef1c0fd57d7355794dbb80dca0176ee0875e77 Mon Sep 17 00:00:00 2001 From: Jason Wen Date: Fri, 6 Jun 2025 22:43:42 -0400 Subject: [PATCH] slight more cleanup --- .../mapd/live_map_data/base_map_data.py | 4 ++-- sunnypilot/mapd/live_map_data/osm_map_data.py | 20 ++++++++----------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/sunnypilot/mapd/live_map_data/base_map_data.py b/sunnypilot/mapd/live_map_data/base_map_data.py index fb0692b058..c95b51c05f 100644 --- a/sunnypilot/mapd/live_map_data/base_map_data.py +++ b/sunnypilot/mapd/live_map_data/base_map_data.py @@ -25,7 +25,7 @@ class BaseMapData(ABC): pass @abstractmethod - def get_next_speed_limit_and_distance(self) -> tuple[float, float]: + def get_next_speed_limit_and_distance(self, current_location: Coordinate | None) -> tuple[float, float]: pass @abstractmethod @@ -60,7 +60,7 @@ class BaseMapData(ABC): def publish(self) -> None: speed_limit = self.get_current_speed_limit() current_road_name = self.get_current_road_name() - next_speed_limit, next_speed_limit_distance = self.get_next_speed_limit_and_distance() + next_speed_limit, next_speed_limit_distance = self.get_next_speed_limit_and_distance(self._last_gps) mapd_sp_send = messaging.new_message('liveMapDataSP') mapd_sp_send.valid = self._is_gps_data_valid() diff --git a/sunnypilot/mapd/live_map_data/osm_map_data.py b/sunnypilot/mapd/live_map_data/osm_map_data.py index 2a82ae483e..8ea85b792b 100644 --- a/sunnypilot/mapd/live_map_data/osm_map_data.py +++ b/sunnypilot/mapd/live_map_data/osm_map_data.py @@ -9,31 +9,27 @@ from openpilot.sunnypilot.mapd.live_map_data.base_map_data import BaseMapData class OsmMapData(BaseMapData): def __init__(self): super().__init__() - self.last_gps = Coordinate(0.0, 0.0) self.params = Params() self.mem_params = Params("/dev/shm/params") if platform.system() != "Darwin" else self.params def update_location(self, current_location: Coordinate | None) -> None: - self.last_gps = current_location - if not self.last_gps: + if not current_location: return last_gps_position_for_osm = { - "latitude": self.last_gps.latitude, - "longitude": self.last_gps.longitude, - "bearing": self.last_gps.annotations.get("bearingDeg", 0) + "latitude": current_location.latitude, + "longitude": current_location.longitude, + "bearing": current_location.annotations.get("bearingDeg", 0) } self.mem_params.put("LastGPSPosition", json.dumps(last_gps_position_for_osm)) def get_current_speed_limit(self) -> float: - speed_limit = self.mem_params.get("MapSpeedLimit", encoding='utf8') - return float(speed_limit) if speed_limit else 0.0 + return float(self.mem_params.get("MapSpeedLimit", encoding='utf8') or 0.0) def get_current_road_name(self) -> str: - current_road_name = self.mem_params.get("RoadName", encoding='utf8') - return current_road_name if current_road_name else "" + return self.mem_params.get("RoadName", encoding='utf8') or "" - def get_next_speed_limit_and_distance(self) -> tuple[float, float]: + def get_next_speed_limit_and_distance(self, current_location: Coordinate | None) -> tuple[float, float]: next_speed_limit_section_str = self.mem_params.get("NextMapSpeedLimit", encoding='utf8') next_speed_limit_section = json.loads(next_speed_limit_section_str) if next_speed_limit_section_str else {} next_speed_limit = next_speed_limit_section.get('speedlimit', 0.0) @@ -43,6 +39,6 @@ class OsmMapData(BaseMapData): if next_speed_limit_latitude and next_speed_limit_longitude: next_speed_limit_coordinates = Coordinate(next_speed_limit_latitude, next_speed_limit_longitude) - next_speed_limit_distance = (self.last_gps or Coordinate(0, 0)).distance_to(next_speed_limit_coordinates) + next_speed_limit_distance = (current_location or Coordinate(0, 0)).distance_to(next_speed_limit_coordinates) return next_speed_limit, next_speed_limit_distance