PX4 Firmware
PX4 Autopilot Software http://px4.io
KalmanFilter.h
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  * Constant velocity model. Prediction according to
39  * x_{k+1} = A * x_{k}
40  * with A = [1 dt; 0 1]
41  *
42  * Update with a direct measurement of the first state:
43  * H = [1 0]
44  *
45  * @author Nicolas de Palezieux (Sunflower Labs) <ndepal@gmail.com>
46  *
47  */
48 
49 #include <matrix/math.hpp>
50 #include <mathlib/mathlib.h>
51 #include <matrix/Matrix.hpp>
52 #include <matrix/Vector.hpp>
53 
54 #pragma once
55 
57 {
59 {
60 public:
61  /**
62  * Default constructor, state not initialized
63  */
65 
66  /**
67  * Constructor, initialize state
68  */
70 
71  /**
72  * Default desctructor
73  */
74  virtual ~KalmanFilter() {};
75 
76  /**
77  * Initialize filter state
78  * @param initial initial state
79  * @param covInit initial covariance
80  */
82 
83  /**
84  * Initialize filter state, only specifying diagonal covariance elements
85  * @param initial0 first initial state
86  * @param initial1 second initial state
87  * @param covInit00 initial variance of first state
88  * @param covinit11 initial variance of second state
89  */
90  void init(float initial0, float initial1, float covInit00, float covInit11);
91 
92  /**
93  * Predict the state with an external acceleration estimate
94  * @param dt Time delta in seconds since last state change
95  * @param acc Acceleration estimate
96  * @param acc_unc Variance of acceleration estimate
97  */
98  void predict(float dt, float acc, float acc_unc);
99 
100  /**
101  * Update the state estimate with a measurement
102  * @param meas state measeasurement
103  * @param measUnc measurement uncertainty
104  * @return update success (measurement not rejected)
105  */
106  bool update(float meas, float measUnc);
107 
108  /**
109  * Get the current filter state
110  * @param x1 State
111  */
113 
114  /**
115  * Get the current filter state
116  * @param state0 First state
117  * @param state1 Second state
118  */
119  void getState(float &state0, float &state1);
120 
121  /**
122  * Get state covariance
123  * @param covariance Covariance of the state
124  */
125  void getCovariance(matrix::Matrix<float, 2, 2> &covariance);
126 
127  /**
128  * Get state variances (diagonal elements)
129  * @param cov00 Variance of first state
130  * @param cov11 Variance of second state
131  */
132  void getCovariance(float &cov00, float &cov11);
133 
134  /**
135  * Get measurement innovation and covariance of last update call
136  * @param innov Measurement innovation
137  * @param innovCov Measurement innovation covariance
138  */
139  void getInnovations(float &innov, float &innovCov);
140 
141 private:
143 
145 
146  float _residual; // residual of last measurement update
147 
148  float _innovCov; // innovation covariance of last measurement update
149 };
150 } // 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
virtual ~KalmanFilter()
Default desctructor.
Definition: KalmanFilter.h:74
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.
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
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
A simple matrix template library.
Vector class.