-
Notifications
You must be signed in to change notification settings - Fork 5
/
pwa_ctrlsyn_Lyap_input_equal.m
279 lines (249 loc) · 8.75 KB
/
pwa_ctrlsyn_Lyap_input_equal.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
% not done. only name changed
function pwactrl = pwa_ctrlsyn_Lyap_input_equal(pwasys, pwactrl, Param)
% PWA Controller design
% PWQ stability analysis
%
% pwasys
% xbarDot = Abar{i}*xbar + Bbar{i}*u
%
% pwasys.Abar = {Abar1, ..., Abarn}
% pwasys.Bbar = {Bbar1, ..., Bbarn}
%
% Polytopic region i: {x| Ebar{i}*x > 0 }
%
%
% Boundary information table:
% Boundary between Ri and Rj : {x| xbar=Fbar{i,j}*sbar }
%
xcl = pwasys.xcl;
xclbar = [xcl;1];
[NR NS] = size(pwasys.Abar); % Number of Systems, Number of Regions
n = size(pwasys.Abar{1},1)-1; % Number of state variables
m = size(pwasys.Bbar{1},2); % Number of inputs
if isfield(Param,'LimitK'), % The upper bound for controller gains
LimitK = Param.LimitK;
end
if isfield(Param,'LimitP'), % The upper bound for Lyapunov matrix P entries
LimitP = Param.LimitP;
end
if isfield(Param,'Lyapunov'), % Type of the Lyapunov function
Lyapunov = Param.Lyapunov;
else
Lyapunov = 'PWQ'; % Piecewise Quadratic Lyapunov functinons are considered by default
end
if isfield(Param,'Poles'), % Desired poles for the linear controller in the center region
Poles = Param.Poles;
elseif isfield(Param,'KLin'), % Gain for the linear controller in the center region
KLin = Param.KLin;
elseif isfield(Param,'QLin'), % Q and R for the LQR controller in the center region
QLin = Param.QLin;
RLin = Param.RLin;
end
if isfield(Param,'alpha'), % VDot <-\alpha V
alpha=Param.alpha;
else
alpha = 0;
end
istar = [];
Abar = pwasys.Abar;
Bbar = pwasys.Bbar;
Ebar = pwasys.Ebar;
Fbar = pwasys.Fbar;
for i=1:NR,
for j=1:NS,
A{i,j} = Abar{i,j}(1:end-1,1:end-1);
a{i,i} = Abar{i,j}(1:end-1,end);
B{i,j} = Bbar{i,j}(1:end-1,:);
end
Echeck{i} = [Ebar{i};zeros(1,n) 1];
xcl_is_inside_Ri = all(Ebar{i}*xclbar>=0-1e-7);
if xcl_is_inside_Ri,
istar = union(istar,i); % Center region(s)
end
end
Lin2PWA = 1;
for i = istar,
if exist('Poles'),
K{i} = -acker(A{i},B{i},Poles);
elseif exist('KLin'),
K{i} = KLin;
elseif exist('QLin'),
% K{i} = -lqr(A{i},B{i},QLin,RLin);
K{i}=robust_LQR(pwasys, pwactrl, Param);
else
Lin2PWA = 0;
end
end
yalmip('clear');
constraints=set([]);
p = size(Ebar{1},1);
if ~isfield(pwactrl,'Pbar'), % Is Lyapunov function given?
% Lyapunov function % No
if strcmp(Lyapunov,'Global'), % Global Lyapunov function
Pg = sdpvar(n,n);
for i=1:NR,
P{i} = Pg;
r{i} = 0;
% Constrainted Pbar
Pbar{i} = [P{i} -P{i}*xcl; -xcl'*P{i} xcl'*P{i}*xcl+r{i}];
end
else, % Piecewise quadratic Lyapunov function
for i=1:NR,
if ismember(i,istar),
P{i} = sdpvar(n,n);
Pbar{i} = [P{i} -P{i}*xcl; -xcl'*P{i} xcl'*P{i}*xcl];
else
Pbar{i} = sdpvar(n+1,n+1);
end
end
end
else % Lyapunov function is given
for i=1:NR,
if ismember(i,istar),
if Lin2PWA, % If there is also a linear controller for the center region, use the controller but not the given Lyapunov function
P{i} = sdpvar(n,n);
Pbar{i} = [P{i} -P{i}*xcl; -xcl'*P{i} xcl'*P{i}*xcl];
else
if iscell(pwactrl.Pbar),
Pbar = pwactrl.Pbar;
P{i} = Pbar{i}(n,n);
else,
Pbar{i} = pwactrl.Pbar;
P{i} = Pbar{i}(n,n);
end
end
else,
if iscell(pwactrl.Pbar),
Pbar{i} = pwactrl.Pbar{i};
else,
Pbar{i} = pwactrl.Pbar;
end
end
end
end
if ~isfield(pwactrl,'Kbar'), % If a controller is given, use it.
for i=1:NR,
Kbar{i} = sdpvar(m,n+1,'full');
end
else,
Kbar = pwactrl.Kbar;
end
%%%%%%%%%%%%%%%%%%%%%%%% The center region %%%%%%%%%%%%%%%%%%%
I = eye(n);
One = zeros(n+1);
One(end,end)=1;
K(istar)
if Lin2PWA,
for i=istar,
k = sdpvar(m,1);
%k=[0 0 ]';
Kbar{i} = [K{i} k];
end
end
for i=istar,
K{i} = Kbar{i}(:,1:n);
for j=1:NS,
% if ~isfield(pwactrl,'Kbar'),
% constraints=constraints+set( (Abar{i,j}+Bbar{i,j}*Kbar{i})*[xcl;1] == 0,['(Abar+Bbar*Kbar)*xclbar=0']); % The desired equilibrium point
% end
if Lin2PWA,
DV{i,j} = Pbar{i}*(Abar{i,j}+Bbar{i,j}*Kbar{i})+(Abar{i,j}+Bbar{i,j}*Kbar{i})'*Pbar{i}; % Don't use alpha for the center region(s) if you already have the controller
else
DV{i,j} = Pbar{i}*(Abar{i,j}+Bbar{i,j}*Kbar{i})+(Abar{i,j}+Bbar{i,j}*Kbar{i})'*Pbar{i}+alpha*Pbar{i};
end
constraints=constraints+set( DV{i,j}(1:n,1:n) < 0,['DV' num2str(i) ',' num2str(j) '<0']); % Vdot(+alpha V)<0
end
if ~isfield(pwactrl,'Pbar'),
constraints=constraints+set(P{i} > 0*I,['P' num2str(i) '>0']);
if exist('LimitP'),
constraints=constraints+set(P{i}(:) < LimitP,['P' num2str(i) '<' num2str(LimitP) '*I']);
end
end
end
for i=1:NR,
%%%%%%%%%%%%%%%%%%%%%%%% Inequality Constraints %%%%%%%%%%%%%%%%%%%
if ~isfield(pwactrl,'Pbar'),
if strcmp(Lyapunov,'PWQ'),
if i~=istar,
Z{i} = sdpvar(p+1,p+1);
%S procedure
constraints=constraints+set(1e2 < Z{i}(:),['Z{' num2str(i) '}(:)>0']);
constraints=constraints+set(Pbar{i} -Echeck{i}'*Z{i}*Echeck{i} > 0,['Pbar' num2str(i) '>0']);
if exist('LimitP'),
constraints=constraints+set(Pbar{i}(:) < LimitP,['Pbar' num2str(i) '<' num2str(LimitP) '*I']);
end
end
end %PWQ%
end
if ~isfield(pwactrl,'Kbar'),
if exist('LimitK'),
if i~=istar | ~Lin2PWA,
constraints=constraints+set(Kbar{i}(:) < LimitK,'Kbar<LimitK');
constraints=constraints+set(Kbar{i}(:) > -LimitK,'Kbar>-LimitK');
end
end %LimitK%
end
%%%%%%%%%%%%%%%%%%%%%%%%% Negative definite %%%%%%%%%%%%%%%%%%%%%%%%%
if i~=istar,
for j=1:NS,
L{i} = sdpvar(p+1,p+1);
%S procedure
constraints=constraints+set(1e2 < L{i}(:),['L{' num2str(i) '}(:)>0']);
DV{i,j} = Pbar{i}*(Abar{i,j}+Bbar{i,j}*Kbar{i})+(Abar{i,j}+Bbar{i,j}*Kbar{i})'*Pbar{i}+alpha*Pbar{i}+Echeck{i}'*L{i}*Echeck{i};
constraints=constraints+set( DV{i,j} < 0,['DV' num2str(i) '-' num2str(j) '<0']);
end
end
%%%%%%%%%%%%%%%%%%%%%%%% Continuity constraints %%%%%%%%%%%%%%%%%%%%%%%%
for j = i:NR,
if ~isempty(Fbar{i,j}),
% Continuity of the control input
if ~isfield(pwactrl,'Kbar'),
constraints=constraints+set((Kbar{i}-Kbar{j})*Fbar{i,j}== 0,['Ki*F' num2str(i) '_' num2str(j) 's=Kis*F' num2str(i) '_' num2str(j) 's']);
end
if ~isfield(pwactrl,'Pbar'),
if strcmp(Lyapunov,'PWQ'),
% Continuity of the Lyapunov function
constraints=constraints+set(Fbar{i,j}'*(Pbar{i}-Pbar{j})*Fbar{i,j} == 0,['F' num2str(i) '_' num2str(j) 's*Pbari*F' num2str(i) '_' num2str(j) 's=F' num2str(i) '_' num2str(j) 's*Pbaris*F' num2str(i) '_' num2str(j) 's']);
end
end
end
end
end
if ~isfield(pwactrl,'Kbar'),
if Lin2PWA,
for i = 1:NR,
if i~=istar,
setsdpvar(Kbar{i}(:,1:end-1),Kbar{istar(1)}(:,1:end-1));
end
end
end
end
% Bita=sdpvar(1);
% constraints =constraints + set(Bita > 0);
% for i=1:NR
% for j=1:NS
% cl_loop_dif_norm{i,j}= norm(Abar{i,j}+Bbar{i,j}*Kbar{i}-(Abar{istar,j}+Bbar{istar,j}*Kbar{istar}));
% constraints=constraints+ set(cl_loop_dif_norm{i,j}< Bita);
% end
% end
obj = [];
% for i=1:NR,
% obj = obj+norm(Kbar{i});
% end
solvesdp(constraints,obj,sdpsettings('usex0',1, 'shift', 1e-7))
%solvesdp(constraints)
checkset(constraints)
pwactrl.xcl = xcl;
pwactrl.istar = istar;
for i=1:NR,
pwactrl.Kbar{i} = double(Kbar{i});
%pwactrl.L{i}=double(L{i});
try,
pwactrl.Pbar{i} = double(Pbar{i});
end
end
pwactrl.Index = pwasys.Index;
pwactrl.X = pwasys.X;
pwactrl.T = pwasys.T;
pwactrl.xcl = pwasys.xcl;
pwactrl.Ebar = pwasys.Ebar;