-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlqg.m
177 lines (156 loc) · 5.56 KB
/
lqg.m
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
clear all
%% System Parameters %%
syms M m1 m2 l1 l2
% Parameters
g = 10; % gravity
M = 1000;
m1 = 100;
m2 = 100;
l1 = 20;
l2 = 10;
%% Nonlinear System Model %%
% Nx1_dot = x2;
% Nx2_dot = (-m1*g*sin(x3)*cos(x3)-m1*l1*x4*x4*sin(x3) - -m2*g*sin(x5)*cos(x5)-m2*l2*x6*x6*sin(x5)) / (m1*sin(x3)*sin(x3) + m2*sin(x5)*sin(x5)+M);
% Nx3_dot = x4;
% Nx4_dot = (-m1*g*sin(x3)*cos(x3)-m1*l1*x4*x4*sin(x3) - -m2*g*sin(x5)*cos(x5)-m2*l2*x6*x6*sin(x5)) /(l1 * (m1*sin(x3)*sin(x3) + m2*sin(x5)*sin(x5)+M)) - g*sin(x3)/l1;
% Nx5_dot = x6;
% Nx6_dot = (-m1*g*sin(x3)*cos(x3)-m1*l1*x4*x4*sin(x3) - -m2*g*sin(x5)*cos(x5)-m2*l2*x6*x6*sin(x5)) /(l2 * (m1*sin(x3)*sin(x3) + m2*sin(x5)*sin(x5)+M)) - g*sin(x5)/l2;
%% State Spaee Model %%
A = [0 1 0 0 0 0;
0 0 -m1*g/M 0 -m2*g/M 0;
0 0 0 1 0 0;
0 0 (-m1*g-M*g)/(M*l1) 0 -m2*g/(M*l1) 0;
0 0 0 0 0 1;
0 0 -m1*g/(M*l2) 0 (-m2*g-M*g)/(M*l2) 0];
B = [0; 1/M; 0; 1/(M*l1); 0; 1/(M*l2)];
C1 = [1 0 0 0 0 0];
C3 = [1 0 0 0 0 0;0 0 0 0 1 0];
C4 = [1 0 0 0 0 0;0 0 1 0 0 0;0 0 0 0 1 0];
D = 0;
%% LQG controller %%
R = 10;
Q = [100000 0 0 0 0 0;
0 100000 0 0 0 0
0 0 10 0 0 0;
0 0 0 10 0 0;
0 0 0 0 10 0;
0 0 0 0 0 10];
%% Initial Condition %%
x = [0.01; 0.01; 0.01; 0.01; 0.01; 0.01];
P = x*x.';
w = 1;
v = 0;
non_x = x;
x_hat = [0; 0; 0; 0; 0; 0];
dt = 0.001;
num = 1;
K = lqr(A,B,Q,R);
Times = 1000/dt; %
record_state_non_x = zeros(6,Times);
record_state_x = zeros(6,Times);
record_state_x_hat = zeros(6,Times);
record_times = zeros(1,Times);
for t = 0.0 : dt : Times*dt
%% Nonlinear System Model %%
non_x_A = [non_x(2);
(-m1*g*sin(non_x(3))*cos(non_x(3))-m1*l1*non_x(4)*non_x(4)*sin(non_x(3)) - m2*g*sin(non_x(5))*cos(non_x(5)) - m2*l2*non_x(6)*non_x(6)*sin(non_x(5))) / (m1*sin(non_x(3))*sin(non_x(3)) + m2*sin(non_x(5))*sin(non_x(5))+M);
non_x(4);
(-m1*g*sin(non_x(3))*cos(non_x(3))-m1*l1*non_x(4)*non_x(4)*sin(non_x(3)) - m2*g*sin(non_x(5))*cos(non_x(5)) - m2*l2*non_x(6)*non_x(6)*sin(non_x(5))) * cos(non_x(3)) / (l1 * (m1*sin(non_x(3))*sin(non_x(3)) + m2*sin(non_x(5))*sin(non_x(5))+M)) - g*sin(non_x(3))/l1;
non_x(6);
(-m1*g*sin(non_x(3))*cos(non_x(3))-m1*l1*non_x(4)*non_x(4)*sin(non_x(3)) - m2*g*sin(non_x(5))*cos(non_x(5)) - m2*l2*non_x(6)*non_x(6)*sin(non_x(5))) * cos(non_x(5)) / (l2 * (m1*sin(non_x(3))*sin(non_x(3)) + m2*sin(non_x(5))*sin(non_x(5))+M)) - g*sin(non_x(5))/l2];
non_x_B = [0;
1/(M + m1 * sin(non_x(3)) * sin(non_x(3)));
0;
cos(non_x(3))/(l1*(M + m1 * sin(non_x(3)) * sin(non_x(3)) + m2 * sin(non_x(5)) * sin(non_x(5))));
0;
cos(non_x(5))/(l2*(M + m1 * sin(non_x(3)) * sin(non_x(3)) + m2 * sin(non_x(5)) * sin(non_x(5))))];
%% Observer %%
% Observer 1
F = 10;
y = C1 * non_x;
y_hat = C1 * x_hat;
non_x_dot = non_x_A - B * K * x_hat;
P_dot = A*P + P*A.' - P*C1.'*(1/w)*C1*P + v;
L = P*C1.'*(1/w);
x_hat_dot = A*x_hat - B*K*x_hat + L*(y - y_hat);
non_x = non_x + non_x_dot * dt;
x_hat = x_hat + x_hat_dot * dt;
P = P + P_dot*dt;
%% Constant Force %%
% F = 10;
% y = C1 * non_x;
% y_hat = C1 * x_hat;
% non_x_dot = non_x_A - B * K * x_hat + B*F;
% P_dot = A*P + P*A.' - P*C1.'*(1/w)*C1*P + v;
% L = P*C1.'*(1/w);
% x_hat_dot = A*x_hat - B*K*x_hat + L*(y - y_hat) + B*F;
% non_x = non_x + non_x_dot * dt;
% x_hat = x_hat + x_hat_dot * dt;
% P = P + P_dot*dt;
%% Record
record_state_x_hat(1,num) = x_hat(1);
record_state_x_hat(2,num) = x_hat(2);
record_state_x_hat(3,num) = x_hat(3);
record_state_x_hat(4,num) = x_hat(4);
record_state_x_hat(5,num) = x_hat(5);
record_state_x_hat(6,num) = x_hat(6);
record_state_non_x(1,num) = non_x(1);
record_state_non_x(2,num) = non_x(2);
record_state_non_x(3,num) = non_x(3);
record_state_non_x(4,num) = non_x(4);
record_state_non_x(5,num) = non_x(5);
record_state_non_x(6,num) = non_x(6);
record_times(num) = t;
num = num + 1;
end
%% Observer Figure %%
%
subplot(2,3,1);
plot(record_times,record_state_x_hat(1,:),'b')
title('State 1: x')
hold on;
subplot(2,3,4);
plot(record_times,record_state_x_hat(2,:),'b')
title('State 2: x dot')
hold on;
subplot(2,3,2);
plot(record_times,record_state_x_hat(3,:),'b')
title('State 3: theta1')
hold on;
subplot(2,3,5);
plot(record_times,record_state_x_hat(4,:),'b')
title('State 4: theta1 dot')
hold on;
subplot(2,3,3);
plot(record_times,record_state_x_hat(5,:),'b')
title('State 5: theta2')
hold on;
subplot(2,3,6);
plot(record_times,record_state_x_hat(6,:),'b')
title('State 6: theta2 dot')
hold on;
%% Nonlinear Figures %%
subplot(2,3,1);
plot(record_times,record_state_non_x(1,:),'r')
legend('Observer','State')
title('State 1: x')
subplot(2,3,4);
plot(record_times,record_state_non_x(2,:),'r')
legend('Observer','State')
title('State 2: x dot')
subplot(2,3,2);
plot(record_times,record_state_non_x(3,:),'r')
legend('Observer','State')
title('State 3: theta1')
subplot(2,3,5);
plot(record_times,record_state_non_x(4,:),'r')
legend('Observer','State')
title('State 4: theta1 dot')
subplot(2,3,3);
plot(record_times,record_state_non_x(5,:),'r')
legend('Observer','State')
title('State 5: theta2')
subplot(2,3,6);
plot(record_times,record_state_non_x(6,:),'r')
legend('Observer','State')
title('State 6: theta2 dot')