6#include "core/math/geometry/pose2d.h"
7#include "core/math/kinematics/chassis_accelerations.h"
8#include "core/math/kinematics/chassis_velocities.h"
9#include "core/math/kinematics/tank_kinematics.h"
10#include "core/math/kinematics/tank_wheel_velocities.h"
11#include "core/math/units.h"
20 units::Curvature curvature;
44 units::Length
s = 0_in;
64 units::Curvature
curvature, units::Length
s = 0_in
86 const units::Time new_t =
t + (end_value.
t -
t) * i;
87 const units::Time delta_t = new_t -
t;
93 const bool reversing =
96 const units::Length new_s =
100 const double interpolation_frac = distance > 1E-9_in ? double(new_s / distance) : i;
104 const units::Curvature new_curvature =
118 TankWheelVelocities to_wheel_velocities(
const TankKinematics& kin)
const {
122 TankWheelAccelerations to_wheel_accelerations(
const TankKinematics& kin)
const {
135 if (!states_.empty()) {
136 total_time_ = states_.back().t;
141 bool empty()
const {
return states_.empty(); }
147 const std::vector<State>&
states()
const {
return states_; }
150 const std::vector<TrajectoryEvent>&
events()
const {
return events_; }
161 if (states_.empty() || s <= 0_in)
return 0_s;
162 if (s >= states_.back().s)
return total_time_;
164 std::vector<State>::const_iterator it = std::lower_bound(
165 states_.begin() + 1, states_.end(), s,
166 [](
const State& st, units::Length val) { return st.s < val; }
169 const State& s0 = *(it - 1);
170 const State& s1 = *it;
172 if (s1.
s == s0.
s)
return s1.
t;
173 double alpha = (s - s0.
s) / (s1.
s - s0.
s);
174 return s0.
t + (s1.
t - s0.
t) * alpha;
183 if (states_.empty()) {
187 if (t <= states_.front().t) {
188 return states_.front();
190 if (t >= total_time_) {
191 return states_.back();
194 std::vector<State>::const_iterator
sample = std::lower_bound(
195 states_.cbegin() + 1, states_.cend(), t,
196 [](
const State& a,
const units::Time& b) { return a.t < b; }
199 std::vector<State>::const_iterator prev_sample =
sample - 1;
201 if (units::abs(
sample->t - prev_sample->t) < 1E-9_s) {
205 return prev_sample->interpolate(*
sample, ((t - prev_sample->t) / (
sample->t - prev_sample->t)));
214 std::vector<State> new_states = states_;
215 for (
State& state : new_states) {
216 state.pose = state.pose.relative_to(pose);
231 std::vector<State> new_states;
232 new_states.reserve(states_.size());
233 for (
const State& st : states_) {
236 const Pose2d reflected_local_pose(local_pose.
x(), -local_pose.
y(), -local_pose.
rotation());
239 new_states.push_back(new_st);
250 std::vector<State> new_states;
251 new_states.reserve(states_.size());
252 for (
const State& st : states_) {
254 const Translation2d new_translation = 2.0 * center - st.
pose.
translation();
255 new_st.
pose =
Pose2d(new_translation, st.pose.rotation().opposite());
257 new_states.push_back(new_st);
268 std::vector<State> new_states;
269 new_states.reserve(states_.size());
270 for (std::vector<State>::const_reverse_iterator it = states_.rbegin(); it != states_.rend();
273 st.
t = total_time_ - st.
t;
276 st.
s = states_.back().s - st.
s;
277 new_states.push_back(st);
288 if (states_.empty())
return other;
289 if (other.
empty())
return *
this;
291 std::vector<State>
states = states_;
292 std::vector<State> other_states = other.
states();
293 const units::Length total_distance = states_.back().s;
294 for (
State& other_state : other_states) {
295 other_state.t += total_time_;
296 other_state.s += total_distance;
299 states.insert(
states.end(), other_states.begin() + 1, other_states.end());
302 std::vector<TrajectoryEvent> combined_events = events_;
304 combined_events.push_back({ev.time + total_time_, ev.name});
306 combined.
set_events(std::move(combined_events));
316 return total_time_ == other.total_time_ && states_ == other.states_;
320 std::vector<State> states_;
321 std::vector<TrajectoryEvent> events_;
322 units::Time total_time_ = 0_s;
342 : trajectory_(&trajectory), cached_index_(0) {}
353 if (!trajectory_ || trajectory_->empty()) {
356 const std::vector<Trajectory::State>& states = trajectory_->states();
357 if (t <= states.front().t) {
359 return states.front();
361 if (t >= trajectory_->total_time()) {
362 cached_index_ = states.size() - 1;
363 return states.back();
366 while (cached_index_ + 1 < states.size() && states[cached_index_ + 1].t <= t) {
369 while (cached_index_ > 0 && states[cached_index_].t > t) {
373 if (cached_index_ + 1 >= states.size()) {
374 return states.back();
379 if (units::abs(next.
t - prev.
t) < 1E-9_s) {
382 return prev.
interpolate(next, ((t - prev.
t) / (next.
t - prev.
t)));
387 size_t cached_index_ = 0;
constexpr Q distance(LinearVector2d other) const
Gets the distance between two vectors.
Definition translation2d.h:107
constexpr Rotation2d rotation() const
Gets the rotation.
Definition pose2d.h:101
constexpr Pose2d transform_by(const Transform2d &transform) const
Definition pose2d.h:176
constexpr units::Length y() const
Gets the y value of the translation.
Definition pose2d.h:86
constexpr Pose2d interpolate(const Pose2d &end, double fraction) const
Definition pose2d.h:189
constexpr Translation2d translation() const
Gets the translation.
Definition pose2d.h:95
constexpr units::Length x() const
Gets the x value of the translation.
Definition pose2d.h:77
constexpr Pose2d relative_to(const Pose2d &other) const
Definition pose2d.h:163
constexpr Pose2d()
Default Constructor for Pose2d.
Definition pose2d.h:28
TankWheelAccelerations to_wheel_accelerations(const ChassisAccelerations &chassis_accels) const
Returns left and right component accelerations from a chassis acceleration using inverse kinematics.
Definition tank_kinematics.h:92
TankWheelVelocities to_wheel_velocities(const ChassisVelocities &chassis_velocities) const
Returns left and right component velocities from a chassis velocity using inverse kinematics.
Definition tank_kinematics.h:55
void reset()
Resets cached index back to start.
Definition trajectory.h:345
Trajectory::State sample(units::Time t)
Samples state at timestamp t using cached sequential lookup (O(1)).
Definition trajectory.h:352
TrajectorySampler(const Trajectory &trajectory)
Constructs a TrajectorySampler bound to a target Trajectory.
Definition trajectory.h:341
Represents a time-parameterized 2D motion trajectory.
Definition trajectory.h:37
units::Time total_time() const
Definition trajectory.h:144
Trajectory reverse() const
Returns a time-reversed version of this trajectory (traces the path from end to start).
Definition trajectory.h:267
Trajectory mirror(const Pose2d &line_pose) const
Mirrors the trajectory across the line represented by a pose.
Definition trajectory.h:230
Trajectory rotate_center(Translation2d center=Translation2d(3_tiles, 3_tiles)) const
Rotates the trajectory 180 degrees around a center point (Point reflection).
Definition trajectory.h:249
Trajectory relative_to(const Pose2d &pose) const
Expresses all poses in the trajectory relative to a reference pose.
Definition trajectory.h:213
void set_events(std::vector< TrajectoryEvent > events)
Attaches a vector of time-parameterized events to the trajectory.
Definition trajectory.h:153
units::Time time_from_distance(units::Length s) const
Interpolates the time at which a given arc distance is reached.
Definition trajectory.h:160
Trajectory(std::vector< State > states)
Constructs a Trajectory from a vector of State samples.
Definition trajectory.h:134
Trajectory operator+(const Trajectory &other) const
Concatenates two trajectories end-to-end.
Definition trajectory.h:287
Pose2d initial_pose() const
Definition trajectory.h:312
bool operator==(const Trajectory &other) const
Checks equality between two trajectories.
Definition trajectory.h:315
State sample(units::Time t) const
Samples trajectory state at timestamp t using binary search interpolation.
Definition trajectory.h:182
bool empty() const
Definition trajectory.h:141
const std::vector< State > & states() const
Definition trajectory.h:147
Trajectory()=default
Default constructor.
const std::vector< TrajectoryEvent > & events() const
Definition trajectory.h:150
Represents robot chassis accelerations.
Definition chassis_accelerations.h:12
Represents robot chassis velocities.
Definition chassis_velocities.h:14
Pair representing 2D Pose and scalar path units::Curvature.
Definition trajectory.h:18
Represents an action event triggered at a specific time along a trajectory.
Definition trajectory.h:26
Represents a single discrete state sample along a trajectory.
Definition trajectory.h:42
bool operator==(const State &other) const
Checks equality between two States.
Definition trajectory.h:74
State interpolate(State end_value, double i) const
Linearly interpolates between this state and end_value at fraction i in [0, 1].
Definition trajectory.h:85
State(units::Time t, units::Velocity velocity, units::Acceleration acceleration, Pose2d pose, units::Curvature curvature, units::Length s=0_in)
Constructs a Trajectory State with explicit values.
Definition trajectory.h:62
Pose2d pose
2D robot pose (x, y, theta)
Definition trajectory.h:47
units::Length s
Distance along trajectory.
Definition trajectory.h:44
units::Curvature curvature
Path curvature at pose.
Definition trajectory.h:48
units::Acceleration acceleration
Linear chassis acceleration.
Definition trajectory.h:46
units::Time t
Elapsed time from trajectory start.
Definition trajectory.h:43
State()=default
Default constructor.
units::Velocity velocity
Linear chassis velocity.
Definition trajectory.h:45