2012-02-23 07:56:40 -04:00
|
|
|
#include "AP_Math.h"
|
2015-04-28 02:36:53 -03:00
|
|
|
#include <float.h>
|
|
|
|
|
2012-02-23 07:56:40 -04:00
|
|
|
// a varient of asin() that checks the input ranges and ensures a
|
|
|
|
// valid angle as output. If nan is given as input then zero is
|
|
|
|
// returned.
|
|
|
|
float safe_asin(float v)
|
|
|
|
{
|
2012-08-17 03:20:14 -03:00
|
|
|
if (isnan(v)) {
|
2015-04-24 00:50:50 -03:00
|
|
|
return 0.0f;
|
2012-08-17 03:20:14 -03:00
|
|
|
}
|
2013-01-10 14:42:24 -04:00
|
|
|
if (v >= 1.0f) {
|
2012-08-17 03:20:14 -03:00
|
|
|
return PI/2;
|
|
|
|
}
|
2013-01-10 14:42:24 -04:00
|
|
|
if (v <= -1.0f) {
|
2012-08-17 03:20:14 -03:00
|
|
|
return -PI/2;
|
|
|
|
}
|
2013-01-10 14:42:24 -04:00
|
|
|
return asinf(v);
|
2012-02-23 07:56:40 -04:00
|
|
|
}
|
2012-02-23 19:40:56 -04:00
|
|
|
|
|
|
|
// a varient of sqrt() that checks the input ranges and ensures a
|
|
|
|
// valid value as output. If a negative number is given then 0 is
|
|
|
|
// returned. The reasoning is that a negative number for sqrt() in our
|
|
|
|
// code is usually caused by small numerical rounding errors, so the
|
|
|
|
// real input should have been zero
|
|
|
|
float safe_sqrt(float v)
|
|
|
|
{
|
2013-01-10 14:42:24 -04:00
|
|
|
float ret = sqrtf(v);
|
2012-08-17 03:20:14 -03:00
|
|
|
if (isnan(ret)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ret;
|
2012-02-23 19:40:56 -04:00
|
|
|
}
|
2012-03-11 09:17:05 -03:00
|
|
|
|
2013-01-13 02:26:48 -04:00
|
|
|
#if ROTATION_COMBINATION_SUPPORT
|
2012-03-11 09:17:05 -03:00
|
|
|
// find a rotation that is the combination of two other
|
|
|
|
// rotations. This is used to allow us to add an overall board
|
|
|
|
// rotation to an existing rotation of a sensor such as the compass
|
|
|
|
// Note that this relies the set of rotations being complete. The
|
|
|
|
// optional 'found' parameter is for the test suite to ensure that it is.
|
|
|
|
enum Rotation rotation_combination(enum Rotation r1, enum Rotation r2, bool *found)
|
|
|
|
{
|
2012-08-17 03:20:14 -03:00
|
|
|
Vector3f tv1, tv2;
|
|
|
|
enum Rotation r;
|
|
|
|
tv1(1,2,3);
|
|
|
|
tv1.rotate(r1);
|
|
|
|
tv1.rotate(r2);
|
2012-03-11 09:17:05 -03:00
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
for (r=ROTATION_NONE; r<ROTATION_MAX;
|
|
|
|
r = (enum Rotation)((uint8_t)r+1)) {
|
|
|
|
Vector3f diff;
|
|
|
|
tv2(1,2,3);
|
|
|
|
tv2.rotate(r);
|
|
|
|
diff = tv1 - tv2;
|
2013-01-10 14:42:24 -04:00
|
|
|
if (diff.length() < 1.0e-6f) {
|
2012-08-17 03:20:14 -03:00
|
|
|
// we found a match
|
|
|
|
if (found) {
|
|
|
|
*found = true;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
2012-03-11 09:17:05 -03:00
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
// we found no matching rotation. Someone has edited the
|
|
|
|
// rotations list and broken its completeness property ...
|
|
|
|
if (found) {
|
|
|
|
*found = false;
|
|
|
|
}
|
|
|
|
return ROTATION_NONE;
|
2012-03-11 09:17:05 -03:00
|
|
|
}
|
2013-01-13 02:26:48 -04:00
|
|
|
#endif
|
2012-12-18 22:33:52 -04:00
|
|
|
|