-
Notifications
You must be signed in to change notification settings - Fork 0
/
Run.m
55 lines (46 loc) · 1.64 KB
/
Run.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
function [u, zstar, ustar, MPCconfig, MPCfailed] = Run(Q, R, curZ, zDesired, uDesired, MPCconfig, pObs, rObs)
% Grab constraints from file
[qLim, qdotLim, uLim] = Ballbot.defineConstraints(Control.OptimizationType.MPC);
zLim = interleave2(qLim, qdotLim, 'row');
% Solve optimal control
[H, c, H_PCC, c_PCC, Aeq, beq, A, b] = Control.MPC.getQP_funcs( ...
Q, R, MPCconfig.timeHorizon, curZ, zDesired, uDesired, ...
MPCconfig.zTrajExpected, MPCconfig.uTrajExpected, zLim, uLim,...
pObs, rObs,...
MPCconfig.previousDV, 1 ...
);
if MPCconfig.planMade
H_used = H_PCC;
c_used = c_PCC;
else
H_used = H;
c_used = c;
end
persistent ws
if coder.target('MATLAB')
options = optimoptions("quadprog","Display",'off');
[xtildestar, fval, exitFlag, output] = quadprog(H_used, c_used, A, b, Aeq, beq, [],[],[],options);
else
options = optimoptions("quadprog","Display",'off','Algorithm','active-set');
x0 = zeros(Control.MPC.get_N_decisionVars(),1);
if isempty(ws)
ws = optimwarmstart(x0,options);
end
[ws, fval, exitFlag, output] = quadprog(H_used, c_used, A, b, Aeq, beq, [],[],ws);
xtildestar = ws.X;
end
if exitFlag < 0
xtildestar = zeros(Control.MPC.get_N_decisionVars(),1);
sprintf('MPC Failed. Quadprog exit flag %.1f',exitFlag)
end
MPCfailed = exitFlag < 0;
zstar = Control.MPC.extract_zN_from_DV(xtildestar);
ustar = Control.MPC.extract_uN_from_DV(xtildestar);
% Return the first control output
u = ustar(1);
% Update the expected trajectories
MPCconfig.zTrajExpected = zstar;
MPCconfig.uTrajExpected = ustar';
MPCconfig.previousDV = xtildestar;
MPCconfig.planMade = true;
end