mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-12-02 03:03:41 +08:00
Track sustained lane-position error through curves
This commit is contained in:
@@ -5,6 +5,7 @@ This module neither reads settings nor sends commands.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from collections import deque
|
||||
from enum import StrEnum
|
||||
import math
|
||||
from numbers import Real
|
||||
@@ -38,6 +39,11 @@ E2E_BREAK_IN_START = 0.15
|
||||
E2E_BREAK_IN_FULL = 0.50
|
||||
ACQUIRE_TAU = 0.4
|
||||
RELEASE_TAU = 0.2
|
||||
POSITION_ACQUIRE_SECONDS = 0.5
|
||||
POSITION_GAIN = 0.5
|
||||
POSITION_MAX_JERK = 0.1
|
||||
POSITION_TRACKING_ERROR = 0.1
|
||||
POSITION_ACQUIRE_ERROR = 0.2
|
||||
|
||||
|
||||
class ControlMode(StrEnum):
|
||||
@@ -267,6 +273,10 @@ def _reader_geometry_key(model):
|
||||
class LaneCenteringController:
|
||||
def __init__(self, *, body: BodyDimensions | None = None, body_required: bool = False) -> None:
|
||||
self._correction = 0.0
|
||||
self._position_trim = 0.0
|
||||
self._position_age = 0.0
|
||||
self._position_clock = 0.0
|
||||
self._command_history: deque[tuple[float, float]] = deque(maxlen=150)
|
||||
self._geometry_cache = None
|
||||
self._tracker = SingleLineTracker()
|
||||
self._body = body
|
||||
@@ -275,9 +285,20 @@ class LaneCenteringController:
|
||||
|
||||
def reset(self) -> None:
|
||||
self._correction = 0.0
|
||||
self._position_trim = 0.0
|
||||
self._position_age = 0.0
|
||||
self._position_clock = 0.0
|
||||
self._command_history.clear()
|
||||
self._geometry_cache = None
|
||||
self._tracker.reset()
|
||||
|
||||
def _remember_command(self, curvature: float) -> None:
|
||||
sample = (self._position_clock, curvature)
|
||||
if self._command_history and self._command_history[-1][0] == self._position_clock:
|
||||
self._command_history[-1] = sample
|
||||
else:
|
||||
self._command_history.append(sample)
|
||||
|
||||
def _result(self, base: float, reason: str) -> LaneCenteringResult:
|
||||
if self._body is not None and self._correction != 0.:
|
||||
source = self._clearance_observation
|
||||
@@ -286,12 +307,17 @@ class LaneCenteringController:
|
||||
guarded = (0. if measured is None or delay is None else
|
||||
limit_correction(source.model, base, self._correction, measured, source.speed_mps, delay, self._body))
|
||||
if guarded != self._correction:
|
||||
self._position_trim *= abs(guarded / self._correction)
|
||||
self._correction = guarded
|
||||
reason = 'clearance_limited'
|
||||
correction = self._correction
|
||||
self._remember_command(base + correction)
|
||||
return LaneCenteringResult(base + correction, correction, (correction > 0) - (correction < 0), reason)
|
||||
|
||||
def _fade(self, base: float, dt: float, reason: str) -> LaneCenteringResult:
|
||||
self._position_trim = 0.0
|
||||
self._position_age = 0.0
|
||||
self._command_history.clear()
|
||||
self._correction *= math.exp(-dt / RELEASE_TAU)
|
||||
if abs(self._correction) < 1e-12:
|
||||
self._correction = 0.0
|
||||
@@ -315,6 +341,7 @@ class LaneCenteringController:
|
||||
self.reset()
|
||||
return base
|
||||
if base == candidate or self._body is None and self._tracker.path is None:
|
||||
self._remember_command(candidate)
|
||||
return candidate
|
||||
checked_delay = _finite_number(delay)
|
||||
delta = candidate - base
|
||||
@@ -325,7 +352,10 @@ class LaneCenteringController:
|
||||
allowed = limit_correction(model, base, allowed, measured, speed, checked_delay, self._body)
|
||||
if allowed != delta:
|
||||
self._correction *= abs(allowed / delta)
|
||||
return base + allowed
|
||||
self._position_trim *= abs(allowed / delta)
|
||||
final = base + allowed
|
||||
self._remember_command(final)
|
||||
return final
|
||||
|
||||
def update(self, observation: LaneCenteringInput) -> LaneCenteringResult:
|
||||
if not isinstance(observation, LaneCenteringInput):
|
||||
@@ -371,6 +401,7 @@ class LaneCenteringController:
|
||||
if settings.pause_on_signal and observation.turn_signal_active:
|
||||
self._tracker.reset()
|
||||
return self._fade(base, dt, "signal_release")
|
||||
self._position_clock += dt
|
||||
# Filtering runs per control tick, including repeated model samples.
|
||||
geometry = _reader_geometry_key(observation.model)
|
||||
cache_key = (observation.model_sample_id, geometry)
|
||||
@@ -396,7 +427,39 @@ class LaneCenteringController:
|
||||
measured = _finite_number(observation.measured_curvature)
|
||||
delay = _finite_number(observation.lateral_delay_s)
|
||||
if measured is not None and delay is not None:
|
||||
target = self._tracking_correction(base, target, measured, speed, delay, damp_recovery=True)
|
||||
if self._tracker.side is None and self._tracker.width is not None and self._tracker.path is not None:
|
||||
near = _at(self._tracker.path, 0.)
|
||||
if near is not None:
|
||||
bound = min(MAX_OFFSET, max(0., self._tracker.width * .5 - MIN_CENTER_TO_LINE))
|
||||
position_error = near + clip_scalar(offset, -bound, bound)
|
||||
self._position_age = self._position_age + dt if abs(position_error) > POSITION_ACQUIRE_ERROR or self._position_trim != 0. else 0.
|
||||
error = math.copysign(max(0., abs(position_error) - CENTER_ERROR_DEADBAND), position_error)
|
||||
unattenuated = raw_correction(observation.model, speed, offset, 0., _paths=paths)
|
||||
weight = 1. if unattenuated == 0. else abs(raw / unattenuated) if unattenuated is not None else 0.
|
||||
if weight == 0.:
|
||||
self._position_trim = 0.
|
||||
self._position_age = 0.
|
||||
delayed = next((command for stamp, command in reversed(self._command_history) if stamp <= self._position_clock - delay), None)
|
||||
tracking_error = (delayed - measured) * speed * speed if delayed is not None else None
|
||||
if (self._position_age >= POSITION_ACQUIRE_SECONDS and tracking_error is not None and
|
||||
(error * tracking_error <= 0. or abs(tracking_error) <= POSITION_TRACKING_ERROR)):
|
||||
limit = MAX_CORRECTION * speed * speed
|
||||
rate = clip_scalar(error * POSITION_GAIN * weight * strength / STRENGTH_BASELINE, -POSITION_MAX_JERK, POSITION_MAX_JERK)
|
||||
self._position_trim = clip_scalar(self._position_trim + rate * dt, -limit, limit)
|
||||
target = clip_scalar(target + self._position_trim / speed / speed, -MAX_CORRECTION, MAX_CORRECTION)
|
||||
else:
|
||||
self._position_trim = 0.
|
||||
self._position_age = 0.
|
||||
else:
|
||||
self._position_trim = 0.
|
||||
self._position_age = 0.
|
||||
allowed = self._tracking_correction(base, target, measured, speed, delay, damp_recovery=True)
|
||||
if allowed != target and target != 0.:
|
||||
self._position_trim *= abs(allowed / target)
|
||||
target = allowed
|
||||
else:
|
||||
self._position_trim = 0.
|
||||
self._position_age = 0.
|
||||
step = (target - self._correction) * (1.0 - math.exp(-dt / ACQUIRE_TAU))
|
||||
max_step = MAX_CORRECTION_JERK * dt / speed / speed
|
||||
self._correction += clip_scalar(step, -max_step, max_step)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""Native model-message fixtures for the standalone curvature contribution."""
|
||||
from dataclasses import replace
|
||||
from collections import deque
|
||||
import json
|
||||
import math
|
||||
from pathlib import Path
|
||||
@@ -48,6 +49,147 @@ def settled(controller, source, ticks=300):
|
||||
return result
|
||||
|
||||
|
||||
class TestCurvePositionFeedback(unittest.TestCase):
|
||||
@staticmethod
|
||||
def source(displacement=.6, curvature=.003, *, authority=.5):
|
||||
m = model()
|
||||
x = np.asarray(XS)
|
||||
center = .5 * curvature * x ** 2 - displacement
|
||||
for index, shift in ((1, -1.8), (2, 1.8)):
|
||||
m.laneLines[index].y = (center + shift).tolist()
|
||||
m.position.y = (.5 * curvature * x ** 2).tolist()
|
||||
return observation(m, base_curvature=curvature, measured_curvature=curvature,
|
||||
speed_mps=25., lateral_delay_s=.1,
|
||||
settings=LaneCenteringSettings(True, e2e_authority=authority))
|
||||
|
||||
@staticmethod
|
||||
def run_fresh(controller, source, ticks, *, following=True):
|
||||
result = None
|
||||
start = (controller._tracker.last_id or 0) + 1
|
||||
for tick in range(ticks):
|
||||
current = replace(source, model_sample_id=start + tick // 5)
|
||||
if following and source.measured_curvature is not None:
|
||||
current = replace(current, measured_curvature=source.base_curvature + controller._correction)
|
||||
result = controller.update(current)
|
||||
assert result is not None
|
||||
return result
|
||||
|
||||
def test_sustained_curve_position_error_extends_the_partial_path_nudge(self):
|
||||
for sign in (-1., 1.):
|
||||
source = self.source(sign * .6, sign * .003)
|
||||
controller = LaneCenteringController()
|
||||
result = self.run_fresh(controller, source, 1000)
|
||||
raw = raw_correction(source.model, source.speed_mps, 0., .5)
|
||||
assert raw is not None
|
||||
self.assertLess(sign * result.correction, 0.)
|
||||
self.assertGreater(abs(result.correction), abs(raw) * .3 * source.settings.strength * 1.1)
|
||||
self.assertLessEqual(abs(result.correction), MAX_CORRECTION)
|
||||
|
||||
def test_mild_position_error_does_not_build_a_trim(self):
|
||||
source = self.source(.12)
|
||||
controller = LaneCenteringController()
|
||||
self.run_fresh(controller, source, 1000)
|
||||
self.assertEqual(controller._position_trim, 0.)
|
||||
|
||||
def test_position_trim_does_not_wind_up_behind_the_steering_response(self):
|
||||
source = replace(self.source(), measured_curvature=.005)
|
||||
controller = LaneCenteringController()
|
||||
self.run_fresh(controller, source, 1000, following=False)
|
||||
self.assertEqual(controller._position_trim, 0.)
|
||||
|
||||
def test_full_breakaway_releases_a_previously_acquired_trim(self):
|
||||
source = self.source()
|
||||
controller = LaneCenteringController()
|
||||
self.run_fresh(controller, source, 300)
|
||||
self.assertLess(controller._position_trim, 0.)
|
||||
source = replace(source, settings=replace(source.settings, e2e_authority=1.))
|
||||
result = self.run_fresh(controller, source, 1000)
|
||||
self.assertEqual(controller._position_trim, 0.)
|
||||
self.assertAlmostEqual(result.correction, 0., delta=1e-10)
|
||||
|
||||
def test_feedback_cannot_wind_up_through_withdrawal_or_missing_motion(self):
|
||||
for reason in ('override', 'signal', 'confidence', 'motion', 'inactive'):
|
||||
source = self.source()
|
||||
controller = LaneCenteringController()
|
||||
self.run_fresh(controller, source, 300)
|
||||
self.assertLess(controller._position_trim, 0.)
|
||||
if reason == 'override':
|
||||
source = replace(source, driver_override=True)
|
||||
elif reason == 'signal':
|
||||
source = replace(source, turn_signal_active=True)
|
||||
elif reason == 'confidence':
|
||||
source.model.laneLineProbs = [.0, .1, .1, .0]
|
||||
elif reason == 'motion':
|
||||
source = replace(source, lateral_delay_s=None)
|
||||
else:
|
||||
source = replace(source, lateral_active=False)
|
||||
controller.update(replace(source, model_sample_id=1000))
|
||||
self.assertEqual(controller._position_trim, 0.)
|
||||
|
||||
def test_planner_recovery_discards_opposing_position_memory(self):
|
||||
from openpilot.starpilot.lateral.lane_clearance import _center_motion
|
||||
source = self.source()
|
||||
controller = LaneCenteringController()
|
||||
self.run_fresh(controller, source, 300)
|
||||
source = self.source(0.)
|
||||
x, y, _ = _center_motion(source.base_curvature, source.base_curvature, source.speed_mps, .1)
|
||||
x, y = np.r_[0., x, 50.], np.r_[0., y, y[-1]]
|
||||
for index, shift in ((1, -1.8), (2, 1.8)):
|
||||
source.model.laneLines[index].x = x.tolist()
|
||||
source.model.laneLines[index].y = (y + shift).tolist()
|
||||
result = controller.update(replace(source, model_sample_id=1000))
|
||||
assert result.candidate_curvature is not None
|
||||
assert source.measured_curvature is not None and source.lateral_delay_s is not None
|
||||
final = controller.guard_applied(source.model, source.base_curvature, result.candidate_curvature,
|
||||
source.measured_curvature, source.speed_mps, source.lateral_delay_s)
|
||||
self.assertAlmostEqual(final, source.base_curvature, delta=1e-8)
|
||||
self.assertAlmostEqual(controller._position_trim, 0., delta=1e-5)
|
||||
|
||||
def test_feedback_build_and_reversal_retain_the_acceleration_rate_bound(self):
|
||||
controller = LaneCenteringController()
|
||||
previous = 0.
|
||||
for tick in range(900):
|
||||
source = self.source(.8 if tick < 450 else -.8)
|
||||
result = controller.update(replace(source, model_sample_id=tick + 1))
|
||||
self.assertLessEqual(abs(result.correction), MAX_CORRECTION)
|
||||
self.assertLessEqual(abs(result.correction - previous) * source.speed_mps ** 2,
|
||||
source.elapsed_seconds + 1e-12)
|
||||
previous = result.correction
|
||||
|
||||
def test_delayed_curve_tracking_settles_a_biased_planner_within_the_quiet_zone(self):
|
||||
for speed in (25., 35.):
|
||||
for sign in (-1., 1.):
|
||||
with self.subTest(speed=speed, direction=sign):
|
||||
controller = LaneCenteringController()
|
||||
road, bias = sign * .003, sign * .6
|
||||
offset, heading, measured = bias, 0., road
|
||||
base = road
|
||||
commands = deque([road] * 20)
|
||||
x = np.asarray(XS)
|
||||
m = model()
|
||||
for tick in range(2000):
|
||||
if tick % 5 == 0:
|
||||
center = .5 * road * x ** 2 - offset - heading * x
|
||||
for index, shift in ((1, -1.8), (2, 1.8)):
|
||||
m.laneLines[index].y = (center + shift).tolist()
|
||||
lookahead = speed * 1.2
|
||||
base = road + 2 * (bias - offset - heading * lookahead) / lookahead ** 2
|
||||
m.position.y = (.5 * base * x ** 2).tolist()
|
||||
source = observation(m, model_sample_id=1 + tick // 5, base_curvature=base,
|
||||
measured_curvature=measured, speed_mps=speed, lateral_delay_s=.2,
|
||||
settings=LaneCenteringSettings(True, e2e_authority=.5))
|
||||
result = controller.update(source)
|
||||
assert result.candidate_curvature is not None
|
||||
command = controller.guard_applied(m, base, result.candidate_curvature, measured, speed, .2)
|
||||
commands.append(command)
|
||||
measured += (commands.popleft() - measured) * (1 - math.exp(-.01 / .15))
|
||||
heading += speed * (measured - road) * .01
|
||||
offset += speed * math.sin(heading) * .01
|
||||
self.assertLessEqual(abs(offset), .65)
|
||||
self.assertLessEqual(abs(result.correction), MAX_CORRECTION)
|
||||
self.assertLess(abs(offset), .10)
|
||||
|
||||
|
||||
class TestLaneCentering(unittest.TestCase):
|
||||
def test_lane_width_avoids_first_use_numpy_median_import(self):
|
||||
from openpilot.starpilot.lateral.lane_tracking import SingleLineTracker
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
{"protected_inventory_sha256": "06a6e0aedf79e769ab141a43123ccc83cd996978adb432a98028401fef3e62ad", "schema_version": 1}
|
||||
{"protected_inventory_sha256": "2a089790f04f0d54aae678f2dedec9b9423c81af849ffa7cb187071614f11d8d", "schema_version": 1}
|
||||
|
||||
Reference in New Issue
Block a user