Autoware.Auto
esrcf.hpp
Go to the documentation of this file.
1 // Copyright 2018 the Autoware Foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Co-developed by Tier IV, Inc. and Apex.AI, Inc.
16 
21 #ifndef KALMAN_FILTER__ESRCF_HPP_
22 #define KALMAN_FILTER__ESRCF_HPP_
23 
24 #include <common/types.hpp>
27 
28 #include <chrono>
29 
32 
33 namespace autoware
34 {
35 namespace prediction
36 {
37 namespace kalman_filter
38 {
39 
47 template<int32_t NumStates, int32_t ProcessNoiseDim>
48 class Esrcf
49 {
50  using state_vec_t = typename SrcfCore<NumStates, ProcessNoiseDim>::state_vec_t;
51  using square_mat_t = typename SrcfCore<NumStates, ProcessNoiseDim>::square_mat_t;
52 
53 public:
57  explicit Esrcf(
59  const Eigen::Matrix<float32_t, NumStates, ProcessNoiseDim> & GQ_chol_prod)
60  : m_model_ptr{&model},
61  m_is_mat1_covariance{false},
62  m_B_mat{GQ_chol_prod},
63  m_GQ_factor{GQ_chol_prod} {}
64 
70  explicit Esrcf(
72  const Eigen::Matrix<float32_t, NumStates, ProcessNoiseDim> & GQ_chol_prod,
73  const state_vec_t & x0,
74  const square_mat_t & P0_chol)
75  : Esrcf{model, GQ_chol_prod}
76  {
77  reset(x0, P0_chol);
78  }
79 
82  void reset(const state_vec_t & x0)
83  {
84  m_model_ptr->reset(x0);
85  }
86 
87 
91  void reset(const state_vec_t & x0, const square_mat_t & P0_chol)
92  {
93  reset(x0);
94  m_square_mat2 = P0_chol;
95  m_is_mat1_covariance = false;
96  }
97 
100  const square_mat_t & get_covariance() const
101  {
102  return m_is_mat1_covariance ? m_square_mat1 : m_square_mat2;
103  }
104 
107  void temporal_update(const std::chrono::nanoseconds & dt)
108  {
109  // alternate which matrix you store F * C in to take advantage of efficient matrix mult
110  const square_mat_t & cov_mat = m_is_mat1_covariance ? m_square_mat1 : m_square_mat2;
111  square_mat_t & jac_mat = m_is_mat1_covariance ? m_square_mat2 : m_square_mat1;
112  // compute jacobian and predict
113  m_model_ptr->compute_jacobian_and_predict(jac_mat, dt);
115  // store jacobian old cov matrix, update F = alpha * F * C
116  jac_mat = jac_mat * cov_mat;
117  // Do temporal update on F, 0 out B
118  m_srcf_core.right_lower_triangularize_matrices(jac_mat, m_B_mat);
119  m_is_mat1_covariance = !m_is_mat1_covariance;
120  m_B_mat = m_GQ_factor;
121  }
122 
128  template<int32_t NumObs>
130  const Eigen::Matrix<float32_t, NumObs, 1U> & z,
131  const Eigen::Matrix<float32_t, NumObs, NumStates> & H,
132  const Eigen::Matrix<float32_t, NumObs, 1U> & R_diag)
133  {
134  square_mat_t & cov_mat = m_is_mat1_covariance ? m_square_mat1 : m_square_mat2;
135  // This is ugly, but promotes encapsulation
136  m_state_tmp = m_model_ptr->get_state();
137  // do sequential update
138  float32_t likelihood = 0.0F;
139  for (index_t idx = index_t(); idx < NumObs; ++idx) {
140  likelihood += m_srcf_core.scalar_update(
141  z[idx],
142  R_diag[idx],
143  H.row(idx),
144  cov_mat,
145  m_state_tmp);
146  }
147  // still ugly, but promotes encapsulation
148  m_model_ptr->reset(m_state_tmp);
149  return likelihood;
150  }
151 
159  void imm_self_mix(const float32_t self_transition_prob, const state_vec_t & x_mix)
160  {
161  m_state_tmp = m_model_ptr->get_state();
162  m_state_tmp -= x_mix;
163  const float32_t sq_prob = sqrtf(self_transition_prob);
164  square_mat_t & cov = m_is_mat1_covariance ? m_square_mat1 : m_square_mat2;
165  cov *= sq_prob;
166  m_state_tmp *= sq_prob;
167  m_srcf_core.right_lower_triangularize_matrices(cov, m_state_tmp);
168  m_model_ptr->reset(x_mix);
169  }
170 
183  const float32_t other_transition_prob,
184  const state_vec_t & x_other,
185  square_mat_t & cov_other,
186  const index_t rank_other = NumStates)
187  {
188  // compute dx = x_other - x_mix
189  m_state_tmp = x_other;
190  m_state_tmp -= m_model_ptr->get_state();
191  // compute sq(u), apply to dx, cov_other
192  const float32_t sq_prob = sqrtf(other_transition_prob);
193  m_state_tmp *= sq_prob;
194  cov_other *= sq_prob;
195  square_mat_t & cov = m_is_mat1_covariance ? m_square_mat1 : m_square_mat2;
196  // triangularize stacked matrix [C_self / sq(u) * C_other / sq(u) * (x_other - x_mix)]
197  for (index_t i = index_t(); i < NumStates; ++i) {
198  // For each element in column, starting from the bottom
199  m_srcf_core.zero_row(cov, m_state_tmp, i, index_t(), 1U);
200  m_srcf_core.zero_row(cov, cov_other, i, index_t(), rank_other);
201  // Zero out elements of cov up to, but not including the diagonal
202  m_srcf_core.zero_row(cov, cov, i, i + 1, NumStates);
203  }
204  }
205 
206 private:
209  state_vec_t m_state_tmp;
210  square_mat_t m_square_mat1;
211  square_mat_t m_square_mat2;
212  bool8_t m_is_mat1_covariance;
213  Eigen::Matrix<float32_t, NumStates, ProcessNoiseDim> m_B_mat;
214  Eigen::Matrix<float32_t, NumStates, ProcessNoiseDim> m_GQ_factor;
215  static_assert(NumStates > 0U, "must have positive number of states");
216  static_assert(ProcessNoiseDim > 0U, "must have positive number of process noise dimensions");
217 }; // class Esrcf
218 } // namespace kalman_filter
219 } // namespace prediction
220 } // namespace autoware
221 
222 #endif // KALMAN_FILTER__ESRCF_HPP_
autoware::prediction::kalman_filter::Esrcf::temporal_update
void temporal_update(const std::chrono::nanoseconds &dt)
Do temporal update: update state estimate, compute jacobian, update covariance.
Definition: esrcf.hpp:107
autoware::prediction::kalman_filter::Esrcf::Esrcf
Esrcf(motion::motion_model::MotionModel< NumStates > &model, const Eigen::Matrix< float32_t, NumStates, ProcessNoiseDim > &GQ_chol_prod)
constructor
Definition: esrcf.hpp:57
autoware::prediction::kalman_filter::Esrcf::observation_update
float32_t observation_update(const Eigen::Matrix< float32_t, NumObs, 1U > &z, const Eigen::Matrix< float32_t, NumObs, NumStates > &H, const Eigen::Matrix< float32_t, NumObs, 1U > &R_diag)
Do observation update: update state estimate and covariance. temporal_update() is assumed to have bee...
Definition: esrcf.hpp:129
autoware::prediction::kalman_filter::SrcfCore::square_mat_t
Eigen::Matrix< float32_t, NumStates, NumStates > square_mat_t
Definition: srcf_core.hpp:51
motion_model.hpp
This file defines the motion model interface used by kalman filters.
types.hpp
This file includes common type definition.
autoware::prediction::kalman_filter::index_t
Eigen::Index index_t
Type for matrix indexing.
Definition: srcf_core.hpp:40
autoware::common::types::bool8_t
bool bool8_t
Definition: types.hpp:33
autoware::prediction::kalman_filter::SrcfCore
This class is an implementation of the carlson-schmidt temporal and observation update for the square...
Definition: srcf_core.hpp:47
autoware
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24
autoware::prediction::kalman_filter::Esrcf
This class wraps the carlson-schmidt square root covariance filter with some vector-valued motion mod...
Definition: esrcf.hpp:48
autoware::prediction::kalman_filter::Esrcf::Esrcf
Esrcf(motion::motion_model::MotionModel< NumStates > &model, const Eigen::Matrix< float32_t, NumStates, ProcessNoiseDim > &GQ_chol_prod, const state_vec_t &x0, const square_mat_t &P0_chol)
constructor, equivalent of construct(model, GQ, DIAG); reset(x, P);
Definition: esrcf.hpp:70
autoware::motion::motion_model::MotionModel
Virtual interface for all motion models for use with prediction.
Definition: motion_model.hpp:46
autoware::prediction::kalman_filter::Esrcf::imm_self_mix
void imm_self_mix(const float32_t self_transition_prob, const state_vec_t &x_mix)
Compute first component of mixed covariance for this model: P_i' = u_i * (P_i + (x_i - x) * (x_i - x)...
Definition: esrcf.hpp:159
autoware::common::types::float32_t
float float32_t
Definition: types.hpp:36
autoware::prediction::kalman_filter::SrcfCore::state_vec_t
Eigen::Matrix< float32_t, NumStates, 1 > state_vec_t
Definition: srcf_core.hpp:50
autoware::prediction::kalman_filter::Esrcf::get_covariance
const square_mat_t & get_covariance() const
Get covariance.
Definition: esrcf.hpp:100
autoware::prediction::kalman_filter::Esrcf::reset
void reset(const state_vec_t &x0)
Update state externally.
Definition: esrcf.hpp:82
autoware::prediction::kalman_filter::Esrcf::imm_other_mix
void imm_other_mix(const float32_t other_transition_prob, const state_vec_t &x_other, square_mat_t &cov_other, const index_t rank_other=NumStates)
Adds covariance components from another model to this model, computes P0' = P0 + u_other * (P_other +...
Definition: esrcf.hpp:182
autoware::prediction::kalman_filter::Esrcf::reset
void reset(const state_vec_t &x0, const square_mat_t &P0_chol)
Initialize state and covariance.
Definition: esrcf.hpp:91
srcf_core.hpp
This file defines the core algorithms for square root covariance filtering.