-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPC_init.m
434 lines (409 loc) · 14 KB
/
MPC_init.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
disp('===================================================================');
disp('This framework generates JavaScript code to run on Nodejs platform');
disp('The framework uses the State-space model of the system to generate the MPC');
disp('===================================================================');
ss_sys = input('Please, Enter the State-space model of the system: ');
Ts = input('Enter the value of the sampling time (Ts): ');
u_min = input('Set the lower bounds on the Manipulated Variables (MV): ');
u_max = input('Set the upper bounds on the Manipulated Variables (MV): ');
delta_U_min = input('Set the lower bounds on the interval-to-interval change for the MV: ');
delta_U_max = input('Set the upper bounds on the interval-to-interval change for the MV: ');
y_min = input('Set the lower bounds on the Output Variables (OV): ');
y_max = input('Set the upper bounds on the Output Variables (OV): ');
Np = input('Enter the value of Prediction Horizon (Np): ');
Nc = input('Enter the value of Control Horizon (Nc): ');
ref = input('Enter the refernce (Set-point): ');
Y_N = input('Do you run your system on Intel Galileo Development Board?(y/n) ','s');
if Y_N == 'y'
dpYN = input ('will you use digital pins? (y/n) ','s');
if dpYN == 'y'
disp('Choose the digital pins that you used');
dp_num = input('How many digital pins will you use? (1-14)');
if dp_num > 0 && dp_num <= 13
pin = 1;
while pin <= dp_num
fprintf('Enter the pin number %d : (0-13) ', pin);
d_pin(pin) = input('');
pin = pin +1;
end
else
disp('!!Undefined pin on the board!!');
end
elseif dpYN == 'n'
disp('You did not select any digital pins!! ');
d_pin = 6;
else
disp('!!Undefined input!!');
d_pin = 6;
end
apYN = input ('will you use analog pins? (y/n) ','s');
if apYN == 'y'
disp('Choose the analog pins that you used');
ap_num = input('How many analog pins will you use? (1-6)');
if ap_num > 0 && ap_num <= 5
pin = 1;
while pin <= ap_num
fprintf('Enter the pin number %d : (0-5) ', pin);
a_pin(pin) = input('');
pin = pin +1;
end
else
disp('!!Undefined pin on the board!!');
end
elseif apYN == 'n'
disp('You did not select any analog pins!! ');
a_pin = 0;
else
disp('!!Undefined input!!');
a_pin = 0;
end
kFilter = input('Do you want to use Kalamn Filter?(y/n) ','s');
if kFilter == 'y'
varProcess = input('Enter the value of the process variance (Q): ');
varVolt = input('Enter the value of the variance of sensor measuerments (R): ');
elseif kFilter == 'n'
disp('You did not use Kalman Filter!! ');
varProcess = 1e-7;
varVolt = 1.12e-5;
else
disp('!!Undefined input!!');
varProcess = 1e-7;
varVolt = 1.12e-5;
kFilter = 'n';
end
elseif Y_N == 'n'
disp('You did not select any digital or analog pins!! ');
Y_N = 'n';
KFilter = 'n';
d_pin = 6;
a_pin = 0;
varProcess = 1e-7;
varVolt = 1.12e-5;
else
disp('!!Undefined input!!');
Y_N = 'n';
KFilter = 'n';
d_pin = 6;
a_pin = 0;
varProcess = 1e-7;
varVolt = 1.12e-5;
end
[Gs,TT]= ssbal(ss_sys);
Gsd = c2d(Gs,Ts);
A = Gsd.A;
B = Gsd.B;
C = Gsd.C;
n = length(A);
k = size(B,2);
m = size(C,1);
if Y_N == 'y' && KFilter == 'y'
% Generating JavaScript file
fileID = fopen('main.js','w');
% Write comments at the top of js file
fprintf(fileID,'%s \n','// Real-time MPC controller on Galileo with Kalman Filter');
fprintf(fileID,'%s \n','//=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=//');
fprintf(fileID,'%s \n','// Initializing of MPC ');
% Matrix Ap
fprintf(fileID,'%s','var Ap = [[');
for r = 1:n
for c = 1:n
fprintf(fileID,'%4.4f',A(r,c));
if c < n
fprintf(fileID,'%s',' , ');
end
end
if r < n
fprintf(fileID,'%s','], [');
end
end
fprintf(fileID,'%s\n',']];');
% Matrix Bp
fprintf(fileID,'%s','var Bp = [[');
for r = 1:n
for c = 1:k
fprintf(fileID,'%4.4f',B(r,c));
if c < k
fprintf(fileID,'%s',' , ');
end
end
if r < n
fprintf(fileID,'%s','], [');
end
end
fprintf(fileID,'%s\n',']];');
% Matrix C
fprintf(fileID,'%s','var Cp = [[');
for r = 1:m
for c = 1:n
fprintf(fileID,'%4.4f',C(r,c));
if c < n
fprintf(fileID,'%s',' , ');
end
end
if r < m
fprintf(fileID,'%s','], [');
end
end
fprintf(fileID,'%s\n',']];');
% Rest constants
fprintf(fileID,'%s %d %s\n','var n =',n, ';');
fprintf(fileID,'%s %d %s\n','var k =',k, ';');
fprintf(fileID,'%s %d %s\n','var m =',m, ';');
fprintf(fileID,'%s %d %s\n','var Np =',Np, ';');
fprintf(fileID,'%s %d %s\n','var Nc =',Nc, ';');
fprintf(fileID,'%s %d %s\n','var u_min =',u_min, ';');
fprintf(fileID,'%s %d %s\n','var u_max =',u_max, ';');
fprintf(fileID,'%s %d %s\n','var delta_U_min =',delta_U_min, ';');
fprintf(fileID,'%s %d %s\n','var delta_U_max =',delta_U_max, ';');
fprintf(fileID,'%s %d %s\n','var y_min =',y_min, ';');
fprintf(fileID,'%s %d %s\n','var y_max =',y_max, ';');
fprintf(fileID,'%s %d %s\n','var ref =',ref, ';');
for i = 1:length(d_pin)
fprintf(fileID,'%s%d %s %d %s\n','var pwmPin',i,'=',d_pin(i), ';');
end
for i = 1:length(a_pin)
fprintf(fileID,'%s%d %s %d %s\n','var SensorPin',i,'=',a_pin(i), ';');
end
fprintf(fileID,'%s %d %s\n','var varProcess =',varProcess, ';');
fprintf(fileID,'%s %d %s\n','var varVolt =',varVolt, ';');
fclose(fileID);
% append the mpc_init file with the main file
fr = fopen( 'mpc_galileo_filter.js', 'rt' );
fw = fopen( 'main.js', 'at' );
while feof( fr ) == 0
tline = fgetl( fr );
fwrite( fw, sprintf('%s\n',tline ) );
end
fclose(fr);
fclose(fw);
elseif Y_N == 'n' && KFilter == 'n'
% Generating JavaScript file
fileID = fopen('main.js','w');
% Write comments at the top of js file
fprintf(fileID,'%s \n','// Real-time MPC controller');
fprintf(fileID,'%s \n','//=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=//');
fprintf(fileID,'%s \n','// Initializing of MPC ');
% Matrix Ap
fprintf(fileID,'%s','var Ap = [[');
for r = 1:n
for c = 1:n
fprintf(fileID,'%4.4f',A(r,c));
if c < n
fprintf(fileID,'%s',' , ');
end
end
if r < n
fprintf(fileID,'%s','], [');
end
end
fprintf(fileID,'%s\n',']];');
% Matrix Bp
fprintf(fileID,'%s','var Bp = [[');
for r = 1:n
for c = 1:k
fprintf(fileID,'%4.4f',B(r,c));
if c < k
fprintf(fileID,'%s',' , ');
end
end
if r < n
fprintf(fileID,'%s','], [');
end
end
fprintf(fileID,'%s\n',']];');
% Matrix C
fprintf(fileID,'%s','var Cp = [[');
for r = 1:m
for c = 1:n
fprintf(fileID,'%4.4f',C(r,c));
if c < n
fprintf(fileID,'%s',' , ');
end
end
if r < m
fprintf(fileID,'%s','], [');
end
end
fprintf(fileID,'%s\n',']];');
% Rest constants
fprintf(fileID,'%s %d %s\n','var n =',n, ';');
fprintf(fileID,'%s %d %s\n','var k =',k, ';');
fprintf(fileID,'%s %d %s\n','var m =',m, ';');
fprintf(fileID,'%s %d %s\n','var Np =',Np, ';');
fprintf(fileID,'%s %d %s\n','var Nc =',Nc, ';');
fprintf(fileID,'%s %d %s\n','var u_min =',u_min, ';');
fprintf(fileID,'%s %d %s\n','var u_max =',u_max, ';');
fprintf(fileID,'%s %d %s\n','var delta_U_min =',delta_U_min, ';');
fprintf(fileID,'%s %d %s\n','var delta_U_max =',delta_U_max, ';');
fprintf(fileID,'%s %d %s\n','var y_min =',y_min, ';');
fprintf(fileID,'%s %d %s\n','var y_max =',y_max, ';');
fprintf(fileID,'%s %d %s\n','var ref =',ref, ';');
fclose(fileID);
% append the mpc_init file with the main file
fr = fopen( 'mpc.js', 'rt' );
fw = fopen( 'main.js', 'at' );
while feof( fr ) == 0
tline = fgetl( fr );
fwrite( fw, sprintf('%s\n',tline ) );
end
fclose(fr);
fclose(fw);
elseif Y_N == 'y'
% Generating JavaScript file
fileID = fopen('main.js','w');
% Write comments at the top of js file
fprintf(fileID,'%s \n','// Real-time MPC controller on Galileo');
fprintf(fileID,'%s \n','//=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=//');
fprintf(fileID,'%s \n','// Initializing of MPC ');
% Matrix Ap
fprintf(fileID,'%s','var Ap = [[');
for r = 1:n
for c = 1:n
fprintf(fileID,'%4.4f',A(r,c));
if c < n
fprintf(fileID,'%s',' , ');
end
end
if r < n
fprintf(fileID,'%s','], [');
end
end
fprintf(fileID,'%s\n',']];');
% Matrix Bp
fprintf(fileID,'%s','var Bp = [[');
for r = 1:n
for c = 1:k
fprintf(fileID,'%4.4f',B(r,c));
if c < k
fprintf(fileID,'%s',' , ');
end
end
if r < n
fprintf(fileID,'%s','], [');
end
end
fprintf(fileID,'%s\n',']];');
% Matrix C
fprintf(fileID,'%s','var Cp = [[');
for r = 1:m
for c = 1:n
fprintf(fileID,'%4.4f',C(r,c));
if c < n
fprintf(fileID,'%s',' , ');
end
end
if r < m
fprintf(fileID,'%s','], [');
end
end
fprintf(fileID,'%s\n',']];');
% Rest constants
fprintf(fileID,'%s %d %s\n','var n =',n, ';');
fprintf(fileID,'%s %d %s\n','var k =',k, ';');
fprintf(fileID,'%s %d %s\n','var m =',m, ';');
fprintf(fileID,'%s %d %s\n','var Np =',Np, ';');
fprintf(fileID,'%s %d %s\n','var Nc =',Nc, ';');
fprintf(fileID,'%s %d %s\n','var u_min =',u_min, ';');
fprintf(fileID,'%s %d %s\n','var u_max =',u_max, ';');
fprintf(fileID,'%s %d %s\n','var delta_U_min =',delta_U_min, ';');
fprintf(fileID,'%s %d %s\n','var delta_U_max =',delta_U_max, ';');
fprintf(fileID,'%s %d %s\n','var y_min =',y_min, ';');
fprintf(fileID,'%s %d %s\n','var y_max =',y_max, ';');
fprintf(fileID,'%s %d %s\n','var ref =',ref, ';');
for i = 1:length(d_pin)
fprintf(fileID,'%s%d %s %d %s\n','var pwmPin',i,'=',d_pin(i), ';');
end
for i = 1:length(a_pin)
fprintf(fileID,'%s%d %s %d %s\n','var SensorPin',i,'=',a_pin(i), ';');
end
fclose(fileID);
% append the mpc_init file with the main file
fr = fopen( 'mpc_galileo_filter.js', 'rt' );
fw = fopen( 'main.js', 'at' );
while feof( fr ) == 0
tline = fgetl( fr );
fwrite( fw, sprintf('%s\n',tline ) );
end
fclose(fr);
fclose(fw);
else
% Generating JavaScript file
fileID = fopen('main.js','w');
% Write comments at the top of js file
fprintf(fileID,'%s \n','// Real-time MPC controller on Galileo with Kalman Filter');
fprintf(fileID,'%s \n','//=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=//');
fprintf(fileID,'%s \n','// Initializing of MPC ');
% Matrix Ap
fprintf(fileID,'%s','var Ap = [[');
for r = 1:n
for c = 1:n
fprintf(fileID,'%4.4f',A(r,c));
if c < n
fprintf(fileID,'%s',' , ');
end
end
if r < n
fprintf(fileID,'%s','], [');
end
end
fprintf(fileID,'%s\n',']];');
% Matrix Bp
fprintf(fileID,'%s','var Bp = [[');
for r = 1:n
for c = 1:k
fprintf(fileID,'%4.4f',B(r,c));
if c < k
fprintf(fileID,'%s',' , ');
end
end
if r < n
fprintf(fileID,'%s','], [');
end
end
fprintf(fileID,'%s\n',']];');
% Matrix C
fprintf(fileID,'%s','var Cp = [[');
for r = 1:m
for c = 1:n
fprintf(fileID,'%4.4f',C(r,c));
if c < n
fprintf(fileID,'%s',' , ');
end
end
if r < m
fprintf(fileID,'%s','], [');
end
end
fprintf(fileID,'%s\n',']];');
% Rest constants
fprintf(fileID,'%s %d %s\n','var n =',n, ';');
fprintf(fileID,'%s %d %s\n','var k =',k, ';');
fprintf(fileID,'%s %d %s\n','var m =',m, ';');
fprintf(fileID,'%s %d %s\n','var Np =',Np, ';');
fprintf(fileID,'%s %d %s\n','var Nc =',Nc, ';');
fprintf(fileID,'%s %d %s\n','var u_min =',u_min, ';');
fprintf(fileID,'%s %d %s\n','var u_max =',u_max, ';');
fprintf(fileID,'%s %d %s\n','var delta_U_min =',delta_U_min, ';');
fprintf(fileID,'%s %d %s\n','var delta_U_max =',delta_U_max, ';');
fprintf(fileID,'%s %d %s\n','var y_min =',y_min, ';');
fprintf(fileID,'%s %d %s\n','var y_max =',y_max, ';');
fprintf(fileID,'%s %d %s\n','var ref =',ref, ';');
for i = 1:length(d_pin)
fprintf(fileID,'%s%d %s %d %s\n','var pwmPin',i,'=',d_pin(i), ';');
end
for i = 1:length(a_pin)
fprintf(fileID,'%s%d %s %d %s\n','var SensorPin',i,'=',a_pin(i), ';');
end
fprintf(fileID,'%s %d %s\n','var varProcess =',varProcess, ';');
fprintf(fileID,'%s %d %s\n','var varVolt =',varVolt, ';');
fclose(fileID);
% append the mpc_init file with the main file
fr = fopen( 'mpc_galileo_filter.js', 'rt' );
fw = fopen( 'main.js', 'at' );
while feof( fr ) == 0
tline = fgetl( fr );
fwrite( fw, sprintf('%s\n',tline ) );
end
fclose(fr);
fclose(fw);
end