PX4 Firmware
PX4 Autopilot Software http://px4.io
KalmanFilter.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2013-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 
34 /*
35  * @file KalmanFilter.h
36  * Simple Kalman Filter for variable gain low-passing
37  *
38  * @author Nicolas de Palezieux (Sunflower Labs) <ndepal@gmail.com>
39  *
40  */
41 
42 #include "KalmanFilter.h"
43 
45 {
47 {
48  init(initial, covInit);
49 }
50 
52 {
53  _x = initial;
54  _covariance = covInit;
55 }
56 
57 void KalmanFilter::init(float initial0, float initial1, float covInit00, float covInit11)
58 {
60  initial(0) = initial0;
61  initial(1) = initial1;
63  covInit(0, 0) = covInit00;
64  covInit(1, 1) = covInit11;
65 
66  init(initial, covInit);
67 }
68 
69 void KalmanFilter::predict(float dt, float acc, float acc_unc)
70 {
71  _x(0) += _x(1) * dt + dt * dt / 2 * acc;
72  _x(1) += acc * dt;
73 
74  matrix::Matrix<float, 2, 2> A; // propagation matrix
75  A(0, 0) = 1;
76  A(1, 1) = 1;
77  A(0, 1) = dt;
78 
79  matrix::Matrix<float, 2, 1> G; // noise model
80  G(0, 0) = dt * dt / 2;
81  G(1, 0) = dt;
82 
83  matrix::Matrix<float, 2, 2> process_noise = G * G.transpose() * acc_unc;
84 
85  _covariance = A * _covariance * A.transpose() + process_noise;
86 }
87 
88 bool KalmanFilter::update(float meas, float measUnc)
89 {
90 
91  // H = [1, 0]
92  _residual = meas - _x(0);
93 
94  // H * P * H^T simply selects P(0,0)
95  _innovCov = _covariance(0, 0) + measUnc;
96 
97  // outlier rejection
98  float beta = _residual / _innovCov * _residual;
99 
100  // 5% false alarm probability
101  if (beta > 3.84f) {
102  return false;
103  }
104 
105  matrix::Vector<float, 2> kalmanGain;
106  kalmanGain(0) = _covariance(0, 0);
107  kalmanGain(1) = _covariance(1, 0);
108  kalmanGain /= _innovCov;
109 
110  _x += kalmanGain * _residual;
111 
113  identity.identity();
114 
115  matrix::Matrix<float, 2, 2> KH; // kalmanGain * H
116  KH(0, 0) = kalmanGain(0);
117  KH(1, 0) = kalmanGain(1);
118 
119  _covariance = (identity - KH) * _covariance;
120 
121  return true;
122 
123 }
124 
126 {
127  state = _x;
128 }
129 
130 void KalmanFilter::getState(float &state0, float &state1)
131 {
132  state0 = _x(0);
133  state1 = _x(1);
134 }
135 
137 {
138  covariance = _covariance;
139 }
140 
141 void KalmanFilter::getCovariance(float &cov00, float &cov11)
142 {
143  cov00 = _covariance(0, 0);
144  cov11 = _covariance(1, 1);
145 }
146 
147 void KalmanFilter::getInnovations(float &innov, float &innovCov)
148 {
149  innov = _residual;
150  innovCov = _innovCov;
151 }
152 
153 } // namespace landing_target_estimator
matrix::Vector< float, 2 > _x
Definition: KalmanFilter.h:142
void predict(float dt, float acc, float acc_unc)
Predict the state with an external acceleration estimate.
static enum @74 state
void init(matrix::Vector< float, 2 > &initial, matrix::Matrix< float, 2, 2 > &covInit)
Initialize filter state.
KalmanFilter()
Default constructor, state not initialized.
Definition: KalmanFilter.h:64
bool update(float meas, float measUnc)
Update the state estimate with a measurement.
Matrix< Type, N, M > transpose() const
Definition: Matrix.hpp:353
void getInnovations(float &innov, float &innovCov)
Get measurement innovation and covariance of last update call.
matrix::Matrix< float, 2, 2 > _covariance
Definition: KalmanFilter.h:144
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
void getState(matrix::Vector< float, 2 > &state)
Get the current filter state.
void getCovariance(matrix::Matrix< float, 2, 2 > &covariance)
Get state covariance.
float dt
Definition: px4io.c:73
void identity()
Definition: Matrix.hpp:457