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