WPILibC++ 2025.3.1
Loading...
Searching...
No Matches
QuinticHermiteSpline.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 <wpi/SymbolExports.h>
8#include <wpi/array.h>
9
10#include "frc/EigenCore.h"
11#include "frc/spline/Spline.h"
12
13namespace frc {
14/**
15 * Represents a hermite spline of degree 5.
16 */
18 public:
19 /**
20 * Constructs a quintic hermite spline with the specified control vectors.
21 * Each control vector contains into about the location of the point, its
22 * first derivative, and its second derivative.
23 *
24 * @param xInitialControlVector The control vector for the initial point in
25 * the x dimension.
26 * @param xFinalControlVector The control vector for the final point in
27 * the x dimension.
28 * @param yInitialControlVector The control vector for the initial point in
29 * the y dimension.
30 * @param yFinalControlVector The control vector for the final point in
31 * the y dimension.
32 */
34 wpi::array<double, 3> xFinalControlVector,
35 wpi::array<double, 3> yInitialControlVector,
36 wpi::array<double, 3> yFinalControlVector)
37 : m_initialControlVector{xInitialControlVector, yInitialControlVector},
38 m_finalControlVector{xFinalControlVector, yFinalControlVector} {
39 const auto hermite = MakeHermiteBasis();
40 const auto x =
41 ControlVectorFromArrays(xInitialControlVector, xFinalControlVector);
42 const auto y =
43 ControlVectorFromArrays(yInitialControlVector, yFinalControlVector);
44
45 // Populate first two rows with coefficients.
46 m_coefficients.template block<1, 6>(0, 0) = (hermite * x).transpose();
47 m_coefficients.template block<1, 6>(1, 0) = (hermite * y).transpose();
48
49 // Populate Row 2 and Row 3 with the derivatives of the equations above.
50 // Then populate row 4 and 5 with the second derivatives.
51 for (int i = 0; i < 6; i++) {
52 // Here, we are multiplying by (5 - i) to manually take the derivative.
53 // The power of the term in index 0 is 5, index 1 is 4 and so on. To find
54 // the coefficient of the derivative, we can use the power rule and
55 // multiply the existing coefficient by its power.
56 m_coefficients.template block<2, 1>(2, i) =
57 m_coefficients.template block<2, 1>(0, i) * (5 - i);
58 }
59 for (int i = 0; i < 5; i++) {
60 // Here, we are multiplying by (4 - i) to manually take the derivative.
61 // The power of the term in index 0 is 4, index 1 is 3 and so on. To find
62 // the coefficient of the derivative, we can use the power rule and
63 // multiply the existing coefficient by its power.
64 m_coefficients.template block<2, 1>(4, i) =
65 m_coefficients.template block<2, 1>(2, i) * (4 - i);
66 }
67 }
68
69 /**
70 * Returns the coefficients matrix.
71 * @return The coefficients matrix.
72 */
73 Matrixd<6, 6> Coefficients() const override { return m_coefficients; }
74
75 /**
76 * Returns the initial control vector that created this spline.
77 *
78 * @return The initial control vector that created this spline.
79 */
80 const ControlVector& GetInitialControlVector() const override {
81 return m_initialControlVector;
82 }
83
84 /**
85 * Returns the final control vector that created this spline.
86 *
87 * @return The final control vector that created this spline.
88 */
89 const ControlVector& GetFinalControlVector() const override {
90 return m_finalControlVector;
91 }
92
93 private:
94 Matrixd<6, 6> m_coefficients = Matrixd<6, 6>::Zero();
95
96 ControlVector m_initialControlVector;
97 ControlVector m_finalControlVector;
98
99 /**
100 * Returns the hermite basis matrix for quintic hermite spline interpolation.
101 * @return The hermite basis matrix for quintic hermite spline interpolation.
102 */
103 static constexpr Matrixd<6, 6> MakeHermiteBasis() {
104 // Given P(i), P'(i), P"(i), P(i+1), P'(i+1), P"(i+1), the control vectors,
105 // we want to find the coefficients of the spline
106 // P(t) = a₅t⁵ + a₄t⁴ + a₃t³ + a₂t² + a₁t + a₀.
107 //
108 // P(i) = P(0) = a₀
109 // P'(i) = P'(0) = a₁
110 // P''(i) = P"(0) = 2a₂
111 // P(i+1) = P(1) = a₅ + a₄ + a₃ + a₂ + a₁ + a₀
112 // P'(i+1) = P'(1) = 5a₅ + 4a₄ + 3a₃ + 2a₂ + a₁
113 // P"(i+1) = P"(1) = 20a₅ + 12a₄ + 6a₃ + 2a₂
114 //
115 // [P(i) ] = [ 0 0 0 0 0 1][a₅]
116 // [P'(i) ] = [ 0 0 0 0 1 0][a₄]
117 // [P"(i) ] = [ 0 0 0 2 0 0][a₃]
118 // [P(i+1) ] = [ 1 1 1 1 1 1][a₂]
119 // [P'(i+1)] = [ 5 4 3 2 1 0][a₁]
120 // [P"(i+1)] = [20 12 6 2 0 0][a₀]
121 //
122 // To solve for the coefficients, we can invert the 6x6 matrix and move it
123 // to the other side of the equation.
124 //
125 // [a₅] = [ -6.0 -3.0 -0.5 6.0 -3.0 0.5][P(i) ]
126 // [a₄] = [ 15.0 8.0 1.5 -15.0 7.0 -1.0][P'(i) ]
127 // [a₃] = [-10.0 -6.0 -1.5 10.0 -4.0 0.5][P"(i) ]
128 // [a₂] = [ 0.0 0.0 0.5 0.0 0.0 0.0][P(i+1) ]
129 // [a₁] = [ 0.0 1.0 0.0 0.0 0.0 0.0][P'(i+1)]
130 // [a₀] = [ 1.0 0.0 0.0 0.0 0.0 0.0][P"(i+1)]
131 return Matrixd<6, 6>{{-06.0, -03.0, -00.5, +06.0, -03.0, +00.5},
132 {+15.0, +08.0, +01.5, -15.0, +07.0, -01.0},
133 {-10.0, -06.0, -01.5, +10.0, -04.0, +00.5},
134 {+00.0, +00.0, +00.5, +00.0, +00.0, +00.0},
135 {+00.0, +01.0, +00.0, +00.0, +00.0, +00.0},
136 {+01.0, +00.0, +00.0, +00.0, +00.0, +00.0}};
137 }
138
139 /**
140 * Returns the control vector for each dimension as a matrix from the
141 * user-provided arrays in the constructor.
142 *
143 * @param initialVector The control vector for the initial point.
144 * @param finalVector The control vector for the final point.
145 *
146 * @return The control vector matrix for a dimension.
147 */
148 static constexpr Vectord<6> ControlVectorFromArrays(
149 wpi::array<double, 3> initialVector, wpi::array<double, 3> finalVector) {
150 return Vectord<6>{{initialVector[0]}, {initialVector[1]},
151 {initialVector[2]}, {finalVector[0]},
152 {finalVector[1]}, {finalVector[2]}};
153 }
154};
155} // namespace frc
156
#define WPILIB_DLLEXPORT
Definition SymbolExports.h:36
Represents a hermite spline of degree 5.
Definition QuinticHermiteSpline.h:17
const ControlVector & GetFinalControlVector() const override
Returns the final control vector that created this spline.
Definition QuinticHermiteSpline.h:89
Matrixd< 6, 6 > Coefficients() const override
Returns the coefficients matrix.
Definition QuinticHermiteSpline.h:73
QuinticHermiteSpline(wpi::array< double, 3 > xInitialControlVector, wpi::array< double, 3 > xFinalControlVector, wpi::array< double, 3 > yInitialControlVector, wpi::array< double, 3 > yFinalControlVector)
Constructs a quintic hermite spline with the specified control vectors.
Definition QuinticHermiteSpline.h:33
const ControlVector & GetInitialControlVector() const override
Returns the initial control vector that created this spline.
Definition QuinticHermiteSpline.h:80
Represents a two-dimensional parametric spline that interpolates between two points.
Definition Spline.h:27
This class is a wrapper around std::array that does compile time size checking.
Definition array.h:26
Definition CAN.h:11
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.h:21