mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-12-02 17:43:40 +08:00
Retain lane-position feedback through curves and line transitions
This commit is contained in:
@@ -278,7 +278,7 @@ class LaneCenteringController:
|
||||
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._command_history: deque[tuple[float, float, float]] = deque(maxlen=150)
|
||||
self._geometry_cache = None
|
||||
self._tracker = SingleLineTracker()
|
||||
self._body = body
|
||||
@@ -295,7 +295,9 @@ class LaneCenteringController:
|
||||
self._tracker.reset()
|
||||
|
||||
def _remember_command(self, curvature: float) -> None:
|
||||
sample = (self._position_clock, curvature)
|
||||
source = self._clearance_observation
|
||||
base = source.base_curvature if source is not None else curvature
|
||||
sample = (self._position_clock, curvature, base)
|
||||
if self._command_history and self._command_history[-1][0] == self._position_clock:
|
||||
self._command_history[-1] = sample
|
||||
else:
|
||||
@@ -354,7 +356,7 @@ class LaneCenteringController:
|
||||
allowed = limit_correction(model, base, allowed, measured, speed, checked_delay, self._body)
|
||||
if allowed != delta:
|
||||
self._correction *= abs(allowed / delta)
|
||||
self._position_trim *= abs(allowed / delta)
|
||||
self._position_trim = clip_scalar(self._position_trim, -abs(allowed) * speed * speed, abs(allowed) * speed * speed)
|
||||
final = base + allowed
|
||||
self._remember_command(final)
|
||||
return final
|
||||
@@ -429,20 +431,24 @@ 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:
|
||||
if self._tracker.side is None and self._tracker.width is not None and self._tracker.path is not None:
|
||||
spacing = (self._tracker.width * .5 if self._tracker.side is None and self._tracker.width is not None
|
||||
else self._tracker.spacing)
|
||||
if spacing 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))
|
||||
bound = min(MAX_OFFSET, max(0., spacing - 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)
|
||||
unattenuated = raw_correction(observation.model, speed, offset, 0., _paths=paths, _single_target=tracked.y)
|
||||
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
|
||||
delayed = next(((command, nominal) for stamp, command, nominal in reversed(self._command_history)
|
||||
if stamp <= self._position_clock - delay), None)
|
||||
tracking_error = ((clip_scalar(measured, min(delayed), max(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
|
||||
@@ -457,7 +463,7 @@ class LaneCenteringController:
|
||||
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)
|
||||
self._position_trim = clip_scalar(self._position_trim, -abs(allowed) * speed * speed, abs(allowed) * speed * speed)
|
||||
target = allowed
|
||||
else:
|
||||
self._position_trim = 0.
|
||||
|
||||
@@ -74,6 +74,43 @@ class TestCurvePositionFeedback(unittest.TestCase):
|
||||
assert result is not None
|
||||
return result
|
||||
|
||||
def test_nominal_curve_response_can_build_a_bounded_position_bias(self):
|
||||
for sign in (-1., 1.):
|
||||
with self.subTest(direction=sign):
|
||||
source = self.source(sign * .6, sign * .003)
|
||||
controller = LaneCenteringController()
|
||||
result = self.run_fresh(controller, source, 1000, following=False)
|
||||
self.assertLess(sign * controller._position_trim, -.05)
|
||||
self.assertLessEqual(abs(controller._position_trim), MAX_CORRECTION * source.speed_mps ** 2 + 1e-12)
|
||||
self.assertLess(sign * result.correction, 0.)
|
||||
self.assertLessEqual(abs(result.correction), MAX_CORRECTION)
|
||||
|
||||
def test_position_feedback_survives_loss_of_the_other_line(self):
|
||||
for sign in (-1., 1.):
|
||||
for missing in (1, 2):
|
||||
with self.subTest(direction=sign, missing=missing):
|
||||
source = self.source(sign * .6, sign * .003)
|
||||
# Cover the origin after projecting the line's normal spacing.
|
||||
for path, shift in ((source.model.position, 0.),
|
||||
(source.model.laneLines[1], -1.8 - sign * .6),
|
||||
(source.model.laneLines[2], 1.8 - sign * .6)):
|
||||
path.x = [-1., *XS]
|
||||
path.y = (.5 * sign * .003 * np.asarray(path.x) ** 2 + shift).tolist()
|
||||
source.model.position.yStd = [.1] * (len(XS) + 1)
|
||||
controller = LaneCenteringController()
|
||||
self.run_fresh(controller, source, 300)
|
||||
self.assertLess(sign * controller._position_trim, 0.)
|
||||
source.model.laneLineProbs[missing] = .05
|
||||
result = self.run_fresh(controller, source, 300)
|
||||
self.assertEqual(result.reason, 'qualified')
|
||||
self.assertEqual(controller._tracker.side, 1 if missing == 1 else 0)
|
||||
self.assertLess(sign * controller._position_trim, -.01)
|
||||
self.assertLess(sign * result.correction, 0.)
|
||||
self.assertLessEqual(abs(result.correction), MAX_CORRECTION)
|
||||
source.model.laneLineProbs = [0., .05, .05, 0.]
|
||||
controller.update(replace(source, model_sample_id=1000))
|
||||
self.assertEqual(controller._position_trim, 0.)
|
||||
|
||||
def test_sustained_curve_position_error_extends_the_partial_path_nudge(self):
|
||||
for sign in (-1., 1.):
|
||||
source = self.source(sign * .6, sign * .003)
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
{"protected_inventory_sha256": "c3c9d9fb3d711d871d5ff883aa6bf8830d2cb14a182060fe7aa8dad226d47d83", "schema_version": 1}
|
||||
{"protected_inventory_sha256": "40c7008cbb4cd9e3672c5673fcd0adb1e71017e3c6b5ec7354178c20c28733e1", "schema_version": 1}
|
||||
|
||||
Reference in New Issue
Block a user