Skip to content

Commit dc30987

Browse files
committed
commit before it's too late
1 parent 461dadb commit dc30987

File tree

5 files changed

+131
-266
lines changed

5 files changed

+131
-266
lines changed

FunctionTemplate.asv

Lines changed: 0 additions & 222 deletions
This file was deleted.

FunctionTemplate.m

Lines changed: 84 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
param.Wmax=shape.Wmax;
2222
param.Tf=shape.Tf;
2323
param.angleConstraint=2.5*pi/180;
24-
param.Ts = 0.1;
2524
param.N=20;
26-
25+
param.Ts = param.Tf/param.N
26+
param.tolerances=shape.tolerances.state(1:8)*0.5;
2727
param.nx = 8;
2828
param.ny = 8;
2929
param.nu = 2;
@@ -33,18 +33,37 @@
3333
param.nlobj.Model.StateFcn = @(x,u) crane_nl_model_mod(x, u, Tx, Ty ,Tl ,Vx ,Vy ,MR ,M ,m, r, Vl);
3434
param.nlobj.Ts = param.Ts;
3535
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+
3839
% Add constraints on inputs
3940
for ct = 1:param.nu
4041
param.nlobj.MV(ct).Min = -1;
4142
param.nlobj.MV(ct).Max = 1;
4243
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]);
4355

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)));
4460

61+
% nlobj.Weights.OutputVariables = Q;
62+
% nlobj.Weights.ManipulatedVariables = R ;
4563

4664

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+
4867

4968
x0 = [-10;0;-10; 0; 0; 0; 0; 0]; % robot parks at [-10, -10], facing north
5069
u0 = zeros(param.nu,1); % thrust is zero
@@ -87,17 +106,26 @@
87106
%% Modify the following function for your controller
88107
function u = myMPController(r, x_hat, param)
89108
%% Do not delete this line
90-
u=zeros(param.numInputs,1);
109+
u=zeros(param.nu,1);
91110
% Create the output array of the appropriate size
92-
persistent it lastMv
111+
persistent it lastMv options
112+
93113
if isempty(it)
114+
options = nlmpcmoveopt;
94115
it=0;
95116
lastMv = zeros(param.nu,1);
96117
end
97118

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
101129
it=it+1;
102130
lastMv=u;
103131
end
@@ -175,13 +203,37 @@
175203
end
176204

177205

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)
179230

180231
p = data.PredictionHorizon;
181232
posX = X(2:p+1,1);
182233
posY = X(2:p+1,3);
183234
angleX = X(2:p+1,5);
184235
angleY = X(2:p+1,7);
236+
stepsLeft = size(X,1);
185237

186238
[DRect,clRect,chRect] = getRectConstraints(constraints.rect);
187239

@@ -192,17 +244,32 @@
192244
posX*DRect(2,1)+posY*DRect(2,2)-chRect(2);
193245
-posX*DRect(2,1)-posY*DRect(2,2)+clRect(2);
194246
% 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);
196248
-DRect(1,1)*(posX+r*angleX) - DRect(1,2)*(posY+r*angleY)+clRect(1);
197249
DRect(2,1)*(posX+r*angleX) + DRect(2,2)*(posY+r*angleY)-chRect(2);
198250
-DRect(2,1)*(posX+r*angleX) - DRect(2,2)*(posY+r*angleY)+clRect(2);
199251
];
200-
for i = 1:length(constraints.ellipses)
252+
% Constraints on cart for elipsis
253+
for i = 1:length(constraints.ellipses)
201254
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
206273

207274

208275

0 commit comments

Comments
 (0)