PX4 Firmware
PX4 Autopilot Software http://px4.io
LowPassFilter2p.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 #include "LowPassFilter2p.hpp"
35 
36 #include <px4_platform_common/defines.h>
37 
38 #include <math.h>
39 
40 namespace math
41 {
42 
43 void LowPassFilter2p::set_cutoff_frequency(float sample_freq, float cutoff_freq)
44 {
45  _cutoff_freq = cutoff_freq;
46 
47  // reset delay elements on filter change
48  _delay_element_1 = 0.0f;
49  _delay_element_2 = 0.0f;
50 
51  if (_cutoff_freq <= 0.0f) {
52  // no filtering
53  _b0 = 1.0f;
54  _b1 = 0.0f;
55  _b2 = 0.0f;
56 
57  _a1 = 0.0f;
58  _a2 = 0.0f;
59 
60  return;
61  }
62 
63  const float fr = sample_freq / _cutoff_freq;
64  const float ohm = tanf(M_PI_F / fr);
65  const float c = 1.0f + 2.0f * cosf(M_PI_F / 4.0f) * ohm + ohm * ohm;
66 
67  _b0 = ohm * ohm / c;
68  _b1 = 2.0f * _b0;
69  _b2 = _b0;
70 
71  _a1 = 2.0f * (ohm * ohm - 1.0f) / c;
72  _a2 = (1.0f - 2.0f * cosf(M_PI_F / 4.0f) * ohm + ohm * ohm) / c;
73 }
74 
76 {
77  // do the filtering
78  float delay_element_0 = sample - _delay_element_1 * _a1 - _delay_element_2 * _a2;
79 
80  if (!PX4_ISFINITE(delay_element_0)) {
81  // don't allow bad values to propagate via the filter
82  delay_element_0 = sample;
83  }
84 
85  const float output = delay_element_0 * _b0 + _delay_element_1 * _b1 + _delay_element_2 * _b2;
86 
88  _delay_element_1 = delay_element_0;
89 
90  // return the value. Should be no need to check limits
91  return output;
92 }
93 
95 {
96  const float dval = sample / (_b0 + _b1 + _b2);
97 
98  if (PX4_ISFINITE(dval)) {
99  _delay_element_1 = dval;
100  _delay_element_2 = dval;
101 
102  } else {
103  _delay_element_1 = sample;
104  _delay_element_2 = sample;
105  }
106 
107  return apply(sample);
108 }
109 
110 } // namespace math
void set_cutoff_frequency(float sample_freq, float cutoff_freq)
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
float reset(float sample)
float apply(float sample)
Add a new raw value to the filter.
#define M_PI_F
Definition: ashtech.cpp:44