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 //
15 // Co-developed by Tier IV, Inc. and Apex.AI, Inc.
16 
19 
20 #ifndef STATE_ESTIMATION_NODES__KALMAN_FILTER_WRAPPER_HPP_
21 #define STATE_ESTIMATION_NODES__KALMAN_FILTER_WRAPPER_HPP_
22 
23 #include <common/types.hpp>
25 #include <nav_msgs/msg/odometry.hpp>
32 
33 #include <Eigen/Core>
34 #include <Eigen/Geometry>
35 
36 #include <chrono>
37 #include <cstdint>
38 #include <limits>
39 #include <memory>
40 #include <string>
41 #include <vector>
42 
43 namespace autoware
44 {
45 namespace common
46 {
47 namespace state_estimation
48 {
49 
56 template<typename FilterT>
57 class STATE_ESTIMATION_NODES_PUBLIC KalmanFilterWrapper
58 {
59  using HistoryT = History<
60  FilterT,
65 
66 public:
67  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
68  using State = typename FilterT::State;
69 
85  const typename FilterT::MotionModel motion_model,
86  const typename FilterT::NoiseModel noise_model,
87  const typename FilterT::State::Matrix initial_state_covariance,
88  const std::chrono::nanoseconds & expected_dt,
89  const std::string & frame_id,
90  const std::chrono::nanoseconds & history_duration = std::chrono::milliseconds{5000},
91  common::types::float32_t mahalanobis_threshold =
92  std::numeric_limits<common::types::float32_t>::max())
93  : m_initial_covariance{initial_state_covariance},
94  m_frame_id{frame_id},
95  m_mahalanobis_threshold{mahalanobis_threshold},
96  m_expected_prediction_period{expected_dt},
97  m_filter{
98  motion_model,
99  noise_model,
100  State{},
101  initial_state_covariance,
102  },
103  m_history{
104  m_filter,
105  static_cast<std::size_t>(history_duration / expected_dt),
106  m_mahalanobis_threshold} {}
107 
115  template<typename MeasurementT>
116  inline void add_reset_event_to_history(const MeasurementT & measurement)
117  {
118  add_reset_event_to_history(
119  measurement.measurement.map_into(State{}),
120  m_initial_covariance,
121  measurement.timestamp);
122  }
123 
133  const State & state,
134  const typename State::Matrix & initial_covariance,
135  const std::chrono::system_clock::time_point & event_timestamp)
136  {
137  m_history.emplace_event(event_timestamp, ResetEvent<FilterT>{state, initial_covariance});
138  m_time_grid = SteadyTimeGrid{event_timestamp, m_expected_prediction_period};
139  }
140 
148  {
149  if (!is_initialized()) {return false;}
150  const auto next_prediction_timestamp =
151  m_time_grid.get_next_timestamp_after(m_history.get_last_timestamp());
152  m_history.emplace_event(next_prediction_timestamp, PredictionEvent{});
153  return true;
154  }
155 
168  template<typename MeasurementT>
169  common::types::bool8_t add_observation_to_history(const MeasurementT & measurement)
170  {
171  if (!is_initialized()) {return false;}
172  m_history.emplace_event(measurement.timestamp, measurement.measurement);
173  return true;
174  }
175 
177  inline common::types::bool8_t is_initialized() const noexcept
178  {
179  return (!m_history.empty()) && m_time_grid.is_initialized();
180  }
181 
183  nav_msgs::msg::Odometry get_state() const;
184 
185 private:
187  typename State::Matrix m_initial_covariance{};
189  SteadyTimeGrid m_time_grid{};
191  std::string m_frame_id{};
193  common::types::float32_t m_mahalanobis_threshold{};
195  std::chrono::nanoseconds m_expected_prediction_period{};
197  FilterT m_filter{};
199  HistoryT m_history{};
200 };
201 
204 
207 
208 } // namespace state_estimation
209 } // namespace common
210 } // namespace autoware
211 
212 #endif // STATE_ESTIMATION_NODES__KALMAN_FILTER_WRAPPER_HPP_
autoware::common::state_estimation::KalmanFilterWrapper
This class provides a high level interface to the Kalman Filter allowing to predict the state of the ...
Definition: kalman_filter_wrapper.hpp:57
autoware::common::state_estimation::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:177
steady_time_grid.hpp
autoware::common::state_estimation::KalmanFilterWrapper::add_next_temporal_update_to_history
common::types::bool8_t add_next_temporal_update_to_history()
Definition: kalman_filter_wrapper.hpp:147
filter_typedefs.hpp
types.hpp
This file includes common type definition.
measurement_typedefs.hpp
autoware::common::state_estimation::KalmanFilterWrapper::KalmanFilterWrapper
KalmanFilterWrapper(const typename FilterT::MotionModel motion_model, const typename FilterT::NoiseModel noise_model, const typename FilterT::State::Matrix initial_state_covariance, 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())
Create an EKF wrapper.
Definition: kalman_filter_wrapper.hpp:84
autoware::common::state_estimation::ResetEvent
An event to reset the state of the filter.
Definition: history.hpp:84
motion::motion_common::State
autoware_auto_vehicle_msgs::msg::VehicleKinematicState State
Definition: motion_common.hpp:40
autoware::common::state_estimation::LinearMeasurement
A class that represents a linear measurement.
Definition: linear_measurement.hpp:46
autoware::common::types::bool8_t
bool bool8_t
Definition: types.hpp:39
autoware::common::state_estimation::PredictionEvent
An event to indicate a prediction step.
Definition: history.hpp:76
autoware::common::state_estimation::KalmanFilterWrapper::State
typename FilterT::State State
Definition: kalman_filter_wrapper.hpp:68
autoware
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24
autoware::common::state_estimation::KalmanFilterWrapper::add_reset_event_to_history
void add_reset_event_to_history(const MeasurementT &measurement)
Definition: kalman_filter_wrapper.hpp:116
autoware::common::state_estimation::History< FilterT, PredictionEvent, ResetEvent< FilterT >, PoseMeasurementXYZ32, PoseMeasurementXYZRPY32 >
autoware::common::state_estimation::ConstantAccelerationFilterWrapperXY
KalmanFilterWrapper< ConstAccelerationKalmanFilterXY > ConstantAccelerationFilterWrapperXY
Definition: kalman_filter_wrapper.hpp:203
autoware::common::types::float32_t
float float32_t
Definition: types.hpp:45
autoware::common::state_estimation::KalmanFilterWrapper::add_observation_to_history
common::types::bool8_t add_observation_to_history(const MeasurementT &measurement)
Definition: kalman_filter_wrapper.hpp:169
autoware::common::state_estimation::SteadyTimeGrid
This is a utility class that allows querying timestamps on a 1D grid.
Definition: steady_time_grid.hpp:41
autoware::drivers::vehicle_interface::Odometry
autoware_auto_vehicle_msgs::msg::VehicleOdometry Odometry
Definition: safety_state_machine.hpp:50
autoware::common::state_estimation::PoseMeasurementXYZ32
PoseMeasurementXYZ< common::types::float32_t > PoseMeasurementXYZ32
Definition: measurement_typedefs.hpp:54
autoware::common::state_estimation::KalmanFilterWrapper::add_reset_event_to_history
void add_reset_event_to_history(const State &state, const typename State::Matrix &initial_covariance, const std::chrono::system_clock::time_point &event_timestamp)
Definition: kalman_filter_wrapper.hpp:132
history.hpp
visibility_control.hpp
kalman_filter.hpp
common_states.hpp
This file holds a collection of states that are commonly used in this package.