diff --git a/sunnypilot/navd/navigationd.py b/sunnypilot/navd/navigationd.py index 7c43c4912c..73d41c0152 100755 --- a/sunnypilot/navd/navigationd.py +++ b/sunnypilot/navd/navigationd.py @@ -28,6 +28,7 @@ class Navigationd: self.recompute_allowed: bool = False self.allow_recompute: bool = False self.reroute_counter: int = 0 + self.cancel_route_counter: int = 0 self.frame: int = -1 self.last_position: Coordinate | None = None @@ -57,6 +58,12 @@ class Navigationd: self.route = self.nav_instructions.get_current_route() self.reroute_counter = 0 + if self.cancel_route_counter == 30: + self.nav_instructions.clear_route_cache() + self.params.put('MapboxSettings', {"navData": {"current": {}, "route": {}}}) + self.route = None + self.destination = None + self.valid = self.route is not None def _update_navigation(self) -> tuple[str, dict | None, dict]: @@ -74,15 +81,21 @@ class Navigationd: banner_instructions = parsed['maneuverPrimaryText'] nav_data['distance_from_route'] = progress['distance_from_route'] + large_distance = nav_data['distance_from_route'] > 100 + + if large_distance: + self.cancel_route_counter += 1 + if self.recompute_allowed: + self.reroute_counter += 1 + else: + self.cancel_route_counter = 0 + self.reroute_counter = 0 # Don't recompute in last segment to prevent reroute loops if self.route: if progress['current_step_idx'] == len(self.route['steps']) - 1: self.allow_recompute = False - if self.recompute_allowed: - self.reroute_counter += 1 if nav_data['distance_from_route'] > 100 else 0 - return banner_instructions, progress, nav_data def _build_navigation_message(self, banner_instructions: str, progress: dict | None, nav_data: dict, valid: bool): diff --git a/sunnypilot/navd/tests/test_navigationd.py b/sunnypilot/navd/tests/test_navigationd.py index aa7c45d3e7..ab84a64f63 100644 --- a/sunnypilot/navd/tests/test_navigationd.py +++ b/sunnypilot/navd/tests/test_navigationd.py @@ -59,3 +59,12 @@ class TestNavigationd: assert received_msg.bannerInstructions == msg.navigationd.bannerInstructions assert received_msg.valid == msg.navigationd.valid + + def test_cancel_route(self): + nav = Navigationd() + nav.last_position = Coordinate(latitude=37.0, longitude=128.0) + nav.route = {'580 Winchester dr, oxnard, CA': True} + nav.cancel_route_counter = 30 + nav._update_params() + assert nav.route == None + assert nav.destination == None