mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-20 05:33:33 +08:00
helper_functions add wrap_2pi
This commit is contained in:
committed by
Lorenz Meier
parent
abc8f82d49
commit
03a3e3ad46
@@ -6,6 +6,8 @@
|
||||
#include <px4_defines.h>
|
||||
#endif
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace matrix
|
||||
{
|
||||
|
||||
@@ -27,18 +29,57 @@ Type wrap_pi(Type x)
|
||||
return x;
|
||||
}
|
||||
|
||||
while (x >= Type(M_PI)) {
|
||||
x -= Type(2.0 * M_PI);
|
||||
int c = 0;
|
||||
|
||||
while (x >= Type(M_PI)) {
|
||||
x -= Type(2 * M_PI);
|
||||
|
||||
if (c++ > 100) {
|
||||
return std::numeric_limits<Type>::quiet_NaN();
|
||||
}
|
||||
}
|
||||
|
||||
while (x < Type(-M_PI)) {
|
||||
x += Type(2.0 * M_PI);
|
||||
c = 0;
|
||||
|
||||
while (x < Type(-M_PI)) {
|
||||
x += Type(2 * M_PI);
|
||||
|
||||
if (c++ > 100) {
|
||||
return std::numeric_limits<Type>::quiet_NaN();
|
||||
}
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
Type wrap_2pi(Type x)
|
||||
{
|
||||
if (!is_finite(x)) {
|
||||
return x;
|
||||
}
|
||||
|
||||
int c = 0;
|
||||
|
||||
while (x >= Type(2 * M_PI)) {
|
||||
x -= Type(2 * M_PI);
|
||||
|
||||
if (c++ > 100) {
|
||||
return std::numeric_limits<Type>::quiet_NaN();
|
||||
}
|
||||
}
|
||||
|
||||
c = 0;
|
||||
|
||||
while (x < Type(0)) {
|
||||
x += Type(2 * M_PI);
|
||||
|
||||
if (c++ > 100) {
|
||||
return std::numeric_limits<Type>::quiet_NaN();
|
||||
}
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user