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