16#include "wpi/units/time.hpp"
43 std::function<T(
const T&,
const T&,
double)> func)
44 : m_historySize(historySize), m_interpolatingFunc(func) {}
55 : m_historySize(historySize),
56 m_interpolatingFunc([](const T& start, const T& end, double t) {
57 if constexpr (
requires(T a, T b,
double t) { a + (b - a) * t; }) {
60 return start.Interpolate(end, t);
70 void AddSample(wpi::units::second_t time, T sample) {
72 if (m_pastSnapshots.size() == 0 || time > m_pastSnapshots.back().first) {
73 m_pastSnapshots.emplace_back(time, sample);
75 auto first_after = std::upper_bound(
76 m_pastSnapshots.begin(), m_pastSnapshots.end(), time,
77 [](
auto t,
const auto& pair) { return t < pair.first; });
79 if (first_after == m_pastSnapshots.begin()) {
81 m_pastSnapshots.insert(first_after, std::pair{time, sample});
82 }
else if (
auto last_not_greater_than = first_after - 1;
83 last_not_greater_than == m_pastSnapshots.begin() ||
84 last_not_greater_than->first < time) {
87 m_pastSnapshots.insert(first_after, std::pair{time, sample});
90 last_not_greater_than->second = sample;
93 while (time - m_pastSnapshots[0].first > m_historySize) {
94 m_pastSnapshots.erase(m_pastSnapshots.begin());
99 void Clear() { m_pastSnapshots.clear(); }
107 std::optional<T>
Sample(wpi::units::second_t time)
const {
108 if (m_pastSnapshots.empty()) {
116 if (time <= m_pastSnapshots.front().first) {
117 return m_pastSnapshots.front().second;
119 if (time > m_pastSnapshots.back().first) {
120 return m_pastSnapshots.back().second;
122 if (m_pastSnapshots.size() < 2) {
123 return m_pastSnapshots[0].second;
127 auto upper_bound = std::lower_bound(
128 m_pastSnapshots.begin(), m_pastSnapshots.end(), time,
129 [](
const auto& pair,
auto t) { return t > pair.first; });
131 if (upper_bound == m_pastSnapshots.begin()) {
132 return upper_bound->second;
135 auto lower_bound = upper_bound - 1;
137 double t = ((time - lower_bound->first) /
138 (upper_bound->first - lower_bound->first));
140 return m_interpolatingFunc(lower_bound->second, upper_bound->second, t);
148 return m_pastSnapshots;
156 return m_pastSnapshots;
160 wpi::units::second_t m_historySize;
161 std::vector<std::pair<wpi::units::second_t, T>> m_pastSnapshots;
162 std::function<T(
const T&,
const T&,
double)> m_interpolatingFunc;
169 wpi::units::second_t historySize)
170 : m_historySize(historySize),
171 m_interpolatingFunc([](const
Pose2d& start, const
Pose2d& end, double t) {
177 Twist2d twist = (end - start).Log();
178 Twist2d scaledTwist = twist * t;
179 return start + scaledTwist.
Exp();
185 wpi::units::second_t historySize)
186 : m_historySize(historySize),
187 m_interpolatingFunc([](const
Pose3d& start, const
Pose3d& end, double t) {
193 Twist3d twist = (end - start).Log();
194 Twist3d scaledTwist = twist * t;
195 return start + scaledTwist.
Exp();
Represents a 2D pose containing translational and rotational elements.
Definition Pose2d.hpp:27
Represents a 3D pose containing translational and rotational elements.
Definition Pose3d.hpp:28
TimeInterpolatableBuffer(wpi::units::second_t historySize)
Create a new TimeInterpolatableBuffer.
Definition TimeInterpolatableBuffer.hpp:54
TimeInterpolatableBuffer(wpi::units::second_t historySize, std::function< T(const T &, const T &, double)> func)
Create a new TimeInterpolatableBuffer.
Definition TimeInterpolatableBuffer.hpp:42
void Clear()
Clear all old samples.
Definition TimeInterpolatableBuffer.hpp:99
std::optional< T > Sample(wpi::units::second_t time) const
Sample the buffer at the given time.
Definition TimeInterpolatableBuffer.hpp:107
const std::vector< std::pair< wpi::units::second_t, T > > & GetInternalBuffer() const
Grant access to the internal sample buffer.
Definition TimeInterpolatableBuffer.hpp:154
std::vector< std::pair< wpi::units::second_t, T > > & GetInternalBuffer()
Grant access to the internal sample buffer.
Definition TimeInterpolatableBuffer.hpp:147
void AddSample(wpi::units::second_t time, T sample)
Add a sample to the buffer.
Definition TimeInterpolatableBuffer.hpp:70
Definition LinearSystem.hpp:20
constexpr T Lerp(const T &startValue, const T &endValue, double t)
Linearly interpolates between two values.
Definition MathExtras.hpp:780
A change in distance along a 2D arc since the last pose update.
Definition Twist2d.hpp:23
constexpr Transform2d Exp() const
Obtain a new Transform2d from a (constant curvature) velocity.
Definition Twist2d.hpp:86
A change in distance along a 3D arc since the last pose update.
Definition Twist3d.hpp:23
constexpr Transform3d Exp() const
Obtain a new Transform3d from a (constant curvature) velocity.
Definition Twist3d.hpp:106