WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
TrajectoryGenerator.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 <functional>
8#include <utility>
9#include <vector>
10
15
16namespace wpi::math {
17/**
18 * Helper class used to generate trajectories with various constraints.
19 */
21 public:
22 using PoseWithCurvature = std::pair<Pose2d, wpi::units::curvature_t>;
23
24 /**
25 * Generates a trajectory from the given control vectors and config. This
26 * method uses clamped cubic splines -- a method in which the exterior control
27 * vectors and interior waypoints are provided. The headings are automatically
28 * determined at the interior points to ensure continuous curvature.
29 *
30 * @param initial The initial control vector.
31 * @param interiorWaypoints The interior waypoints.
32 * @param end The ending control vector.
33 * @param config The configuration for the trajectory.
34 * @return The generated trajectory.
35 */
38 const std::vector<Translation2d>& interiorWaypoints,
40
41 /**
42 * Generates a trajectory from the given waypoints and config. This method
43 * uses clamped cubic splines -- a method in which the initial pose, final
44 * pose, and interior waypoints are provided. The headings are automatically
45 * determined at the interior points to ensure continuous curvature.
46 *
47 * @param start The starting pose.
48 * @param interiorWaypoints The interior waypoints.
49 * @param end The ending pose.
50 * @param config The configuration for the trajectory.
51 * @return The generated trajectory.
52 */
54 const Pose2d& start, const std::vector<Translation2d>& interiorWaypoints,
55 const Pose2d& end, const TrajectoryConfig& config);
56
57 /**
58 * Generates a trajectory from the given quintic control vectors and config.
59 * This method uses quintic hermite splines -- therefore, all points must be
60 * represented by control vectors. Continuous curvature is guaranteed in this
61 * method.
62 *
63 * @param controlVectors List of quintic control vectors.
64 * @param config The configuration for the trajectory.
65 * @return The generated trajectory.
66 */
68 std::vector<Spline<5>::ControlVector> controlVectors,
69 const TrajectoryConfig& config);
70
71 /**
72 * Generates a trajectory from the given waypoints and config. This method
73 * uses quintic hermite splines -- therefore, all points must be represented
74 * by Pose2d objects. Continuous curvature is guaranteed in this method.
75 *
76 * @param waypoints List of waypoints..
77 * @param config The configuration for the trajectory.
78 * @return The generated trajectory.
79 */
80 static Trajectory GenerateTrajectory(const std::vector<Pose2d>& waypoints,
81 const TrajectoryConfig& config);
82
83 /**
84 * Generate spline points from a vector of splines by parameterizing the
85 * splines.
86 *
87 * @param splines The splines to parameterize.
88 *
89 * @return The spline points for use in time parameterization of a trajectory.
90 */
91 template <typename Spline>
92 static std::vector<PoseWithCurvature> SplinePointsFromSplines(
93 const std::vector<Spline>& splines) {
94 // Create the vector of spline points.
95 std::vector<PoseWithCurvature> splinePoints;
96
97 // Add the first point to the vector.
98 splinePoints.push_back(splines.front().GetPoint(0.0).value());
99
100 // Iterate through the vector and parameterize each spline, adding the
101 // parameterized points to the final vector.
102 for (auto&& spline : splines) {
103 auto points = SplineParameterizer::Parameterize(spline);
104 // Append the array of poses to the vector. We are removing the first
105 // point because it's a duplicate of the last point from the previous
106 // spline.
107 splinePoints.insert(std::end(splinePoints), std::begin(points) + 1,
108 std::end(points));
109 }
110 return splinePoints;
111 }
112
113 /**
114 * Set error reporting function. By default, it is output to stderr.
115 *
116 * @param func Error reporting function.
117 */
118 static void SetErrorHandler(std::function<void(const char*)> func);
119
120 private:
121 static void ReportError(const char* error);
122
123 static const Trajectory kDoNothingTrajectory;
124 static std::function<void(const char*)> s_errorFunc;
125};
126} // namespace wpi::math
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
Represents a 2D pose containing translational and rotational elements.
Definition Pose2d.hpp:27
static std::vector< PoseWithCurvature > Parameterize(const Spline< Dim > &spline, double t0=0.0, double t1=1.0)
Parametrizes the spline.
Definition SplineParameterizer.hpp:70
Represents the configuration for generating a trajectory.
Definition TrajectoryConfig.hpp:33
Helper class used to generate trajectories with various constraints.
Definition TrajectoryGenerator.hpp:20
static Trajectory GenerateTrajectory(std::vector< Spline< 5 >::ControlVector > controlVectors, const TrajectoryConfig &config)
Generates a trajectory from the given quintic control vectors and config.
static std::vector< PoseWithCurvature > SplinePointsFromSplines(const std::vector< Spline > &splines)
Generate spline points from a vector of splines by parameterizing the splines.
Definition TrajectoryGenerator.hpp:92
static Trajectory GenerateTrajectory(const std::vector< Pose2d > &waypoints, const TrajectoryConfig &config)
Generates a trajectory from the given waypoints and config.
std::pair< Pose2d, wpi::units::curvature_t > PoseWithCurvature
Definition TrajectoryGenerator.hpp:22
static Trajectory GenerateTrajectory(Spline< 3 >::ControlVector initial, const std::vector< Translation2d > &interiorWaypoints, Spline< 3 >::ControlVector end, const TrajectoryConfig &config)
Generates a trajectory from the given control vectors and config.
static void SetErrorHandler(std::function< void(const char *)> func)
Set error reporting function.
static Trajectory GenerateTrajectory(const Pose2d &start, const std::vector< Translation2d > &interiorWaypoints, const Pose2d &end, const TrajectoryConfig &config)
Generates a trajectory from the given waypoints and config.
Represents a time-parameterized trajectory.
Definition Trajectory.hpp:28
Definition LinearSystem.hpp:20
Represents a control vector for a spline.
Definition Spline.hpp:48