2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
22 typedef struct rateLimitFilter_s
{
26 typedef struct pt1Filter_s
{
32 typedef struct pt2Filter_s
{
37 typedef struct pt3Filter_s
{
44 /* this holds the data required to update samples thru a filter */
45 typedef struct biquadFilter_s
{
46 float b0
, b1
, b2
, a1
, a2
;
51 biquadFilter_t biquad
;
71 typedef struct firFilter_s
{
78 typedef struct alphaBetaGammaFilter_s
{
80 float ak
; // derivative of system velociy (ie: acceleration)
81 float vk
; // derivative of system state (ie: velocity)
82 float xk
; // current system state (ie: position)
83 float jk
; // derivative of system acceleration (ie: jerk)
84 float rk
; // residual error
86 float halfLife
, boost
;
87 pt1Filter_t boostFilter
;
88 } alphaBetaGammaFilter_t
;
90 typedef float (*filterApplyFnPtr
)(void *filter
, float input
);
91 typedef float (*filterApply4FnPtr
)(void *filter
, float input
, float f_cut
, float dt
);
93 #define BIQUAD_BANDWIDTH 1.9f /* bandwidth in octaves */
94 #define BIQUAD_Q 1.0f / sqrtf(2.0f) /* quality factor - butterworth*/
96 float nullFilterApply(void *filter
, float input
);
97 float nullFilterApply4(void *filter
, float input
, float f_cut
, float dt
);
99 void pt1FilterInit(pt1Filter_t
*filter
, float f_cut
, float dT
);
100 void pt1FilterInitRC(pt1Filter_t
*filter
, float tau
, float dT
);
101 void pt1FilterSetTimeConstant(pt1Filter_t
*filter
, float tau
);
102 void pt1FilterUpdateCutoff(pt1Filter_t
*filter
, float f_cut
);
103 float pt1FilterGetLastOutput(pt1Filter_t
*filter
);
104 float pt1FilterApply(pt1Filter_t
*filter
, float input
);
105 float pt1FilterApply3(pt1Filter_t
*filter
, float input
, float dT
);
106 float pt1FilterApply4(pt1Filter_t
*filter
, float input
, float f_cut
, float dt
);
107 void pt1FilterReset(pt1Filter_t
*filter
, float input
);
112 float pt2FilterGain(float f_cut
, float dT
);
113 void pt2FilterInit(pt2Filter_t
*filter
, float k
);
114 void pt2FilterUpdateCutoff(pt2Filter_t
*filter
, float k
);
115 float pt2FilterApply(pt2Filter_t
*filter
, float input
);
120 float pt3FilterGain(float f_cut
, float dT
);
121 void pt3FilterInit(pt3Filter_t
*filter
, float k
);
122 void pt3FilterUpdateCutoff(pt3Filter_t
*filter
, float k
);
123 float pt3FilterApply(pt3Filter_t
*filter
, float input
);
125 void rateLimitFilterInit(rateLimitFilter_t
*filter
);
126 float rateLimitFilterApply4(rateLimitFilter_t
*filter
, float input
, float rate_limit
, float dT
);
128 void biquadFilterInitNotch(biquadFilter_t
*filter
, uint32_t samplingIntervalUs
, uint16_t filterFreq
, uint16_t cutoffHz
);
129 void biquadFilterInitLPF(biquadFilter_t
*filter
, uint16_t filterFreq
, uint32_t samplingIntervalUs
);
130 void biquadFilterInit(biquadFilter_t
*filter
, uint16_t filterFreq
, uint32_t samplingIntervalUs
, float Q
, biquadFilterType_e filterType
);
131 float biquadFilterApply(biquadFilter_t
*filter
, float sample
);
132 float biquadFilterReset(biquadFilter_t
*filter
, float value
);
133 float biquadFilterApplyDF1(biquadFilter_t
*filter
, float input
);
134 float filterGetNotchQ(float centerFrequencyHz
, float cutoffFrequencyHz
);
135 void biquadFilterUpdate(biquadFilter_t
*filter
, float filterFreq
, uint32_t refreshRate
, float Q
, biquadFilterType_e filterType
);
137 void alphaBetaGammaFilterInit(alphaBetaGammaFilter_t
*filter
, float alpha
, float boostGain
, float halfLife
, float dT
);
138 float alphaBetaGammaFilterApply(alphaBetaGammaFilter_t
*filter
, float input
);
140 void initFilter(uint8_t filterType
, filter_t
*filter
, float cutoffFrequency
, uint32_t refreshRate
);
141 void assignFilterApplyFn(uint8_t filterType
, float cutoffFrequency
, filterApplyFnPtr
*applyFn
);