WPILibC++ 2024.3.2
LinearPlantInversionFeedforward.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 <array>
8#include <functional>
9
10#include <Eigen/QR>
11
12#include "frc/EigenCore.h"
15#include "units/time.h"
16
17namespace frc {
18
19/**
20 * Constructs a plant inversion model-based feedforward from a LinearSystem.
21 *
22 * The feedforward is calculated as <strong> u_ff = B<sup>+</sup> (r_k+1 - A
23 * r_k) </strong>, where <strong> B<sup>+</sup> </strong> is the pseudoinverse
24 * of B.
25 *
26 * For more on the underlying math, read
27 * https://file.tavsys.net/control/controls-engineering-in-frc.pdf.
28 *
29 * @tparam States Number of states.
30 * @tparam Inputs Number of inputs.
31 */
32template <int States, int Inputs>
34 public:
37
38 /**
39 * Constructs a feedforward with the given plant.
40 *
41 * @tparam Outputs Number of outputs.
42 * @param plant The plant being controlled.
43 * @param dt Discretization timestep.
44 */
45 template <int Outputs>
47 const LinearSystem<States, Inputs, Outputs>& plant, units::second_t dt)
48 : LinearPlantInversionFeedforward(plant.A(), plant.B(), dt) {}
49
50 /**
51 * Constructs a feedforward with the given coefficients.
52 *
53 * @param A Continuous system matrix of the plant being controlled.
54 * @param B Continuous input matrix of the plant being controlled.
55 * @param dt Discretization timestep.
56 */
59 units::second_t dt)
60 : m_dt(dt) {
61 DiscretizeAB<States, Inputs>(A, B, dt, &m_A, &m_B);
62 Reset();
63 }
64
65 /**
66 * Returns the previously calculated feedforward as an input vector.
67 *
68 * @return The calculated feedforward.
69 */
70 const InputVector& Uff() const { return m_uff; }
71
72 /**
73 * Returns an element of the previously calculated feedforward.
74 *
75 * @param i Row of uff.
76 *
77 * @return The row of the calculated feedforward.
78 */
79 double Uff(int i) const { return m_uff(i); }
80
81 /**
82 * Returns the current reference vector r.
83 *
84 * @return The current reference vector.
85 */
86 const StateVector& R() const { return m_r; }
87
88 /**
89 * Returns an element of the reference vector r.
90 *
91 * @param i Row of r.
92 *
93 * @return The row of the current reference vector.
94 */
95 double R(int i) const { return m_r(i); }
96
97 /**
98 * Resets the feedforward with a specified initial state vector.
99 *
100 * @param initialState The initial state vector.
101 */
102 void Reset(const StateVector& initialState) {
103 m_r = initialState;
104 m_uff.setZero();
105 }
106
107 /**
108 * Resets the feedforward with a zero initial state vector.
109 */
110 void Reset() {
111 m_r.setZero();
112 m_uff.setZero();
113 }
114
115 /**
116 * Calculate the feedforward with only the desired
117 * future reference. This uses the internally stored "current"
118 * reference.
119 *
120 * If this method is used the initial state of the system is the one set using
121 * Reset(const StateVector&). If the initial state is not
122 * set it defaults to a zero vector.
123 *
124 * @param nextR The reference state of the future timestep (k + dt).
125 *
126 * @return The calculated feedforward.
127 */
129 return Calculate(m_r, nextR);
130 }
131
132 /**
133 * Calculate the feedforward with current and future reference vectors.
134 *
135 * @param r The reference state of the current timestep (k).
136 * @param nextR The reference state of the future timestep (k + dt).
137 *
138 * @return The calculated feedforward.
139 */
141 // rₖ₊₁ = Arₖ + Buₖ
142 // Buₖ = rₖ₊₁ − Arₖ
143 // uₖ = B⁺(rₖ₊₁ − Arₖ)
144 m_uff = m_B.householderQr().solve(nextR - (m_A * r));
145 m_r = nextR;
146 return m_uff;
147 }
148
149 private:
152
153 units::second_t m_dt;
154
155 // Current reference
156 StateVector m_r;
157
158 // Computed feedforward
159 InputVector m_uff;
160};
161
162} // namespace frc
Constructs a plant inversion model-based feedforward from a LinearSystem.
Definition: LinearPlantInversionFeedforward.h:33
double R(int i) const
Returns an element of the reference vector r.
Definition: LinearPlantInversionFeedforward.h:95
InputVector Calculate(const StateVector &r, const StateVector &nextR)
Calculate the feedforward with current and future reference vectors.
Definition: LinearPlantInversionFeedforward.h:140
Vectord< Inputs > InputVector
Definition: LinearPlantInversionFeedforward.h:36
const InputVector & Uff() const
Returns the previously calculated feedforward as an input vector.
Definition: LinearPlantInversionFeedforward.h:70
const StateVector & R() const
Returns the current reference vector r.
Definition: LinearPlantInversionFeedforward.h:86
Vectord< States > StateVector
Definition: LinearPlantInversionFeedforward.h:35
void Reset(const StateVector &initialState)
Resets the feedforward with a specified initial state vector.
Definition: LinearPlantInversionFeedforward.h:102
InputVector Calculate(const StateVector &nextR)
Calculate the feedforward with only the desired future reference.
Definition: LinearPlantInversionFeedforward.h:128
void Reset()
Resets the feedforward with a zero initial state vector.
Definition: LinearPlantInversionFeedforward.h:110
LinearPlantInversionFeedforward(const LinearSystem< States, Inputs, Outputs > &plant, units::second_t dt)
Constructs a feedforward with the given plant.
Definition: LinearPlantInversionFeedforward.h:46
double Uff(int i) const
Returns an element of the previously calculated feedforward.
Definition: LinearPlantInversionFeedforward.h:79
LinearPlantInversionFeedforward(const Matrixd< States, States > &A, const Matrixd< States, Inputs > &B, units::second_t dt)
Constructs a feedforward with the given coefficients.
Definition: LinearPlantInversionFeedforward.h:57
A plant defined using state-space notation.
Definition: LinearSystem.h:31
Definition: AprilTagPoseEstimator.h:15
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition: EigenCore.h:21
Eigen::Vector< double, Size > Vectord
Definition: EigenCore.h:12