-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.cpp
108 lines (100 loc) · 2.41 KB
/
test.cpp
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
#include"linear-sys.h"
// MIT License
// Author: Iman Khademi, December 2019
// http://imankhademi.com
/* main function */
#define TEST_NUMBER 4
// Example 1: LQR Control
void runTest1()
{
// reference: https://ece.gmu.edu/~gbeale/ece_620/discrete_lqr_01/discrete_lqr_01.html
int n = 4;
Matrix a({ {2.f} });
Matrix b({ {1.0f} });
Matrix c({ {1.0} });
ColumnVector x0 = { 100.f };
Matrix Q({ {10.f} });
Matrix Qf({ {50.f} });
Matrix R({ {1.0f} });
LQR_Control myLQR(a, b, c, x0, Q, Qf, R, n);
myLQR.runLQRControl();
myLQR.exportResults();
return;
}
// Example 2: LQR Control
Matrix identityMatrix(int size);
void runTest2()
{
// reference: https://stanford.edu/class/ee363/lectures/dlqr.pdf
int n = 20; // control horizon
float rho = 1.0f;
Matrix a({ {1.f,1.f},{0.f,1.f} });
Matrix b({ {0.f},{1.f} });
Matrix c({ {1.0f,0.f} });
ColumnVector x0 = { {1.f,0.f} };
Matrix Q({ {1.f,0.f},{0.f,0.f} });// C'*C
Matrix Qf(Q);
Matrix R;
R = identityMatrix(1);
R = rho * R;
LQR_Control myLQR(a, b, c, x0, Q, Qf, R, n);
myLQR.runLQRControl();
myLQR.exportResults();
return;
}
// Example3: Kalman Filter
ostream& operator<<(ostream& out, Vector2D v);
void runTest3()
{
Matrix a({ {1.9223f,-0.9604f},{1.f,0.f} });
Matrix b({ {1.0f},{1.0f} });
Matrix c({ {1.0f,.0f} });
Matrix g({ {1.0f},{1.0f} });
Matrix P0({ {0.01f,0.},{0.f,0.02f} });// must be Symmetric Positive Semi-Definite
ColumnVector x0 = { {0.f,0.f} };
Matrix wp({ {0.01f} });
Matrix vm({ {0.01f} });
Kalman_Filter filter(a, b, c, g, wp, vm, P0, x0);
filter.runKalmanFilter(100, { {1.25} });
superVector K = filter.getKalmanGain();
filter.exportResults();// kalman_results.csv
}
// Example 4: LQG Control
void runTest4()
{
int n = 100;
Matrix a({ {1.9223f,-0.9604f},{1.f,0.f} });
Matrix b({ {1.0f},{1.0f} });
Matrix c({ {1.0f,.0f} });
Matrix g({ {1.0f},{1.0f} });
Matrix Q({ {10.f,0.f},{0.f,10.f} });//
Matrix Qf(Q);// Qf = Q
Matrix R({ {1.0f} });
Matrix P0({ {0.01f,0.f},{0.f,0.02f} });// must be Symmetric Psitive Semi-Definite
ColumnVector x0 = { {10.f,10.f} };
Matrix wp({ {0.01f} });
Matrix vm({ {0.01f} });
LQG_Control controller(Q, Qf, R, a, b, c, g, wp, vm, P0, x0, n);
controller.runLQGControl();
controller.exportResults("LQG_results.csv");
}
int main()
{
unsigned char test = TEST_NUMBER;
switch (test)
{
case 1:
runTest1();
break;
case 2:
runTest2();
break;
case 3:
runTest3();
break;
case 4:
runTest4();
break;
}
return 0;
}