-
Notifications
You must be signed in to change notification settings - Fork 13
/
guidance_model.m
executable file
·267 lines (218 loc) · 6.97 KB
/
guidance_model.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
function [sys,x0,str,ts,simStateCompliance] = guidance_model(t,x,u,flag,P)
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes(P);
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1,
sys=mdlDerivatives(t,x,u,P);
%%%%%%%%%%
% Update %
%%%%%%%%%%
case 2,
sys=mdlUpdate(t,x,u);
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3,
sys=mdlOutputs(t,x,u,P);
%%%%%%%%%%%%%%%%%%%%%%%
% GetTimeOfNextVarHit %
%%%%%%%%%%%%%%%%%%%%%%%
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
%%%%%%%%%%%%%
% Terminate %
%%%%%%%%%%%%%
case 9,
sys=mdlTerminate(t,x,u);
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
% end sfuntmpl
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes(P)
%
% call simsizes for a sizes structure, fill it in and convert it to a
% sizes array.
%
% Note that in this example, the values are hard coded. This is not a
% recommended practice as the characteristics of the block are typically
% defined by the S-function parameters.
%
sizes = simsizes;
sizes.NumContStates = 7;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 16+12;
sizes.NumInputs = 4;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
%
% initialize the initial conditions
%
x0 = [...
P.pn0;... % initial North position
P.pe0;... % initial East position
P.psi0;... % initial heading
0;... % initial heading rate
-P.pd0;... % initial altitude
0;... % initial climb rate
P.Va0;... % initial airspeed
];
%
% str is always an empty matrix
%
str = [];
%
% initialize the array of sample times
%
ts = [0 0];
% Specify the block simStateCompliance. The allowed values are:
% 'UnknownSimState', < The default setting; warn and assume DefaultSimState
% 'DefaultSimState', < Same sim state as a built-in block
% 'HasNoSimState', < No sim state
% 'DisallowSimState' < Error out when saving or restoring the model sim state
simStateCompliance = 'UnknownSimState';
% end mdlInitializeSizes
%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
function sys=mdlDerivatives(t,x,u,P)
pn = x(1); % North position
pe = x(2); % East position
chi = x(3); % heading
chidot = x(4); % heading rate
h = x(5); % altitude
hdot = x(6); % climb rate
Va = x(7); % airspeed
Va_c = u(1); % commanded airspeed
h_c = u(2); % commanded altitude
chi_c = u(3); % commanded heading angle
phi_ff = u(4); % feedforward roll command
% compute chi_c_dot from roll feedforward
chi_c_dot = P.gravity/Va*tan(phi_ff);
% solve for heading and groundspeed
psi = chi - asin( (-P.wind_n*sin(chi)+P.wind_e*cos(chi))/Va );
%Vg = [cos(chi), sin(chi)]*(Va*[cos(psi); sin(psi)] + [wn; we]);
% compute groundspeed
pndot = Va*cos(psi) + P.wind_n;
pedot = Va*sin(psi) + P.wind_e;
chiddot = P.b_chidot*(chi_c_dot-chidot) + P.b_chi*(chi_c-chi);
Vadot = P.b_Va*(Va_c-Va);
% don't let climb rate exceed Va*sin(\gamma_max)
hddot = -P.b_hdot*hdot + P.b_h*(h_c-h);
if (hdot>=Va*sin(P.gamma_max)) & (hddot>0),
hddot = 0;
elseif (hdot<=-Va*sin(P.gamma_max)) & (hddot<0),
hddot = 0;
end
sys = [...
pndot;...
pedot;...
chidot;...
chiddot;...
hdot;...
hddot;...
Vadot;...
];
% end mdlDerivatives
%
%=============================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=============================================================================
%
function sys=mdlUpdate(t,x,u)
sys = [];
% end mdlUpdate
%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(t,x,u,P)
pn = x(1); % North position
pe = x(2); % East position
chi = x(3); % course
chidot = x(4); % course rate
h = x(5); % altitude
hdot = x(6); % climb rate
Va = x(7); % airspeed
alpha = 0;
beta = 0;
% wind speed
wn = P.wind_n;
we = P.wind_e;
% solve for heading and groundspeed
psi = chi - asin( (-P.wind_n*sin(chi)+P.wind_e*cos(chi))/Va );
Vg = [cos(chi), sin(chi)]*(Va*[cos(psi); sin(psi)] + [wn; we]);
% roll angle is given by psidot = g/V*tan(phi)
phi = atan(Vg*chidot/P.gravity);
% letting theta equal flight path angle given by hdot = V sin(gamma)
theta = asin(hdot/Va);
% set angular rates to zero
p = 0;
q = 0;
r = 0;
% output the same states that are returned by the state estimation block
% pnhat - estimated North position,
% pehat - estimated East position,
% hhat - estimated altitude,
% Vahat - estimated airspeed,
% alphahat - estimated angle of attack
% betahat - estimated sideslip angle
% phihat - estimated roll angle,
% thetahat - estimated pitch angel,
% chihat - estimated course,
% phat - estimated roll rate,
% qhat - estimated pitch rate,
% rhat - estimated yaw rate,
% Vghat - estimated ground speed,
% wnhat - estimate of North wind,
% wehat - estimate of East wind
% psihat - estimate of heading angle
% also need to return the normal state vector so that we don't need to
% change the drawing routine.
sys = [pn; pe; h; Va; alpha; beta; phi; theta; chi; p; q; r; Vg; wn; we; psi;...
pn; pe; -h; Va; 0; 0; phi; theta; psi; p; q; r];
% end mdlOutputs
%
%=============================================================================
% mdlGetTimeOfNextVarHit
% Return the time of the next hit for this block. Note that the result is
% absolute time. Note that this function is only used when you specify a
% variable discrete-time sample time [-2 0] in the sample time array in
% mdlInitializeSizes.
%=============================================================================
%
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1; % Example, set the next hit to be one second later.
sys = t + sampleTime;
% end mdlGetTimeOfNextVarHit
%
%=============================================================================
% mdlTerminate
% Perform any end of simulation tasks.
%=============================================================================
%
function sys=mdlTerminate(t,x,u)
sys = [];
% end mdlTerminate