mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-12-02 06:03:41 +08:00
disengaged path (#39453)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import numpy as np
|
||||
from openpilot.starpilot.ui.raylib import rl
|
||||
from openpilot.cereal import messaging
|
||||
from openpilot.cereal import log, messaging
|
||||
from opendbc.car.structs import car
|
||||
from openpilot.common.params import Params
|
||||
from openpilot.starpilot.ui.path_markers import PathMarkers, valid_lead_numbers
|
||||
@@ -82,6 +82,7 @@ class ModelRenderer(PathMarkers, ModelGeometry, Widget):
|
||||
colors=[],
|
||||
stops=[],
|
||||
)
|
||||
self._disengaged_path_alpha = FirstOrderFilter(0.0, 0.1, 1 / gui_app.target_fps)
|
||||
self._rainbow_path = RainbowPath()
|
||||
self.road_style = {}
|
||||
|
||||
@@ -142,14 +143,43 @@ class ModelRenderer(PathMarkers, ModelGeometry, Widget):
|
||||
self._update_model(lead_one, path_x_array, update_lane_geometry=model_updated or self._transform_dirty)
|
||||
self._transform_dirty = False
|
||||
|
||||
active = self._visual_status() != UIStatus.DISENGAGED
|
||||
available = active or self._can_show_disengaged_path(sm)
|
||||
self._update_markers(sm)
|
||||
|
||||
# Draw elements (hide when disengaged)
|
||||
if self._visual_status() != UIStatus.DISENGAGED:
|
||||
if active:
|
||||
self._disengaged_path_alpha.x = 0.0
|
||||
self._draw_lane_lines()
|
||||
self._draw_path(sm)
|
||||
else:
|
||||
self._draw_disengaged_path(available)
|
||||
self._draw_markers()
|
||||
|
||||
@staticmethod
|
||||
def _can_show_disengaged_path(sm) -> bool:
|
||||
cp = ui_state.CP
|
||||
if not ui_state.started or cp is None or cp.passive or not sm.all_checks(['carState', 'modelV2', 'extrinsicsCalibration']):
|
||||
return False
|
||||
cs = sm['carState']
|
||||
return (sm['extrinsicsCalibration'].calStatus == log.ExtrinsicsCalibration.Status.calibrated and
|
||||
cs.canValid and not cs.canTimeout and
|
||||
cs.gearShifter in (car.CarState.GearShifter.drive, car.CarState.GearShifter.low) and
|
||||
not (cs.parkingBrake or cs.accFaulted or cs.steerFaultPermanent or cs.espDisabled or cs.invalidLkasSetting))
|
||||
|
||||
def _draw_disengaged_path(self, available: bool) -> None:
|
||||
if not available:
|
||||
self._disengaged_path_alpha.x = 0.0
|
||||
return
|
||||
alpha = round(128 * self._disengaged_path_alpha.update(True))
|
||||
path = self._path.projected_points
|
||||
if alpha <= 1 or len(path) < 4 or len(path) % 2 or not np.isfinite(path).all():
|
||||
return
|
||||
left, right = np.split(path, 2)
|
||||
inner_left = .75 * left + .25 * right[::-1]
|
||||
inner_right = .75 * right + .25 * left[::-1]
|
||||
points = np.vstack((inner_left, inner_right)) + np.array(self._draw_offset(), dtype=np.float32)
|
||||
draw_polygon(self._rect, points, rl.Color(0, 0, 0, alpha))
|
||||
|
||||
def _update_model(self, lead, path_x_array, *, update_lane_geometry: bool = True):
|
||||
"""Update model visualization data based on model message"""
|
||||
max_distance = np.clip(path_x_array[-1], MIN_DRAW_DISTANCE, MAX_DRAW_DISTANCE)
|
||||
|
||||
@@ -222,3 +222,42 @@ def test_replay_missing_longitudinal_plan_uses_neutral_path_feedback(monkeypatch
|
||||
renderer._draw_path(sm)
|
||||
renderer._blend_filter.update.assert_not_called()
|
||||
assert rgba(draw.call_args.args[2]) == (255, 255, 255, 30)
|
||||
|
||||
|
||||
def test_compact_disengaged_path_narrows_around_original_center():
|
||||
renderer = object.__new__(compact.ModelRenderer)
|
||||
renderer._rect = rl.Rectangle(10, 20, 476, 240)
|
||||
renderer._path = compact.ModelPoints()
|
||||
renderer._path.projected_points = np.array([[0, 20], [0, 10], [20, 10], [20, 20]], dtype=np.float32)
|
||||
renderer._disengaged_path_alpha = compact.FirstOrderFilter(0., 0.1, 1 / 60)
|
||||
with patch.object(compact, 'draw_polygon') as draw:
|
||||
renderer._draw_disengaged_path(False)
|
||||
draw.assert_not_called()
|
||||
renderer._draw_disengaged_path(True)
|
||||
_, points, color = draw.call_args.args
|
||||
np.testing.assert_array_equal(points, [[15, 40], [15, 30], [25, 30], [25, 40]])
|
||||
assert rgba(color)[:3] == (0, 0, 0) and color.a > 0
|
||||
|
||||
|
||||
def test_compact_available_path_requires_fresh_drive_calibration_and_hardware():
|
||||
from openpilot.cereal import log
|
||||
from opendbc.car.structs import car
|
||||
|
||||
cs = car.CarState.new_message(canValid=True, gearShifter=car.CarState.GearShifter.drive)
|
||||
calibration = log.ExtrinsicsCalibration.new_message(calStatus=log.ExtrinsicsCalibration.Status.calibrated)
|
||||
services = {'carState': cs, 'extrinsicsCalibration': calibration}
|
||||
from unittest.mock import MagicMock
|
||||
sm = MagicMock()
|
||||
sm.__getitem__.side_effect = services.__getitem__
|
||||
sm.all_checks.return_value = True
|
||||
with patch.object(compact, 'ui_state', NS(started=True, CP=NS(passive=False))):
|
||||
assert compact.ModelRenderer._can_show_disengaged_path(sm)
|
||||
cs.brakePressed = True
|
||||
assert compact.ModelRenderer._can_show_disengaged_path(sm)
|
||||
cs.gearShifter = car.CarState.GearShifter.reverse
|
||||
assert not compact.ModelRenderer._can_show_disengaged_path(sm)
|
||||
cs.gearShifter = car.CarState.GearShifter.drive
|
||||
calibration.calStatus = log.ExtrinsicsCalibration.Status.uncalibrated
|
||||
assert not compact.ModelRenderer._can_show_disengaged_path(sm)
|
||||
sm.all_checks.return_value = False
|
||||
assert not compact.ModelRenderer._can_show_disengaged_path(sm)
|
||||
|
||||
Reference in New Issue
Block a user