-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
200 lines (182 loc) · 4.64 KB
/
main.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
%%%
%%% Setup environment
clc;
clear all;
addpath("./Functions")
%% Simulation parameters
dt = 0.01; % Time step
step = cast(1/dt, 'uint32') / 10; % Animation time step
T = 50.0; % Total time
x0 = [0, 0, pi, 0]; % Initial condition
tspan = 0:dt:T; % Time instants
%% Plant parameters nominal
paramsN.M = 11.0; % Cart mass
paramsN.m = 6.0; % Pendulum mass
paramsN.L = 4.0; % Rod length
paramsN.g = 9.81; % Gravity acceleration
paramsN.k = 0.0; % Elastic coefficient
paramsN.c = 0.0; % Friction force
%% Plant paramters real
paramsR.M = 11.0; % Cart mass
paramsR.m = 6.0; % Pendulum mass
paramsR.L = 4.0; % Rod length
paramsR.g = 9.81; % Gravity acceleration
paramsR.k = 0.0; % Elastic coefficient
paramsR.c = 0.0; % Friction force
%% Example simulation
% u = @(t) feedForwardAction(t, params);
% f = @(t, x) dynamics(x, u(t), params);
%
% x0 = [0, 0, -pi, 0];
% tspan = 0:dt:T;
% [t, xx] = ode45(f, tspan, x0);
%% Example simulation euler
% xx(:, 1) = x0';
% for tt=1:length(tspan)-1
% u = feedForwardAction(tspan(tt), paramsN);
% xx(:, tt+1) = xx(:, tt) + dynamics(xx(:, tt), u, paramsR)*dt;
% end
%
% xx = xx';
%% Example place
u_ff = feedForwardAction(tspan(1), paramsN);
[y, dy, ~] = trajectory(tspan(1));
y = wrapTo2Pi(y);
x_ = feedForwardState(y, dy);
x_(3) = wrapTo2Pi(x_(3));
A_ = A(x_, u_ff, paramsN);
B_ = B(x_, paramsN);
C_ = C();
B_ = B_(3:4);
A_ = A_(3:4, 3:4);
C_ = C_(3:4);
AA = [A_, zeros(2,1); C_, 0];
BB = [B_; 0];
poles = [-4, -5, -2];
K = place(AA, -BB, poles);
Kx = K(1:2);
Ks = K(end);
xx(:, 1) = x0;
sigma(1) = 1/Ks * ( u_ff' - Kx * x_(3:4)' );
for tt=1:length(tspan)-1
xx(3, tt) = wrapTo2Pi(xx(3, tt));
u_ff = feedForwardAction(tspan(tt), paramsN);
[y, dy, ~] = trajectory(tspan(tt));
y = wrapTo2Pi(y);
yy(tt) = y;
x_ = feedForwardState(y, dy);
x_(3) = wrapTo2Pi(x_(3));
A_ = A(x_, u_ff, paramsN);
B_ = B(x_, paramsN);
C_ = C();
B_ = B_(3:4);
A_ = A_(3:4, 3:4);
C_ = C_(3:4);
AA = [A_, zeros(2,1); C_, 0];
BB = [B_; 0];
aux = wrapTo2Pi(xx(3, tt));
if ( abs(pi/2 - aux) < 0.01 || abs(-pi/2 - aux) < 0.01 )
u = u_ff;
else
poles = [-7, -5, -2];
K = place(AA, -BB, poles);
Kx = K(1:2);
Ks = K(end);
u = Kx*(xx(3:4, tt)) + Ks*sigma(tt);
end
xx(:, tt+1) = xx(:, tt) + dynamics(xx(:, tt), u_ff, paramsR)*dt;
sigma(tt+1) = sigma(tt) + (xx(3, tt) - y)*dt;
end
xx = xx';
%% Example steady state
% xx(:, 1) = x0;
% y = x0(3);
%
% x_ = [0; 0; y; 0];
% u_ff = uStar(y, paramsN);
% A_ = A(x_, u_ff, paramsN);
% B_ = B(x_, paramsN);
% C_ = C();
% B_ = B_(3:4);
% A_ = A_(3:4, 3:4);
% C_ = C_(3:4);
% AA = [A_, zeros(2,1); C_, 0];
% BB = [B_; 0];
% poles = [-4, -5, -2];
% K = place(AA, -BB, poles);
% Kx = K(1:2);
% Ks = K(end);
%
% xx(:, 1) = x0;
% sigma(1) = 1/Ks * ( u_ff' - Kx * x_(3:4) );
%
%
% y = y - 0.001;
% for tt=1:length(tspan)-1
% xx(3, tt) = wrapTo2Pi(xx(3, tt));
% u_ff = uStar(y, paramsN);
% x_ = [0; 0; y; 0];
% A_ = A(x_, u_ff, paramsN);
% B_ = B(x_, paramsN);
% C_ = C();
% B_ = B_(3:4);
% A_ = A_(3:4, 3:4);
% C_ = C_(3:4);
%
% AA = [A_, zeros(2,1); C_, 0];
% BB = [B_; 0];
% poles = [-7, -5, -2];
% K = place(AA, -BB, poles);
% Kx = K(1:2);
% Ks = K(end);
% u = Kx*(xx(3:4, tt)) + Ks*sigma(tt);
%
% xx(:, tt+1) = xx(:, tt) + dynamics(xx(:, tt), u, paramsR)*dt;
% sigma(tt+1) = sigma(tt) + (xx(3, tt) - y)*dt;
%
% y = y - 0.01;
% if (y < 0)
% y = 0;
% end
% end
%
% xx = xx';
%% Debug
% x = [0.0; 0.0; -pi/2; 0];
% u = 0;
%
% B_ = B(x, paramsN);
% A_ = A(x, u, paramsN);
% C_ = C();
%
% % Check controllability/observability properties
% Co = ctrb(A_, B_);
% Oo = obsv(A_, C_);
% rank(Co)
% rank(Oo)
% rank([-A_, B_; C_, 0])
%
% % Notice that the linearization is completely controllable, but
% % it has unosservable dynamics
%% Cart plot
% figure(1)
% xaxis([-10, 10])
% yaxis([-10, 10])
% hold on
% plotCart([0, 0, 0, 0], params)
% hold off
fig = figure(1);
for ii = 1:step:length(tspan)
clf(fig)
xaxis([-10 + xx(ii,1), 10 + xx(ii,1)])
yaxis([-10, 10])
hold on
plotCart(xx(ii, :), paramsR)
hold off
drawnow
end
% plot(yy)
% hold on
% plot(xx(:, 3))
% hold off
% legend('y', 'x')