fix(motion_planning): clamp the waypoint angle cosine before acosf (#29141)

On an out-and-back leg the dot product of the two unit vectors can round above 1, and acosf then returns NaN, which skipped the turn speed limit so the vehicle reached the turnaround too fast.

Co-authored-by: Jacob Dahl <dahl.jakejacob@gmail.com>
This commit is contained in:
trestonandJacob Dahl authored and GitHub committed 2026-10-13 10:49:33 -06:00
1 parent 2d7f7e029f
commit c2f785d2c7
2 files changed
+8 -4

No files matched your search

@@ -121,8 +121,10 @@ float PositionSmoothing::_getMaxZSpeed(const Vector3f(&waypoints)[3]) const
const bool target_next_different = fabsf(target(2) - next_target(2)) > 0.001f;
if (target_next_different) {
const float alpha = acosf(Vector2f((target_xy_z - start_position_xy_z)).unit_or_zero().dot(
Vector2f((target_xy_z - next_target_xy_z)).unit_or_zero()));
// rounding can push the dot product of two unit vectors past 1, which acosf turns into NaN
const float cos_alpha = math::constrain(Vector2f((target_xy_z - start_position_xy_z)).unit_or_zero().dot(
Vector2f((target_xy_z - next_target_xy_z)).unit_or_zero()), -1.f, 1.f);
const float alpha = acosf(cos_alpha);
const float safe_alpha = math::constrain(alpha, 0.f, M_PI_F - FLT_EPSILON);
float accel_tmp = _trajectory[2].getMaxAccel();
@@ -80,8 +80,10 @@ inline float computeStartXYSpeedFromWaypoints(const Vector3f &start_position, co
if (target_next_different &&
!waypoint_overlap
) {
const float alpha = acosf(Vector2f((target - start_position).xy()).unit_or_zero().dot(
Vector2f((target - next_target).xy()).unit_or_zero()));
// rounding can push the dot product of two unit vectors past 1, which acosf turns into NaN
const float cos_alpha = constrain(Vector2f((target - start_position).xy()).unit_or_zero().dot(
Vector2f((target - next_target).xy()).unit_or_zero()), -1.f, 1.f);
const float alpha = acosf(cos_alpha);
const float safe_alpha = constrain(alpha, 0.f, M_PI_F - FLT_EPSILON);
float accel_tmp = config.max_acc_xy_radius_scale * config.max_acc_xy;
float max_speed_in_turn = computeMaxSpeedInWaypoint(safe_alpha, accel_tmp, config.xy_accept_rad);