Prevent goal oscillation and unintended differential drive arcs
This commit is contained in:
@@ -102,13 +102,23 @@ class TwoSideDrive:
|
||||
dt = min(dt, self.limits.max_step_seconds)
|
||||
old = (self.applied.left_mps, self.applied.right_mps)
|
||||
goal = (target.left_mps, target.right_mps)
|
||||
goal = tuple(0.0 if a * b < 0 else b for a, b in zip(old, goal, strict=True))
|
||||
fraction = 1.0
|
||||
for a, b in zip(old, goal, strict=True):
|
||||
if a * b < 0:
|
||||
continue
|
||||
rate = (
|
||||
self.limits.acceleration_mps2 if abs(b) > abs(a) else self.limits.deceleration_mps2
|
||||
)
|
||||
if a != b:
|
||||
fraction = min(fraction, rate * dt / abs(b - a))
|
||||
self.applied = SideSpeeds(*(a + (b - a) * fraction for a, b in zip(old, goal, strict=True)))
|
||||
# A reversing side owns its braking phase. Coupling its zero target to
|
||||
# the other side's acceleration fraction can approach zero forever and
|
||||
# leave one belt held while the other accelerates into an unwanted arc.
|
||||
# Reach zero at the declared braking rate; accelerate in the opposite
|
||||
# direction only on a later tick. Ordinary arcs keep the common fraction.
|
||||
self.applied = SideSpeeds(*(
|
||||
math.copysign(max(0.0, abs(a) - self.limits.deceleration_mps2 * dt), a)
|
||||
if a * b < 0 else a + (b - a) * fraction
|
||||
for a, b in zip(old, goal, strict=True)
|
||||
))
|
||||
return self.applied
|
||||
|
||||
Reference in New Issue
Block a user