WPILibC++ 2024.3.2
CubicHermiteSpline.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 3.
16 */
18 public:
19 /**
20 * Constructs a cubic hermite spline with the specified control vectors. Each
21 * control vector contains info about the location of the point and its first
22 * 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, 2> xFinalControlVector,
35 wpi::array<double, 2> yInitialControlVector,
36 wpi::array<double, 2> yFinalControlVector);
37
38 /**
39 * Returns the coefficients matrix.
40 * @return The coefficients matrix.
41 */
42 Matrixd<6, 3 + 1> 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, 4> m_coefficients = Matrixd<6, 4>::Zero();
64
65 ControlVector m_initialControlVector;
66 ControlVector m_finalControlVector;
67
68 /**
69 * Returns the hermite basis matrix for cubic hermite spline interpolation.
70 * @return The hermite basis matrix for cubic hermite spline interpolation.
71 */
72 static Eigen::Matrix4d MakeHermiteBasis() {
73 // Given P(i), P'(i), P(i+1), P'(i+1), the control vectors, we want to find
74 // the coefficients of the spline P(t) = a₃t³ + a₂t² + a₁t + a₀.
75 //
76 // P(i) = P(0) = a₀
77 // P'(i) = P'(0) = a₁
78 // P(i+1) = P(1) = a₃ + a₂ + a₁ + a₀
79 // P'(i+1) = P'(1) = 3a₃ + 2a₂ + a₁
80 //
81 // [P(i) ] = [0 0 0 1][a₃]
82 // [P'(i) ] = [0 0 1 0][a₂]
83 // [P(i+1) ] = [1 1 1 1][a₁]
84 // [P'(i+1)] = [3 2 1 0][a₀]
85 //
86 // To solve for the coefficients, we can invert the 4x4 matrix and move it
87 // to the other side of the equation.
88 //
89 // [a₃] = [ 2 1 -2 1][P(i) ]
90 // [a₂] = [-3 -2 3 -1][P'(i) ]
91 // [a₁] = [ 0 1 0 0][P(i+1) ]
92 // [a₀] = [ 1 0 0 0][P'(i+1)]
93
94 static const Eigen::Matrix4d basis{{+2.0, +1.0, -2.0, +1.0},
95 {-3.0, -2.0, +3.0, -1.0},
96 {+0.0, +1.0, +0.0, +0.0},
97 {+1.0, +0.0, +0.0, +0.0}};
98 return basis;
99 }
100
101 /**
102 * Returns the control vector for each dimension as a matrix from the
103 * user-provided arrays in the constructor.
104 *
105 * @param initialVector The control vector for the initial point.
106 * @param finalVector The control vector for the final point.
107 *
108 * @return The control vector matrix for a dimension.
109 */
110 static Eigen::Vector4d ControlVectorFromArrays(
111 wpi::array<double, 2> initialVector, wpi::array<double, 2> finalVector) {
112 return Eigen::Vector4d{initialVector[0], initialVector[1], finalVector[0],
113 finalVector[1]};
114 }
115};
116} // namespace frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
Represents a hermite spline of degree 3.
Definition: CubicHermiteSpline.h:17
const ControlVector & GetInitialControlVector() const override
Returns the initial control vector that created this spline.
Definition: CubicHermiteSpline.h:49
Matrixd< 6, 3+1 > Coefficients() const override
Returns the coefficients matrix.
Definition: CubicHermiteSpline.h:42
CubicHermiteSpline(wpi::array< double, 2 > xInitialControlVector, wpi::array< double, 2 > xFinalControlVector, wpi::array< double, 2 > yInitialControlVector, wpi::array< double, 2 > yFinalControlVector)
Constructs a cubic hermite spline with the specified control vectors.
const ControlVector & GetFinalControlVector() const override
Returns the final control vector that created this spline.
Definition: CubicHermiteSpline.h:58
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
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition: EigenCore.h:21