Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
low_pass_filter.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013 Gautier Hattenberger
3 *
4 * This file is part of paparazzi.
5 *
6 * paparazzi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * paparazzi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with paparazzi; see the file COPYING. If not, write to
18 * the Free Software Foundation, 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
30#ifndef LOW_PASS_FILTER_H
31#define LOW_PASS_FILTER_H
32
33#include "std.h"
35
36#define INT32_FILT_FRAC 8
37
44 float last_in;
45 float last_out;
46};
47
60static inline void init_first_order_low_pass(struct FirstOrderLowPass *filter, float tau, const float sample_time,
61 float value)
62{
63 filter->last_in = value;
64 filter->last_out = value;
65 filter->time_const = 2.0f * tau / sample_time;
66}
67
74static inline float update_first_order_low_pass(struct FirstOrderLowPass *filter, const float value)
75{
76 float out = (value + filter->last_in + (filter->time_const - 1.0f) * filter->last_out) / (1.0f + filter->time_const);
77 filter->last_in = value;
78 filter->last_out = out;
79 return out;
80}
81
89static inline float reset_first_order_low_pass(struct FirstOrderLowPass *filter, const float value)
90{
91 filter->last_in = value;
92 filter->last_out = value;
93 return value;
94}
95
101static inline float get_first_order_low_pass(const struct FirstOrderLowPass *filter)
102{
103 return filter->last_out;
104}
105
112static inline void update_first_order_low_pass_tau(struct FirstOrderLowPass *filter, const float tau,
113 const float sample_time)
114{
115 filter->time_const = 2.0f * tau / sample_time;
116}
117
155 float a[2];
156 float b[2];
157 float i[2];
158 float o[2];
159};
160
161
170static inline void init_second_order_low_pass(struct SecondOrderLowPass *filter, const float tau, const float Q,
171 const float sample_time,
172 float value)
173{
174 float K = tanf(sample_time / (2.0f * tau));
175 float poly = K * K + K / Q + 1.0f;
176 filter->a[0] = 2.0f * (K * K - 1.0f) / poly;
177 filter->a[1] = (K * K - K / Q + 1.0f) / poly;
178 filter->b[0] = K * K / poly;
179 filter->b[1] = 2.0f * filter->b[0];
180 filter->i[0] = filter->i[1] = filter->o[0] = filter->o[1] = value;
181}
182
190static inline float reset_second_order_low_pass(struct SecondOrderLowPass *filter, const float value)
191{
192 filter->i[0] = filter->i[1] = filter->o[0] = filter->o[1] = value;
193 return value;
194}
195
202static inline float update_second_order_low_pass(struct SecondOrderLowPass *filter, const float value)
203{
204 float out = filter->b[0] * value
205 + filter->b[1] * filter->i[0]
206 + filter->b[0] * filter->i[1]
207 - filter->a[0] * filter->o[0]
208 - filter->a[1] * filter->o[1];
209 filter->i[1] = filter->i[0];
210 filter->i[0] = value;
211 filter->o[1] = filter->o[0];
212 filter->o[0] = out;
213 return out;
214}
215
221static inline float get_second_order_low_pass(const struct SecondOrderLowPass *filter)
222{
223 return filter->o[0];
224}
225
233
242static inline void init_second_order_low_pass_int(struct SecondOrderLowPass_int *filter, const float cut_off,
243 const float Q,
244 float sample_time, int32_t value)
245{
247 float tau = 7.0f / (44.0f * cut_off);
248 float K = sample_time / (2.0f * tau);
249 float poly = K * K + K / Q + 1.0f;
250 float loop_gain_f;
251
252 filter_temp.a[0] = 2.0f * (K * K - 1.0f) / poly;
253 filter_temp.a[1] = (K * K - K / Q + 1.0f) / poly;
254 filter_temp.b[0] = K * K / poly;
255 filter_temp.b[1] = 2.0f * filter_temp.b[0];
256 loop_gain_f = 1.0f / filter_temp.b[0];
257
258 filter->a[0] = BFP_OF_REAL((filter_temp.a[0] * loop_gain_f), INT32_FILT_FRAC);
259 filter->a[1] = BFP_OF_REAL((filter_temp.a[1] * loop_gain_f), INT32_FILT_FRAC);
260 filter->b[0] = BFP_OF_REAL(1, INT32_FILT_FRAC);
261 filter->b[1] = 2 * filter->b[0];
262 filter->i[0] = filter->i[1] = filter->o[0] = filter->o[1] = value;
264}
265
273{
274 int32_t out = filter->b[0] * value
275 + filter->b[1] * filter->i[0]
276 + filter->b[0] * filter->i[1]
277 - filter->a[0] * filter->o[0]
278 - filter->a[1] * filter->o[1];
279
280 filter->i[1] = filter->i[0];
281 filter->i[0] = value;
282 filter->o[1] = filter->o[0];
283 filter->o[0] = out / (filter->loop_gain);
284 return filter->o[0];
285}
286
293{
294 return filter->o[0];
295}
296
300
313static inline void init_butterworth_2_low_pass(Butterworth2LowPass *filter, const float tau, const float sample_time,
314 const float value)
315{
316 init_second_order_low_pass((struct SecondOrderLowPass *)filter, tau, 0.7071, sample_time, value);
317}
318
325static inline float update_butterworth_2_low_pass(Butterworth2LowPass *filter, const float value)
326{
327 return update_second_order_low_pass((struct SecondOrderLowPass *)filter, value);
328}
329
337static inline float reset_butterworth_2_low_pass(Butterworth2LowPass *filter, const float value)
338{
339 return reset_second_order_low_pass((struct SecondOrderLowPass *)filter, value);
340}
341
347static inline float get_butterworth_2_low_pass(const Butterworth2LowPass *filter)
348{
349 return get_second_order_low_pass((const struct SecondOrderLowPass *)filter);
350}
351
355
369 const float sample_time,
370 int32_t value)
371{
373}
374
382{
383 return update_second_order_low_pass_int((struct SecondOrderLowPass_int *)filter, value);
384}
385
392{
393 return filter->o[0];
394}
395
400typedef struct {
404
418static inline void init_butterworth_4_low_pass(Butterworth4LowPass *filter, const float tau, const float sample_time,
419 const float value)
420{
421 init_second_order_low_pass(&filter->lp1, tau, 1.30651, sample_time, value);
422 init_second_order_low_pass(&filter->lp2, tau, 0.51184, sample_time, value);
423}
424
433static inline float update_butterworth_4_low_pass(Butterworth4LowPass *filter, const float value)
434{
435 float tmp = update_second_order_low_pass(&filter->lp1, value);
436 return update_second_order_low_pass(&filter->lp2, tmp);
437}
438
444static inline float get_butterworth_4_low_pass(const Butterworth4LowPass *filter)
445{
446 return filter->lp2.o[0];
447}
448
455static inline void reset_butterworth_4_low_pass(Butterworth4LowPass *filter, const float value)
456{
457 filter->lp1.i[0] = filter->lp1.i[1] = filter->lp1.o[0] = filter->lp1.o[1] = filter->lp2.i[0] = filter->lp2.i[1] =
458 filter->lp2.o[0] = filter->lp2.o[1] = value;
459}
460
469
484 const float sample_time,
485 int32_t value)
486{
487 init_second_order_low_pass_int(&filter->lp1, cut_off, 1.30651, sample_time, value);
488 init_second_order_low_pass_int(&filter->lp2, cut_off, 0.51184, sample_time, value);
489}
490
504
511{
512 return filter->lp2.o[0];
513}
514
515#endif
516
#define BFP_OF_REAL(_vr, _frac)
int32_t loop_gain
loop gain
static float get_second_order_low_pass(const struct SecondOrderLowPass *filter)
Get current value of the second order low pass filter.
static int32_t get_second_order_low_pass_int(const struct SecondOrderLowPass_int *filter)
Get current value of the second order low pass filter(fixed point version).
float o[2]
output history
static int32_t update_second_order_low_pass_int(struct SecondOrderLowPass_int *filter, const int32_t value)
Update second order low pass filter state with a new value(fixed point version).
static void update_first_order_low_pass_tau(struct FirstOrderLowPass *filter, const float tau, const float sample_time)
Update time constant (tau parameter) for first order low pass filter.
static int32_t get_butterworth_4_low_pass_int(const Butterworth4LowPass_int *filter)
Get current value of the fourth order Butterworth low pass filter(fixed point version).
static void reset_butterworth_4_low_pass(Butterworth4LowPass *filter, const float value)
Reset a Butterworth low-pass filter to a specific value.
static float reset_first_order_low_pass(struct FirstOrderLowPass *filter, const float value)
Reset the first order low-pass filter to a specific value.
static int32_t update_butterworth_2_low_pass_int(Butterworth2LowPass_int *filter, const int32_t value)
Update second order Butterworth low pass filter state with a new value(fixed point version).
static float get_butterworth_4_low_pass(const Butterworth4LowPass *filter)
Get current value of the fourth order Butterworth low pass filter.
static void init_first_order_low_pass(struct FirstOrderLowPass *filter, float tau, const float sample_time, float value)
Init first order low pass filter.
struct SecondOrderLowPass lp2
static float update_first_order_low_pass(struct FirstOrderLowPass *filter, const float value)
Update first order low pass filter state with a new value.
static float update_second_order_low_pass(struct SecondOrderLowPass *filter, const float value)
Update second order low pass filter state with a new value.
#define INT32_FILT_FRAC
static void init_butterworth_2_low_pass(Butterworth2LowPass *filter, const float tau, const float sample_time, const float value)
Init a second order Butterworth filter.
static int32_t get_butterworth_2_low_pass_int(const Butterworth2LowPass_int *filter)
Get current value of the second order Butterworth low pass filter(fixed point version).
static float reset_second_order_low_pass(struct SecondOrderLowPass *filter, const float value)
Reset the second order low-pass filter to a specific value.
static float reset_butterworth_2_low_pass(Butterworth2LowPass *filter, const float value)
Reset a Butterworth low-pass filter to a specific value.
float a[2]
denominator gains
static float get_first_order_low_pass(const struct FirstOrderLowPass *filter)
Get current value of the first order low pass filter.
struct SecondOrderLowPass_int lp1
int32_t o[2]
output history
static void init_butterworth_2_low_pass_int(Butterworth2LowPass_int *filter, const float cut_off, const float sample_time, int32_t value)
Init a second order Butterworth filter.
static float update_butterworth_2_low_pass(Butterworth2LowPass *filter, const float value)
Update second order Butterworth low pass filter state with a new value.
int32_t b[2]
numerator gains
static int32_t update_butterworth_4_low_pass_int(Butterworth4LowPass_int *filter, const int32_t value)
Update fourth order Butterworth low pass filter state with a new value(fixed point version).
static void init_second_order_low_pass_int(struct SecondOrderLowPass_int *filter, const float cut_off, const float Q, float sample_time, int32_t value)
Init second order low pass filter(fixed point version).
struct SecondOrderLowPass_int lp2
static float update_butterworth_4_low_pass(Butterworth4LowPass *filter, const float value)
Update fourth order Butterworth low pass filter state with a new value.
static void init_butterworth_4_low_pass_int(Butterworth4LowPass_int *filter, const float cut_off, const float sample_time, int32_t value)
Init a fourth order Butterworth filter(fixed point version).
int32_t i[2]
input history
float b[2]
numerator gains
struct SecondOrderLowPass lp1
int32_t a[2]
denominator gains
static float get_butterworth_2_low_pass(const Butterworth2LowPass *filter)
Get current value of the second order Butterworth low pass filter.
float i[2]
input history
static void init_butterworth_4_low_pass(Butterworth4LowPass *filter, const float tau, const float sample_time, const float value)
Init a fourth order Butterworth filter.
static void init_second_order_low_pass(struct SecondOrderLowPass *filter, const float tau, const float Q, const float sample_time, float value)
Init second order low pass filter.
Fourth order Butterworth low pass filter.
Fourth order Butterworth low pass filter(fixed point version).
First order low pass filter structure.
Second order low pass filter structure.
uint16_t foo
Definition main_demo5.c:58
Paparazzi fixed point algebra.
static float K[9]
int int32_t
Typedef defining 32 bit int type.