15#include "wpi/units/angle.hpp"
16#include "wpi/units/base.hpp"
17#include "wpi/units/length.hpp"
18#include "wpi/units/math.hpp"
19#include "wpi/units/time.hpp"
20#include "wpi/units/velocity.hpp"
37 requires std::is_arithmetic_v<T> || wpi::units::traits::is_unit_t_v<T>
38constexpr T
ApplyDeadband(T value, T deadband, T maxMagnitude = T{1.0}) {
40 if constexpr (std::is_arithmetic_v<T>) {
43 magnitude = wpi::units::math::abs(value);
46 if (magnitude < deadband) {
71 return (1.0 + deadband / (maxMagnitude - deadband)) * (value - deadband);
93 return (1.0 + deadband / (maxMagnitude - deadband)) * (value + deadband);
108template <
typename T,
int N>
109 requires std::is_arithmetic_v<T> || wpi::units::traits::is_unit_t_v<T>
110Eigen::Vector<T, N>
ApplyDeadband(
const Eigen::Vector<T, N>& value, T deadband,
111 T maxMagnitude = T{1.0}) {
112 if constexpr (std::is_arithmetic_v<T>) {
113 if (value.norm() < T{1e-9}) {
114 return Eigen::Vector<T, N>::Zero();
116 return value.normalized() *
119 const Eigen::Vector<double, N> asDouble = value.template cast<double>();
120 const Eigen::Vector<double, N> processed =
121 ApplyDeadband(asDouble, deadband.value(), maxMagnitude.value());
122 return processed.template cast<T>();
145 requires std::is_arithmetic_v<T> || wpi::units::traits::is_unit_t_v<T>
147 T maxMagnitude = T{1.0}) {
148 if constexpr (std::is_arithmetic_v<T>) {
153 return wpi::units::math::copysign(
154 gcem::pow((wpi::units::math::abs(value) / maxMagnitude).value(),
179template <
typename T,
int N>
180 requires std::is_arithmetic_v<T> || wpi::units::traits::is_unit_t_v<T>
182 double exponent, T maxMagnitude = T{1.0}) {
183 if constexpr (std::is_arithmetic_v<T>) {
184 if (value.norm() < T{1e-9}) {
185 return Eigen::Vector<T, N>::Zero();
187 return value.normalized() *
190 const Eigen::Vector<double, N> asDouble = value.template cast<double>();
191 const Eigen::Vector<double, N> processed =
193 return processed.template cast<T>();
206 T modulus = maximumInput - minimumInput;
209 int numMax = (input - minimumInput) / modulus;
210 input -= numMax * modulus;
213 int numMin = (input - maximumInput) / modulus;
214 input -= numMin * modulus;
230 requires std::is_arithmetic_v<T> || wpi::units::traits::is_unit_t_v<T>
231constexpr bool IsNear(T expected, T actual, T tolerance) {
232 if constexpr (std::is_arithmetic_v<T>) {
233 return std::abs(expected - actual) < tolerance;
235 return wpi::units::math::abs(expected - actual) < tolerance;
259 requires std::is_arithmetic_v<T> || wpi::units::traits::is_unit_t_v<T>
260constexpr bool IsNear(T expected, T actual, T tolerance, T min, T max) {
261 T errorBound = (max - min) / 2.0;
265 if constexpr (std::is_arithmetic_v<T>) {
266 return std::abs(error) < tolerance;
268 return wpi::units::math::abs(error) < tolerance;
278constexpr wpi::units::radian_t
AngleModulus(wpi::units::radian_t angle) {
280 angle, wpi::units::radian_t{-std::numbers::pi},
281 wpi::units::radian_t{std::numbers::pi});
295constexpr std::signed_integral
auto FloorDiv(std::signed_integral
auto x,
296 std::signed_integral
auto y) {
300 if ((x < 0) != (y < 0) && rem != 0) {
317constexpr std::signed_integral
auto FloorMod(std::signed_integral
auto x,
318 std::signed_integral
auto y) {
333 wpi::units::second_t dt, wpi::units::meters_per_second_t maxVelocity) {
334 if (maxVelocity < 0_mps) {
336 "maxVelocity must be a non-negative number, got {}!", maxVelocity);
340 wpi::units::meter_t dist = diff.
Norm();
344 if (dist > maxVelocity * dt) {
347 return current + diff * (maxVelocity * dt / dist);
363 wpi::units::second_t dt, wpi::units::meters_per_second_t maxVelocity) {
364 if (maxVelocity < 0_mps) {
366 "maxVelocity must be a non-negative number, got {}!", maxVelocity);
370 wpi::units::meter_t dist = diff.
Norm();
374 if (dist > maxVelocity * dt) {
377 return current + diff * (maxVelocity * dt / dist);
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
static void ReportError(const S &format, Args &&... args)
Definition MathShared.hpp:48
Represents a translation in 2D space.
Definition Translation2d.hpp:30
constexpr wpi::units::meter_t Norm() const
Returns the norm, or distance from the origin to the translation.
Definition Translation2d.hpp:125
Represents a translation in 3D space.
Definition Translation3d.hpp:31
constexpr wpi::units::meter_t Norm() const
Returns the norm, or distance from the origin to the translation.
Definition Translation3d.hpp:155
constexpr T abs(const T x) noexcept
Compile-time absolute value function.
Definition abs.hpp:40
constexpr T1 copysign(const T1 x, const T2 y) noexcept
Compile-time copy sign function.
Definition copysign.hpp:41
constexpr common_t< T1, T2 > pow(const T1 base, const T2 exp_term) noexcept
Compile-time power function.
Definition pow.hpp:82
Definition LinearSystem.hpp:20
constexpr Translation2d SlewRateLimit(const Translation2d ¤t, const Translation2d &next, wpi::units::second_t dt, wpi::units::meters_per_second_t maxVelocity)
Limits translation velocity.
Definition MathUtil.hpp:331
constexpr std::signed_integral auto FloorMod(std::signed_integral auto x, std::signed_integral auto y)
Returns the floor modulus of the int arguments.
Definition MathUtil.hpp:317
constexpr T ApplyDeadband(T value, T deadband, T maxMagnitude=T{1.0})
Returns 0.0 if the given value is within the specified range around zero.
Definition MathUtil.hpp:38
WPILIB_DLLEXPORT constexpr wpi::units::radian_t AngleModulus(wpi::units::radian_t angle)
Wraps an angle to the range -π to π radians (-180 to 180 degrees).
Definition MathUtil.hpp:278
constexpr T InputModulus(T input, T minimumInput, T maximumInput)
Returns modulus of input.
Definition MathUtil.hpp:205
constexpr bool IsNear(T expected, T actual, T tolerance)
Checks if the given value matches an expected value within a certain tolerance.
Definition MathUtil.hpp:231
constexpr std::signed_integral auto FloorDiv(std::signed_integral auto x, std::signed_integral auto y)
Returns the largest (closest to positive infinity) int value that is less than or equal to the algebr...
Definition MathUtil.hpp:295
constexpr T CopyDirectionPow(T value, double exponent, T maxMagnitude=T{1.0})
Raises the input to the power of the given exponent while preserving its sign.
Definition MathUtil.hpp:146