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"
13using namespace units::literals;
20 units::Curvature curvature;
44 units::Length
s = 0_in;
63 units::Curvature
curvature, units::Length
s = 0_in)
84 const units::Time new_t =
t + (end_value.
t -
t) * i;
85 const units::Time delta_t = new_t -
t;
91 const bool reversing =
94 const units::Length new_s =
98 const double interpolation_frac = distance > 1E-9_in ? double(new_s / distance) : i;
115 TankWheelVelocities to_wheel_velocities(
const TankKinematics& kin)
const {
119 TankWheelAccelerations to_wheel_accelerations(
const TankKinematics& kin)
const {
132 if (!states_.empty()) {
133 total_time_ = states_.back().t;
138 bool empty()
const {
return states_.empty(); }
144 const std::vector<State>&
states()
const {
return states_; }
147 const std::vector<TrajectoryEvent>&
events()
const {
return events_; }
158 if (states_.empty() || s <= 0_in)
return 0_s;
159 if (s >= states_.back().s)
return total_time_;
161 std::vector<State>::const_iterator it =
162 std::lower_bound(states_.begin() + 1, states_.end(), s,
163 [](
const State& st, units::Length val) { return st.s < val; });
165 const State& s0 = *(it - 1);
166 const State& s1 = *it;
168 if (s1.
s == s0.
s)
return s1.
t;
169 double alpha = (s - s0.
s) / (s1.
s - s0.
s);
170 return s0.
t + (s1.
t - s0.
t) * alpha;
179 if (states_.empty()) {
183 if (t <= states_.front().t) {
184 return states_.front();
186 if (t >= total_time_) {
187 return states_.back();
190 std::vector<State>::const_iterator
sample =
191 std::lower_bound(states_.cbegin() + 1, states_.cend(), t,
192 [](
const State& a,
const units::Time& b) { return a.t < b; });
194 std::vector<State>::const_iterator prev_sample =
sample - 1;
196 if (units::abs(
sample->t - prev_sample->t) < 1E-9_s) {
200 return prev_sample->interpolate(
201 *
sample, ((t - prev_sample->t) / (
sample->t - prev_sample->t)));
210 std::vector<State> new_states = states_;
211 for (
State& state : new_states) {
212 state.pose = state.pose.relative_to(pose);
227 std::vector<State> new_states;
228 new_states.reserve(states_.size());
229 for (
const State& st : states_) {
232 const Pose2d reflected_local_pose(local_pose.
x(), -local_pose.
y(), -local_pose.
rotation());
235 new_states.push_back(new_st);
246 std::vector<State> new_states;
247 new_states.reserve(states_.size());
248 for (
const State& st : states_) {
250 const Translation2d new_translation = 2.0 * center - st.
pose.
translation();
251 new_st.
pose =
Pose2d(new_translation, st.pose.rotation().opposite());
253 new_states.push_back(new_st);
264 std::vector<State> new_states;
265 new_states.reserve(states_.size());
266 for (std::vector<State>::const_reverse_iterator it = states_.rbegin(); it != states_.rend();
269 st.
t = total_time_ - st.
t;
272 st.
s = states_.back().s - st.
s;
273 new_states.push_back(st);
284 if (states_.empty())
return other;
285 if (other.
empty())
return *
this;
287 std::vector<State>
states = states_;
288 std::vector<State> other_states = other.
states();
289 const units::Length total_distance = states_.back().s;
290 for (
State& other_state : other_states) {
291 other_state.t += total_time_;
292 other_state.s += total_distance;
295 states.insert(
states.end(), other_states.begin() + 1, other_states.end());
298 std::vector<TrajectoryEvent> combined_events = events_;
300 combined_events.push_back({ev.time + total_time_, ev.name});
302 combined.
set_events(std::move(combined_events));
312 return total_time_ == other.total_time_ && states_ == other.states_;
316 std::vector<State> states_;
317 std::vector<TrajectoryEvent> events_;
318 units::Time total_time_ = 0_s;
338 : trajectory_(&trajectory), cached_index_(0) {}
349 if (!trajectory_ || trajectory_->empty()) {
352 const std::vector<Trajectory::State>& states = trajectory_->states();
353 if (t <= states.front().t) {
355 return states.front();
357 if (t >= trajectory_->total_time()) {
358 cached_index_ = states.size() - 1;
359 return states.back();
362 while (cached_index_ + 1 < states.size() && states[cached_index_ + 1].t <= t) {
365 while (cached_index_ > 0 && states[cached_index_].t > t) {
369 if (cached_index_ + 1 >= states.size()) {
370 return states.back();
375 if (units::abs(next.
t - prev.
t) < 1E-9_s) {
378 return prev.
interpolate(next, ((t - prev.
t) / (next.
t - prev.
t)));
383 size_t cached_index_ = 0;
constexpr Q distance(LinearVector2d other) const
Gets the distance between two vectors.
Definition translation2d.h:108
constexpr Rotation2d rotation() const
Gets the rotation.
Definition pose2d.h:102
constexpr Pose2d transform_by(const Transform2d &transform) const
Definition pose2d.h:175
constexpr units::Length y() const
Gets the y value of the translation.
Definition pose2d.h:87
constexpr Pose2d interpolate(const Pose2d &end, double fraction) const
Definition pose2d.h:188
constexpr Translation2d translation() const
Gets the translation.
Definition pose2d.h:96
constexpr units::Length x() const
Gets the x value of the translation.
Definition pose2d.h:78
constexpr Pose2d relative_to(const Pose2d &other) const
Definition pose2d.h:162
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:91
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:341
Trajectory::State sample(units::Time t)
Samples state at timestamp t using cached sequential lookup (O(1)).
Definition trajectory.h:348
TrajectorySampler(const Trajectory &trajectory)
Constructs a TrajectorySampler bound to a target Trajectory.
Definition trajectory.h:337
Represents a time-parameterized 2D motion trajectory.
Definition trajectory.h:37
units::Time total_time() const
Definition trajectory.h:141
Trajectory reverse() const
Returns a time-reversed version of this trajectory (traces the path from end to start).
Definition trajectory.h:263
Trajectory mirror(const Pose2d &line_pose) const
Mirrors the trajectory across the line represented by a pose.
Definition trajectory.h:226
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:245
Trajectory relative_to(const Pose2d &pose) const
Expresses all poses in the trajectory relative to a reference pose.
Definition trajectory.h:209
void set_events(std::vector< TrajectoryEvent > events)
Attaches a vector of time-parameterized events to the trajectory.
Definition trajectory.h:150
units::Time time_from_distance(units::Length s) const
Interpolates the time at which a given arc distance is reached.
Definition trajectory.h:157
Trajectory(std::vector< State > states)
Constructs a Trajectory from a vector of State samples.
Definition trajectory.h:131
Trajectory operator+(const Trajectory &other) const
Concatenates two trajectories end-to-end.
Definition trajectory.h:283
Pose2d initial_pose() const
Definition trajectory.h:308
bool operator==(const Trajectory &other) const
Checks equality between two trajectories.
Definition trajectory.h:311
State sample(units::Time t) const
Samples trajectory state at timestamp t using binary search interpolation.
Definition trajectory.h:178
bool empty() const
Definition trajectory.h:138
const std::vector< State > & states() const
Definition trajectory.h:144
Trajectory()=default
Default constructor.
const std::vector< TrajectoryEvent > & events() const
Definition trajectory.h:147
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:72
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:83
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