Autoware.Auto
kalman_filter_wrapper.hpp
Go to the documentation of this file.
1 // Copyright 2020 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>
28 
29 #include <Eigen/Core>
30 #include <Eigen/Geometry>
31 
32 #include <limits>
33 #include <cstdint>
34 #include <memory>
35 #include <string>
36 #include <chrono>
37 
38 namespace autoware
39 {
40 namespace prediction
41 {
42 
50 template<typename MotionModelT, std::int32_t kNumOfStates, std::int32_t kProcessNoiseDim>
51 class STATE_ESTIMATION_NODES_PUBLIC KalmanFilterWrapper
52 {
54 
55  template<std::int32_t kRows, std::int32_t kCols>
56  using RectangularMatrixT = Eigen::Matrix<common::types::float32_t, kRows, kCols>;
57 
58  template<std::int32_t kNum>
59  using SquareMatrixT = Eigen::Matrix<common::types::float32_t, kNum, kNum>;
60 
61  template<std::int32_t kLength>
62  using VectorT = Eigen::Matrix<common::types::float32_t, kLength, 1>;
63 
64 public:
84  const SquareMatrixT<kNumOfStates> & initial_covariance_factor,
85  const RectangularMatrixT<kNumOfStates, kProcessNoiseDim> & process_noise,
86  const std::chrono::nanoseconds & expected_dt,
87  const std::string & frame_id,
88  common::types::float32_t mahalanobis_threshold =
89  std::numeric_limits<common::types::float32_t>::max(),
90  const MotionModelT & motion_model = MotionModelT{})
91  : m_motion_model{motion_model},
92  m_initial_covariance_factor{initial_covariance_factor},
93  m_frame_id{frame_id},
94  m_mahalanobis_threshold{mahalanobis_threshold}
95  {
96  static_assert(
97  motion_model.get_num_states() == kNumOfStates,
98  "Wrong number of states in the motion model.");
99 
100  SquareMatrixT<kNumOfStates> F;
101  m_motion_model.compute_jacobian(F, expected_dt);
102  m_GQ_left_factor = F * process_noise;
103  m_ekf = std::make_unique<FilterT>(m_motion_model, m_GQ_left_factor);
104  }
105 
114  template<typename MeasurementT>
115  void reset(const MeasurementT & measurement, const GlobalTime & time_of_event_occurance);
116 
129  void reset(
130  const VectorT<kNumOfStates> & state,
131  const SquareMatrixT<kNumOfStates> & initial_covariance_chol,
132  const MeasurementBasedTime & event_timestamp,
133  const GlobalTime & time_of_event_occurance);
134 
146  template<TimeReferenceFrame kTimeReferenceFrame>
147  common::types::bool8_t temporal_update(
148  const Time<kTimeReferenceFrame> & time_of_update);
149 
164  template<typename MeasurementT>
165  common::types::bool8_t observation_update(
166  const GlobalTime & message_received_time,
167  const MeasurementT & measurement);
168 
171  {
172  return m_ekf_initialized && m_time_keeper.is_initialized();
173  }
174 
176  nav_msgs::msg::Odometry get_state() const;
177 
178 private:
180  common::types::bool8_t passes_mahalanobis_gate(
181  const VectorT<kNumOfStates> & sample,
182  const VectorT<kNumOfStates> & mean,
183  const SquareMatrixT<kNumOfStates> & covariance_factor) const;
184 
186  common::types::bool8_t m_ekf_initialized{};
188  MeasurementBasedTimeKeeper m_time_keeper{};
190  MotionModelT m_motion_model;
192  RectangularMatrixT<kNumOfStates, kProcessNoiseDim> m_GQ_left_factor;
197  SquareMatrixT<kNumOfStates> m_initial_covariance_factor;
199  std::unique_ptr<FilterT> m_ekf{};
201  std::string m_frame_id{};
203  common::types::float32_t m_mahalanobis_threshold{};
204 };
205 
208 
209 } // namespace prediction
210 } // namespace autoware
211 
212 #endif // STATE_ESTIMATION_NODES__KALMAN_FILTER_WRAPPER_HPP_
autoware::prediction::MeasurementBasedTime
Time< TimeReferenceFrame::kMeasurement > MeasurementBasedTime
Utility typedef to define time based on a measurement clock.
Definition: time.hpp:138
types.hpp
This file includes common type definition.
autoware::prediction::KalmanFilterWrapper::is_initialized
common::types::bool8_t is_initialized() const noexcept
Check if the filter is is_initialized with a state.
Definition: kalman_filter_wrapper.hpp:170
measurement_time_keeper.hpp
autoware::prediction::KalmanFilterWrapper::KalmanFilterWrapper
KalmanFilterWrapper(const SquareMatrixT< kNumOfStates > &initial_covariance_factor, const RectangularMatrixT< kNumOfStates, kProcessNoiseDim > &process_noise, const std::chrono::nanoseconds &expected_dt, const std::string &frame_id, 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:83
motion::motion_common::sample
void sample(const Trajectory &in, Trajectory &out, std::chrono::nanoseconds period, SlerpF slerp_fn)
Sample a trajectory using interpolation; does not extrapolate.
Definition: motion_common.hpp:198
autoware::common::types::bool8_t
bool bool8_t
Definition: types.hpp:33
autoware::drivers::vehicle_interface::Odometry
autoware_auto_msgs::msg::VehicleOdometry Odometry
Definition: safety_state_machine.hpp:43
autoware
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24
measurement.hpp
autoware::prediction::GlobalTime
Time< TimeReferenceFrame::kGlobal > GlobalTime
Utility typedef to define time based on some global clock.
Definition: time.hpp:140
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::common::types::float32_t
float float32_t
Definition: types.hpp:36
esrcf.hpp
This file defines a class for extended square root covariance filter.
visibility_control.hpp
constant_acceleration.hpp
This file defines the constant velocity motion model.
autoware::prediction::KalmanFilterWrapper
This class provides a high level interface to the Kalman Filter allowing to predict the state of the ...
Definition: kalman_filter_wrapper.hpp:51
autoware::prediction::ConstantAccelerationFilter
KalmanFilterWrapper< motion::motion_model::ConstantAcceleration, 6, 2 > ConstantAccelerationFilter
Definition: kalman_filter_wrapper.hpp:207