diff --git a/libraries/AP_Math/AP_Math.cpp b/libraries/AP_Math/AP_Math.cpp index 6cb2dc6b3d..1dcd3bd6ef 100644 --- a/libraries/AP_Math/AP_Math.cpp +++ b/libraries/AP_Math/AP_Math.cpp @@ -313,9 +313,13 @@ T constrain_value(const T amt, const T low, const T high) } template int constrain_value(const int amt, const int low, const int high); +template unsigned int constrain_value(const unsigned int amt, const unsigned int low, const unsigned int high); template long constrain_value(const long amt, const long low, const long high); +template unsigned long constrain_value(const unsigned long amt, const unsigned long low, const unsigned long high); template long long constrain_value(const long long amt, const long long low, const long long high); +template unsigned long long constrain_value(const unsigned long long amt, const unsigned long long low, const unsigned long long high); template short constrain_value(const short amt, const short low, const short high); +template unsigned short constrain_value(const unsigned short amt, const unsigned short low, const unsigned short high); template float constrain_value(const float amt, const float low, const float high); template double constrain_value(const double amt, const double low, const double high); diff --git a/libraries/AP_Math/AP_Math.h b/libraries/AP_Math/AP_Math.h index 5b71dea1f9..52237f025c 100644 --- a/libraries/AP_Math/AP_Math.h +++ b/libraries/AP_Math/AP_Math.h @@ -179,16 +179,31 @@ inline int16_t constrain_int16(const int16_t amt, const int16_t low, const int16 return constrain_value(amt, low, high); } +inline uint16_t constrain_uint16(const uint16_t amt, const uint16_t low, const uint16_t high) +{ + return constrain_value(amt, low, high); +} + inline int32_t constrain_int32(const int32_t amt, const int32_t low, const int32_t high) { return constrain_value(amt, low, high); } +inline uint32_t constrain_uint32(const uint32_t amt, const uint32_t low, const uint32_t high) +{ + return constrain_value(amt, low, high); +} + inline int64_t constrain_int64(const int64_t amt, const int64_t low, const int64_t high) { return constrain_value(amt, low, high); } +inline uint64_t constrain_uint64(const uint64_t amt, const uint64_t low, const uint64_t high) +{ + return constrain_value(amt, low, high); +} + // degrees -> radians static inline constexpr ftype radians(ftype deg) { diff --git a/libraries/AP_Math/tests/test_math.cpp b/libraries/AP_Math/tests/test_math.cpp index bdd4e2ec4f..4cde50bae3 100644 --- a/libraries/AP_Math/tests/test_math.cpp +++ b/libraries/AP_Math/tests/test_math.cpp @@ -377,6 +377,10 @@ TEST(MathTest, Constrain) EXPECT_EQ(19.9, constrain_value(19.8, 19.9, 20.1)); EXPECT_EQ(19.9f, constrain_value(19.8f, 19.9f, 20.1f)); + // test that constrain on 32 bit integer works correctly. Note the asymmetry + EXPECT_EQ(10, constrain_int32( 0xFFFFFFFFU, 10U, 1200U)); + EXPECT_EQ(1200U, constrain_uint32(0xFFFFFFFFU, 10U, 1200U)); + #if CONFIG_HAL_BOARD == HAL_BOARD_LINUX EXPECT_EQ(1.0f, constrain_float(nanf("0x4152"), 1.0f, 1.0f)); EXPECT_EQ(1.0f, constrain_value(nanf("0x4152"), 1.0f, 1.0f));