Apollo 10.0
自动驾驶开放平台
kalman_filter.cc
浏览该文件的文档.
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
18
19#include <algorithm>
20
21#include "Eigen/LU"
22
23#include "cyber/common/log.h"
24
25namespace apollo {
26namespace perception {
27namespace camera {
28
30 // other value should be changed in predict
31 state_transition_matrix_.setIdentity();
32 measure_matrix_ << 1, 0, 0, 0, 0, 1, 0, 0;
33 variance_.setIdentity();
34 measure_noise_.setIdentity();
35 process_noise_.setIdentity();
36 inited_ = false;
37}
38
39void KalmanFilterConstVelocity::Init(const Eigen::VectorXd &x) {
40 state_ << x(0), x(1), 0, 0;
41 inited_ = true;
42}
43
55void KalmanFilterConstVelocity::MagicVelocity(const Eigen::VectorXd &vel) {
56 state_(2) = vel(0);
57 state_(3) = vel(1);
58}
59void KalmanFilterConstVelocity::Correct(const Eigen::VectorXd &z) {
60 if (inited_) {
61 Eigen::Vector2d measure;
62 measure << z[0], z[1];
63 // measurement covariance: S = H*P*H^T + R
64 Eigen::Matrix2d cov =
67
68 kalman_gain_ = variance_ * measure_matrix_.transpose() * cov.inverse();
71
72 // compute likelihood
73 auto residual = measure - predict_state_.head(2);
75 std::exp(-0.5 * residual.transpose() * cov.inverse() * residual) /
76 std::sqrt(2 * M_PI * cov.determinant());
77 } else {
78 Init(z);
79 }
80}
81
82Eigen::Vector4d KalmanFilterConstVelocity::get_state() const { return state_; }
83void KalmanFilterConstVelocity::MagicPosition(const Eigen::VectorXd &pos) {
84 state_(0) = pos(0);
85 state_(1) = pos(1);
86}
87
89 // other value should be changed in predict
90 state_transition_matrix_.setIdentity();
91 measure_matrix_ << 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1;
92 variance_.setIdentity();
93 measure_noise_.setIdentity();
94 process_noise_.setIdentity();
95 inited_ = false;
96}
97
98void ExtendedKalmanFilter::Init(const Eigen::VectorXd &x) {
99 Init();
100 state_ << x(0), x(1), 0, x(2);
101 inited_ = true;
102}
103
104void ExtendedKalmanFilter::Predict(float delta_t) {
105 if (inited_) {
106 float sin_theta = static_cast<float>(std::sin(state_(3)));
107 float cos_theta = static_cast<float>(std::cos(state_(3)));
108 state_transition_matrix_(0, 2) = delta_t * cos_theta;
109 state_transition_matrix_(0, 3) = -delta_t * state_(2) * sin_theta;
110 state_transition_matrix_(1, 2) = delta_t * sin_theta;
111 state_transition_matrix_(1, 3) = delta_t * state_(2) * cos_theta;
112 state_(0) += state_(2) * cos_theta * delta_t;
113 state_(1) += state_(2) * sin_theta * delta_t;
114 // No change
115 // state_(2) = state_(2);
116 // state_(3) = state_(3);
118 state_transition_matrix_.transpose() +
120 }
121}
122
123void ExtendedKalmanFilter::Correct(const Eigen::VectorXd &z) {
124 if (inited_) {
125 Eigen::Vector3d measure;
126 measure << z[0], z[1], z[2];
127 Eigen::Matrix3d cov =
130
131 kalman_gain_ = variance_ * measure_matrix_.transpose() * cov.inverse();
133 state_ = state_ + kalman_gain_ * (measure - measure_matrix_ * state_);
134 } else {
135 inited_ = true;
136 state_ << z[0], z[1], 0, z[2];
137 }
138}
139
140Eigen::Vector4d ExtendedKalmanFilter::get_state() const { return state_; }
141
142void MeanFilter::SetWindow(int window) {
143 window_ = window;
144 index_ = 0;
145}
146
147void MeanFilter::AddMeasure(const Eigen::VectorXd &z) {
148 if (measures_.size() < static_cast<unsigned int>(window_)) {
149 measures_.push_back(z);
150 } else {
151 measures_[index_] = z;
152 }
153 index_ = (index_ + 1) % window_;
154 int n = static_cast<int>(measures_.size());
155
156 state_.resizeLike(z);
157 state_.setConstant(0);
158 for (int i = 0; i < n; ++i) {
159 state_ += measures_[i];
160 }
161 state_ = state_ / n;
162
163 variance_.resize(state_.rows(), state_.rows());
164 variance_.setConstant(0);
165 for (int i = 0; i < n; ++i) {
166 auto z = measures_[i] - state_;
167 variance_ += z * z.transpose();
168 }
169 if (n > 1) {
170 variance_ /= n - 1;
171 }
172}
173
174const Eigen::VectorXd &MeanFilter::get_state() const { return state_; }
175
176const Eigen::MatrixXd &MeanFilter::get_variance() const { return variance_; }
177
179 alpha_ = alpha;
180 inited_ = false;
181}
182
183void FirstOrderRCLowPassFilter::AddMeasure(const Eigen::VectorXd &z) {
184 if (inited_) {
185 state_ = z + alpha_ * (state_ - z);
186 } else {
187 state_ = z;
188 inited_ = true;
189 }
190}
191
192Eigen::VectorXd FirstOrderRCLowPassFilter::get_state() const { return state_; }
193
194struct {
195 bool operator()(Eigen::VectorXd a, Eigen::VectorXd b) const {
196 return a[0] > b[0];
197 }
199
201 window_ = window;
202 index_ = 0;
203}
204
205void MaxNMeanFilter::AddMeasure(const Eigen::VectorXd &z) {
206 measures_.push_back(z);
207 std::sort(measures_.begin(), measures_.end(), customLess);
208 if (measures_.size() > static_cast<unsigned int>(window_)) {
209 measures_.resize(window_);
210 }
211}
212
213Eigen::VectorXd MaxNMeanFilter::get_state() const {
214 Eigen::VectorXd x = measures_[0];
215 for (size_t i = 1; i < measures_.size(); ++i) {
216 x += measures_[i];
217 }
218 x = x / static_cast<double>(measures_.size());
219 return x;
220}
221void MaxNMeanFilter::Clear() { measures_.clear(); }
222} // namespace camera
223} // namespace perception
224} // namespace apollo
Eigen::Matrix< double, 3, 4 > measure_matrix_
void AddMeasure(const Eigen::VectorXd &z)
void AddMeasure(const Eigen::VectorXd &z)
const Eigen::MatrixXd & get_variance() const
const Eigen::VectorXd & get_state() const
struct apollo::perception::camera::@13 customLess
class register implement
Definition arena_queue.h:37