WPILibC++ 2024.1.1-beta-4
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 */
28template <int States, int Inputs>
30 public:
33
34 /**
35 * Constructs a controller with the given coefficients and plant.
36 *
37 * @param plant The plant being controlled.
38 * @param plantRef The plant whose dynamics should be followed.
39 */
40 template <int Outputs>
43 : ImplicitModelFollower<States, Inputs>(plant.A(), plant.B(),
44 plantRef.A(), plantRef.B()) {}
45
46 /**
47 * Constructs a controller with the given coefficients and plant.
48 *
49 * @param A Continuous system matrix of the plant being controlled.
50 * @param B Continuous input matrix of the plant being controlled.
51 * @param Aref Continuous system matrix whose dynamics should be followed.
52 * @param Bref Continuous input matrix whose dynamics should be followed.
53 */
56 const Matrixd<States, States>& Aref,
57 const Matrixd<States, Inputs>& Bref) {
58 // Find u_imf that makes real model match reference model.
59 //
60 // dx/dt = Ax + Bu_imf
61 // dz/dt = A_ref z + B_ref u
62 //
63 // Let x = z.
64 //
65 // dx/dt = dz/dt
66 // Ax + Bu_imf = Aref x + B_ref u
67 // Bu_imf = A_ref x - Ax + B_ref u
68 // Bu_imf = (A_ref - A)x + B_ref u
69 // u_imf = B⁻¹((A_ref - A)x + Bref u)
70 // u_imf = -B⁻¹(A - A_ref)x + B⁻¹B_ref u
71
72 // The first term makes the open-loop poles that of the reference
73 // system, and the second term makes the input behave like that of the
74 // reference system.
75 m_A = -B.householderQr().solve(A - Aref);
76 m_B = B.householderQr().solve(Bref);
77
78 Reset();
79 }
80
81 /**
82 * Returns the control input vector u.
83 *
84 * @return The control input.
85 */
86 const InputVector& U() const { return m_u; }
87
88 /**
89 * Returns an element of the control input vector u.
90 *
91 * @param i Row of u.
92 *
93 * @return The row of the control input vector.
94 */
95 double U(int i) const { return m_u(i); }
96
97 /**
98 * Resets the controller.
99 */
100 void Reset() { m_u.setZero(); }
101
102 /**
103 * Returns the next output of the controller.
104 *
105 * @param x The current state x.
106 * @param u The current input for the original model.
107 */
109 m_u = m_A * x + m_B * u;
110 return m_u;
111 }
112
113 private:
114 // Computed controller output
115 InputVector m_u;
116
117 // State space conversion gain
119
120 // Input space conversion gain
122};
123
124} // namespace frc
Contains the controller coefficients and logic for an implicit model follower.
Definition: ImplicitModelFollower.h:29
const InputVector & U() const
Returns the control input vector u.
Definition: ImplicitModelFollower.h:86
Vectord< Inputs > InputVector
Definition: ImplicitModelFollower.h:32
double U(int i) const
Returns an element of the control input vector u.
Definition: ImplicitModelFollower.h:95
void Reset()
Resets the controller.
Definition: ImplicitModelFollower.h:100
Vectord< States > StateVector
Definition: ImplicitModelFollower.h:31
InputVector Calculate(const StateVector &x, const InputVector &u)
Returns the next output of the controller.
Definition: ImplicitModelFollower.h:108
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:41
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:54
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