PX4 Firmware
PX4 Autopilot Software http://px4.io
LowPassFilter2pVector3f.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2018 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 
35 
36 #include <px4_platform_common/defines.h>
37 
38 #include <math.h>
39 
40 namespace math
41 {
42 
43 void LowPassFilter2pVector3f::set_cutoff_frequency(float sample_freq, float cutoff_freq)
44 {
45  _cutoff_freq = cutoff_freq;
46 
47  // reset delay elements on filter change
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  const matrix::Vector3f dval = sample / (_b0 + _b1 + _b2);
78 
79  if (PX4_ISFINITE(dval(0)) && PX4_ISFINITE(dval(1)) && PX4_ISFINITE(dval(2))) {
80  _delay_element_1 = dval;
81  _delay_element_2 = dval;
82 
83  } else {
84  _delay_element_1 = sample;
85  _delay_element_2 = sample;
86  }
87 
88  return apply(sample);
89 }
90 
91 } // namespace math
A class to implement a second order low pass filter on a Vector3f Based on LowPassFilter2p.hpp by Leonard Hall LeonardTHall@gmail.com
void set_cutoff_frequency(float sample_freq, float cutoff_freq)
matrix::Vector3f reset(const matrix::Vector3f &sample)
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
void zero()
Definition: Matrix.hpp:421
matrix::Vector3f apply(const matrix::Vector3f &sample)
Add a new raw value to the filter.
#define M_PI_F
Definition: ashtech.cpp:44