WPILibC++ 2024.3.2
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
38 /**
39 * Returns the coefficients matrix.
40 * @return The coefficients matrix.
41 */
42 Matrixd<6, 6> Coefficients() const override { return m_coefficients; }
43
44 /**
45 * Returns the initial control vector that created this spline.
46 *
47 * @return The initial control vector that created this spline.
48 */
49 const ControlVector& GetInitialControlVector() const override {
50 return m_initialControlVector;
51 }
52
53 /**
54 * Returns the final control vector that created this spline.
55 *
56 * @return The final control vector that created this spline.
57 */
58 const ControlVector& GetFinalControlVector() const override {
59 return m_finalControlVector;
60 }
61
62 private:
63 Matrixd<6, 6> m_coefficients = Matrixd<6, 6>::Zero();
64
65 ControlVector m_initialControlVector;
66 ControlVector m_finalControlVector;
67
68 /**
69 * Returns the hermite basis matrix for quintic hermite spline interpolation.
70 * @return The hermite basis matrix for quintic hermite spline interpolation.
71 */
72 static Matrixd<6, 6> MakeHermiteBasis() {
73 // Given P(i), P'(i), P"(i), P(i+1), P'(i+1), P"(i+1), the control vectors,
74 // we want to find the coefficients of the spline
75 // P(t) = a₅t⁵ + a₄t⁴ + a₃t³ + a₂t² + a₁t + a₀.
76 //
77 // P(i) = P(0) = a₀
78 // P'(i) = P'(0) = a₁
79 // P''(i) = P"(0) = 2a₂
80 // P(i+1) = P(1) = a₅ + a₄ + a₃ + a₂ + a₁ + a₀
81 // P'(i+1) = P'(1) = 5a₅ + 4a₄ + 3a₃ + 2a₂ + a₁
82 // P"(i+1) = P"(1) = 20a₅ + 12a₄ + 6a₃ + 2a₂
83 //
84 // [P(i) ] = [ 0 0 0 0 0 1][a₅]
85 // [P'(i) ] = [ 0 0 0 0 1 0][a₄]
86 // [P"(i) ] = [ 0 0 0 2 0 0][a₃]
87 // [P(i+1) ] = [ 1 1 1 1 1 1][a₂]
88 // [P'(i+1)] = [ 5 4 3 2 1 0][a₁]
89 // [P"(i+1)] = [20 12 6 2 0 0][a₀]
90 //
91 // To solve for the coefficients, we can invert the 6x6 matrix and move it
92 // to the other side of the equation.
93 //
94 // [a₅] = [ -6.0 -3.0 -0.5 6.0 -3.0 0.5][P(i) ]
95 // [a₄] = [ 15.0 8.0 1.5 -15.0 7.0 -1.0][P'(i) ]
96 // [a₃] = [-10.0 -6.0 -1.5 10.0 -4.0 0.5][P"(i) ]
97 // [a₂] = [ 0.0 0.0 0.5 0.0 0.0 0.0][P(i+1) ]
98 // [a₁] = [ 0.0 1.0 0.0 0.0 0.0 0.0][P'(i+1)]
99 // [a₀] = [ 1.0 0.0 0.0 0.0 0.0 0.0][P"(i+1)]
100
101 static const Matrixd<6, 6> basis{
102 {-06.0, -03.0, -00.5, +06.0, -03.0, +00.5},
103 {+15.0, +08.0, +01.5, -15.0, +07.0, -01.0},
104 {-10.0, -06.0, -01.5, +10.0, -04.0, +00.5},
105 {+00.0, +00.0, +00.5, +00.0, +00.0, +00.0},
106 {+00.0, +01.0, +00.0, +00.0, +00.0, +00.0},
107 {+01.0, +00.0, +00.0, +00.0, +00.0, +00.0}};
108 return basis;
109 }
110
111 /**
112 * Returns the control vector for each dimension as a matrix from the
113 * user-provided arrays in the constructor.
114 *
115 * @param initialVector The control vector for the initial point.
116 * @param finalVector The control vector for the final point.
117 *
118 * @return The control vector matrix for a dimension.
119 */
120 static Vectord<6> ControlVectorFromArrays(wpi::array<double, 3> initialVector,
121 wpi::array<double, 3> finalVector) {
122 return Vectord<6>{initialVector[0], initialVector[1], initialVector[2],
123 finalVector[0], finalVector[1], finalVector[2]};
124 }
125};
126} // namespace frc
#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:58
Matrixd< 6, 6 > Coefficients() const override
Returns the coefficients matrix.
Definition: QuinticHermiteSpline.h:42
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.
const ControlVector & GetInitialControlVector() const override
Returns the initial control vector that created this spline.
Definition: QuinticHermiteSpline.h:49
Represents a two-dimensional parametric spline that interpolates between two points.
Definition: Spline.h:25
Definition: AprilTagPoseEstimator.h:15
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition: EigenCore.h:21