19template <
int States,
int Inputs,
int Outputs,
typename KalmanFilterType>
30 :
xHat(observer.Xhat()),
39 void Reset() { m_pastObserverSnapshots.clear(); }
52 m_pastObserverSnapshots.emplace_back(timestamp,
56 if (m_pastObserverSnapshots.size() > kMaxPastObserverStates) {
57 m_pastObserverSnapshots.erase(m_pastObserverSnapshots.begin());
74 KalmanFilterType* observer, units::second_t nominalDt,
Vectord<Rows> y,
76 globalMeasurementCorrect,
77 units::second_t timestamp) {
78 if (m_pastObserverSnapshots.size() == 0) {
86 auto it = std::lower_bound(
87 m_pastObserverSnapshots.cbegin(), m_pastObserverSnapshots.cend(),
89 [](
const auto& entry,
const auto& ts) { return entry.first < ts; });
91 size_t indexOfClosestEntry;
93 if (it == m_pastObserverSnapshots.cbegin()) {
97 if (timestamp < it->
first) {
103 indexOfClosestEntry = 0;
104 }
else if (it == m_pastObserverSnapshots.cend()) {
107 indexOfClosestEntry = m_pastObserverSnapshots.size() - 1;
110 int nextIdx = std::distance(m_pastObserverSnapshots.cbegin(), it);
115 int prevIdx = nextIdx - 1;
118 units::second_t prevTimeDiff =
120 units::second_t nextTimeDiff =
122 indexOfClosestEntry = prevTimeDiff < nextTimeDiff ? prevIdx : nextIdx;
125 units::second_t lastTimestamp =
126 m_pastObserverSnapshots[indexOfClosestEntry].first - nominalDt;
133 for (
size_t i = indexOfClosestEntry; i < m_pastObserverSnapshots.size();
135 auto& [key, snapshot] = m_pastObserverSnapshots[i];
137 if (i == indexOfClosestEntry) {
138 observer->SetS(snapshot.squareRootErrorCovariances);
139 observer->SetXhat(snapshot.xHat);
142 observer->Predict(snapshot.inputs, key - lastTimestamp);
143 observer->Correct(snapshot.inputs, snapshot.localMeasurements);
145 if (i == indexOfClosestEntry) {
151 globalMeasurementCorrect(snapshot.inputs, y);
156 snapshot.localMeasurements};
161 static constexpr size_t kMaxPastObserverStates = 300;
162 std::vector<std::pair<units::second_t, ObserverSnapshot>>
163 m_pastObserverSnapshots;
Definition: KalmanFilterLatencyCompensator.h:20
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:73
void Reset()
Clears the observer snapshot buffer.
Definition: KalmanFilterLatencyCompensator.h:39
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:49
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
Definition: KalmanFilterLatencyCompensator.h:22
Vectord< Inputs > inputs
Definition: KalmanFilterLatencyCompensator.h:25
Vectord< States > xHat
Definition: KalmanFilterLatencyCompensator.h:23
Vectord< Outputs > localMeasurements
Definition: KalmanFilterLatencyCompensator.h:26
Matrixd< States, States > squareRootErrorCovariances
Definition: KalmanFilterLatencyCompensator.h:24
ObserverSnapshot(const KalmanFilterType &observer, const Vectord< Inputs > &u, const Vectord< Outputs > &localY)
Definition: KalmanFilterLatencyCompensator.h:28
#define S(label, offset, message)
Definition: Errors.h:119