Apollo 10.0
自动驾驶开放平台
kalman_filter.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16#pragma once
17
18#include <cmath>
19#include <vector>
20
21#include "Eigen/Core"
23
24namespace apollo {
25namespace perception {
26namespace camera {
27
29 public:
30 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
31
32 public:
34
35 void Init(const Eigen::VectorXd &x);
36
37 void Predict(float delta_t);
38
39 void Correct(const Eigen::VectorXd &z);
40
41 Eigen::Vector4d get_state() const;
42 void MagicPosition(const Eigen::VectorXd &pos);
43 void MagicVelocity(const Eigen::VectorXd &vel);
44 Eigen::Matrix4d variance_;
45 Eigen::Matrix2d measure_noise_;
46 Eigen::Matrix4d process_noise_;
47
48 Eigen::Matrix<double, 4, 1> predict_state_;
49 double likelihood_ = 1.0;
50
51 protected:
52 // x = (x,y,vx,vy)'
53 // z = (x,y)'
54 // X = A*x
55 // z = H*x
56 Eigen::Matrix4d state_transition_matrix_;
57
58 Eigen::Matrix<double, 4, 1> state_;
59 Eigen::Matrix<double, 2, 4> measure_matrix_;
60 Eigen::Matrix<double, 4, 2> kalman_gain_;
61 bool inited_;
62};
63
64template <std::size_t N>
66 public:
67 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
68
69 public:
70 typedef Eigen::Matrix<double, N, N> MatrixNd;
71 typedef Eigen::Matrix<double, N, 1> VectorNd;
72
73 public:
76
77 bool Init(const VectorNd &param);
78
79 bool Predict(float delta_t);
80
81 void Correct(const VectorNd &measurement);
82
83 VectorNd get_state() const { return state_; }
84
88
91 double likelihood_ = 1.0;
92
93 protected:
94 // x = (x, y)
95 // z = (x, y)
96 bool inited_ = false;
97
100};
101
103 public:
104 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
105
106 public:
108 void Init();
109
110 void Init(const Eigen::VectorXd &x);
111
112 void Predict(float delta_t);
113
114 void Correct(const Eigen::VectorXd &z);
115
116 Eigen::Vector4d get_state() const;
117
118 Eigen::Matrix4d variance_;
119 Eigen::Matrix3d measure_noise_;
120 Eigen::Matrix4d process_noise_;
121
122 protected:
123 // X = (x,y,v,theta)'
124 // x = x + v cos(theta)
125 // y = y + v sin(theta)
126 // v = v + noise
127 // theta = theta + noise
128 // z = (x,y,theta)'
130
131 Eigen::Matrix<double, 4, 1> state_;
132 Eigen::Matrix<double, 3, 4> measure_matrix_;
133 Eigen::Matrix<double, 4, 3> kalman_gain_;
135};
136
138 public:
139 FirstOrderRCLowPassFilter() : alpha_(0), inited_(false) {}
140 void SetAlpha(float alpha);
141 void AddMeasure(const Eigen::VectorXd &z);
142 Eigen::VectorXd get_state() const;
143
144 private:
145 float alpha_;
146 bool inited_;
147 Eigen::VectorXd state_;
148};
149
151 public:
152 MaxNMeanFilter() : index_(0), window_(1) {}
153 void SetWindow(int window);
154
155 void AddMeasure(const Eigen::VectorXd &z);
156
157 void Clear();
158
159 Eigen::VectorXd get_state() const;
160
161 private:
163 int index_;
164 int window_;
165};
166
168 public:
169 MeanFilter() : index_(0), window_(1) {}
170 void SetWindow(int window);
171
172 void AddMeasure(const Eigen::VectorXd &z);
173
174 const Eigen::VectorXd &get_state() const;
175 const Eigen::MatrixXd &get_variance() const;
176 int size() const { return static_cast<int>(measures_.size()); }
177
178 private:
180 Eigen::VectorXd state_;
181 Eigen::MatrixXd variance_;
182 int index_;
183 int window_;
184};
185
186// [BEGIN] KalmanFilterConstState
187template <std::size_t N>
189 state_ = param;
190 inited_ = true;
191
192 return true;
193}
194
195template <std::size_t N>
197 if (!inited_) {
198 return false;
199 }
200 predict_state_ = state_;
201 // state_transition_matrix is Identity
202 covariance_ += process_noise_;
203
204 return true;
205}
206
207template <std::size_t N>
209 if (!inited_) {
210 Init(measurement);
211 } else {
212 auto measurements_cov = covariance_ + measure_noise_;
213 kalman_gain_ = covariance_ * measurements_cov.inverse();
214 state_ = state_ + kalman_gain_ * (measurement - state_);
215 covariance_ = (MatrixNd::Identity() - kalman_gain_) * covariance_;
216
217 // compute likelihood
218 residual_ = measurement - predict_state_;
219 // Ref: https://eigen.tuxfamily.org/bz/show_bug.cgi?id=1610
220 double kval = -0.5 * residual_.transpose().adjoint().dot(
221 measurements_cov.inverse() * residual_);
222 likelihood_ =
223 std::exp(kval) / std::sqrt(2 * M_PI * measurements_cov.determinant());
224 }
225}
226// [END] KalmanFilterConstState
227
228} // namespace camera
229} // namespace perception
230} // namespace apollo
Eigen::Matrix< double, 3, 4 > measure_matrix_
void Correct(const VectorNd &measurement)
void AddMeasure(const Eigen::VectorXd &z)
void AddMeasure(const Eigen::VectorXd &z)
const Eigen::MatrixXd & get_variance() const
const Eigen::VectorXd & get_state() const
std::vector< EigenType, Eigen::aligned_allocator< EigenType > > EigenVector
Definition eigen_defs.h:33
class register implement
Definition arena_queue.h:37