-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_optimal_trajectory.m
86 lines (62 loc) · 1.95 KB
/
get_optimal_trajectory.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
function [xHistory, uHistory] = get_optimal_trajectory()
%% create NLMPC controller with appropriate states, outputs and inputs
nx = 4;
ny = 1;
nu = 1;
nlobj = nlmpc(nx, ny, nu);
%Set sampling time, prediction and control horizon
global Ts Duration;
nlobj.Ts = Ts;
nlobj.PredictionHorizon = 100;
nlobj.ControlHorizon = 2;
nlobj.Model.StateFcn = "pendulum_ur_DT0";
nlobj.Model.IsContinuousTime = false;
nlobj.Model.NumberOfParameters = 1;
nlobj.Model.OutputFcn = @(x,u,Ts) [x(3)];
nlobj.Jacobian.OutputFcn = @(x,u,Ts) [0 0 1 0];
nlobj.Weights.OutputVariables = [3];
nlobj.Weights.ManipulatedVariablesRate = 0.1;
%nlobj.OV(1).Min = -10;
%nlobj.OV(1).Max = 10;
%x
global x_bound;
nlobj.States(1).Min = -x_bound;
nlobj.States(1).Max = x_bound;
%x dot
global x_1_bound;
nlobj.States(2).Min = -x_1_bound;
nlobj.States(2).Max = x_1_bound;
global u_bound_opt;
nlobj.MV.Min = -u_bound_opt;
nlobj.MV.Max = u_bound_opt;
global x0;
x = x0;
y = [x(3)];
mv = 0; %manipulated variable
yref = [0]; %cart position and angle
nloptions = nlmpcmoveopt;
nloptions.Parameters = {Ts};
%%
% Run the simulation.
hbar = waitbar(0,'Simulation Progress');
xHistory = x;
uHistory = mv;
for ct = 1:(Duration/Ts)
% Correct previous prediction using current measurement
% Compute optimal control moves
[mv,nloptions,info] = nlmpcmove(nlobj,x,mv,yref,[],nloptions);
% Predict prediction model states for the next iteration
%predict(EKF, [mv; Ts]);
%xk = x;
% Implement first optimal control move and update plant states.
x = pendulum_ur_DT0(x,mv,Ts);
% Generate sensor data with some white noise
%y = x([1 3]) + randn(2,1)*0.01;
y = x([3]);% randn(2,1)*0.01;
% Save plant states for display.
xHistory = [xHistory x]; %#ok<*AGROW>
uHistory = [uHistory mv];
waitbar(ct*Ts/Duration,hbar);
end
uHistory=[uHistory(2:length(uHistory)) 0];
close(hbar);