2012-02-23 07:56:40 -04:00
|
|
|
#include "AP_Math.h"
|
2017-01-24 13:57:43 -04:00
|
|
|
|
2015-04-28 02:36:53 -03:00
|
|
|
#include <float.h>
|
|
|
|
|
2019-04-04 18:07:57 -03:00
|
|
|
#include <AP_InternalError/AP_InternalError.h>
|
|
|
|
|
2017-01-24 13:57:43 -04:00
|
|
|
/*
|
|
|
|
* is_equal(): Integer implementation, provided for convenience and
|
|
|
|
* compatibility with old code. Expands to the same as comparing the values
|
|
|
|
* directly
|
|
|
|
*/
|
2017-06-20 21:42:39 -03:00
|
|
|
template <typename Arithmetic1, typename Arithmetic2>
|
2016-12-17 14:16:39 -04:00
|
|
|
typename std::enable_if<std::is_integral<typename std::common_type<Arithmetic1, Arithmetic2>::type>::value ,bool>::type
|
|
|
|
is_equal(const Arithmetic1 v_1, const Arithmetic2 v_2)
|
|
|
|
{
|
|
|
|
typedef typename std::common_type<Arithmetic1, Arithmetic2>::type common_type;
|
|
|
|
return static_cast<common_type>(v_1) == static_cast<common_type>(v_2);
|
|
|
|
}
|
|
|
|
|
2017-01-24 13:57:43 -04:00
|
|
|
/*
|
|
|
|
* is_equal(): double/float implementation - takes into account
|
|
|
|
* std::numeric_limits<T>::epsilon() to return if 2 values are equal.
|
|
|
|
*/
|
2017-06-20 21:42:39 -03:00
|
|
|
template <typename Arithmetic1, typename Arithmetic2>
|
2016-12-17 14:16:39 -04:00
|
|
|
typename std::enable_if<std::is_floating_point<typename std::common_type<Arithmetic1, Arithmetic2>::type>::value, bool>::type
|
|
|
|
is_equal(const Arithmetic1 v_1, const Arithmetic2 v_2)
|
2016-04-16 06:59:20 -03:00
|
|
|
{
|
2018-05-04 19:10:30 -03:00
|
|
|
#ifdef ALLOW_DOUBLE_MATH_FUNCTIONS
|
2017-01-24 13:57:43 -04:00
|
|
|
typedef typename std::common_type<Arithmetic1, Arithmetic2>::type common_type;
|
|
|
|
typedef typename std::remove_cv<common_type>::type common_type_nonconst;
|
|
|
|
if (std::is_same<double, common_type_nonconst>::value) {
|
|
|
|
return fabs(v_1 - v_2) < std::numeric_limits<double>::epsilon();
|
|
|
|
}
|
2018-05-04 19:10:30 -03:00
|
|
|
#endif
|
2019-09-17 06:50:00 -03:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wabsolute-value"
|
|
|
|
// clang doesn't realise we catch the double case above and warns
|
|
|
|
// about loss of precision here.
|
2017-01-24 13:57:43 -04:00
|
|
|
return fabsf(v_1 - v_2) < std::numeric_limits<float>::epsilon();
|
2019-09-17 06:50:00 -03:00
|
|
|
#pragma clang diagnostic pop
|
2016-04-16 06:59:20 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
template bool is_equal<int>(const int v_1, const int v_2);
|
|
|
|
template bool is_equal<short>(const short v_1, const short v_2);
|
2018-01-05 19:37:30 -04:00
|
|
|
template bool is_equal<long>(const long v_1, const long v_2);
|
2016-04-16 06:59:20 -03:00
|
|
|
template bool is_equal<float>(const float v_1, const float v_2);
|
|
|
|
template bool is_equal<double>(const double v_1, const double v_2);
|
|
|
|
|
2017-06-20 21:42:39 -03:00
|
|
|
template <typename T>
|
2016-04-16 06:59:08 -03:00
|
|
|
float safe_asin(const T v)
|
2012-02-23 07:56:40 -04:00
|
|
|
{
|
2017-01-24 13:59:47 -04:00
|
|
|
const float f = static_cast<const float>(v);
|
|
|
|
if (isnan(f)) {
|
2015-04-24 00:50:50 -03:00
|
|
|
return 0.0f;
|
2012-08-17 03:20:14 -03:00
|
|
|
}
|
2017-01-24 13:59:47 -04:00
|
|
|
if (f >= 1.0f) {
|
2016-04-16 06:59:08 -03:00
|
|
|
return static_cast<float>(M_PI_2);
|
2012-08-17 03:20:14 -03:00
|
|
|
}
|
2017-01-24 13:59:47 -04:00
|
|
|
if (f <= -1.0f) {
|
2016-04-16 06:59:08 -03:00
|
|
|
return static_cast<float>(-M_PI_2);
|
2012-08-17 03:20:14 -03:00
|
|
|
}
|
2017-01-24 13:59:47 -04:00
|
|
|
return asinf(f);
|
2012-02-23 07:56:40 -04:00
|
|
|
}
|
2012-02-23 19:40:56 -04:00
|
|
|
|
2016-04-16 06:59:08 -03:00
|
|
|
template float safe_asin<int>(const int v);
|
|
|
|
template float safe_asin<short>(const short v);
|
|
|
|
template float safe_asin<float>(const float v);
|
|
|
|
template float safe_asin<double>(const double v);
|
|
|
|
|
2017-06-20 21:42:39 -03:00
|
|
|
template <typename T>
|
2016-04-16 06:59:08 -03:00
|
|
|
float safe_sqrt(const T v)
|
2012-02-23 19:40:56 -04:00
|
|
|
{
|
2016-04-16 06:59:08 -03:00
|
|
|
float ret = sqrtf(static_cast<float>(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
|
|
|
|
2016-04-16 06:59:08 -03:00
|
|
|
template float safe_sqrt<int>(const int v);
|
|
|
|
template float safe_sqrt<short>(const short v);
|
|
|
|
template float safe_sqrt<float>(const float v);
|
|
|
|
template float safe_sqrt<double>(const double v);
|
|
|
|
|
2021-05-18 20:19:52 -03:00
|
|
|
/*
|
|
|
|
replacement for std::swap() needed for STM32
|
|
|
|
*/
|
|
|
|
static void swap_float(float &f1, float &f2)
|
|
|
|
{
|
|
|
|
float tmp = f1;
|
|
|
|
f1 = f2;
|
|
|
|
f2 = tmp;
|
|
|
|
}
|
|
|
|
|
2016-04-02 08:44:47 -03:00
|
|
|
/*
|
2019-04-06 15:38:40 -03:00
|
|
|
* linear interpolation based on a variable in a range
|
2016-04-02 08:44:47 -03:00
|
|
|
*/
|
|
|
|
float linear_interpolate(float low_output, float high_output,
|
|
|
|
float var_value,
|
|
|
|
float var_low, float var_high)
|
|
|
|
{
|
2021-05-18 20:19:52 -03:00
|
|
|
if (var_low > var_high) {
|
|
|
|
// support either polarity
|
|
|
|
swap_float(var_low, var_high);
|
|
|
|
swap_float(low_output, high_output);
|
|
|
|
}
|
2016-04-02 08:44:47 -03:00
|
|
|
if (var_value <= var_low) {
|
|
|
|
return low_output;
|
|
|
|
}
|
|
|
|
if (var_value >= var_high) {
|
|
|
|
return high_output;
|
|
|
|
}
|
|
|
|
float p = (var_value - var_low) / (var_high - var_low);
|
|
|
|
return low_output + p * (high_output - low_output);
|
|
|
|
}
|
2012-12-18 22:33:52 -04:00
|
|
|
|
2019-04-06 15:38:40 -03:00
|
|
|
/* cubic "expo" curve generator
|
|
|
|
* alpha range: [0,1] min to max expo
|
|
|
|
* input range: [-1,1]
|
|
|
|
*/
|
2021-07-17 03:39:46 -03:00
|
|
|
float expo_curve(float alpha, float x)
|
2019-04-06 15:38:40 -03:00
|
|
|
{
|
|
|
|
return (1.0f - alpha) * x + alpha * x * x * x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* throttle curve generator
|
|
|
|
* thr_mid: output at mid stick
|
|
|
|
* alpha: expo coefficient
|
|
|
|
* thr_in: [0-1]
|
|
|
|
*/
|
|
|
|
float throttle_curve(float thr_mid, float alpha, float thr_in)
|
|
|
|
{
|
|
|
|
float alpha2 = alpha + 1.25 * (1.0f - alpha) * (0.5f - thr_mid) / 0.5f;
|
|
|
|
alpha2 = constrain_float(alpha2, 0.0f, 1.0f);
|
|
|
|
float thr_out = 0.0f;
|
|
|
|
if (thr_in < 0.5f) {
|
|
|
|
float t = linear_interpolate(-1.0f, 0.0f, thr_in, 0.0f, 0.5f);
|
|
|
|
thr_out = linear_interpolate(0.0f, thr_mid, expo_curve(alpha, t), -1.0f, 0.0f);
|
|
|
|
} else {
|
|
|
|
float t = linear_interpolate(0.0f, 1.0f, thr_in, 0.5f, 1.0f);
|
|
|
|
thr_out = linear_interpolate(thr_mid, 1.0f, expo_curve(alpha2, t), 0.0f, 1.0f);
|
|
|
|
}
|
|
|
|
return thr_out;
|
|
|
|
}
|
|
|
|
|
2017-06-20 21:42:39 -03:00
|
|
|
template <typename T>
|
2019-09-18 00:13:51 -03:00
|
|
|
T wrap_180(const T angle)
|
2016-04-27 06:35:18 -03:00
|
|
|
{
|
2019-09-18 00:13:51 -03:00
|
|
|
auto res = wrap_360(angle);
|
|
|
|
if (res > T(180)) {
|
|
|
|
res -= T(360);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T wrap_180_cd(const T angle)
|
|
|
|
{
|
|
|
|
auto res = wrap_360_cd(angle);
|
|
|
|
if (res > T(18000)) {
|
|
|
|
res -= T(36000);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template int wrap_180<int>(const int angle);
|
|
|
|
template short wrap_180<short>(const short angle);
|
|
|
|
template float wrap_180<float>(const float angle);
|
|
|
|
#ifdef ALLOW_DOUBLE_MATH_FUNCTIONS
|
|
|
|
template double wrap_180<double>(const double angle);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template int wrap_180_cd<int>(const int angle);
|
|
|
|
template long wrap_180_cd<long>(const long angle);
|
|
|
|
template short wrap_180_cd<short>(const short angle);
|
|
|
|
template float wrap_180_cd<float>(const float angle);
|
|
|
|
#ifdef ALLOW_DOUBLE_MATH_FUNCTIONS
|
|
|
|
template double wrap_180_cd<double>(const double angle);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
float wrap_360(const float angle)
|
|
|
|
{
|
|
|
|
float res = fmodf(angle, 360.0f);
|
|
|
|
if (res < 0) {
|
|
|
|
res += 360.0f;
|
2016-04-27 06:35:18 -03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-09-17 06:50:00 -03:00
|
|
|
#ifdef ALLOW_DOUBLE_MATH_FUNCTIONS
|
2019-09-18 00:13:51 -03:00
|
|
|
double wrap_360(const double angle)
|
|
|
|
{
|
|
|
|
double res = fmod(angle, 360.0);
|
|
|
|
if (res < 0) {
|
|
|
|
res += 360.0;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2019-09-17 06:50:00 -03:00
|
|
|
#endif
|
2016-04-27 06:35:18 -03:00
|
|
|
|
2019-09-18 00:13:51 -03:00
|
|
|
int wrap_360(const int angle)
|
2016-04-27 06:35:18 -03:00
|
|
|
{
|
2019-09-18 00:13:51 -03:00
|
|
|
int res = angle % 360;
|
2019-09-17 06:50:00 -03:00
|
|
|
if (res < 0) {
|
2019-09-18 00:13:51 -03:00
|
|
|
res += 360;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
float wrap_360_cd(const float angle)
|
|
|
|
{
|
|
|
|
float res = fmodf(angle, 36000.0f);
|
|
|
|
if (res < 0) {
|
|
|
|
res += 36000.0f;
|
2019-09-17 06:50:00 -03:00
|
|
|
}
|
|
|
|
return res;
|
2016-04-27 06:35:18 -03:00
|
|
|
}
|
|
|
|
|
2019-09-17 06:50:00 -03:00
|
|
|
#ifdef ALLOW_DOUBLE_MATH_FUNCTIONS
|
2019-09-18 00:13:51 -03:00
|
|
|
double wrap_360_cd(const double angle)
|
2016-04-27 06:35:18 -03:00
|
|
|
{
|
2019-09-18 00:13:51 -03:00
|
|
|
double res = fmod(angle, 36000.0);
|
2016-04-27 06:35:18 -03:00
|
|
|
if (res < 0) {
|
2019-09-18 00:13:51 -03:00
|
|
|
res += 36000.0;
|
2016-04-27 06:35:18 -03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2019-09-17 06:50:00 -03:00
|
|
|
#endif
|
2016-04-27 06:35:18 -03:00
|
|
|
|
2019-09-18 00:13:51 -03:00
|
|
|
int wrap_360_cd(const int angle)
|
2016-04-27 06:35:18 -03:00
|
|
|
{
|
2019-09-18 00:13:51 -03:00
|
|
|
int res = angle % 36000;
|
2019-09-17 06:50:00 -03:00
|
|
|
if (res < 0) {
|
2019-09-18 00:13:51 -03:00
|
|
|
res += 36000;
|
2019-09-17 06:50:00 -03:00
|
|
|
}
|
|
|
|
return res;
|
2016-04-27 06:35:18 -03:00
|
|
|
}
|
|
|
|
|
2019-09-18 00:13:51 -03:00
|
|
|
long wrap_360_cd(const long angle)
|
|
|
|
{
|
|
|
|
long res = angle % 36000;
|
|
|
|
if (res < 0) {
|
|
|
|
res += 36000;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2021-07-08 23:29:47 -03:00
|
|
|
|
|
|
|
ftype wrap_PI(const ftype radian)
|
2016-04-27 06:35:18 -03:00
|
|
|
{
|
2021-07-08 23:29:47 -03:00
|
|
|
ftype res = wrap_2PI(radian);
|
2016-05-02 13:23:12 -03:00
|
|
|
if (res > M_PI) {
|
|
|
|
res -= M_2PI;
|
2016-04-27 06:35:18 -03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-07-08 23:29:47 -03:00
|
|
|
ftype wrap_2PI(const ftype radian)
|
2016-04-27 06:35:18 -03:00
|
|
|
{
|
2021-07-08 23:29:47 -03:00
|
|
|
ftype res = fmodF(radian, M_2PI);
|
2016-04-27 06:35:18 -03:00
|
|
|
if (res < 0) {
|
|
|
|
res += M_2PI;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-10-09 15:54:45 -03:00
|
|
|
template <typename T>
|
|
|
|
T constrain_value_line(const T amt, const T low, const T high, uint32_t line)
|
|
|
|
{
|
|
|
|
// the check for NaN as a float prevents propagation of floating point
|
|
|
|
// errors through any function that uses constrain_value(). The normal
|
|
|
|
// float semantics already handle -Inf and +Inf
|
|
|
|
if (isnan(amt)) {
|
|
|
|
AP::internalerror().error(AP_InternalError::error_t::constraining_nan, line);
|
|
|
|
return (low + high) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (amt < low) {
|
|
|
|
return low;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (amt > high) {
|
|
|
|
return high;
|
|
|
|
}
|
|
|
|
|
|
|
|
return amt;
|
|
|
|
}
|
|
|
|
|
|
|
|
template float constrain_value_line<float>(const float amt, const float low, const float high, uint32_t line);
|
2021-05-04 08:12:23 -03:00
|
|
|
template double constrain_value_line<double>(const double amt, const double low, const double high, uint32_t line);
|
2020-10-09 15:54:45 -03:00
|
|
|
|
2017-06-20 21:42:39 -03:00
|
|
|
template <typename T>
|
2016-04-16 06:58:58 -03:00
|
|
|
T constrain_value(const T amt, const T low, const T high)
|
|
|
|
{
|
2016-05-12 14:02:03 -03:00
|
|
|
// the check for NaN as a float prevents propagation of floating point
|
2017-01-24 14:00:38 -04:00
|
|
|
// errors through any function that uses constrain_value(). The normal
|
2016-04-16 06:58:58 -03:00
|
|
|
// float semantics already handle -Inf and +Inf
|
2020-10-14 19:30:43 -03:00
|
|
|
if (std::is_floating_point<T>::value) {
|
|
|
|
if (isnan(amt)) {
|
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::constraining_nan);
|
|
|
|
return (low + high) / 2;
|
|
|
|
}
|
2016-04-16 06:58:58 -03:00
|
|
|
}
|
2016-05-02 17:15:08 -03:00
|
|
|
|
|
|
|
if (amt < low) {
|
|
|
|
return low;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (amt > high) {
|
|
|
|
return high;
|
|
|
|
}
|
|
|
|
|
|
|
|
return amt;
|
2016-04-16 06:58:58 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
template int constrain_value<int>(const int amt, const int low, const int high);
|
2022-04-03 19:17:10 -03:00
|
|
|
template unsigned int constrain_value<unsigned int>(const unsigned int amt, const unsigned int low, const unsigned int high);
|
2018-01-03 19:38:12 -04:00
|
|
|
template long constrain_value<long>(const long amt, const long low, const long high);
|
2022-04-03 19:17:10 -03:00
|
|
|
template unsigned long constrain_value<unsigned long>(const unsigned long amt, const unsigned long low, const unsigned long high);
|
2018-04-17 00:54:06 -03:00
|
|
|
template long long constrain_value<long long>(const long long amt, const long long low, const long long high);
|
2022-04-03 19:17:10 -03:00
|
|
|
template unsigned long long constrain_value<unsigned long long>(const unsigned long long amt, const unsigned long long low, const unsigned long long high);
|
2016-04-16 06:58:58 -03:00
|
|
|
template short constrain_value<short>(const short amt, const short low, const short high);
|
2022-04-03 19:17:10 -03:00
|
|
|
template unsigned short constrain_value<unsigned short>(const unsigned short amt, const unsigned short low, const unsigned short high);
|
2016-04-16 06:58:58 -03:00
|
|
|
template float constrain_value<float>(const float amt, const float low, const float high);
|
|
|
|
template double constrain_value<double>(const double amt, const double low, const double high);
|
2016-11-17 01:23:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
simple 16 bit random number generator
|
|
|
|
*/
|
|
|
|
uint16_t get_random16(void)
|
|
|
|
{
|
|
|
|
static uint32_t m_z = 1234;
|
|
|
|
static uint32_t m_w = 76542;
|
2017-01-01 02:28:53 -04:00
|
|
|
m_z = 36969 * (m_z & 0xFFFFu) + (m_z >> 16);
|
|
|
|
m_w = 18000 * (m_w & 0xFFFFu) + (m_w >> 16);
|
2016-11-17 01:23:11 -04:00
|
|
|
return ((m_z << 16) + m_w) & 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2017-04-12 08:16:07 -03:00
|
|
|
|
2021-10-29 22:15:49 -03:00
|
|
|
#if AP_SIM_ENABLED
|
2017-04-12 08:16:07 -03:00
|
|
|
// generate a random float between -1 and 1, for use in SITL
|
|
|
|
float rand_float(void)
|
|
|
|
{
|
2021-10-29 22:15:49 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2017-04-12 08:16:07 -03:00
|
|
|
return ((((unsigned)random()) % 2000000) - 1.0e6) / 1.0e6;
|
2021-10-29 22:15:49 -03:00
|
|
|
#else
|
|
|
|
return get_random16() / 65535.0;
|
|
|
|
#endif
|
2017-04-12 08:16:07 -03:00
|
|
|
}
|
2017-06-21 14:39:07 -03:00
|
|
|
|
|
|
|
Vector3f rand_vec3f(void)
|
|
|
|
{
|
|
|
|
Vector3f v = Vector3f(rand_float(),
|
|
|
|
rand_float(),
|
|
|
|
rand_float());
|
2018-08-14 23:36:10 -03:00
|
|
|
if (!is_zero(v.length())) {
|
2017-06-21 14:39:07 -03:00
|
|
|
v.normalize();
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
2017-04-12 08:16:07 -03:00
|
|
|
#endif
|
2018-05-22 14:33:07 -03:00
|
|
|
|
2018-10-01 00:04:11 -03:00
|
|
|
/*
|
|
|
|
return true if two rotations are equivalent
|
|
|
|
This copes with the fact that we have some duplicates, like ROLL_180_YAW_90 and PITCH_180_YAW_270
|
|
|
|
*/
|
|
|
|
bool rotation_equal(enum Rotation r1, enum Rotation r2)
|
|
|
|
{
|
|
|
|
if (r1 == r2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Vector3f v(1,2,3);
|
|
|
|
Vector3f v1 = v;
|
|
|
|
Vector3f v2 = v;
|
|
|
|
v1.rotate(r1);
|
|
|
|
v2.rotate(r2);
|
|
|
|
return (v1 - v2).length() < 0.001;
|
|
|
|
}
|
|
|
|
|
2020-05-19 02:05:48 -03:00
|
|
|
/*
|
|
|
|
* return a velocity correction (in m/s in NED) for a sensor's position given it's position offsets
|
2020-05-28 22:14:54 -03:00
|
|
|
* this correction should be added to the sensor NED measurement
|
2020-05-19 02:05:48 -03:00
|
|
|
* sensor_offset_bf is in meters in body frame (Foward, Right, Down)
|
|
|
|
* rot_ef_to_bf is a rotation matrix to rotate from earth-frame (NED) to body frame
|
|
|
|
* angular_rate is rad/sec
|
|
|
|
*/
|
2021-05-04 08:12:23 -03:00
|
|
|
Vector3F get_vel_correction_for_sensor_offset(const Vector3F &sensor_offset_bf, const Matrix3F &rot_ef_to_bf, const Vector3F &angular_rate)
|
2020-05-19 02:05:48 -03:00
|
|
|
{
|
|
|
|
if (sensor_offset_bf.is_zero()) {
|
2021-05-04 08:12:23 -03:00
|
|
|
return Vector3F();
|
2020-05-19 02:05:48 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// correct velocity
|
2021-05-04 08:12:23 -03:00
|
|
|
const Vector3F vel_offset_body = angular_rate % sensor_offset_bf;
|
|
|
|
return rot_ef_to_bf.mul_transpose(vel_offset_body) * -1.0;
|
2020-05-19 02:05:48 -03:00
|
|
|
}
|
2019-09-20 21:32:54 -03:00
|
|
|
|
2020-11-08 06:56:41 -04:00
|
|
|
/*
|
|
|
|
calculate a low pass filter alpha value
|
|
|
|
*/
|
|
|
|
float calc_lowpass_alpha_dt(float dt, float cutoff_freq)
|
|
|
|
{
|
2022-12-04 06:35:37 -04:00
|
|
|
if (is_negative(dt) || is_negative(cutoff_freq)) {
|
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::invalid_arg_or_result);
|
2020-11-08 06:56:41 -04:00
|
|
|
return 1.0;
|
|
|
|
}
|
2022-12-04 06:35:37 -04:00
|
|
|
if (is_zero(cutoff_freq)) {
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
if (is_zero(dt)) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
float rc = 1.0f / (M_2PI * cutoff_freq);
|
|
|
|
return dt / (dt + rc);
|
2020-11-08 06:56:41 -04:00
|
|
|
}
|
|
|
|
|
2021-11-29 08:03:38 -04:00
|
|
|
#ifndef AP_MATH_FILL_NANF_USE_MEMCPY
|
|
|
|
#define AP_MATH_FILL_NANF_USE_MEMCPY (CONFIG_HAL_BOARD == HAL_BOARD_SITL)
|
|
|
|
#endif
|
|
|
|
|
2019-09-28 04:31:46 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2019-09-20 21:32:54 -03:00
|
|
|
// fill an array of float with NaN, used to invalidate memory in SITL
|
|
|
|
void fill_nanf(float *f, uint16_t count)
|
|
|
|
{
|
2021-11-29 08:03:38 -04:00
|
|
|
#if AP_MATH_FILL_NANF_USE_MEMCPY
|
|
|
|
static bool created;
|
|
|
|
static float many_nanfs[2048];
|
|
|
|
if (!created) {
|
|
|
|
for (uint16_t i=0; i<ARRAY_SIZE(many_nanfs); i++) {
|
|
|
|
created = true;
|
|
|
|
many_nanfs[i] = std::numeric_limits<float>::signaling_NaN();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count > ARRAY_SIZE(many_nanfs)) {
|
|
|
|
AP_HAL::panic("Too big an area to fill");
|
|
|
|
}
|
|
|
|
memcpy(f, many_nanfs, count*sizeof(many_nanfs[0]));
|
|
|
|
#else
|
2020-11-08 18:43:51 -04:00
|
|
|
const float n = std::numeric_limits<float>::signaling_NaN();
|
2019-09-20 21:32:54 -03:00
|
|
|
while (count--) {
|
2020-11-08 18:43:51 -04:00
|
|
|
*f++ = n;
|
2019-09-20 21:32:54 -03:00
|
|
|
}
|
2021-11-29 08:03:38 -04:00
|
|
|
#endif
|
2019-09-20 21:32:54 -03:00
|
|
|
}
|
2021-05-04 08:12:23 -03:00
|
|
|
|
|
|
|
void fill_nanf(double *f, uint16_t count)
|
|
|
|
{
|
2021-11-29 08:03:38 -04:00
|
|
|
#if AP_MATH_FILL_NANF_USE_MEMCPY
|
|
|
|
static bool created;
|
|
|
|
static double many_nanfs[2048];
|
|
|
|
if (!created) {
|
|
|
|
for (uint16_t i=0; i<ARRAY_SIZE(many_nanfs); i++) {
|
|
|
|
created = true;
|
|
|
|
many_nanfs[i] = std::numeric_limits<double>::signaling_NaN();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count > ARRAY_SIZE(many_nanfs)) {
|
|
|
|
AP_HAL::panic("Too big an area to fill");
|
|
|
|
}
|
|
|
|
memcpy(f, many_nanfs, count*sizeof(many_nanfs[0]));
|
|
|
|
#else
|
2021-05-04 08:12:23 -03:00
|
|
|
while (count--) {
|
2021-07-09 06:41:43 -03:00
|
|
|
*f++ = std::numeric_limits<double>::signaling_NaN();
|
2021-05-04 08:12:23 -03:00
|
|
|
}
|
2019-09-28 04:31:46 -03:00
|
|
|
#endif
|
2021-11-29 08:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2021-02-26 22:36:36 -04:00
|
|
|
|
2021-04-22 12:54:02 -03:00
|
|
|
// Convert 16-bit fixed-point to float
|
|
|
|
float fixed2float(const uint16_t input, const uint8_t fractional_bits)
|
|
|
|
{
|
|
|
|
return ((float)input / (float)(1U << fractional_bits));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert float to 16-bit fixed-point
|
|
|
|
uint16_t float2fixed(const float input, const uint8_t fractional_bits)
|
|
|
|
{
|
|
|
|
return (uint16_t)(roundf(input * (1U << fractional_bits)));
|
|
|
|
}
|
|
|
|
|
2021-02-26 22:36:36 -04:00
|
|
|
/*
|
|
|
|
calculate turn rate in deg/sec given a bank angle and airspeed for a
|
|
|
|
fixed wing aircraft
|
|
|
|
*/
|
|
|
|
float fixedwing_turn_rate(float bank_angle_deg, float airspeed)
|
|
|
|
{
|
|
|
|
bank_angle_deg = constrain_float(bank_angle_deg, -80, 80);
|
|
|
|
return degrees(GRAVITY_MSS*tanf(radians(bank_angle_deg))/MAX(airspeed,1));
|
|
|
|
}
|
2021-12-22 17:26:26 -04:00
|
|
|
|
|
|
|
// convert degrees farenheight to Kelvin
|
|
|
|
float degF_to_Kelvin(float temp_f)
|
|
|
|
{
|
|
|
|
return (temp_f + 459.67) * 0.55556;
|
|
|
|
}
|
2022-07-10 20:18:21 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
conversion functions to prevent undefined behaviour
|
|
|
|
*/
|
|
|
|
int16_t float_to_int16(const float v)
|
|
|
|
{
|
|
|
|
return int16_t(constrain_float(v, INT16_MIN, INT16_MAX));
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t float_to_int32(const float v)
|
|
|
|
{
|
|
|
|
return int32_t(constrain_float(v, INT32_MIN, INT32_MAX));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t float_to_uint16(const float v)
|
|
|
|
{
|
|
|
|
return uint16_t(constrain_float(v, 0, UINT16_MAX));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t float_to_uint32(const float v)
|
|
|
|
{
|
|
|
|
return uint32_t(constrain_float(v, 0, UINT32_MAX));
|
|
|
|
}
|
2022-07-20 22:26:09 -03:00
|
|
|
|
|
|
|
uint32_t double_to_uint32(const double v)
|
|
|
|
{
|
|
|
|
return uint32_t(constrain_double(v, 0, UINT32_MAX));
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t double_to_int32(const double v)
|
|
|
|
{
|
|
|
|
return int32_t(constrain_double(v, INT32_MIN, UINT32_MAX));
|
|
|
|
}
|