-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinear-sys.h
132 lines (120 loc) · 3.92 KB
/
linear-sys.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <random>
#include"matrix.h"
// MIT LicenseF
// Author: Iman Khademi, December 2019
// http://imankhademi.com
typedef vector<Vector2D> superVector;
/*
x(k+1) = A*x(k) + B*u(k)
y(k) = C*x(k)
*/
class DT_SS_Model // Discrete-Time Linear State-Space Model
{
private:
Matrix A;
Matrix B;
Matrix C;
Vector2D X;
Vector2D Y;
public:
DT_SS_Model() {};
DT_SS_Model(Matrix a, Matrix b, Matrix c, ColumnVector x0);
Matrix getA() { return A; };
Matrix getB() { return B; };
Matrix getC() { return C; };
Vector2D& getStates() { return X; };
Vector2D& getOutput() { return Y; };
void updateState(ColumnVector u);
void outputMeasurement();
void sysUpdate(ColumnVector ui);
void stepResponse(int noSamples);
void exportSysVariables(string filename="results.csv");
};
/*
x(k+1) = A*x(k) + B*u(k) + G*w(k)
y(k) = C*x(k) + v(k)
w : process noise, Wc = Cov(w)
v : measurement noise, Vc = Cov(v)
P0: Covariance of Estimation Error at k=0 : P0 = Cov(x(0) - X_hat(0))
x_hat : Estimation of 'x'
*/
class SS_Noisy_Model :public DT_SS_Model
{
private:
Matrix Vc;// covariance matrix of measurement noise
Matrix Wc;// covariance matrix of system noise
Matrix P;// Covariance of estimation error
Matrix G;
public:
SS_Noisy_Model(void) {};
SS_Noisy_Model(Matrix a, Matrix b, Matrix c, Matrix g, Matrix wp, Matrix vm, Matrix P0, ColumnVector x0);
Matrix getP() { return P; };
Matrix getVcov() { return Vc; };
Matrix getWcov() { return Wc; };
Matrix getG() { return G; };
void updateState(ColumnVector u, ColumnVector w);
void outputMeasurement(ColumnVector v);
void sysUpdate(ColumnVector ui);
};
class LQR_Control:public DT_SS_Model
{
private:
// Minimize: J = X(N)'*Qf*X(N) + SUM(X'*Q*X + U'*R*U)
Matrix Q;// State Cost (Positive Semi-Definite)
Matrix Qf;// Final State Cost (Positive Semi-Definite)
Matrix R; // Input Cost (Positive Definite)
int Nh; // Finite Time Horizon
superVector FeedbackGain;
superVector Pr; // Ricatti Equation Solution Matrix over time
public:
void RicattiSolver();
superVector getFeedbackGain() { return FeedbackGain; };
superVector getRicattiSolution() { return Pr; };
LQR_Control(Matrix a, Matrix b, Matrix c, ColumnVector x0, Matrix qx, Matrix qf, Matrix ru, int horizon);
void runLQRControl();
void exportResults(string filename = "results.csv");
};
class Kalman_Filter:public SS_Noisy_Model
{
private:
superVector KalmanGain;
superVector ErrorCovariance;
Vector2D Xhat;
public:
Kalman_Filter(Matrix a, Matrix b, Matrix c, Matrix g, Matrix wp, Matrix vm, Matrix P0, ColumnVector x0);
superVector getKalmanGain() { return KalmanGain; };
Vector2D getEstimates() { return Xhat; };
superVector getErrorCovariance() { return ErrorCovariance; };
void singleStepEstimation(ColumnVector referenceInput,ColumnVector observation);
void exportResults(string filename = "kalman_results.csv");
void runKalmanFilter(int samples,ColumnVector uref);
};
// LQG_Control: Kalman Filter + LQR
// Note: Kalman_Filter already includes the noisy process model
class LQG_Control :public Kalman_Filter
{
/* Minimize: J = E{X(N)'*Qf*X(N) + SUM(X'*Q*X + U'*R*U)}
subject to:
x(k+1) = A*x(k) + B*u(k) + G*w(k)
y(k) = C*x(k) + v(k)
u(k) = G*x_hat(k)
x_hat(k) = Kalman_Filter(y(k))
w : process noise, Wc = Cov(w)
v : measurement noise, Vc = Cov(v)
P0: Covariance of Estimation Error at k=0 : P0 = Cov(x(0) - X_hat(0))
x_hat : Estimation of 'x' obtained from Kalman Filter
*/
private:
Matrix Q;// State Cost (Positive Semi-Definite)
Matrix Qf;// Final State Cost (Positive Semi-Definite)
Matrix R; // Input Cost (Positive Definite)
int Nh; // Finite Time Horizon
superVector FeedbackGain;
superVector Pr; // Ricatti Equation Solution Matrix over time
void RicattiSolver();
public:
LQG_Control(Matrix qx, Matrix qf, Matrix ru, Matrix a, Matrix b, Matrix c, Matrix g, Matrix wp, Matrix vm, Matrix P0, ColumnVector x0, int horizon);
void runLQGControl();
void exportResults(string filename);
superVector getFeedbackGain() { return FeedbackGain; };
};