Autoware.Auto
kalman_filter_wrapper.hpp
Go to the documentation of this file.
1 // Copyright 2021 Apex.AI, Inc.
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 
17 
18 #ifndef STATE_ESTIMATION_NODES__KALMAN_FILTER_WRAPPER_HPP_
19 #define STATE_ESTIMATION_NODES__KALMAN_FILTER_WRAPPER_HPP_
20 
21 #include <common/types.hpp>
22 #include <kalman_filter/esrcf.hpp>
24 #include <nav_msgs/msg/odometry.hpp>
30 
31 #include <Eigen/Core>
32 #include <Eigen/Geometry>
33 
34 #include <limits>
35 #include <cstdint>
36 #include <memory>
37 #include <string>
38 #include <chrono>
39 
40 namespace autoware
41 {
42 namespace prediction
43 {
44 
53 template<typename MotionModelT, std::int32_t kNumOfStates, std::int32_t kProcessNoiseDim>
54 class STATE_ESTIMATION_NODES_PUBLIC KalmanFilterWrapper
55 {
57 
58  template<std::int32_t kRows, std::int32_t kCols>
59  using RectangularMatrixT = Eigen::Matrix<common::types::float32_t, kRows, kCols>;
60 
61  template<std::int32_t kNum>
62  using SquareMatrixT = Eigen::Matrix<common::types::float32_t, kNum, kNum>;
63 
64  template<std::int32_t kLength>
65  using VectorT = Eigen::Matrix<common::types::float32_t, kLength, 1>;
66 
67  using HistoryT = History<
68  FilterT,
69  kNumOfStates,
75 
76 public:
97  const SquareMatrixT<kNumOfStates> & initial_covariance_factor,
98  const RectangularMatrixT<kNumOfStates, kProcessNoiseDim> & process_noise,
99  const std::chrono::nanoseconds & expected_dt,
100  const std::string & frame_id,
101  const std::chrono::nanoseconds & history_duration = std::chrono::milliseconds{5000},
102  common::types::float32_t mahalanobis_threshold =
103  std::numeric_limits<common::types::float32_t>::max(),
104  const MotionModelT & motion_model = MotionModelT{})
105  : m_motion_model{motion_model},
106  m_initial_covariance_factor{initial_covariance_factor},
107  m_frame_id{frame_id},
108  m_mahalanobis_threshold{mahalanobis_threshold},
109  m_expected_prediction_period{expected_dt}
110  {
111  static_assert(
112  motion_model.get_num_states() == kNumOfStates,
113  "Wrong number of states in the motion model.");
114 
115  SquareMatrixT<kNumOfStates> F;
116  m_motion_model.compute_jacobian(F, expected_dt);
117  m_GQ_left_factor = F * process_noise;
118  m_filter = std::make_unique<FilterT>(m_motion_model, m_GQ_left_factor);
119  m_history = std::make_unique<HistoryT>(
120  *m_filter,
121  static_cast<std::size_t>(history_duration / expected_dt),
122  m_mahalanobis_threshold);
123  }
124 
132  template<typename MeasurementT>
133  void add_reset_event_to_history(const MeasurementT & measurement);
134 
146  void add_reset_event_to_history(
147  const VectorT<kNumOfStates> & state,
148  const SquareMatrixT<kNumOfStates> & initial_covariance_chol,
149  const std::chrono::system_clock::time_point & event_timestamp);
150 
157  common::types::bool8_t add_next_temporal_update_to_history();
158 
171  template<typename MeasurementT>
172  common::types::bool8_t add_observation_to_history(const MeasurementT & measurement);
173 
175  inline common::types::bool8_t is_initialized() const noexcept
176  {
177  return (m_history) && (!m_history->empty()) && m_time_grid.is_initialized();
178  }
179 
181  nav_msgs::msg::Odometry get_state() const;
182 
183 private:
185  std::unique_ptr<FilterT> m_filter{};
187  SteadyTimeGrid m_time_grid{};
189  MotionModelT m_motion_model;
191  RectangularMatrixT<kNumOfStates, kProcessNoiseDim> m_GQ_left_factor;
196  SquareMatrixT<kNumOfStates> m_initial_covariance_factor;
198  std::string m_frame_id{};
200  common::types::float32_t m_mahalanobis_threshold{};
202  std::unique_ptr<HistoryT> m_history{};
204  std::chrono::nanoseconds m_expected_prediction_period{};
205 };
206 
209 
210 } // namespace prediction
211 } // namespace autoware
212 
213 #endif // STATE_ESTIMATION_NODES__KALMAN_FILTER_WRAPPER_HPP_
common::types::bool8_t is_initialized() const noexcept
Check if the filter is is_initialized with a state.
Definition: kalman_filter_wrapper.hpp:175
This file defines a class for extended square root covariance filter.
Measurement< common::types::float32_t, motion::motion_model::ConstantAcceleration::States::VELOCITY_X, motion::motion_model::ConstantAcceleration::States::VELOCITY_Y > MeasurementSpeed
Definition: measurement_conversion.hpp:47
float float32_t
Definition: types.hpp:36
An event to indicate a prediction step.
Definition: history.hpp:64
Measurement< common::types::float32_t, motion::motion_model::ConstantAcceleration::States::POSE_X, motion::motion_model::ConstantAcceleration::States::POSE_Y > MeasurementPose
Definition: measurement_conversion.hpp:37
bool bool8_t
Definition: types.hpp:33
This file includes common type definition.
An event to reset the state of the filter.
Definition: history.hpp:72
This file defines the constant velocity motion model.
KalmanFilterWrapper(const SquareMatrixT< kNumOfStates > &initial_covariance_factor, const RectangularMatrixT< kNumOfStates, kProcessNoiseDim > &process_noise, const std::chrono::nanoseconds &expected_dt, const std::string &frame_id, const std::chrono::nanoseconds &history_duration=std::chrono::milliseconds{5000}, common::types::float32_t mahalanobis_threshold=std::numeric_limits< common::types::float32_t >::max(), const MotionModelT &motion_model=MotionModelT{})
Create an EKF wrapper.
Definition: kalman_filter_wrapper.hpp:96
This class encapsulates a history of events used with EKF.
Definition: history.hpp:95
This is a utility class that allows querying timestamps on a 1D grid.
Definition: steady_time_grid.hpp:39
This class provides a high level interface to the Kalman Filter allowing to predict the state of the ...
Definition: kalman_filter_wrapper.hpp:54
Definition: measurement.hpp:46
autoware_auto_msgs::msg::VehicleOdometry Odometry
Definition: safety_state_machine.hpp:43
This class wraps the carlson-schmidt square root covariance filter with some vector-valued motion mod...
Definition: esrcf.hpp:48
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24