-
Notifications
You must be signed in to change notification settings - Fork 17.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Filter: LowPass: use calc_lowpass_alpha_dt
in apply
and prefix all class varables with _
#27582
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
#pragma GCC optimize("O2") | ||
#endif | ||
#include "LowPassFilter.h" | ||
#include <AP_InternalError/AP_InternalError.h> | ||
|
||
//////////////////////////////////////////////////////////////////////////////////////////// | ||
// DigitalLPF | ||
|
@@ -24,33 +23,15 @@ DigitalLPF<T>::DigitalLPF() { | |
// add a new raw value to the filter, retrieve the filtered result | ||
template <class T> | ||
T DigitalLPF<T>::apply(const T &sample, float cutoff_freq, float dt) { | ||
if (is_negative(cutoff_freq) || is_negative(dt)) { | ||
INTERNAL_ERROR(AP_InternalError::error_t::invalid_arg_or_result); | ||
_output = sample; | ||
return _output; | ||
} | ||
if (is_zero(cutoff_freq)) { | ||
_output = sample; | ||
return _output; | ||
} | ||
if (is_zero(dt)) { | ||
return _output; | ||
} | ||
float rc = 1.0f/(M_2PI*cutoff_freq); | ||
alpha = constrain_float(dt/(dt+rc), 0.0f, 1.0f); | ||
_output += (sample - _output) * alpha; | ||
if (!initialised) { | ||
initialised = true; | ||
_output = sample; | ||
} | ||
return _output; | ||
_alpha = calc_lowpass_alpha_dt(dt, cutoff_freq); | ||
return apply(sample); | ||
} | ||
|
||
template <class T> | ||
T DigitalLPF<T>::apply(const T &sample) { | ||
_output += (sample - _output) * alpha; | ||
if (!initialised) { | ||
initialised = true; | ||
_output += (sample - _output) * _alpha; | ||
if (!_initialised) { | ||
_initialised = true; | ||
_output = sample; | ||
} | ||
return _output; | ||
|
@@ -59,9 +40,9 @@ T DigitalLPF<T>::apply(const T &sample) { | |
template <class T> | ||
void DigitalLPF<T>::compute_alpha(float sample_freq, float cutoff_freq) { | ||
if (sample_freq <= 0) { | ||
alpha = 1; | ||
_alpha = 1; | ||
} else { | ||
alpha = calc_lowpass_alpha_dt(1.0/sample_freq, cutoff_freq); | ||
_alpha = calc_lowpass_alpha_dt(1.0/sample_freq, cutoff_freq); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note that we are already using the |
||
} | ||
} | ||
|
||
|
@@ -74,7 +55,7 @@ const T &DigitalLPF<T>::get() const { | |
template <class T> | ||
void DigitalLPF<T>::reset(T value) { | ||
_output = value; | ||
initialised = true; | ||
_initialised = true; | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////////////////// | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,18 +59,18 @@ class DigitalLPF { | |
CLASS_NO_COPY(DigitalLPF); | ||
|
||
void compute_alpha(float sample_freq, float cutoff_freq); | ||
|
||
// get latest filtered value from filter (equal to the value returned by latest call to apply method) | ||
const T &get() const; | ||
void reset(T value); | ||
void reset() { | ||
initialised = false; | ||
_initialised = false; | ||
} | ||
|
||
private: | ||
T _output; | ||
float alpha = 1.0f; | ||
bool initialised; | ||
float _alpha = 1.0f; | ||
bool _initialised; | ||
Comment on lines
+72
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we were trying to canonicalise the other way, to remove leading underscores over time |
||
}; | ||
|
||
// LPF base class | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this constrain be added to calc_lowpass_alpha_dt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See: #20911
The constrain will never trigger because we have already checked both values are > 0.