WPILibC++ 2023.4.3
TrajectoryConfig.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 <memory>
8#include <utility>
9#include <vector>
10
11#include <wpi/SymbolExports.h>
12
20#include "units/acceleration.h"
21#include "units/velocity.h"
22
23namespace frc {
24/**
25 * Represents the configuration for generating a trajectory. This class stores
26 * the start velocity, end velocity, max velocity, max acceleration, custom
27 * constraints, and the reversed flag.
28 *
29 * The class must be constructed with a max velocity and max acceleration.
30 * The other parameters (start velocity, end velocity, constraints, reversed)
31 * have been defaulted to reasonable values (0, 0, {}, false). These values can
32 * be changed via the SetXXX methods.
33 */
35 public:
36 /**
37 * Constructs a config object.
38 * @param maxVelocity The max velocity of the trajectory.
39 * @param maxAcceleration The max acceleration of the trajectory.
40 */
41 TrajectoryConfig(units::meters_per_second_t maxVelocity,
42 units::meters_per_second_squared_t maxAcceleration)
43 : m_maxVelocity(maxVelocity), m_maxAcceleration(maxAcceleration) {}
44
47
50
51 /**
52 * Sets the start velocity of the trajectory.
53 * @param startVelocity The start velocity of the trajectory.
54 */
55 void SetStartVelocity(units::meters_per_second_t startVelocity) {
56 m_startVelocity = startVelocity;
57 }
58
59 /**
60 * Sets the end velocity of the trajectory.
61 * @param endVelocity The end velocity of the trajectory.
62 */
63 void SetEndVelocity(units::meters_per_second_t endVelocity) {
64 m_endVelocity = endVelocity;
65 }
66
67 /**
68 * Sets the reversed flag of the trajectory.
69 * @param reversed Whether the trajectory should be reversed or not.
70 */
71 void SetReversed(bool reversed) { m_reversed = reversed; }
72
73 /**
74 * Adds a user-defined constraint to the trajectory.
75 * @param constraint The user-defined constraint.
76 */
77 template <typename Constraint, typename = std::enable_if_t<std::is_base_of_v<
78 TrajectoryConstraint, Constraint>>>
79 void AddConstraint(Constraint constraint) {
80 m_constraints.emplace_back(std::make_unique<Constraint>(constraint));
81 }
82
83 /**
84 * Adds a differential drive kinematics constraint to ensure that
85 * no wheel velocity of a differential drive goes above the max velocity.
86 *
87 * @param kinematics The differential drive kinematics.
88 */
90 AddConstraint(
91 DifferentialDriveKinematicsConstraint(kinematics, m_maxVelocity));
92 }
93
94 /**
95 * Adds a mecanum drive kinematics constraint to ensure that
96 * no wheel velocity of a mecanum drive goes above the max velocity.
97 *
98 * @param kinematics The mecanum drive kinematics.
99 */
101 AddConstraint(MecanumDriveKinematicsConstraint(kinematics, m_maxVelocity));
102 }
103
104 /**
105 * Adds a swerve drive kinematics constraint to ensure that
106 * no wheel velocity of a swerve drive goes above the max velocity.
107 *
108 * @param kinematics The swerve drive kinematics.
109 */
110 template <size_t NumModules>
112 AddConstraint(SwerveDriveKinematicsConstraint(kinematics, m_maxVelocity));
113 }
114
115 /**
116 * Returns the starting velocity of the trajectory.
117 * @return The starting velocity of the trajectory.
118 */
119 units::meters_per_second_t StartVelocity() const { return m_startVelocity; }
120
121 /**
122 * Returns the ending velocity of the trajectory.
123 * @return The ending velocity of the trajectory.
124 */
125 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 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 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 units::meters_per_second_t m_startVelocity = 0_mps;
158 units::meters_per_second_t m_endVelocity = 0_mps;
159 units::meters_per_second_t m_maxVelocity;
160 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 frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
A class that enforces constraints on the differential drive kinematics.
Definition: DifferentialDriveKinematicsConstraint.h:21
Helper class that converts a chassis velocity (dx and dtheta components) to left and right wheel velo...
Definition: DifferentialDriveKinematics.h:25
A class that enforces constraints on the mecanum drive kinematics.
Definition: MecanumDriveKinematicsConstraint.h:23
Helper class that converts a chassis velocity (dx, dy, and dtheta components) into individual wheel s...
Definition: MecanumDriveKinematics.h:42
A class that enforces constraints on the swerve drive kinematics.
Definition: SwerveDriveKinematicsConstraint.h:21
Helper class that converts a chassis velocity (dx, dy, and dtheta components) into individual module ...
Definition: SwerveDriveKinematics.h:47
Represents the configuration for generating a trajectory.
Definition: TrajectoryConfig.h:34
units::meters_per_second_t StartVelocity() const
Returns the starting velocity of the trajectory.
Definition: TrajectoryConfig.h:119
units::meters_per_second_t MaxVelocity() const
Returns the maximum velocity of the trajectory.
Definition: TrajectoryConfig.h:131
void SetStartVelocity(units::meters_per_second_t startVelocity)
Sets the start velocity of the trajectory.
Definition: TrajectoryConfig.h:55
units::meters_per_second_t EndVelocity() const
Returns the ending velocity of the trajectory.
Definition: TrajectoryConfig.h:125
void SetKinematics(const DifferentialDriveKinematics &kinematics)
Adds a differential drive kinematics constraint to ensure that no wheel velocity of a differential dr...
Definition: TrajectoryConfig.h:89
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.h:111
bool IsReversed() const
Returns whether the trajectory is reversed or not.
Definition: TrajectoryConfig.h:154
TrajectoryConfig(units::meters_per_second_t maxVelocity, units::meters_per_second_squared_t maxAcceleration)
Constructs a config object.
Definition: TrajectoryConfig.h:41
units::meters_per_second_squared_t MaxAcceleration() const
Returns the maximum acceleration of the trajectory.
Definition: TrajectoryConfig.h:137
void SetKinematics(MecanumDriveKinematics kinematics)
Adds a mecanum drive kinematics constraint to ensure that no wheel velocity of a mecanum drive goes a...
Definition: TrajectoryConfig.h:100
TrajectoryConfig(const TrajectoryConfig &)=delete
void SetReversed(bool reversed)
Sets the reversed flag of the trajectory.
Definition: TrajectoryConfig.h:71
void AddConstraint(Constraint constraint)
Adds a user-defined constraint to the trajectory.
Definition: TrajectoryConfig.h:79
TrajectoryConfig(TrajectoryConfig &&)=default
TrajectoryConfig & operator=(TrajectoryConfig &&)=default
TrajectoryConfig & operator=(const TrajectoryConfig &)=delete
void SetEndVelocity(units::meters_per_second_t endVelocity)
Sets the end velocity of the trajectory.
Definition: TrajectoryConfig.h:63
const std::vector< std::unique_ptr< TrajectoryConstraint > > & Constraints() const
Returns the user-defined constraints of the trajectory.
Definition: TrajectoryConfig.h:145
An interface for defining user-defined velocity and acceleration constraints while generating traject...
Definition: TrajectoryConstraint.h:22
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:298
Definition: AprilTagFieldLayout.h:22