13#include "wpi/units/acceleration.hpp"
14#include "wpi/units/curvature.hpp"
15#include "wpi/units/math.hpp"
16#include "wpi/units/time.hpp"
17#include "wpi/units/velocity.hpp"
38 wpi::units::second_t
t = 0_s;
41 wpi::units::meters_per_second_t
velocity = 0_mps;
71 const auto deltaT = newT -
t;
79 const auto reversing =
85 const wpi::units::meters_per_second_t newV =
90 const wpi::units::meter_t newS =
92 (reversing ? -1.0 : 1.0);
98 const double interpolationFrac =
116 explicit Trajectory(
const std::vector<State>& states) : m_states(states) {
117 if (m_states.empty()) {
118 throw std::invalid_argument(
119 "Trajectory manually initialized with no states.");
122 m_totalTime = states.back().t;
129 wpi::units::second_t
TotalTime()
const {
return m_totalTime; }
136 const std::vector<State>&
States()
const {
return m_states; }
146 if (m_states.empty()) {
147 throw std::runtime_error(
148 "Trajectory cannot be sampled if it has no states.");
151 if (t <= m_states.front().t) {
152 return m_states.front();
154 if (t >= m_totalTime) {
155 return m_states.back();
162 std::lower_bound(m_states.cbegin() + 1, m_states.cend(), t,
163 [](
const auto& a,
const auto& b) { return a.t < b; });
165 auto prevSample = sample - 1;
173 if (wpi::units::math::abs(sample->t - prevSample->t) < 1E-9_s) {
177 return prevSample->Interpolate(
178 *sample, (t - prevSample->t) / (sample->t - prevSample->t));
190 auto& firstState = m_states[0];
191 auto& firstPose = firstState.pose;
194 auto newFirstPose = firstPose + transform;
195 auto newStates = m_states;
196 newStates[0].pose = newFirstPose;
198 for (
unsigned int i = 1; i < newStates.size(); i++) {
199 auto& state = newStates[i];
202 state.pose = newFirstPose + (state.pose - firstPose);
218 auto newStates = m_states;
219 for (
auto& state : newStates) {
220 state.pose = state.pose.RelativeTo(pose);
236 if (m_states.empty()) {
240 auto states = m_states;
241 auto otherStates = other.
States();
242 for (
auto& otherState : otherStates) {
243 otherState.t += m_totalTime;
250 states.insert(states.end(), otherStates.begin() + 1, otherStates.end());
267 std::vector<State> m_states;
268 wpi::units::second_t m_totalTime = 0_s;
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
Represents a 2D pose containing translational and rotational elements.
Definition Pose2d.hpp:30
constexpr const Translation2d & Translation() const
Returns the underlying translation.
Definition Pose2d.hpp:110
Pose2d InitialPose() const
Returns the initial pose of the trajectory.
Definition Trajectory.hpp:259
wpi::units::second_t TotalTime() const
Returns the overall duration of the trajectory.
Definition Trajectory.hpp:129
Trajectory RelativeTo(const Pose2d &pose)
Transforms all poses in the trajectory so that they are relative to the given pose.
Definition Trajectory.hpp:217
bool operator==(const Trajectory &) const =default
Checks equality between this Trajectory and another object.
State Sample(wpi::units::second_t t) const
Sample the trajectory at a point in time.
Definition Trajectory.hpp:145
const std::vector< State > & States() const
Return the states of the trajectory.
Definition Trajectory.hpp:136
Trajectory operator+(const Trajectory &other) const
Concatenates another trajectory to the current trajectory.
Definition Trajectory.hpp:233
Trajectory(const std::vector< State > &states)
Constructs a trajectory from a vector of states.
Definition Trajectory.hpp:116
Trajectory TransformBy(const Transform2d &transform)
Transforms all poses in the trajectory by the given transform.
Definition Trajectory.hpp:189
constexpr wpi::units::meter_t Distance(const Translation2d &other) const
Calculates the distance between two translations in 2D space.
Definition Translation2d.hpp:79
Definition LinearSystem.hpp:20
WPILIB_DLLEXPORT void to_json(wpi::util::json &json, const Translation2d &state)
WPILIB_DLLEXPORT void from_json(const wpi::util::json &json, Translation2d &state)
Definition raw_os_ostream.hpp:19
constexpr T Lerp(const T &startValue, const T &endValue, double t)
Linearly interpolates between two values.
Definition MathExtras.hpp:780
Represents one point on the trajectory.
Definition Trajectory.hpp:36
wpi::units::second_t t
The time elapsed since the beginning of the trajectory.
Definition Trajectory.hpp:38
constexpr State Interpolate(State endValue, double i) const
Interpolates between two States.
Definition Trajectory.hpp:65
constexpr bool operator==(const State &) const =default
Checks equality between this State and another object.
Pose2d pose
The pose at that point of the trajectory.
Definition Trajectory.hpp:47
wpi::units::meters_per_second_squared_t acceleration
The acceleration at that point of the trajectory.
Definition Trajectory.hpp:44
wpi::units::meters_per_second_t velocity
The velocity at that point of the trajectory.
Definition Trajectory.hpp:41
wpi::units::curvature_t curvature
The curvature at that point of the trajectory.
Definition Trajectory.hpp:50