fix: avoid perpetual movement when the target ang motor position is overshot

This commit is contained in:
Sébastien Crozet
2023-07-10 09:14:16 +02:00
parent 9f3b5c8642
commit a05622cfe9
6 changed files with 165 additions and 57 deletions

View File

@@ -804,3 +804,13 @@ impl<T> IndexMut2<usize> for [T] {
}
}
}
/// Calculate the difference with smallest absolute value between the two given values.
pub fn smallest_abs_diff_between_sin_angles<N: WReal>(a: N, b: N) -> N {
// Select the smallest path among the two angles to reach the target.
let s_err = a - b;
let sgn = s_err.simd_signum();
let s_err_complement = s_err - sgn * N::splat(2.0);
let s_err_is_smallest = s_err.simd_abs().simd_lt(s_err_complement.simd_abs());
s_err.select(s_err_is_smallest, s_err_complement)
}