WPILibC++ 2024.3.2
SplineHelper.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 <utility>
8#include <vector>
9
10#include <wpi/SymbolExports.h>
11#include <wpi/array.h>
12
15
16namespace frc {
17/**
18 * Helper class that is used to generate cubic and quintic splines from user
19 * provided waypoints.
20 */
22 public:
23 /**
24 * Returns 2 cubic control vectors from a set of exterior waypoints and
25 * interior translations.
26 *
27 * @param start The starting pose.
28 * @param interiorWaypoints The interior waypoints.
29 * @param end The ending pose.
30 * @return 2 cubic control vectors.
31 */
34 const Pose2d& start, const std::vector<Translation2d>& interiorWaypoints,
35 const Pose2d& end);
36
37 /**
38 * Returns quintic splines from a set of waypoints.
39 *
40 * @param waypoints The waypoints
41 * @return List of quintic splines.
42 */
43 static std::vector<QuinticHermiteSpline> QuinticSplinesFromWaypoints(
44 const std::vector<Pose2d>& waypoints);
45
46 /**
47 * Returns a set of cubic splines corresponding to the provided control
48 * vectors. The user is free to set the direction of the start and end
49 * point. The directions for the middle waypoints are determined
50 * automatically to ensure continuous curvature throughout the path.
51 *
52 * The derivation for the algorithm used can be found here:
53 * <https://www.uio.no/studier/emner/matnat/ifi/nedlagte-emner/INF-MAT4350/h08/undervisningsmateriale/chap7alecture.pdf>
54 *
55 * @param start The starting control vector.
56 * @param waypoints The middle waypoints. This can be left blank if you
57 * only wish to create a path with two waypoints.
58 * @param end The ending control vector.
59 *
60 * @return A vector of cubic hermite splines that interpolate through the
61 * provided waypoints.
62 */
63 static std::vector<CubicHermiteSpline> CubicSplinesFromControlVectors(
64 const Spline<3>::ControlVector& start,
65 std::vector<Translation2d> waypoints,
66 const Spline<3>::ControlVector& end);
67
68 /**
69 * Returns a set of quintic splines corresponding to the provided control
70 * vectors. The user is free to set the direction of all waypoints. Continuous
71 * curvature is guaranteed throughout the path.
72 *
73 * @param controlVectors The control vectors.
74 * @return A vector of quintic hermite splines that interpolate through the
75 * provided waypoints.
76 */
77 static std::vector<QuinticHermiteSpline> QuinticSplinesFromControlVectors(
78 const std::vector<Spline<5>::ControlVector>& controlVectors);
79
80 /**
81 * Optimizes the curvature of 2 or more quintic splines at knot points.
82 * Overall, this reduces the integral of the absolute value of the second
83 * derivative across the set of splines.
84 *
85 * @param splines A vector of un-optimized quintic splines.
86 * @return A vector of optimized quintic splines.
87 */
88 static std::vector<QuinticHermiteSpline> OptimizeCurvature(
89 const std::vector<QuinticHermiteSpline>& splines);
90
91 private:
92 static Spline<3>::ControlVector CubicControlVector(double scalar,
93 const Pose2d& point) {
94 return {{point.X().value(), scalar * point.Rotation().Cos()},
95 {point.Y().value(), scalar * point.Rotation().Sin()}};
96 }
97
98 static Spline<5>::ControlVector QuinticControlVector(double scalar,
99 const Pose2d& point) {
100 return {{point.X().value(), scalar * point.Rotation().Cos(), 0.0},
101 {point.Y().value(), scalar * point.Rotation().Sin(), 0.0}};
102 }
103
104 /**
105 * Thomas algorithm for solving tridiagonal systems Af = d.
106 *
107 * @param a the values of A above the diagonal
108 * @param b the values of A on the diagonal
109 * @param c the values of A below the diagonal
110 * @param d the vector on the rhs
111 * @param solutionVector the unknown (solution) vector, modified in-place
112 */
113 static void ThomasAlgorithm(const std::vector<double>& a,
114 const std::vector<double>& b,
115 const std::vector<double>& c,
116 const std::vector<double>& d,
117 std::vector<double>* solutionVector);
118};
119} // namespace frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
Represents a 2D pose containing translational and rotational elements.
Definition: Pose2d.h:23
constexpr const Rotation2d & Rotation() const
Returns the underlying rotation.
Definition: Pose2d.h:103
constexpr units::meter_t Y() const
Returns the Y component of the pose's translation.
Definition: Pose2d.h:96
constexpr units::meter_t X() const
Returns the X component of the pose's translation.
Definition: Pose2d.h:89
constexpr double Cos() const
Returns the cosine of the rotation.
Definition: Rotation2d.h:142
constexpr double Sin() const
Returns the sine of the rotation.
Definition: Rotation2d.h:149
Helper class that is used to generate cubic and quintic splines from user provided waypoints.
Definition: SplineHelper.h:21
static std::vector< CubicHermiteSpline > CubicSplinesFromControlVectors(const Spline< 3 >::ControlVector &start, std::vector< Translation2d > waypoints, const Spline< 3 >::ControlVector &end)
Returns a set of cubic splines corresponding to the provided control vectors.
static std::vector< QuinticHermiteSpline > OptimizeCurvature(const std::vector< QuinticHermiteSpline > &splines)
Optimizes the curvature of 2 or more quintic splines at knot points.
static std::vector< QuinticHermiteSpline > QuinticSplinesFromControlVectors(const std::vector< Spline< 5 >::ControlVector > &controlVectors)
Returns a set of quintic splines corresponding to the provided control vectors.
static wpi::array< Spline< 3 >::ControlVector, 2 > CubicControlVectorsFromWaypoints(const Pose2d &start, const std::vector< Translation2d > &interiorWaypoints, const Pose2d &end)
Returns 2 cubic control vectors from a set of exterior waypoints and interior translations.
static std::vector< QuinticHermiteSpline > QuinticSplinesFromWaypoints(const std::vector< Pose2d > &waypoints)
Returns quintic splines from a set of waypoints.
Represents a two-dimensional parametric spline that interpolates between two points.
Definition: Spline.h:25
This class is a wrapper around std::array that does compile time size checking.
Definition: array.h:26
Definition: AprilTagPoseEstimator.h:15
class WPILIB_DLLEXPORT Pose2d
Definition: Transform2d.h:13
static constexpr const velocity::meters_per_second_t c(299792458.0)
Speed of light in vacuum.
unit< std::ratio< 1 >, units::category::scalar_unit > scalar
Definition: base.h:2510
b
Definition: data.h:44