Go to the documentation of this file.
20 #ifndef STATE_ESTIMATION_NODES__HISTORY_HPP_
21 #define STATE_ESTIMATION_NODES__HISTORY_HPP_
25 #include <mpark_variant_vendor/variant.hpp>
28 #include <Eigen/Cholesky>
40 namespace state_estimation
58 template<std::
int32_t kNumOfStates>
60 const Eigen::Matrix<common::types::float32_t, kNumOfStates, 1> &
sample,
61 const Eigen::Matrix<common::types::float32_t, kNumOfStates, 1> & mean,
62 const Eigen::Matrix<common::types::float32_t, kNumOfStates, kNumOfStates> & covariance,
65 using Matrix = Eigen::Matrix<common::types::float32_t, kNumOfStates, kNumOfStates>;
66 const Matrix L = covariance.llt().matrixL();
67 const auto squared_mahalanobis_distance =
69 const auto squared_threshold = mahalanobis_threshold * mahalanobis_threshold;
70 return squared_mahalanobis_distance < squared_threshold;
83 template<
typename FilterT>
86 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
104 template<
typename FilterT,
typename ... EventT>
118 using Timestamp = std::chrono::system_clock::time_point;
120 using HistoryMap = std::multimap<Timestamp, HistoryEntry>;
132 const std::size_t max_history_size,
135 m_max_history_size{max_history_size},
136 m_mahalanobis_threshold{mahalanobis_threshold} {}
145 void emplace_event(
const Timestamp & timestamp,
const HistoryEntry & entry);
147 inline bool empty() const noexcept {
return m_history.empty();}
149 inline std::size_t
size() const noexcept {
return m_history.size();}
153 inline const HistoryEntry &
get_last_event() const noexcept {
return m_history.rbegin()->second;}
155 const FilterT &
get_filter() const noexcept {
return m_filter;}
163 inline void drop_oldest_event_if_needed()
165 if ((m_history.size() >= m_max_history_size) && (m_max_history_size > 0U)) {
166 (void) m_history.erase(m_history.begin());
175 void update_impacted_events(
const typename HistoryMap::iterator & start_iter);
177 HistoryMap m_history{};
178 FilterT & m_filter{};
179 std::size_t m_max_history_size{};
183 template<
typename FilterT,
typename ... EventT>
188 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
190 template<
typename SingleEventT>
197 m_stored_state = state;
204 m_stored_covariance = covariance;
209 return m_stored_covariance;
212 const mpark::variant<EventT...> &
event()
const {
return m_event;}
218 typename FilterT::State::Matrix m_stored_covariance{FilterT::State::Matrix::Zero()};
220 mpark::variant<EventT...> m_event;
223 template<
typename FilterT,
typename ... EventT>
230 const std::chrono::system_clock::duration & dt = std::chrono::milliseconds{0})
231 : m_filter {filter}, m_mahalanobis_threshold{mahalanobis_threshold}, m_dt{dt}
241 template<
typename MeasurementT>
244 m_filter.predict(m_dt);
250 const auto measurement_as_state =
event.map_into(m_filter.state());
252 measurement_as_state.vector(),
253 m_filter.state().vector(),
254 m_filter.covariance(),
255 m_mahalanobis_threshold)) {
return;}
256 m_filter.correct(event);
268 m_filter.predict(m_dt);
272 FilterT & m_filter{};
274 std::chrono::system_clock::duration m_dt{};
277 template<
typename FilterT,
typename ... EventT>
279 const Timestamp & timestamp,
const HistoryEntry & entry)
281 drop_oldest_event_if_needed();
282 const auto iterator_to_inserted_position = m_history.emplace(timestamp, entry);
283 update_impacted_events(iterator_to_inserted_position);
286 template<
typename FilterT,
typename ... EventT>
288 const typename HistoryMap::iterator & start_iter)
290 Timestamp previous_timestamp{};
291 if (start_iter == m_history.begin()) {
292 if (!mpark::holds_alternative<ResetEvent<FilterT>>(start_iter->second.event())) {
293 (void) m_history.erase(start_iter);
294 throw std::runtime_error(
295 "Non-reset event inserted to the beginning of history. This might "
296 "happen if a very old event is inserted into the queue. Consider "
297 "increasing the queue size or debug program latencies.");
300 const auto prev_iter = std::prev(start_iter);
301 previous_timestamp = prev_iter->first;
302 const auto & prev_entry = prev_iter->second;
305 prev_entry.stored_covariance());
307 for (
auto iter = start_iter; iter != m_history.end(); ++iter) {
308 const auto current_timestamp = iter->first;
309 auto & entry = iter->second;
311 EkfStateUpdater{m_filter, m_mahalanobis_threshold, current_timestamp - previous_timestamp},
313 entry.update_stored_state(m_filter.state());
314 entry.update_stored_covariance(m_filter.covariance());
315 previous_timestamp = iter->first;
325 #endif // STATE_ESTIMATION_NODES__HISTORY_HPP_
bool passes_mahalanobis_gate(const Eigen::Matrix< common::types::float32_t, kNumOfStates, 1 > &sample, const Eigen::Matrix< common::types::float32_t, kNumOfStates, 1 > &mean, const Eigen::Matrix< common::types::float32_t, kNumOfStates, kNumOfStates > &covariance, const common::types::float32_t mahalanobis_threshold)
Definition: history.hpp:59
EIGEN_MAKE_ALIGNED_OPERATOR_NEW HistoryEntry(const SingleEventT &event)
Definition: history.hpp:192
This file includes common type definition.
const FilterT::State::Matrix & stored_covariance() const noexcept
Get the stored covariance.
Definition: history.hpp:207
void operator()(const MeasurementT &event)
An operator that passes a measurement event to the filter implementation.
Definition: history.hpp:242
Definition: history.hpp:224
const mpark::variant< EventT... > & event() const
Get the event stored in this history entry.
Definition: history.hpp:212
const FilterT & get_filter() const noexcept
Get the filter as a const ref.
Definition: history.hpp:155
An event to reset the state of the filter.
Definition: history.hpp:84
autoware_auto_vehicle_msgs::msg::VehicleKinematicState State
Definition: motion_common.hpp:40
EIGEN_MAKE_ALIGNED_OPERATOR_NEW FilterT::State state
Definition: history.hpp:87
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:181
void emplace_event(const Timestamp ×tamp, const HistoryEntry &entry)
Add an event to history. If it is added to the middle the following ones are automatically replayed o...
Definition: history.hpp:278
An event to indicate a prediction step.
Definition: history.hpp:76
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24
void operator()(const PredictionEvent &)
An operator that applies the prediction event to the filter implementation.
Definition: history.hpp:266
FilterT::State::Matrix covariance
Definition: history.hpp:88
This class encapsulates a history of events used with EKF.
Definition: history.hpp:105
std::size_t size() const noexcept
Get size of history.
Definition: history.hpp:149
Definition: history.hpp:184
float float32_t
Definition: types.hpp:45
History(FilterT &filter, const std::size_t max_history_size, const common::types::float32_t mahalanobis_threshold)
Construct history from a filter pointer with a specific size.
Definition: history.hpp:130
const FilterT::State & stored_state() const noexcept
Get the stored state.
Definition: history.hpp:200
const Timestamp & get_last_timestamp() const noexcept
Get last timestamp in history.
Definition: history.hpp:151
void update_stored_state(const typename FilterT::State &state) noexcept
Update the stored state.
Definition: history.hpp:195
const HistoryEntry & get_last_event() const noexcept
Get last event in history.
Definition: history.hpp:153
EkfStateUpdater(FilterT &filter, const common::types::float32_t mahalanobis_threshold, const std::chrono::system_clock::duration &dt=std::chrono::milliseconds{0})
Definition: history.hpp:227
FilterT & get_filter() noexcept
Get the filter.
Definition: history.hpp:157
bool empty() const noexcept
Check if the history is empty.
Definition: history.hpp:147
types::float32_t calculate_squared_mahalanobis_distance(const Eigen::Matrix< T, kNumOfStates, 1 > &sample, const Eigen::Matrix< T, kNumOfStates, 1 > &mean, const Eigen::Matrix< T, kNumOfStates, kNumOfStates > &covariance_factor)
Calculate square of mahalanobis distance.
Definition: mahalanobis_distance.hpp:36
void update_stored_covariance(const typename FilterT::State::Matrix &covariance) noexcept
Set the stored covariance.
Definition: history.hpp:202
constexpr COMMON_PUBLIC std::enable_if_t< I==sizeof...(TypesT)> visit(std::tuple< TypesT... > &, Callable) noexcept
Visit every element in a tuple.
Definition: type_traits.hpp:80
void operator()(const ResetEvent< FilterT > &event)
An operator that resets the state of the filter implementation.
Definition: history.hpp:260