Skip to content

Commit b10626f

Browse files
committed
changed truncation direction
1 parent ff0c837 commit b10626f

File tree

8 files changed

+50
-29
lines changed

8 files changed

+50
-29
lines changed

FunctionTemplate.asv

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,34 @@ param.K = [2, 0, 0, 0, 0, 0, 0, 0;
88
% This is a sample way to send reference points
99
param.xTar = shape.target(1);
1010
param.yTar = shape.target(2);
11+
param.target=shape.target;r = zeros(8,1);
1112

12-
param.target=shape.target;
13+
% Make the crane go to (xTar, yTar)
14+
r=zeros(8,1); r(1,1) = param.xTar; r(3,1) = param.yTar;
15+
param.targetMod=r;
1316
param.start=shape.start;
1417
param.constraints=shape.constraints;
1518
param.eps_r=shape.eps_r;
1619
param.eps_t=shape.eps_t;
1720
param.Wmax=shape.Wmax;
1821
param.Tf=shape.Tf;
1922
param.angleConstraint=3*pi/180;
20-
param.Ts = 0.1;
23+
param.Ts = 0.05;
2124
param.Tf=shape.Tf;
2225
param.useShrinkingHorizon=true;
2326
param.N=ceil(param.Tf/param.Ts)
27+
2428
% Declare penalty matrices:
25-
Qt = [100 10 100 10 100 10 100 10];
29+
Qt = [100 1 100 1 100 10 100 10];
2630
Pt = [100 10 100 10 100 10 100 10];
2731
Rt = [1 1];
2832

2933
param.Q = 1*diag(Qt);
3034
param.P = 100*diag(Pt);
3135
param.R = 1*diag(Rt);
36+
param.numStates=size(param.Q,1);
37+
param.numInputs=size(param.R,1);
38+
3239

3340
load('Crane_NominalParameters.mat');
3441
[param.A,param.B,param.C,~] = genCraneODE(m,M,MR,r,9.81,Tx,Ty,Vx,Vy,param.Ts);
@@ -103,7 +110,7 @@ end % End of myStateEstimator
103110
%% Modify the following function for your controller
104111
function u = myMPController(r, x_hat, param)
105112
%% Do not delete this line
106-
u=zeros(2,1);
113+
u=zeros(param.numInputs,1);
107114
% Create the output array of the appropriate size
108115
persistent it EE DD Gamma Phi bb
109116
if isempty(it)
@@ -119,28 +126,25 @@ if isempty(it)
119126
H=param.H;
120127
G=param.G;
121128
else
122-
% Shave off top of matrices as the horizon recedes
123-
DD=reduceMatrix(DD,param.numConstraints,8);
124-
EE=reduceMatrix(EE,param.numConstraints,2);
129+
% Shave off matrices as the horizon shrinks
130+
DD=reduceMatrix(DD,param.numConstraints,param.numStates);
131+
EE=reduceMatrix(EE,param.numConstraints,param.numInputs);
125132
bb=reduceMatrix(bb,param.numConstraints,0);
126-
Gamma=reduceMatrix(Gamma,8,2);
127-
Phi=reduceMatrix(Phi,8,0);
133+
Gamma=reduceMatrix(Gamma,param.numStates,param.numInputs);
134+
Phi=reduceMatrix(Phi,param.numStates,0);
128135
end
129136

130-
if (param.useShrinkingHorizon==true)
137+
if (param.useShrinkingHorizon==true) && (it<param.N)
131138
N=param.N-it;
132139
[F,J,L]=genConstraintMatrices(DD,EE,Gamma,Phi,N);
133140
[H,G] = genCostMatrices(Gamma,Phi,param.Q,param.R,param.P,N);
134-
[Lchol , p] = chol(H,'lower');
135-
Linv = linsolve(Lchol,eye(size(Lchol)),struct('LT',true));
136141
u = genMPController(H,G,F,bb,J,L,x_hat,r,2);
137142
it=it+1;
138143
end
139144
end % End of myMPController
140145

141146
function [F,J,L] = genConstraintMatrices(DD,EE,Gamma,Phi,N)
142147
sx = size(Gamma,1) / N;
143-
sx=8;
144148
shift = [zeros(sx, sx*(N-1));eye(sx*(N-1))]*[eye(sx*(N-1)), zeros(sx*(N-1),sx)];
145149
F = DD*shift*Gamma + EE;
146150
J = -DD*(shift*Phi + [eye(sx);zeros(sx*(N-1), sx)]);
@@ -263,7 +267,7 @@ function [u,status,iA1] = genMPController(H,G,F,bb,J,L,x,xTarget,m)
263267
opt.IntegrityChecks = false;%% for code generation
264268
opt.ConstraintTolerance = 1e-3;
265269
opt.DataType = 'double';
266-
opt.UseHessianAsInput = false;
270+
opt.UseHessianAsInput = true;
267271

268272

269273
% Compute the linear term of the cost function

FunctionTemplate.m

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
% This is a sample way to send reference points
99
param.xTar = shape.target(1);
1010
param.yTar = shape.target(2);
11+
param.target=shape.target;r = zeros(8,1);
1112

12-
param.target=shape.target;
13+
% Make the crane go to (xTar, yTar)
14+
r=zeros(8,1); r(1,1) = param.xTar; r(3,1) = param.yTar;
15+
param.targetMod=r;
1316
param.start=shape.start;
1417
param.constraints=shape.constraints;
1518
param.eps_r=shape.eps_r;
@@ -124,11 +127,11 @@
124127
G=param.G;
125128
else
126129
% Shave off matrices as the horizon shrinks
127-
DD=reduceMatrix(DD,param.numConstraints,param.numStates);
128-
EE=reduceMatrix(EE,param.numConstraints,param.numInputs);
129-
bb=reduceMatrix(bb,param.numConstraints,0);
130-
Gamma=reduceMatrix(Gamma,param.numStates,param.numInputs);
131-
Phi=reduceMatrix(Phi,param.numStates,0);
130+
DD=reduceMatrixTop(DD,param.numConstraints,param.numStates);
131+
EE=reduceMatrixTop(EE,param.numConstraints,param.numInputs);
132+
bb=reduceMatrixTop(bb,param.numConstraints,0);
133+
Gamma=reduceMatrixBottom(Gamma,param.numStates,param.numInputs);
134+
Phi=reduceMatrixBottom(Phi,param.numStates,0);
132135
end
133136

134137
if (param.useShrinkingHorizon==true) && (it<param.N)
@@ -335,10 +338,13 @@
335338

336339
end
337340

338-
function reducedMatrix = reduceMatrix(A,nRows,nCols)
339-
reducedMatrix=A(1:size(A,1)-nRows,1:size(A,2)-nCols);
341+
function reducedMatrix = reduceMatrixTop(A,nRows,nCols)
342+
reducedMatrix=A(nRows+1:size(A,1),nCols+1:size(A,2));
340343
end
341344

345+
function reducedMatrix = reduceMatrixBottom(A,nRows,nCols)
346+
reducedMatrix=A(1:size(A,1)-nRows,1:size(A,2)-nCols);
347+
end
342348

343349
function [A,B,C] = getLineParamsStd(pointA,pointB)
344350
% Get line parameters in standard form Ax+By=C

basicMPCFunctions/RectConstraints.mlx

80 Bytes
Binary file not shown.

myMPController.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
G=param.G;
1818
else
1919
% Shave off matrices as the horizon shrinks
20-
DD=reduceMatrix(DD,param.numConstraints,param.numStates);
21-
EE=reduceMatrix(EE,param.numConstraints,param.numInputs);
22-
bb=reduceMatrix(bb,param.numConstraints,0);
23-
Gamma=reduceMatrix(Gamma,param.numStates,param.numInputs);
24-
Phi=reduceMatrix(Phi,param.numStates,0);
20+
DD=reduceMatrixTop(DD,param.numConstraints,param.numStates);
21+
EE=reduceMatrixTop(EE,param.numConstraints,param.numInputs);
22+
bb=reduceMatrixTop(bb,param.numConstraints,0);
23+
Gamma=reduceMatrixBottom(Gamma,param.numStates,param.numInputs);
24+
Phi=reduceMatrixBottom(Phi,param.numStates,0);
2525
end
2626

2727
if (param.useShrinkingHorizon==true) && (it<param.N)

mySetup.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
% This is a sample way to send reference points
99
param.xTar = shape.target(1);
1010
param.yTar = shape.target(2);
11+
param.target=shape.target;r = zeros(8,1);
1112

12-
param.target=shape.target;
13+
% Make the crane go to (xTar, yTar)
14+
r=zeros(8,1); r(1,1) = param.xTar; r(3,1) = param.yTar;
15+
param.targetMod=r;
1316
param.start=shape.start;
1417
param.constraints=shape.constraints;
1518
param.eps_r=shape.eps_r;

reduceMatrix.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function reducedMatrix = reduceMatrix(A,nRows,nCols)
2-
reducedMatrix=A(1:size(A,1)-nRows,1:size(A,2)-nCols);
2+
reducedMatrix=A(nRows+1:size(A,1),nCols+1:size(A,2));
33
end
44

55

reduceMatrixBottom.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function reducedMatrix = reduceMatrixBottom(A,nRows,nCols)
2+
reducedMatrix=A(1:size(A,1)-nRows,1:size(A,2)-nCols);
3+
end
4+

reduceMatrixTop.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function reducedMatrix = reduceMatrixTop(A,nRows,nCols)
2+
reducedMatrix=A(nRows+1:size(A,1),nCols+1:size(A,2));
3+
end
4+

0 commit comments

Comments
 (0)