我正在使用 MATLAB 的非线性 MPC 工具箱,并尝试实现一些简单的控制器。但是,给定一个初始控制信号uk
,nlmpcmove 生成的任何控制信号都与uk
(在零时间saveStates
填充任何内容)相同。uk
代码的相关部分;
%% Create MPC object
numStates = 12;
numOutputs = 12;
numControl = 2;
nlobj = nlmpc(numStates,numOutputs,numControl);
nlobj.Ts = constants.Ts;
Tswing = 0.1;
nlobj.PredictionHorizon = 5;
nlobj.ControlHorizon = 4;
nlobj.Model.StateFcn = @legStateFcn;
nlobj.Model.IsContinuousTime = false;
% Output function is exact state
nlobj.Model.OutputFcn = @(x,u) x;
nlobj.Jacobian.OutputFcn = @(x,u) eye(numStates);
% Weights
nlobj.Optimization.CustomCostFcn = @costFcn;
nlobj.Optimization.ReplaceStandardCost = true;
nlobj.Optimization.CustomIneqConFcn = @ineqFcn;
% Kalman filter
EKF = extendedKalmanFilter(@legStateFcn,@legMeasurementFcn);
EKF.State = x0;
%% Run MPC
u0 = zeros(numControl,1);
uk = u0;
yinit = x0;
yfinal = x0 + 1;
yref = [yinit yfinal]';
validateFcns(nlobj,yinit,u0) % THIS SAYS IT IS ALL OK
saveState = zeros(length(yinit),length(t));
saveControl = zeros(length(uk),length(t));
y = yinit;
for i = 1:length(t)
disp(i/length(t)*100)
xk = correct(EKF,y);
saveState(:,i) = xk;
uk = nlmpcmove(nlobj,xk,uk,yref(2,:));
saveControl(:,i) = uk;
% Predict state for next iteration
predict(EKF,uk);
% Implement optimal control move
x = legStateFcn(xk,uk);
y = x;
end
由于validateFcns(nlobj,yinit,u0)
返回一切正常,所以我没有理由向您展示所有函数句柄,但只是为了更好的衡量;
LegStateFcn.m;
function xk1 = legStateFcn(xk,uk)
成本Fcn.m;
function J = costFcn(X,U,e,data)
ineqFcn.m;
function ineq = ineqFcn(X,U,e,data)
腿测量Fcn.m;
function yk = legMeasurementFcn(xk)
MATLAB 不会抱怨我在这里所做的任何事情,但是,u0
在. 我已经通过重命名before 和 after 进行了调试,删除了,模拟了结果状态(不遵守不等式约束)并看到它确实像我所期望的那样遵循系统的动态。for-loop
nlmpcmove
u0
uk
nlmpcmove
nlobj.Jacobian.OutputFcn
我不知道我做错了什么,我担心这是一个如此明显的小错误,我什至不应该一开始就犯它。