2019-06-17 05:43:08 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
#include <cmath>
|
|
|
|
#include <AP_Param/AP_Param.h>
|
|
|
|
#include "NotchFilter.h"
|
|
|
|
|
2019-10-10 14:36:09 -03:00
|
|
|
#define HNF_MAX_HARMONICS 8
|
|
|
|
|
2019-08-28 17:18:00 -03:00
|
|
|
/*
|
|
|
|
a filter that manages a set of notch filters targetted at a fundamental center frequency
|
|
|
|
and multiples of that fundamental frequency
|
|
|
|
*/
|
2019-06-17 05:43:08 -03:00
|
|
|
template <class T>
|
|
|
|
class HarmonicNotchFilter {
|
|
|
|
public:
|
2019-08-28 17:18:00 -03:00
|
|
|
~HarmonicNotchFilter();
|
|
|
|
// allocate a bank of notch filters for this harmonic notch filter
|
2021-11-13 18:39:35 -04:00
|
|
|
void allocate_filters(uint8_t num_notches, uint8_t harmonics, bool double_notch);
|
2019-08-28 17:18:00 -03:00
|
|
|
// initialize the underlying filters using the provided filter parameters
|
2019-06-17 05:43:08 -03:00
|
|
|
void init(float sample_freq_hz, float center_freq_hz, float bandwidth_hz, float attenuation_dB);
|
2019-08-28 17:18:00 -03:00
|
|
|
// update the underlying filters' center frequencies using center_freq_hz as the fundamental
|
|
|
|
void update(float center_freq_hz);
|
2020-05-29 13:28:43 -03:00
|
|
|
// update all o fthe underlying center frequencies individually
|
|
|
|
void update(uint8_t num_centers, const float center_freq_hz[]);
|
2019-08-28 17:18:00 -03:00
|
|
|
// apply a sample to each of the underlying filters in turn
|
2019-06-17 05:43:08 -03:00
|
|
|
T apply(const T &sample);
|
2019-08-28 17:18:00 -03:00
|
|
|
// reset each of the underlying filters
|
2019-06-17 05:43:08 -03:00
|
|
|
void reset();
|
|
|
|
|
|
|
|
private:
|
2019-08-28 17:18:00 -03:00
|
|
|
// underlying bank of notch filters
|
2019-06-17 05:43:08 -03:00
|
|
|
NotchFilter<T>* _filters;
|
2019-08-28 17:18:00 -03:00
|
|
|
// sample frequency for each filter
|
2019-06-17 05:43:08 -03:00
|
|
|
float _sample_freq_hz;
|
2019-12-27 08:06:21 -04:00
|
|
|
// base double notch bandwidth for each filter
|
|
|
|
float _notch_spread;
|
2019-08-28 17:18:00 -03:00
|
|
|
// attenuation for each filter
|
2019-06-17 05:43:08 -03:00
|
|
|
float _A;
|
2019-08-28 17:18:00 -03:00
|
|
|
// quality factor of each filter
|
2019-06-17 05:43:08 -03:00
|
|
|
float _Q;
|
2019-08-28 17:18:00 -03:00
|
|
|
// a bitmask of the harmonics to use
|
2019-06-17 05:43:08 -03:00
|
|
|
uint8_t _harmonics;
|
2019-12-27 08:06:21 -04:00
|
|
|
// whether to use double-notches
|
|
|
|
bool _double_notch;
|
2019-08-28 17:18:00 -03:00
|
|
|
// number of allocated filters
|
2019-06-17 05:43:08 -03:00
|
|
|
uint8_t _num_filters;
|
2021-11-13 18:39:35 -04:00
|
|
|
// pre-calculated number of harmonics
|
|
|
|
uint8_t _num_harmonics;
|
2019-08-30 04:34:36 -03:00
|
|
|
// number of enabled filters
|
|
|
|
uint8_t _num_enabled_filters;
|
2019-06-17 05:43:08 -03:00
|
|
|
bool _initialised;
|
|
|
|
};
|
|
|
|
|
2019-11-17 01:16:06 -04:00
|
|
|
// Harmonic notch update mode
|
|
|
|
enum class HarmonicNotchDynamicMode {
|
|
|
|
Fixed = 0,
|
|
|
|
UpdateThrottle = 1,
|
|
|
|
UpdateRPM = 2,
|
|
|
|
UpdateBLHeli = 3,
|
2019-07-18 16:19:08 -03:00
|
|
|
UpdateGyroFFT = 4,
|
2019-11-17 01:16:06 -04:00
|
|
|
};
|
|
|
|
|
2019-06-17 05:43:08 -03:00
|
|
|
/*
|
2019-08-28 17:18:00 -03:00
|
|
|
harmonic notch filter configuration parameters
|
2019-06-17 05:43:08 -03:00
|
|
|
*/
|
|
|
|
class HarmonicNotchFilterParams : public NotchFilterParams {
|
|
|
|
public:
|
2019-12-27 08:06:21 -04:00
|
|
|
enum class Options {
|
|
|
|
DoubleNotch = 1<<0,
|
2020-05-29 13:28:43 -03:00
|
|
|
DynamicHarmonic = 1<<1,
|
2021-05-12 13:33:11 -03:00
|
|
|
LoopRateUpdate = 2<<1,
|
2019-12-27 08:06:21 -04:00
|
|
|
};
|
|
|
|
|
2019-06-17 05:43:08 -03:00
|
|
|
HarmonicNotchFilterParams(void);
|
2019-08-28 17:18:00 -03:00
|
|
|
// set the fundamental center frequency of the harmonic notch
|
2019-06-17 05:43:08 -03:00
|
|
|
void set_center_freq_hz(float center_freq) { _center_freq_hz.set(center_freq); }
|
2019-08-28 17:18:00 -03:00
|
|
|
// harmonics enabled on the harmonic notch
|
2021-11-13 18:39:35 -04:00
|
|
|
uint8_t harmonics(void) const { return _harmonics; }
|
|
|
|
// has the user set the harmonics value
|
|
|
|
void set_default_harmonics(uint8_t hmncs) { _harmonics.set_default(hmncs); }
|
2019-08-28 17:18:00 -03:00
|
|
|
// reference value of the harmonic notch
|
2019-06-17 05:43:08 -03:00
|
|
|
float reference(void) const { return _reference; }
|
2019-12-27 08:06:21 -04:00
|
|
|
// notch options
|
|
|
|
bool hasOption(Options option) const { return _options & uint16_t(option); }
|
2019-10-10 14:36:09 -03:00
|
|
|
// notch dynamic tracking mode
|
2019-11-17 01:16:06 -04:00
|
|
|
HarmonicNotchDynamicMode tracking_mode(void) const { return HarmonicNotchDynamicMode(_tracking_mode.get()); }
|
2019-06-17 05:43:08 -03:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
|
|
|
private:
|
2020-07-12 16:10:13 -03:00
|
|
|
// configured notch harmonics
|
2019-06-17 05:43:08 -03:00
|
|
|
AP_Int8 _harmonics;
|
2019-08-28 17:18:00 -03:00
|
|
|
// notch reference value
|
2019-06-17 05:43:08 -03:00
|
|
|
AP_Float _reference;
|
2019-10-10 14:36:09 -03:00
|
|
|
// notch dynamic tracking mode
|
|
|
|
AP_Int8 _tracking_mode;
|
2019-12-27 08:06:21 -04:00
|
|
|
// notch options
|
|
|
|
AP_Int16 _options;
|
2019-06-17 05:43:08 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef HarmonicNotchFilter<Vector3f> HarmonicNotchFilterVector3f;
|
|
|
|
|