27template <
int States,
int Inputs,
int Outputs,
typename KalmanFilterType>
45 :
xHat(observer.Xhat()),
54 void Reset() { m_pastObserverSnapshots.clear(); }
67 m_pastObserverSnapshots.emplace_back(timestamp,
71 if (m_pastObserverSnapshots.size() > kMaxPastObserverStates) {
72 m_pastObserverSnapshots.erase(m_pastObserverSnapshots.begin());
89 KalmanFilterType* observer, units::second_t nominalDt,
Vectord<Rows> y,
91 globalMeasurementCorrect,
92 units::second_t timestamp) {
93 if (m_pastObserverSnapshots.size() == 0) {
101 auto it = std::lower_bound(
102 m_pastObserverSnapshots.cbegin(), m_pastObserverSnapshots.cend(),
104 [](
const auto& entry,
const auto& ts) { return entry.first < ts; });
106 size_t indexOfClosestEntry;
108 if (it == m_pastObserverSnapshots.cbegin()) {
112 if (timestamp < it->
first) {
118 indexOfClosestEntry = 0;
119 }
else if (it == m_pastObserverSnapshots.cend()) {
122 indexOfClosestEntry = m_pastObserverSnapshots.size() - 1;
125 int nextIdx = std::distance(m_pastObserverSnapshots.cbegin(), it);
130 int prevIdx = nextIdx - 1;
133 units::second_t prevTimeDiff =
135 units::second_t nextTimeDiff =
137 indexOfClosestEntry = prevTimeDiff < nextTimeDiff ? prevIdx : nextIdx;
140 units::second_t lastTimestamp =
141 m_pastObserverSnapshots[indexOfClosestEntry].first - nominalDt;
148 for (
size_t i = indexOfClosestEntry; i < m_pastObserverSnapshots.size();
150 auto& [key, snapshot] = m_pastObserverSnapshots[i];
152 if (i == indexOfClosestEntry) {
153 observer->SetS(snapshot.squareRootErrorCovariances);
154 observer->SetXhat(snapshot.xHat);
157 observer->Predict(snapshot.inputs, key - lastTimestamp);
158 observer->Correct(snapshot.inputs, snapshot.localMeasurements);
160 if (i == indexOfClosestEntry) {
166 globalMeasurementCorrect(snapshot.inputs, y);
171 snapshot.localMeasurements};
176 static constexpr size_t kMaxPastObserverStates = 300;
177 std::vector<std::pair<units::second_t, ObserverSnapshot>>
178 m_pastObserverSnapshots;
This class incorporates time-delayed measurements into a Kalman filter's state estimate.
Definition: KalmanFilterLatencyCompensator.h:28
void ApplyPastGlobalMeasurement(KalmanFilterType *observer, units::second_t nominalDt, Vectord< Rows > y, std::function< void(const Vectord< Inputs > &u, const Vectord< Rows > &y)> globalMeasurementCorrect, units::second_t timestamp)
Add past global measurements (such as from vision)to the estimator.
Definition: KalmanFilterLatencyCompensator.h:88
void Reset()
Clears the observer snapshot buffer.
Definition: KalmanFilterLatencyCompensator.h:54
void AddObserverState(const KalmanFilterType &observer, Vectord< Inputs > u, Vectord< Outputs > localY, units::second_t timestamp)
Add past observer states to the observer snapshots list.
Definition: KalmanFilterLatencyCompensator.h:64
UnitType abs(const UnitType x) noexcept
Compute absolute value.
Definition: math.h:721
const T & first(const T &value, const Tail &...)
Definition: compile.h:60
Definition: AprilTagPoseEstimator.h:15
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition: EigenCore.h:21
Eigen::Vector< double, Size > Vectord
Definition: EigenCore.h:12
This class contains all the information about our observer at a given time.
Definition: KalmanFilterLatencyCompensator.h:33
Vectord< Inputs > inputs
The inputs.
Definition: KalmanFilterLatencyCompensator.h:39
Vectord< States > xHat
The state estimate.
Definition: KalmanFilterLatencyCompensator.h:35
Vectord< Outputs > localMeasurements
The local measurements.
Definition: KalmanFilterLatencyCompensator.h:41
Matrixd< States, States > squareRootErrorCovariances
The square root error covariance.
Definition: KalmanFilterLatencyCompensator.h:37
ObserverSnapshot(const KalmanFilterType &observer, const Vectord< Inputs > &u, const Vectord< Outputs > &localY)
Definition: KalmanFilterLatencyCompensator.h:43
#define S(label, offset, message)
Definition: Errors.h:119