-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_drone.m
238 lines (135 loc) · 4.78 KB
/
main_drone.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
%This version has the measured covariances
%% Variables:
t_samp=0.065; %Discretization
delay=0.26; %Delay
load ssH.mat;
load ssYaw.mat;
load tf.mat;
%% Height Subsystem
%Continuous-Time State-Space Matrices:
Gz_A = ssH.A;
Gz_B = ssH.B;
Gz_C = ssH.C;
Gz_D = ssH.D;
%Discrete-Time and Continuous-Time Transfer Functions:
[num,den]=ss2tf(Gz_A,Gz_B,Gz_C,Gz_D);
Gzc=tf(num,den);
Gzd = c2d(Gzc,t_samp,'ZOH');
numH_discrete = Gzd.Numerator{:,:};
denH_discrete = Gzd.Denominator{:,:};
%Continuous-Time Observable Canonical Form:
[Gzc_A_obs,Gzc_B_obs,Gzc_C_obs] = observable(num,den,2);
%Discrete-Time Observable Canonical Form:
[Gzd_A_obs,Gzd_B_obs,Gzd_C_obs] = observable(Gzd.numerator{:,:},Gzd.denominator{:,:},2);
%Discrete-Time Observer Gain Matrix - Deadbeat Response:
Ke_z = observersDiscrete(Gzd_A_obs,Gzd_C_obs);
%Weighting Matrices: (change these values) - ch
Q_z = [ 0 0 0;
0 3000 0
0 0 0];
R_z = 100;
%LQR Gains:
[K_Z, K0_Z, P_Z, cloop_poles_Z] = gainLQR(Gzd_A_obs,Gzd_B_obs,Gzd_C_obs,Q_z,R_z,t_samp);
%Plot LQR
%Kalman Filter:
QE_Z=eye(2)*10^(-3);
RE_Z=0.002762;
KGe_Z = dlqe(Gzd_A_obs,QE_Z,Gzd_C_obs,QE_Z,RE_Z);
%% Heading Subsystem
%Continuous-Time State-Space Matrices:
Gyaw_A = ssYaw.A;
Gyaw_B = ssYaw.B;
Gyaw_C = ssYaw.C;
Gyaw_D = ssYaw.D;
%Discrete-Time and Continuous-Time Transfer Functions;
[num,den]=ss2tf(Gyaw_A,Gyaw_B,Gyaw_C,Gyaw_D);
Gyawc=tf(num,den);
Gyawd = c2d(Gyawc,t_samp,'ZOH');
numYaw_discrete = Gyawd.Numerator{:,:};
denYaw_discrete = Gyawd.Denominator{:,:};
%Continuous-Time Observable Canonical Form:
[Gyawc_A_obs,Gyawc_B_obs,Gyawc_C_obs] = observable(num,den,1);
%Discrete-Time Observable Canonical Form:
[Gyawd_A_obs,Gyawd_B_obs,Gyawd_C_obs] = observable(Gyawd.numerator{:,:},Gyawd.denominator{:,:},1);
%Observer Discrete-Time Gain Matrix - Deadbeat Response:
Ke_yaw = observersDiscrete(Gyawd_A_obs,Gyawd_C_obs);
%Weighting Matrices:
Q_Yaw = [1 0
0 1];
R_Yaw = 10;
%LQR Gains:
[K_yaw, K0_yaw, P_yaw, cloop_poles_yaw] = gainLQR(Gyawd_A_obs,Gyawd_B_obs,Gyawd_C_obs,Q_Yaw,R_Yaw,t_samp);
%Use PlotLQR?
%Kalman Filter:
QE_yaw=eye(1)*10^(-4);
RE_yaw=0.000061;
KGe_yaw = dlqe(Gyawd_A_obs,QE_yaw,Gyawd_C_obs,QE_yaw,RE_yaw);
%% Longitudinal Position Subsystem
%Continuous-Time State-Space Matrices:
[Gxc_A,Gxc_B,Gxc_C,Gxc_D] = tf2ss(tfX.Numerator{:,:},tfX.Denominator{:,:});
[num,den]=ss2tf(Gxc_A,Gxc_B,Gxc_C,Gxc_D);
Gxc=tf(num,den);
%Discrete-Time Transfer Function:
Gxd = c2d(Gxc,t_samp,'ZOH');
numX_discrete = Gxd.Numerator{:,:};
denX_discrete = Gxd.Denominator{:,:};
%Continuous-Time Observable Canonical Form:
[Gxc_A_obs,Gxc_B_obs,Gxc_C_obs] = observable(Gxc.numerator{:,:},Gxc.denominator{:,:},4);
%Discrete-Time Observable Canonical Form:
[Gxd_A_obs,Gxd_B_obs,Gxd_C_obs] = observable(Gxd.numerator{:,:},Gxd.denominator{:,:},4);
%Observer Discrete-Time Gain Matrix - Deadbeat Response:
Ke_X = observersDiscrete(Gxd_A_obs,Gxd_C_obs);
%Observer Continuous-Time Gain Matrix - Deadbeat Response:
%Kec_X = observersContinuous(Gxc_A_obs,Gxc_C_obs);
%Weighting Matrices:
Q_X=[1 0 0 0 0;
0 1 0 0 0;
0 0 1 0 0;
0 0 0 1 0
0 0 0 0 1];
R_X = 700;
%LQR Gains:
[K_X, K0_X, P_X, cloop_poles_X] = gainLQR(Gxd_A_obs,Gxd_B_obs,Gxd_C_obs,Q_X,R_X,t_samp);
%Plot LQR:
%[Gx_A_lqr, Gx_B_lqr, Gx_C_lqr, D] = plotLQR(Gxc_A_obs,Gxc_B_obs,Gxc_C_obs, K_X);
%Pole placement: (?)
%[A_acker, B_acker, C_acker, D_acker, Kx_acker] = poleplace(Gxc.numerator{:,:},Gxc.denominator{:,:});
%Kalman Filter
QE_X=eye(4)*10^(-4);
RE_X=0.001166;
KGe_X = dlqe(Gxd_A_obs,QE_X,Gxd_C_obs,QE_X,RE_X);
%% Lateral Position Subsystem
%Continuous-Time State-Space Matrices:
[Gyc_A,Gyc_B,Gyc_C,Gyc_D] = tf2ss(tfY.Numerator{:,:},tfY.Denominator{:,:});
[num,den]=ss2tf(Gyc_A,Gyc_B,Gyc_C,Gyc_D);
Gyc=tf(num,den);
%Discrete-Time Transfer Function:
Gyd = c2d(Gyc,t_samp,'ZOH');
numY_discrete = Gyd.numerator{:,:};
denY_discrete = Gyd.denominator{:,:};
%Continuous-Time Observable Canonical Form:
[Gyc_A_obs,Gyc_B_obs,Gyc_C_obs] = observable(Gyc.numerator{:,:},Gyc.denominator{:,:},4);
%Discrete-Time Observable Canonical Form:
[Gyd_A_obs,Gyd_B_obs,Gyd_C_obs] = observable(Gyd.numerator{:,:},Gyd.denominator{:,:},4);
%Discrete-Time Observer Gain Matrix:
Ke_Y = observersDiscrete(Gyd_A_obs,Gyd_C_obs);
%Weighting Matrices:
Q_Y=[1 0 0 0 0;
0 1 0 0 0;
0 0 2 0 0;
0 0 0 2 0
0 0 0 0 2];
R_Y = 1500;
%LQR Gains:
[K_Y, K0_Y, P_Y, cloop_poles_Y] = gainLQR(Gyd_A_obs,Gyd_B_obs,Gyd_C_obs,Q_Y,R_Y,t_samp);
%Kalman Filter:
QE_Y=eye(4)*10^(-5);
RE_Y=0.000961;
KGe_Y = dlqe(Gyd_A_obs,QE_Y,Gyd_C_obs,QE_Y,RE_Y);
%% Discrete SS for devkit:
ssRoll_discrete=c2d(ssRoll,t_samp);
ssRoll2V_discrete=c2d(ssRoll2V,t_samp);
ssPitch_discrete=c2d(ssPitch,t_samp);
ssPitch2U_discrete=c2d(ssPitch2U,t_samp);
ssYaw_discrete=c2d(ssYaw,t_samp);
ssH_discrete=c2d(ssH,t_samp);