WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
TrajectoryParameterizer.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/*
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
38
39namespace wpi::math {
40/**
41 * Class used to parameterize a trajectory by time.
42 */
44 public:
45 using PoseWithCurvature = std::pair<Pose2d, wpi::units::curvature_t>;
46
47 /**
48 * Parameterize the trajectory by time. This is where the velocity profile is
49 * generated.
50 *
51 * The derivation of the algorithm used can be found here:
52 * <http://www2.informatik.uni-freiburg.de/~lau/students/Sprunk2008.pdf>
53 *
54 * @param points Reference to the spline points.
55 * @param constraints A vector of various velocity and acceleration
56 * constraints.
57 * @param startVelocity The start velocity for the trajectory.
58 * @param endVelocity The end velocity for the trajectory.
59 * @param maxVelocity The max velocity for the trajectory.
60 * @param maxAcceleration The max acceleration for the trajectory.
61 * @param reversed Whether the robot should move backwards. Note that the
62 * robot will still move from a -> b -> ... -> z as defined in the waypoints.
63 *
64 * @return The trajectory.
65 */
67 const std::vector<PoseWithCurvature>& points,
68 const std::vector<std::unique_ptr<TrajectoryConstraint>>& constraints,
69 wpi::units::meters_per_second_t startVelocity,
70 wpi::units::meters_per_second_t endVelocity,
71 wpi::units::meters_per_second_t maxVelocity,
72 wpi::units::meters_per_second_squared_t maxAcceleration, bool reversed);
73
74 private:
75 constexpr static double kEpsilon = 1E-6;
76
77 /**
78 * Represents a constrained state that is used when time parameterizing a
79 * trajectory. Each state has the pose, curvature, distance from the start of
80 * the trajectory, max velocity, min acceleration and max acceleration.
81 */
82 struct ConstrainedState {
83 PoseWithCurvature pose = {Pose2d{}, wpi::units::curvature_t{0.0}};
84 wpi::units::meter_t distance = 0_m;
85 wpi::units::meters_per_second_t maxVelocity = 0_mps;
86 wpi::units::meters_per_second_squared_t minAcceleration = 0_mps_sq;
87 wpi::units::meters_per_second_squared_t maxAcceleration = 0_mps_sq;
88 };
89
90 /**
91 * Enforces acceleration limits as defined by the constraints. This function
92 * is used when time parameterizing a trajectory.
93 *
94 * @param reverse Whether the robot is traveling backwards.
95 * @param constraints A vector of the user-defined velocity and acceleration
96 * constraints.
97 * @param state Pointer to the constrained state that we are operating on.
98 * This is mutated in place.
99 */
100 static void EnforceAccelerationLimits(
101 bool reverse,
102 const std::vector<std::unique_ptr<TrajectoryConstraint>>& constraints,
103 ConstrainedState* state);
104};
105} // namespace wpi::math
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
Represents a 2D pose containing translational and rotational elements.
Definition Pose2d.hpp:27
Represents a time-parameterized trajectory.
Definition Trajectory.hpp:28
Class used to parameterize a trajectory by time.
Definition TrajectoryParameterizer.hpp:43
std::pair< Pose2d, wpi::units::curvature_t > PoseWithCurvature
Definition TrajectoryParameterizer.hpp:45
static Trajectory TimeParameterizeTrajectory(const std::vector< PoseWithCurvature > &points, const std::vector< std::unique_ptr< TrajectoryConstraint > > &constraints, wpi::units::meters_per_second_t startVelocity, wpi::units::meters_per_second_t endVelocity, wpi::units::meters_per_second_t maxVelocity, wpi::units::meters_per_second_squared_t maxAcceleration, bool reversed)
Parameterize the trajectory by time.
@ reverse
Definition color.h:185
Definition LinearSystem.hpp:20