|
21 | 21 | param.Wmax=shape.Wmax;
|
22 | 22 | param.Tf=shape.Tf;
|
23 | 23 | param.angleConstraint=2.5*pi/180;
|
24 |
| -param.Ts = 0.1; |
25 | 24 | param.N=20;
|
26 |
| - |
| 25 | +param.Ts = param.Tf/param.N |
| 26 | +param.tolerances=shape.tolerances.state(1:8)*0.5; |
27 | 27 | param.nx = 8;
|
28 | 28 | param.ny = 8;
|
29 | 29 | param.nu = 2;
|
|
33 | 33 | param.nlobj.Model.StateFcn = @(x,u) crane_nl_model_mod(x, u, Tx, Ty ,Tl ,Vx ,Vy ,MR ,M ,m, r, Vl);
|
34 | 34 | param.nlobj.Ts = param.Ts;
|
35 | 35 | param.nlobj.PredictionHorizon = param.N;
|
36 |
| -param.nlobj.ControlHorizon = 20; |
37 |
| -param.nlobj.Weights.OutputVariables = [10 0 10 0 1 0 1 0]; |
| 36 | +param.nlobj.ControlHorizon = param.N; |
| 37 | +param.nlobj.Weights.OutputVariables = [10 0 10 0 5 0 5 0]; |
| 38 | + |
38 | 39 | % Add constraints on inputs
|
39 | 40 | for ct = 1:param.nu
|
40 | 41 | param.nlobj.MV(ct).Min = -1;
|
41 | 42 | param.nlobj.MV(ct).Max = 1;
|
42 | 43 | end
|
| 44 | +% param.nlobj.Optimization.ReplaceStandardCost=true; |
| 45 | +param.nlobj.Optimization.CustomIneqConFcn = @(X,U,e,data)myIneqConFunction(X,U,e,data,shape.constraints,r,param); |
| 46 | +% param.nlobj.Optimization.CustomCostFcn = @myCostFunction; |
| 47 | +param.nloptions = nlmpcmoveopt; |
| 48 | +pos = 1; vel = 100; angle = 0 ; angle_vel = 2; |
| 49 | + |
| 50 | +Q = diag([pos vel pos vel angle angle_vel angle angle_vel]); |
| 51 | +%Q = [pos vel pos vel angle angle_vel angle angle_vel]; |
| 52 | + |
| 53 | +%R = [2 2]; |
| 54 | +R = diag([1 1]); |
43 | 55 |
|
| 56 | +J = 0; |
| 57 | +%% Input rate |
| 58 | +% nlobj.Weights.ManipulatedVariablesRate = [0 0]; |
| 59 | +costFun_k = @(x,u) x'*Q*x + u'*R*u + J*(max(0,u(1)*x(2)) + max(0,u(2)*x(3))); |
44 | 60 |
|
| 61 | +% nlobj.Weights.OutputVariables = Q; |
| 62 | +% nlobj.Weights.ManipulatedVariables = R ; |
45 | 63 |
|
46 | 64 |
|
47 |
| -param.nlobj.Optimization.CustomIneqConFcn = @(X,U,e,data)myIneqConFunction(X,U,e,data,shape.constraints,r); |
| 65 | +nlobj.Optimization.CustomCostFcn = @(x,u,e,data) genCostFun(x,u,e,data,costFun_k); |
| 66 | + |
48 | 67 |
|
49 | 68 | x0 = [-10;0;-10; 0; 0; 0; 0; 0]; % robot parks at [-10, -10], facing north
|
50 | 69 | u0 = zeros(param.nu,1); % thrust is zero
|
|
87 | 106 | %% Modify the following function for your controller
|
88 | 107 | function u = myMPController(r, x_hat, param)
|
89 | 108 | %% Do not delete this line
|
90 |
| -u=zeros(param.numInputs,1); |
| 109 | +u=zeros(param.nu,1); |
91 | 110 | % Create the output array of the appropriate size
|
92 |
| -persistent it lastMv |
| 111 | +persistent it lastMv options |
| 112 | + |
93 | 113 | if isempty(it)
|
| 114 | + options = nlmpcmoveopt; |
94 | 115 | it=0;
|
95 | 116 | lastMv = zeros(param.nu,1);
|
96 | 117 | end
|
97 | 118 |
|
98 |
| -if (it<param.N) |
99 |
| - N=param.N-it; |
100 |
| - u = nlmpcmove(param.nlobj,x_hat,lastMv,param.targetMod.') |
| 119 | +% if (it>1) |
| 120 | +% param.nlobj.Optimization.UseSuboptimalSolution=true; |
| 121 | +% param.nlobj.Optimization.SolverOptions.MaxIter=2; |
| 122 | +% end |
| 123 | + |
| 124 | +if (it<param.N-1) |
| 125 | + param.nlobj.PredictionHorizon = param.N-it; |
| 126 | + param.nlobj.ControlHorizon = param.N-it; |
| 127 | + [u,options,info] = nlmpcmove(param.nlobj,x_hat,lastMv,param.targetMod.',[],options); |
| 128 | + conv=info.Iterations |
101 | 129 | it=it+1;
|
102 | 130 | lastMv=u;
|
103 | 131 | end
|
|
175 | 203 | end
|
176 | 204 |
|
177 | 205 |
|
178 |
| -function cineq = myIneqConFunction(X,U,e,data,constraints,r) |
| 206 | + |
| 207 | +function J = myCostFunction(X,U,e,data) |
| 208 | +% xWork=max(U(1:end-1,data.MVIndex(1)).*X(2:end,2),0); |
| 209 | +% yWork=max(U(1:end-1,data.MVIndex(2)).*X(2:end,4),0);[10 0 10 0 5 0 5 0] |
| 210 | +Q=[10 0 10 0 5 0 5 0]; |
| 211 | +R=[1 1]; |
| 212 | +J=max(0,U(1:end-1,1)*X(2:end)) + max(0,U(1:end-1,1)*X(4)); |
| 213 | +J=0 |
| 214 | +end |
| 215 | + |
| 216 | +function [ costFun ] = genCostFun(x,u,e,data,costFunk) |
| 217 | +p = data.PredictionHorizon; |
| 218 | +costFun = []; |
| 219 | + |
| 220 | +for i = 2:p+1 |
| 221 | + xk = x(i,:); |
| 222 | + uk = u(i,:); |
| 223 | + costFun = costFunk(xk',uk'); |
| 224 | +end |
| 225 | +end |
| 226 | + |
| 227 | +% costFun_k = @(x,u) x'*Q*x + u'*R*u + J*(max(0,u(1)*x(1)) + max(0,u(2)*x(3))); |
| 228 | + |
| 229 | +function cineq = myIneqConFunction(X,U,e,data,constraints,r,param) |
179 | 230 |
|
180 | 231 | p = data.PredictionHorizon;
|
181 | 232 | posX = X(2:p+1,1);
|
182 | 233 | posY = X(2:p+1,3);
|
183 | 234 | angleX = X(2:p+1,5);
|
184 | 235 | angleY = X(2:p+1,7);
|
| 236 | +stepsLeft = size(X,1); |
185 | 237 |
|
186 | 238 | [DRect,clRect,chRect] = getRectConstraints(constraints.rect);
|
187 | 239 |
|
|
192 | 244 | posX*DRect(2,1)+posY*DRect(2,2)-chRect(2);
|
193 | 245 | -posX*DRect(2,1)-posY*DRect(2,2)+clRect(2);
|
194 | 246 | % Constraints on Load
|
195 |
| - DRect(1,1)*(posX+r*angleX) + DRect(1,2)*(posY+r*angleY)-chRect(1); |
| 247 | + DRect(1,1)*(posX+r*angleX) + DRect(1,2)*(posX+r*angleX)-chRect(1); |
196 | 248 | -DRect(1,1)*(posX+r*angleX) - DRect(1,2)*(posY+r*angleY)+clRect(1);
|
197 | 249 | DRect(2,1)*(posX+r*angleX) + DRect(2,2)*(posY+r*angleY)-chRect(2);
|
198 | 250 | -DRect(2,1)*(posX+r*angleX) - DRect(2,2)*(posY+r*angleY)+clRect(2);
|
199 | 251 | ];
|
200 |
| - for i = 1:length(constraints.ellipses) |
| 252 | +% Constraints on cart for elipsis |
| 253 | + for i = 1:length(constraints.ellipses) |
201 | 254 | el=constraints.ellipses{i};
|
202 |
| - elCon=-(((posX-el.xc)/el.a).^2 + ((posY-el.yc)/el.b).^2) +1; |
203 |
| - cineq = [cineq; elCon]; |
204 |
| - end |
205 |
| - end |
| 255 | + elCon=-((((posX)-el.xc)/el.a).^2 + (((posY)-el.yc)/el.b).^2) +1.05; |
| 256 | + elConLd=-((((posX+r*angleX)-el.xc)/el.a).^2 + (((posY+r*angleY)-el.yc)/el.b).^2) +1.05; |
| 257 | + cineq = [cineq; elCon; elConLd]; |
| 258 | + end |
| 259 | + |
| 260 | +for i= 1:min(5,stepsLeft-1) |
| 261 | + xFin=X(p+2-i,:); |
| 262 | + cineq=[ cineq; |
| 263 | + xFin' - param.targetMod - param.tolerances; |
| 264 | + -xFin' + param.targetMod - param.tolerances; |
| 265 | + xFin(1) + r*xFin(5) - param.target(1) - param.tolerances(1); |
| 266 | + -xFin(1) - r*xFin(5) + param.target(1) - param.tolerances(1); |
| 267 | + xFin(3) + r*xFin(7) - param.target(2) - param.tolerances(3); |
| 268 | + - xFin(3) - r*xFin(7) + param.target(2) - param.tolerances(3) |
| 269 | + ]; |
| 270 | +end |
| 271 | + |
| 272 | +end |
206 | 273 |
|
207 | 274 |
|
208 | 275 |
|
0 commit comments