Brick
Loading...
Searching...
No Matches
linear_system.h
1#pragma once
2
3#include "core/math/eigen_interface.h"
4#include "core/math/systems/discretization.h"
5
6/**
7 * This class represents a state-space model of a linear system.
8 *
9 * It contains the following continuous matrices:
10 * A, System matrix
11 * B, Input matrix
12 * C, Output matrix
13 * D, Feedthrough matrix
14 */
15template <int STATES, int INPUTS, int OUTPUTS>
17 public:
18 using MatrixA = EMat<STATES, STATES>;
19 using MatrixB = EMat<STATES, INPUTS>;
20 using MatrixC = EMat<OUTPUTS, STATES>;
21 using MatrixD = EMat<OUTPUTS, INPUTS>;
22
23 using VectorX = EVec<STATES>;
24 using VectorU = EVec<INPUTS>;
25 using VectorY = EVec<OUTPUTS>;
26
27 /**
28 * Constructs a discrete linear system with the given continuous matrices.
29 *
30 * @param A The continuous system matrix
31 * @param B The continuous input matrix
32 * @param C The output matrix
33 * @param D The feedthrough matrix
34 */
35 LinearSystem(const MatrixA& A, const MatrixB& B, const MatrixC& C, const MatrixD& D)
36 : m_Ac(A), m_Bc(B), m_C(C), m_D(D) {}
37
38 /// Returns the continuous system matrix A.
39 MatrixA A() { return m_Ac; }
40
41 /// Returns the continuous input matrix B.
42 MatrixB B() { return m_Bc; }
43
44 /// Returns a tuple of A and B after being discretized.
45 const std::tuple<std::tuple<MatrixA, MatrixB>>& discAB(const double& dt) {
46 return discretize_AB(m_Ac, m_Bc, dt);
47 }
48
49 /// Returns the output matrix C.
50 MatrixC C() { return m_C; }
51
52 /// Returns the feedthrough matrix D.
53 MatrixD D() { return m_D; }
54
55 /**
56 * Computes the new state vector given the previous state vector, an input
57 * vector, and the timestep in seconds.
58 *
59 * @param x The current state vector.
60 * @param u The input vector.
61 * @param dt The timestep in seconds.
62 *
63 * @return The new state vector.
64 */
65 VectorX compute_X(const VectorX& x, const VectorU& u, double dt) {
66 // Discretize A and B
67 auto [Ad, Bd] = discretize_AB(m_Ac, m_Bc, dt);
68
69 return Ad * x + Bd * u;
70 }
71
72 /**
73 * Computes the output vector given a state and an input.
74 *
75 * @param x The state vector.
76 * @param u The input vector.
77 *
78 * @return The output vector.
79 */
80 VectorY compute_Y(const VectorX& x, const VectorU& u) { return m_C * x + m_D * u; }
81
82 private:
83 // Continuous system matrix
84 MatrixA m_Ac;
85 // Continuous input matrix
86 MatrixB m_Bc;
87 // Output matrix
88 MatrixC m_C;
89 // Feedthrough matrix
90 MatrixD m_D;
91};
VectorX compute_X(const VectorX &x, const VectorU &u, double dt)
Definition linear_system.h:65
MatrixB B()
Returns the continuous input matrix B.
Definition linear_system.h:42
const std::tuple< std::tuple< MatrixA, MatrixB > > & discAB(const double &dt)
Returns a tuple of A and B after being discretized.
Definition linear_system.h:45
LinearSystem(const MatrixA &A, const MatrixB &B, const MatrixC &C, const MatrixD &D)
Definition linear_system.h:35
MatrixC C()
Returns the output matrix C.
Definition linear_system.h:50
MatrixD D()
Returns the feedthrough matrix D.
Definition linear_system.h:53
MatrixA A()
Returns the continuous system matrix A.
Definition linear_system.h:39
VectorY compute_Y(const VectorX &x, const VectorU &u)
Definition linear_system.h:80