WPILibC++ 2024.3.2
TrajectoryParameterizer.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/*
6 * MIT License
7 *
8 * Copyright (c) 2018 Team 254
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29#pragma once
30
31#include <memory>
32#include <utility>
33#include <vector>
34
35#include <wpi/SymbolExports.h>
36
39
40namespace frc {
41/**
42 * Class used to parameterize a trajectory by time.
43 */
45 public:
46 using PoseWithCurvature = std::pair<Pose2d, units::curvature_t>;
47
48 /**
49 * Parameterize the trajectory by time. This is where the velocity profile is
50 * generated.
51 *
52 * The derivation of the algorithm used can be found here:
53 * <http://www2.informatik.uni-freiburg.de/~lau/students/Sprunk2008.pdf>
54 *
55 * @param points Reference to the spline points.
56 * @param constraints A vector of various velocity and acceleration
57 * constraints.
58 * @param startVelocity The start velocity for the trajectory.
59 * @param endVelocity The end velocity for the trajectory.
60 * @param maxVelocity The max velocity for the trajectory.
61 * @param maxAcceleration The max acceleration for the trajectory.
62 * @param reversed Whether the robot should move backwards. Note that the
63 * robot will still move from a -> b -> ... -> z as defined in the waypoints.
64 *
65 * @return The trajectory.
66 */
68 const std::vector<PoseWithCurvature>& points,
69 const std::vector<std::unique_ptr<TrajectoryConstraint>>& constraints,
70 units::meters_per_second_t startVelocity,
71 units::meters_per_second_t endVelocity,
72 units::meters_per_second_t maxVelocity,
73 units::meters_per_second_squared_t maxAcceleration, bool reversed);
74
75 private:
76 constexpr static double kEpsilon = 1E-6;
77
78 /**
79 * Represents a constrained state that is used when time parameterizing a
80 * trajectory. Each state has the pose, curvature, distance from the start of
81 * the trajectory, max velocity, min acceleration and max acceleration.
82 */
83 struct ConstrainedState {
85 units::meter_t distance = 0_m;
86 units::meters_per_second_t maxVelocity = 0_mps;
87 units::meters_per_second_squared_t minAcceleration = 0_mps_sq;
88 units::meters_per_second_squared_t maxAcceleration = 0_mps_sq;
89 };
90
91 /**
92 * Enforces acceleration limits as defined by the constraints. This function
93 * is used when time parameterizing a trajectory.
94 *
95 * @param reverse Whether the robot is traveling backwards.
96 * @param constraints A vector of the user-defined velocity and acceleration
97 * constraints.
98 * @param state Pointer to the constrained state that we are operating on.
99 * This is mutated in place.
100 */
101 static void EnforceAccelerationLimits(
102 bool reverse,
103 const std::vector<std::unique_ptr<TrajectoryConstraint>>& constraints,
104 ConstrainedState* state);
105};
106} // namespace frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
Represents a 2D pose containing translational and rotational elements.
Definition: Pose2d.h:23
Represents a time-parameterized trajectory.
Definition: Trajectory.h:25
Class used to parameterize a trajectory by time.
Definition: TrajectoryParameterizer.h:44
static Trajectory TimeParameterizeTrajectory(const std::vector< PoseWithCurvature > &points, const std::vector< std::unique_ptr< TrajectoryConstraint > > &constraints, units::meters_per_second_t startVelocity, units::meters_per_second_t endVelocity, units::meters_per_second_t maxVelocity, units::meters_per_second_squared_t maxAcceleration, bool reversed)
Parameterize the trajectory by time.
std::pair< Pose2d, units::curvature_t > PoseWithCurvature
Definition: TrajectoryParameterizer.h:46
state
Definition: core.h:2271
Definition: AprilTagPoseEstimator.h:15