WPILibC++ 2024.3.2
ImplicitModelFollower.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
8
9#include <Eigen/QR>
10
11#include "frc/EigenCore.h"
12#include "units/time.h"
13
14namespace frc {
15
16/**
17 * Contains the controller coefficients and logic for an implicit model
18 * follower.
19 *
20 * Implicit model following lets us design a feedback controller that erases the
21 * dynamics of our system and makes it behave like some other system. This can
22 * be used to make a drivetrain more controllable during teleop driving by
23 * making it behave like a slower or more benign drivetrain.
24 *
25 * For more on the underlying math, read appendix B.3 in
26 * https://file.tavsys.net/control/controls-engineering-in-frc.pdf.
27 *
28 * @tparam States Number of states.
29 * @tparam Inputs Number of inputs.
30 */
31template <int States, int Inputs>
33 public:
36
37 /**
38 * Constructs a controller with the given coefficients and plant.
39 *
40 * @param plant The plant being controlled.
41 * @param plantRef The plant whose dynamics should be followed.
42 */
43 template <int Outputs>
46 : ImplicitModelFollower<States, Inputs>(plant.A(), plant.B(),
47 plantRef.A(), plantRef.B()) {}
48
49 /**
50 * Constructs a controller with the given coefficients and plant.
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 Aref Continuous system matrix whose dynamics should be followed.
55 * @param Bref Continuous input matrix whose dynamics should be followed.
56 */
59 const Matrixd<States, States>& Aref,
60 const Matrixd<States, Inputs>& Bref) {
61 // Find u_imf that makes real model match reference model.
62 //
63 // dx/dt = Ax + Bu_imf
64 // dz/dt = A_ref z + B_ref u
65 //
66 // Let x = z.
67 //
68 // dx/dt = dz/dt
69 // Ax + Bu_imf = Aref x + B_ref u
70 // Bu_imf = A_ref x - Ax + B_ref u
71 // Bu_imf = (A_ref - A)x + B_ref u
72 // u_imf = B⁻¹((A_ref - A)x + Bref u)
73 // u_imf = -B⁻¹(A - A_ref)x + B⁻¹B_ref u
74
75 // The first term makes the open-loop poles that of the reference
76 // system, and the second term makes the input behave like that of the
77 // reference system.
78 m_A = -B.householderQr().solve(A - Aref);
79 m_B = B.householderQr().solve(Bref);
80
81 Reset();
82 }
83
84 /**
85 * Returns the control input vector u.
86 *
87 * @return The control input.
88 */
89 const InputVector& U() const { return m_u; }
90
91 /**
92 * Returns an element of the control input vector u.
93 *
94 * @param i Row of u.
95 *
96 * @return The row of the control input vector.
97 */
98 double U(int i) const { return m_u(i); }
99
100 /**
101 * Resets the controller.
102 */
103 void Reset() { m_u.setZero(); }
104
105 /**
106 * Returns the next output of the controller.
107 *
108 * @param x The current state x.
109 * @param u The current input for the original model.
110 */
112 m_u = m_A * x + m_B * u;
113 return m_u;
114 }
115
116 private:
117 // Computed controller output
118 InputVector m_u;
119
120 // State space conversion gain
122
123 // Input space conversion gain
125};
126
127} // namespace frc
Contains the controller coefficients and logic for an implicit model follower.
Definition: ImplicitModelFollower.h:32
const InputVector & U() const
Returns the control input vector u.
Definition: ImplicitModelFollower.h:89
Vectord< Inputs > InputVector
Definition: ImplicitModelFollower.h:35
double U(int i) const
Returns an element of the control input vector u.
Definition: ImplicitModelFollower.h:98
void Reset()
Resets the controller.
Definition: ImplicitModelFollower.h:103
Vectord< States > StateVector
Definition: ImplicitModelFollower.h:34
InputVector Calculate(const StateVector &x, const InputVector &u)
Returns the next output of the controller.
Definition: ImplicitModelFollower.h:111
ImplicitModelFollower(const LinearSystem< States, Inputs, Outputs > &plant, const LinearSystem< States, Inputs, Outputs > &plantRef)
Constructs a controller with the given coefficients and plant.
Definition: ImplicitModelFollower.h:44
ImplicitModelFollower(const Matrixd< States, States > &A, const Matrixd< States, Inputs > &B, const Matrixd< States, States > &Aref, const Matrixd< States, Inputs > &Bref)
Constructs a controller with the given coefficients and plant.
Definition: ImplicitModelFollower.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