WPILibC++ 2023.4.3
Trajectory.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <vector>
8
9#include <wpi/SymbolExports.h>
10
11#include "frc/geometry/Pose2d.h"
13#include "units/acceleration.h"
14#include "units/curvature.h"
15#include "units/time.h"
16#include "units/velocity.h"
17
18namespace wpi {
19class json;
20} // namespace wpi
21
22namespace frc {
23/**
24 * Represents a time-parameterized trajectory. The trajectory contains of
25 * various States that represent the pose, curvature, time elapsed, velocity,
26 * and acceleration at that point.
27 */
29 public:
30 /**
31 * Represents one point on the trajectory.
32 */
34 // The time elapsed since the beginning of the trajectory.
35 units::second_t t = 0_s;
36
37 // The speed at that point of the trajectory.
38 units::meters_per_second_t velocity = 0_mps;
39
40 // The acceleration at that point of the trajectory.
41 units::meters_per_second_squared_t acceleration = 0_mps_sq;
42
43 // The pose at that point of the trajectory.
45
46 // The curvature at that point of the trajectory.
47 units::curvature_t curvature{0.0};
48
49 /**
50 * Checks equality between this State and another object.
51 */
52 bool operator==(const State&) const = default;
53
54 /**
55 * Interpolates between two States.
56 *
57 * @param endValue The end value for the interpolation.
58 * @param i The interpolant (fraction).
59 *
60 * @return The interpolated state.
61 */
62 State Interpolate(State endValue, double i) const;
63 };
64
65 Trajectory() = default;
66
67 /**
68 * Constructs a trajectory from a vector of states.
69 */
70 explicit Trajectory(const std::vector<State>& states);
71
72 /**
73 * Returns the overall duration of the trajectory.
74 * @return The duration of the trajectory.
75 */
76 units::second_t TotalTime() const { return m_totalTime; }
77
78 /**
79 * Return the states of the trajectory.
80 * @return The states of the trajectory.
81 */
82 const std::vector<State>& States() const { return m_states; }
83
84 /**
85 * Sample the trajectory at a point in time.
86 *
87 * @param t The point in time since the beginning of the trajectory to sample.
88 * @return The state at that point in time.
89 */
90 State Sample(units::second_t t) const;
91
92 /**
93 * Transforms all poses in the trajectory by the given transform. This is
94 * useful for converting a robot-relative trajectory into a field-relative
95 * trajectory. This works with respect to the first pose in the trajectory.
96 *
97 * @param transform The transform to transform the trajectory by.
98 * @return The transformed trajectory.
99 */
101
102 /**
103 * Transforms all poses in the trajectory so that they are relative to the
104 * given pose. This is useful for converting a field-relative trajectory
105 * into a robot-relative trajectory.
106 *
107 * @param pose The pose that is the origin of the coordinate frame that
108 * the current trajectory will be transformed into.
109 * @return The transformed trajectory.
110 */
112
113 /**
114 * Concatenates another trajectory to the current trajectory. The user is
115 * responsible for making sure that the end pose of this trajectory and the
116 * start pose of the other trajectory match (if that is the desired behavior).
117 *
118 * @param other The trajectory to concatenate.
119 * @return The concatenated trajectory.
120 */
121 Trajectory operator+(const Trajectory& other) const;
122
123 /**
124 * Returns the initial pose of the trajectory.
125 *
126 * @return The initial pose of the trajectory.
127 */
128 Pose2d InitialPose() const { return Sample(0_s).pose; }
129
130 /**
131 * Checks equality between this Trajectory and another object.
132 */
133 bool operator==(const Trajectory&) const = default;
134
135 private:
136 std::vector<State> m_states;
137 units::second_t m_totalTime = 0_s;
138};
139
141void to_json(wpi::json& json, const Trajectory::State& state);
142
144void from_json(const wpi::json& json, Trajectory::State& state);
145
146} // namespace frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
Represents a 2D pose containing translational and rotational elements.
Definition: Pose2d.h:25
Represents a time-parameterized trajectory.
Definition: Trajectory.h:28
units::second_t TotalTime() const
Returns the overall duration of the trajectory.
Definition: Trajectory.h:76
Trajectory operator+(const Trajectory &other) const
Concatenates another trajectory to the current trajectory.
Trajectory TransformBy(const Transform2d &transform)
Transforms all poses in the trajectory by the given transform.
Trajectory RelativeTo(const Pose2d &pose)
Transforms all poses in the trajectory so that they are relative to the given pose.
Trajectory()=default
bool operator==(const Trajectory &) const =default
Checks equality between this Trajectory and another object.
Trajectory(const std::vector< State > &states)
Constructs a trajectory from a vector of states.
State Sample(units::second_t t) const
Sample the trajectory at a point in time.
Pose2d InitialPose() const
Returns the initial pose of the trajectory.
Definition: Trajectory.h:128
const std::vector< State > & States() const
Return the states of the trajectory.
Definition: Trajectory.h:82
Represents a transformation for a Pose2d.
Definition: Transform2d.h:18
a class to store JSON values
Definition: json.h:2655
Definition: AprilTagFieldLayout.h:22
WPILIB_DLLEXPORT void from_json(const wpi::json &json, AprilTagFieldLayout &layout)
WPILIB_DLLEXPORT void to_json(wpi::json &json, const AprilTagFieldLayout &layout)
/file This file defines the SmallVector class.
Definition: AprilTagFieldLayout.h:18
Represents one point on the trajectory.
Definition: Trajectory.h:33
Pose2d pose
Definition: Trajectory.h:44
State Interpolate(State endValue, double i) const
Interpolates between two States.
bool operator==(const State &) const =default
Checks equality between this State and another object.