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 public:
56  explicit Esrcf(
58  const Eigen::Matrix<float32_t, NumStates, ProcessNoiseDim> & GQ_chol_prod)
59  : m_model_ptr{&model},
60  m_is_mat1_covariance{false},
61  m_B_mat{GQ_chol_prod},
62  m_GQ_factor{GQ_chol_prod} {}
63 
69  explicit Esrcf(
71  const Eigen::Matrix<float32_t, NumStates, ProcessNoiseDim> & GQ_chol_prod,
72  const state_vec_t & x0,
73  const square_mat_t & P0_chol)
74  : Esrcf{model, GQ_chol_prod}
75  {
76  reset(x0, P0_chol);
77  }
78 
81  void reset(const state_vec_t & x0)
82  {
83  m_model_ptr->reset(x0);
84  }
85 
86 
90  void reset(const state_vec_t & x0, const square_mat_t & P0_chol)
91  {
92  reset(x0);
93  m_square_mat2 = P0_chol;
94  m_is_mat1_covariance = false;
95  }
96 
99  const square_mat_t & get_covariance() const
100  {
101  return m_is_mat1_covariance ? m_square_mat1 : m_square_mat2;
102  }
103 
105  const state_vec_t & get_state() const
106  {
107  return m_model_ptr->get_state();
108  }
109 
112  void temporal_update(const std::chrono::nanoseconds & dt)
113  {
114  // alternate which matrix you store F * C in to take advantage of efficient matrix mult
115  const square_mat_t & cov_mat = m_is_mat1_covariance ? m_square_mat1 : m_square_mat2;
116  square_mat_t & jac_mat = m_is_mat1_covariance ? m_square_mat2 : m_square_mat1;
117  // compute jacobian and predict
118  m_model_ptr->compute_jacobian_and_predict(jac_mat, dt);
120  // store jacobian old cov matrix, update F = alpha * F * C
121  jac_mat = jac_mat * cov_mat;
122  // Do temporal update on F, 0 out B
123  m_srcf_core.right_lower_triangularize_matrices(jac_mat, m_B_mat);
124  m_is_mat1_covariance = !m_is_mat1_covariance;
125  m_B_mat = m_GQ_factor;
126  }
127 
133  template<int32_t NumObs>
135  const Eigen::Matrix<float32_t, NumObs, 1U> & z,
136  const Eigen::Matrix<float32_t, NumObs, NumStates> & H,
137  const Eigen::Matrix<float32_t, NumObs, 1U> & R_diag)
138  {
139  square_mat_t & cov_mat = m_is_mat1_covariance ? m_square_mat1 : m_square_mat2;
140  // This is ugly, but promotes encapsulation
141  m_state_tmp = m_model_ptr->get_state();
142  // do sequential update
143  float32_t likelihood = 0.0F;
144  for (index_t idx = index_t(); idx < NumObs; ++idx) {
145  likelihood += m_srcf_core.scalar_update(
146  z[idx],
147  R_diag[idx],
148  H.row(idx),
149  cov_mat,
150  m_state_tmp);
151  }
152  // still ugly, but promotes encapsulation
153  m_model_ptr->reset(m_state_tmp);
154  return likelihood;
155  }
156 
164  void imm_self_mix(const float32_t self_transition_prob, const state_vec_t & x_mix)
165  {
166  m_state_tmp = m_model_ptr->get_state();
167  m_state_tmp -= x_mix;
168  const float32_t sq_prob = sqrtf(self_transition_prob);
169  square_mat_t & cov = m_is_mat1_covariance ? m_square_mat1 : m_square_mat2;
170  cov *= sq_prob;
171  m_state_tmp *= sq_prob;
172  m_srcf_core.right_lower_triangularize_matrices(cov, m_state_tmp);
173  m_model_ptr->reset(x_mix);
174  }
175 
188  const float32_t other_transition_prob,
189  const state_vec_t & x_other,
190  square_mat_t & cov_other,
191  const index_t rank_other = NumStates)
192  {
193  // compute dx = x_other - x_mix
194  m_state_tmp = x_other;
195  m_state_tmp -= m_model_ptr->get_state();
196  // compute sq(u), apply to dx, cov_other
197  const float32_t sq_prob = sqrtf(other_transition_prob);
198  m_state_tmp *= sq_prob;
199  cov_other *= sq_prob;
200  square_mat_t & cov = m_is_mat1_covariance ? m_square_mat1 : m_square_mat2;
201  // triangularize stacked matrix [C_self / sq(u) * C_other / sq(u) * (x_other - x_mix)]
202  for (index_t i = index_t(); i < NumStates; ++i) {
203  // For each element in column, starting from the bottom
204  m_srcf_core.zero_row(cov, m_state_tmp, i, index_t(), 1U);
205  m_srcf_core.zero_row(cov, cov_other, i, index_t(), rank_other);
206  // Zero out elements of cov up to, but not including the diagonal
207  m_srcf_core.zero_row(cov, cov, i, i + 1, NumStates);
208  }
209  }
210 
211 private:
214  state_vec_t m_state_tmp;
215  square_mat_t m_square_mat1;
216  square_mat_t m_square_mat2;
217  bool8_t m_is_mat1_covariance;
218  Eigen::Matrix<float32_t, NumStates, ProcessNoiseDim> m_B_mat;
219  Eigen::Matrix<float32_t, NumStates, ProcessNoiseDim> m_GQ_factor;
220  static_assert(NumStates > 0U, "must have positive number of states");
221  static_assert(ProcessNoiseDim > 0U, "must have positive number of process noise dimensions");
222 }; // class Esrcf
223 } // namespace kalman_filter
224 } // namespace prediction
225 } // namespace autoware
226 
227 #endif // KALMAN_FILTER__ESRCF_HPP_
Eigen::Index index_t
Type for matrix indexing.
Definition: srcf_core.hpp:40
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:69
void temporal_update(const std::chrono::nanoseconds &dt)
Do temporal update: update state estimate, compute jacobian, update covariance.
Definition: esrcf.hpp:112
float float32_t
Definition: types.hpp:36
This class is an implementation of the carlson-schmidt temporal and observation update for the square...
Definition: srcf_core.hpp:47
void reset(const state_vec_t &x0, const square_mat_t &P0_chol)
Initialize state and covariance.
Definition: esrcf.hpp:90
bool bool8_t
Definition: types.hpp:33
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&#39; = u_i * (P_i + (x_i - x) * (x_i - x)...
Definition: esrcf.hpp:164
This file includes common type definition.
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&#39; = P0 + u_other * (P_other +...
Definition: esrcf.hpp:187
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:134
Eigen::Matrix< float32_t, NumStates, NumStates > square_mat_t
Definition: srcf_core.hpp:51
This file defines the core algorithms for square root covariance filtering.
Eigen::Matrix< float32_t, NumStates, 1 > state_vec_t
Definition: srcf_core.hpp:50
const state_vec_t & get_state() const
Get state as a passthrough to the motion model.
Definition: esrcf.hpp:105
typename SrcfCore< NumStates, ProcessNoiseDim >::state_vec_t state_vec_t
Definition: esrcf.hpp:51
void reset(const state_vec_t &x0)
Update state externally.
Definition: esrcf.hpp:81
This file defines the motion model interface used by kalman filters.
Esrcf(motion::motion_model::MotionModel< NumStates > &model, const Eigen::Matrix< float32_t, NumStates, ProcessNoiseDim > &GQ_chol_prod)
constructor
Definition: esrcf.hpp:56
Virtual interface for all motion models for use with prediction.
Definition: motion_model.hpp:46
typename SrcfCore< NumStates, ProcessNoiseDim >::square_mat_t square_mat_t
Definition: esrcf.hpp:52
This class wraps the carlson-schmidt square root covariance filter with some vector-valued motion mod...
Definition: esrcf.hpp:48
const square_mat_t & get_covariance() const
Get covariance.
Definition: esrcf.hpp:99
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24