diff --git a/libraries/AP_Math/AP_Math.cpp b/libraries/AP_Math/AP_Math.cpp index fbcbd17531..0e6ce9c1af 100644 --- a/libraries/AP_Math/AP_Math.cpp +++ b/libraries/AP_Math/AP_Math.cpp @@ -382,6 +382,18 @@ Vector3f get_vel_correction_for_sensor_offset(const Vector3f &sensor_offset_bf, return rot_ef_to_bf.mul_transpose(vel_offset_body) * -1.0f; } +/* + calculate a low pass filter alpha value + */ +float calc_lowpass_alpha_dt(float dt, float cutoff_freq) +{ + if (dt <= 0.0f || cutoff_freq <= 0.0f) { + return 1.0; + } + float rc = 1.0f/(M_2PI*cutoff_freq); + return constrain_float(dt/(dt+rc), 0.0f, 1.0f); +} + #if CONFIG_HAL_BOARD == HAL_BOARD_SITL // fill an array of float with NaN, used to invalidate memory in SITL void fill_nanf(float *f, uint16_t count) diff --git a/libraries/AP_Math/AP_Math.h b/libraries/AP_Math/AP_Math.h index 2919926609..3ade72b377 100644 --- a/libraries/AP_Math/AP_Math.h +++ b/libraries/AP_Math/AP_Math.h @@ -290,6 +290,11 @@ bool rotation_equal(enum Rotation r1, enum Rotation r2) WARN_IF_UNUSED; */ Vector3f get_vel_correction_for_sensor_offset(const Vector3f &sensor_offset_bf, const Matrix3f &rot_ef_to_bf, const Vector3f &angular_rate); +/* + calculate a low pass filter alpha value + */ +float calc_lowpass_alpha_dt(float dt, float cutoff_freq); + #if CONFIG_HAL_BOARD == HAL_BOARD_SITL // fill an array of float with NaN, used to invalidate memory in SITL void fill_nanf(float *f, uint16_t count);