Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 8 additions & 27 deletions libraries/Filter/LowPassFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#pragma GCC optimize("O2")
#endif
#include "LowPassFilter.h"
#include <AP_InternalError/AP_InternalError.h>

////////////////////////////////////////////////////////////////////////////////////////////
// DigitalLPF
Expand All @@ -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);
Copy link
Collaborator

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?

Copy link
Member Author

@IamPete1 IamPete1 Jul 18, 2024

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.

_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;
Expand All @@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that we are already using the calc_lowpass_alpha_dt function in this case.

}
}

Expand All @@ -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;
}

////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
8 changes: 4 additions & 4 deletions libraries/Filter/LowPassFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down
Loading