ControlMath: add addIfNotNan helper functions

This commit is contained in:
Matthias Grob
2019-12-14 19:45:52 +01:00
committed by Kabir Mohammed
parent eb50e89d87
commit e53ae45188
3 changed files with 66 additions and 0 deletions
@@ -215,4 +215,32 @@ bool cross_sphere_line(const Vector3f &sphere_c, const float sphere_r,
return false;
}
}
void addIfNotNan(float &setpoint, const float addition)
{
if (PX4_ISFINITE(setpoint) && PX4_ISFINITE(addition)) {
// No NAN, add to the setpoint
setpoint += addition;
} else if (!PX4_ISFINITE(setpoint)) {
// Setpoint NAN, take addition
setpoint = addition;
}
// Addition is NAN or both are NAN, nothing to do
}
void addIfNotNanVector3f(Vector3f &setpoint, const Vector3f &addition)
{
for (int i = 0; i < 3; i++) {
addIfNotNan(setpoint(i), addition(i));
}
}
void setZeroIfNanVector3f(Vector3f &vector)
{
// Adding zero vector overwrites elements that are NaN with zero
addIfNotNanVector3f(vector, Vector3f());
}
} // ControlMath