WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
TrajectoryConfig.hpp
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 <concepts>
8#include <memory>
9#include <vector>
10
18#include "wpi/units/acceleration.hpp"
19#include "wpi/units/velocity.hpp"
21
22namespace wpi::math {
23/**
24 * Represents the configuration for generating a trajectory. This class stores
25 * the start velocity, end velocity, max velocity, max acceleration, custom
26 * constraints, and the reversed flag.
27 *
28 * The class must be constructed with a max velocity and max acceleration.
29 * The other parameters (start velocity, end velocity, constraints, reversed)
30 * have been defaulted to reasonable values (0, 0, {}, false). These values can
31 * be changed via the SetXXX methods.
32 */
34 public:
35 /**
36 * Constructs a config object.
37 * @param maxVelocity The max velocity of the trajectory.
38 * @param maxAcceleration The max acceleration of the trajectory.
39 */
40 TrajectoryConfig(wpi::units::meters_per_second_t maxVelocity,
41 wpi::units::meters_per_second_squared_t maxAcceleration)
42 : m_maxVelocity(maxVelocity), m_maxAcceleration(maxAcceleration) {}
43
46
49
50 /**
51 * Sets the start velocity of the trajectory.
52 * @param startVelocity The start velocity of the trajectory.
53 */
54 void SetStartVelocity(wpi::units::meters_per_second_t startVelocity) {
55 m_startVelocity = startVelocity;
56 }
57
58 /**
59 * Sets the end velocity of the trajectory.
60 * @param endVelocity The end velocity of the trajectory.
61 */
62 void SetEndVelocity(wpi::units::meters_per_second_t endVelocity) {
63 m_endVelocity = endVelocity;
64 }
65
66 /**
67 * Sets the reversed flag of the trajectory.
68 * @param reversed Whether the trajectory should be reversed or not.
69 */
70 void SetReversed(bool reversed) { m_reversed = reversed; }
71
72 /**
73 * Adds a user-defined constraint to the trajectory.
74 * @param constraint The user-defined constraint.
75 */
76 template <std::derived_from<TrajectoryConstraint> Constraint>
77 void AddConstraint(Constraint constraint) {
78 m_constraints.emplace_back(std::make_unique<Constraint>(constraint));
79 }
80
81 /**
82 * Adds a differential drive kinematics constraint to ensure that
83 * no wheel velocity of a differential drive goes above the max velocity.
84 *
85 * @param kinematics The differential drive kinematics.
86 */
89 DifferentialDriveKinematicsConstraint(kinematics, m_maxVelocity));
90 }
91
92 /**
93 * Adds a mecanum drive kinematics constraint to ensure that
94 * no wheel velocity of a mecanum drive goes above the max velocity.
95 *
96 * @param kinematics The mecanum drive kinematics.
97 */
99 AddConstraint(MecanumDriveKinematicsConstraint(kinematics, m_maxVelocity));
100 }
101
102 /**
103 * Adds a swerve drive kinematics constraint to ensure that
104 * no wheel velocity of a swerve drive goes above the max velocity.
105 *
106 * @param kinematics The swerve drive kinematics.
107 */
108 template <size_t NumModules>
110 AddConstraint(SwerveDriveKinematicsConstraint(kinematics, m_maxVelocity));
111 }
112
113 /**
114 * Returns the starting velocity of the trajectory.
115 * @return The starting velocity of the trajectory.
116 */
117 wpi::units::meters_per_second_t StartVelocity() const {
118 return m_startVelocity;
119 }
120
121 /**
122 * Returns the ending velocity of the trajectory.
123 * @return The ending velocity of the trajectory.
124 */
125 wpi::units::meters_per_second_t EndVelocity() const { return m_endVelocity; }
126
127 /**
128 * Returns the maximum velocity of the trajectory.
129 * @return The maximum velocity of the trajectory.
130 */
131 wpi::units::meters_per_second_t MaxVelocity() const { return m_maxVelocity; }
132
133 /**
134 * Returns the maximum acceleration of the trajectory.
135 * @return The maximum acceleration of the trajectory.
136 */
137 wpi::units::meters_per_second_squared_t MaxAcceleration() const {
138 return m_maxAcceleration;
139 }
140
141 /**
142 * Returns the user-defined constraints of the trajectory.
143 * @return The user-defined constraints of the trajectory.
144 */
145 const std::vector<std::unique_ptr<TrajectoryConstraint>>& Constraints()
146 const {
147 return m_constraints;
148 }
149
150 /**
151 * Returns whether the trajectory is reversed or not.
152 * @return whether the trajectory is reversed or not.
153 */
154 bool IsReversed() const { return m_reversed; }
155
156 private:
157 wpi::units::meters_per_second_t m_startVelocity = 0_mps;
158 wpi::units::meters_per_second_t m_endVelocity = 0_mps;
159 wpi::units::meters_per_second_t m_maxVelocity;
160 wpi::units::meters_per_second_squared_t m_maxAcceleration;
161 std::vector<std::unique_ptr<TrajectoryConstraint>> m_constraints;
162 bool m_reversed = false;
163};
164} // namespace wpi::math
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
A class that enforces constraints on the differential drive kinematics.
Definition DifferentialDriveKinematicsConstraint.hpp:22
Helper class that converts a chassis velocity (dx and dtheta components) to left and right wheel velo...
Definition DifferentialDriveKinematics.hpp:33
A class that enforces constraints on the mecanum drive kinematics.
Definition MecanumDriveKinematicsConstraint.hpp:21
Helper class that converts a chassis velocity (dx, dy, and dtheta components) into individual wheel v...
Definition MecanumDriveKinematics.hpp:48
A class that enforces constraints on the swerve drive kinematics.
Definition SwerveDriveKinematicsConstraint.hpp:20
Helper class that converts a chassis velocity (dx, dy, and dtheta components) into individual module ...
Definition SwerveDriveKinematics.hpp:59
wpi::units::meters_per_second_t StartVelocity() const
Returns the starting velocity of the trajectory.
Definition TrajectoryConfig.hpp:117
void SetKinematics(SwerveDriveKinematics< NumModules > &kinematics)
Adds a swerve drive kinematics constraint to ensure that no wheel velocity of a swerve drive goes abo...
Definition TrajectoryConfig.hpp:109
TrajectoryConfig(TrajectoryConfig &&)=default
wpi::units::meters_per_second_t EndVelocity() const
Returns the ending velocity of the trajectory.
Definition TrajectoryConfig.hpp:125
const std::vector< std::unique_ptr< TrajectoryConstraint > > & Constraints() const
Returns the user-defined constraints of the trajectory.
Definition TrajectoryConfig.hpp:145
TrajectoryConfig & operator=(TrajectoryConfig &&)=default
void SetReversed(bool reversed)
Sets the reversed flag of the trajectory.
Definition TrajectoryConfig.hpp:70
void SetKinematics(const DifferentialDriveKinematics &kinematics)
Adds a differential drive kinematics constraint to ensure that no wheel velocity of a differential dr...
Definition TrajectoryConfig.hpp:87
TrajectoryConfig(wpi::units::meters_per_second_t maxVelocity, wpi::units::meters_per_second_squared_t maxAcceleration)
Constructs a config object.
Definition TrajectoryConfig.hpp:40
void SetKinematics(MecanumDriveKinematics kinematics)
Adds a mecanum drive kinematics constraint to ensure that no wheel velocity of a mecanum drive goes a...
Definition TrajectoryConfig.hpp:98
bool IsReversed() const
Returns whether the trajectory is reversed or not.
Definition TrajectoryConfig.hpp:154
wpi::units::meters_per_second_t MaxVelocity() const
Returns the maximum velocity of the trajectory.
Definition TrajectoryConfig.hpp:131
void SetStartVelocity(wpi::units::meters_per_second_t startVelocity)
Sets the start velocity of the trajectory.
Definition TrajectoryConfig.hpp:54
void AddConstraint(Constraint constraint)
Adds a user-defined constraint to the trajectory.
Definition TrajectoryConfig.hpp:77
TrajectoryConfig & operator=(const TrajectoryConfig &)=delete
wpi::units::meters_per_second_squared_t MaxAcceleration() const
Returns the maximum acceleration of the trajectory.
Definition TrajectoryConfig.hpp:137
TrajectoryConfig(const TrajectoryConfig &)=delete
void SetEndVelocity(wpi::units::meters_per_second_t endVelocity)
Sets the end velocity of the trajectory.
Definition TrajectoryConfig.hpp:62
Definition LinearSystem.hpp:20