forked from pi-cos/MATMPC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InitData.m
279 lines (219 loc) · 9.39 KB
/
InitData.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
268
269
270
271
272
273
274
275
276
277
278
279
%% Initialize Data
function [input, data] = InitData(settings)
nx = settings.nx; % No. of differential states
nu = settings.nu; % No. of controls
nz = settings.nz; % No. of algebraic states
ny = settings.ny; % No. of outputs (references)
nyN= settings.nyN; % No. of outputs at terminal stage
np = settings.np; % No. of parameters (on-line data)
nc = settings.nc; % No. of constraints
ncN = settings.ncN; % No. of constraints at terminal stage
N = settings.N; % No. of shooting points
nbx = settings.nbx; % No. of state bounds
nbu = settings.nbu; % No. of control bounds
nbu_idx = settings.nbu_idx; % Index of control bounds
switch settings.model
case 'InvertedPendulum'
input.x0 = [0;pi;0;0];
input.u0 = zeros(nu,1);
input.z0 = zeros(nz,1);
para0 = 0;
Q=repmat([10 10 0.1 0.1 0.01]',1,N);
QN=[10 10 0.1 0.1]';
% upper and lower bounds for states (=nbx)
lb_x = -2;
ub_x = 2;
% upper and lower bounds for controls (=nbu)
lb_u = -20;
ub_u = 20;
% upper and lower bounds for general constraints (=nc)
lb_g = [];
ub_g = [];
lb_gN = [];
ub_gN = [];
case 'ChainofMasses_Lin'
n=5;
data.n=n;
input.x0=zeros(nx,1);
for i=1:n
input.x0(i)=7.5*i/n;
end
input.u0=zeros(nu,1);
input.z0 = zeros(nz,1);
para0=0;
wv=[];wx=[];
wu = [0.1 0.1 0.1];
for i=1:3
wx = [wx, 25];
wv = [wv, 0.25*ones(1,n-1)];
end
Q = repmat([wx,wv,wu]',1,N);
QN= [wx,wv]';
% upper and lower bounds for states (=nbx)
lb_x = [];
ub_x = [];
% upper and lower bounds for controls (=nbu)
lb_u = [-1;-1;-1];
ub_u = [1;1;1];
% upper and lower bounds for general constraints (=nc)
lb_g = [];
ub_g = [];
lb_gN = [];
ub_gN = [];
case 'ChainofMasses_NLin'
n=10;
data.n=n;
input.x0=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 zeros(1,nx-n)]';
input.u0=zeros(nu,1);
input.z0 = zeros(nz,1);
para0=0;
wv=[];wx=[];
wu=[0.01, 0.01, 0.01];
for i=1:3
wx=[wx,25];
wv=[wv,ones(1,n-1)];
end
Q = repmat([wx,wv,wu]',1,N);
QN= [wx,wv]';
% upper and lower bounds for states (=nbx)
lb_x = [];
ub_x = [];
% upper and lower bounds for controls (=nbu)
lb_u = [-1;-1;-1];
ub_u = [1;1;1];
% upper and lower bounds for general constraints (=nc)
lb_g = [];
ub_g = [];
lb_gN = [];
ub_gN = [];
case 'TethUAV'
input.x0=[0; 0; 0; 0; 9.81; 0];%zeros(nx,1);
input.u0=[0; 0; 0; 0];%zeros(nu,1);
input.z0 = zeros(nz,1);
alpha = pi/6;
para0=[-alpha; alpha];
% phi phi_dot theta theta_dot
q = [200, 1, 200, 0, 0.0001, 0.0001, 1, 1, 1, 0, 5000, 5000];
qN = q(1:nyN);
Q = repmat(q',1,N);
QN = qN';
fR_min = 0;%-inf;
fR_max = 15;%inf;
tauR_min = -1.2;%-inf;
tauR_max = 1.2;%inf;
fL_min = 0;%-inf;
fL_max = 10;%inf;
constr_max = 0;
constr_min = -inf;
s1_min = 0;
s1_max = inf;
s2_min = 0;
s2_max = inf;
% upper and lower bounds for states (=nbx) if f1,2 are f_R, tau_R
lb_x = [fR_min; tauR_min];%0*ones(nbx,1);
ub_x = [fR_max; tauR_max]; %omegaMax*ones(nbx,1);
% upper and lower bounds for controls (=nbu)
lb_u = [s1_min; s2_min];
ub_u = [s1_max; s2_max];
% upper and lower bounds for general constraints (=nc)
lb_g = [fL_min; constr_min; constr_min];
ub_g = [fL_max; constr_max; constr_max];
lb_gN = [fL_min];
ub_gN = [fL_max];
case 'DiM'
input.x0 = zeros(nx,1); % initial state
input.u0 = zeros(nu,1); % initial control
input.z0 = zeros(nz,1);
para0 = 0; % initial parameters (by default a np by 1 vector, if there is no parameter, set para0=0)
%weighting matrices
Q=[1200,1200,2000,800,800,5800,... % perceived acc and angular vel
32000*1.1,32000*1.1,1600*1,... %px,py,pz hex
3200*1.1,3200*1.1,2000*1,... %vx, vy, vz hex
4600*1,600*1,... % x,y tri
850*1,850*1,... % vx,vy tri
3700,3000,1500,... % phi, theta, psi hex
750,... % phi tri
0.01,0.0,0.0,... % omega phi,theta,psi hex
500.0,... % omega phi tri
0.0,0.0,0.001,... %ax,ay,az hex % 20*1.1,20*1.1,... % ax,ay tri
0.0,0.01,0.1 ... % alpha phi,theta, psi hex
];
Q = repmat(Q',1,N);
QN=Q(1:nyN,1);
% upper and lower bounds for states (=nbx)
lb_x = [];
ub_x = [];
% upper and lower bounds for controls (=nbu)
lb_u = [];
ub_u = [];
% upper and lower bounds for general constraints (=nc)
lb_g=[1.045;1.045;1.045;1.045;1.045;1.045]; % lower bounds for ineq constraints
ub_g=[1.3750;1.3750;1.3750;1.3750;1.3750;1.3750]; % upper bounds for ineq constraints
lb_gN=[1.045;1.045;1.045;1.045;1.045;1.045]; % lower bounds for ineq constraints at terminal point
ub_gN=[1.3750;1.3750;1.3750;1.3750;1.3750;1.3750]; % upper bounds for ineq constraints at terminal point
case 'TurboEngine'
input.x0 = [1.2; 1.2; 0; 0];
input.u0 = zeros(nu,1);
input.z0 = [1.1; 1.1];
para0 = [2000; -0.3];
Q=repmat([10 1e-7*0.05 1e-6*0.05]',1,N);
QN=[10]';
% upper and lower bounds for states (=nbx)
lb_x = [0;0];
ub_x = [100;100];
% upper and lower bounds for controls (=nbu)
lb_u = [-800; -800];
ub_u = [800; 800];
% upper and lower bounds for general constraints (=nc)
lb_g = [0; 0; 0];
ub_g = [2; 90e3/60; 180e3/60];
lb_gN = [0; 0; 0];
ub_gN = [2; 90e3/60; 180e3/60];
end
% prepare the data
input.lb = repmat(lb_g,N,1);
input.ub = repmat(ub_g,N,1);
input.lb = [input.lb;lb_gN];
input.ub = [input.ub;ub_gN];
lbu = -inf(nu,1);
ubu = inf(nu,1);
for i=1:nbu
lbu(nbu_idx(i)) = lb_u(i);
ubu(nbu_idx(i)) = ub_u(i);
end
input.lbu = repmat(lbu,1,N);
input.ubu = repmat(ubu,1,N);
input.lbx = repmat(lb_x,1,N);
input.ubx = repmat(ub_x,1,N);
x = repmat(input.x0,1,N+1); % initialize all shooting points with the same initial state
u = repmat(input.u0,1,N); % initialize all controls with the same initial control
z = repmat(input.z0,1,N); % initialize all algebraic state with the same initial condition
para = repmat(para0,1,N+1); % initialize all parameters with the same initial para
input.x=x; % (nx by N+1)
input.u=u; % (nu by N)
input.z=z; % (nz by N)
input.od=para; % (np by N+1)
input.W=Q; % (ny by N)
input.WN=QN; % (nyN by 1)
input.lambda=zeros(nx,N+1); % langrangian multiplier w.r.t. equality constraints
input.mu=zeros(N*nc+ncN,1); % langrangian multipliers w.r.t. general inequality constraints
input.mu_u = zeros(N*nu,1); % langrangian multipliers w.r.t. input bounds
input.mu_x = zeros(N*nbx,1); % langrangian multipliers w.r.t. state bounds
%% Reference generation
switch settings.model
case 'InvertedPendulum'
data.REF=zeros(1,nx+nu);
case 'ChainofMasses_Lin'
data.REF=[7.5,0,0,zeros(1,3*(n-1)),zeros(1,nu)];
case 'ChainofMasses_NLin'
data.REF=[1,0,0,zeros(1,3*(n-1)),zeros(1,nu)];
case 'TethUAV'
data.REF = zeros(1, ny);
case 'DiM'
load REF_DiM_2;
REF_DiM_2 = [REF_DiM_2, zeros(5000,24)];
data.REF = REF_DiM_2;
case 'TurboEngine'
data.REF=[1.4, 0, 0];
end
end