【carsim+simulink 联合仿真——车辆轨迹MPC跟踪】

article/2025/8/29 21:45:08

学习北理工的无人驾驶车辆模型预测控制第2版第四章,使用的仿真软件为Carsim8和MatlabR2019a联合仿真,使用MPC控制思想对车辆进行轨迹跟踪控制,并给出仿真结果。

mpc控制器函数:s-function

function [sys,x0,str,ts] = MY_MPCController3(t,x,u,flag)
%   该函数是写的第3个S函数控制器(MATLAB版本:R2011a)
%   限定于车辆运动学模型,控制量为速度和前轮偏角,使用的QP为新版本的QP解法
%   [sys,x0,str,ts] = MY_MPCController3(t,x,u,flag)
%
% is an S-function implementing the MPC controller intended for use
% with Simulink. The argument md, which is the only user supplied
% argument, contains the data structures needed by the controller. The
% input to the S-function block is a vector signal consisting of the
% measured outputs and the reference values for the controlled
% outputs. The output of the S-function block is a vector signal
% consisting of the control variables and the estimated state vector,
% potentially including estimated disturbance states.switch flag,case 0[sys,x0,str,ts] = mdlInitializeSizes; % Initializationcase 2sys = mdlUpdates(t,x,u); % Update discrete statescase 3sys = mdlOutputs(t,x,u); % Calculate outputscase {1,4,9} % Unused flagssys = [];otherwiseerror(['unhandled flag = ',num2str(flag)]); % Error handling
end
% End of dsfunc.%==============================================================
% Initialization
%==============================================================function [sys,x0,str,ts] = mdlInitializeSizes% Call simsizes for a sizes structure, fill it in, and convert it
% to a sizes array.sizes = simsizes;
sizes.NumContStates  = 0;
sizes.NumDiscStates  = 3; % this parameter doesn't matter
sizes.NumOutputs     = 2; %[speed, steering]
sizes.NumInputs      = 5; %[x,y,yaw,vx,steer_sw]
sizes.DirFeedthrough = 1; % Matrix D is non-empty.
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 =[0;0;0];
global U; % store current ctrl vector:[vel_m, delta_m]
U=[0;0];
% Initialize the discrete states.
str = [];             % Set str to an empty matrix.
ts  = [0.05 0];       % sample time: [period, offset]
%End of mdlInitializeSizes%==============================================================
% Update the discrete states
%==============================================================
function sys = mdlUpdates(t,x,u)sys = x;
%End of mdlUpdate.%==============================================================
% Calculate outputs
%==============================================================function sys = mdlOutputs(t,x,u)
global a b u_piao;
%u_piao矩阵,用于存储每一个仿真时刻,车辆的实际控制量(实际运动状态)与目标控制量(运动状态)之间的偏差global U; %store chi_tilde=[vel-vel_ref; delta - delta_ref]
global kesi;tic
Nx=3;%状态量的个数
Nu =2;%控制量的个数
Np =60;%预测步长
Nc=30;%控制步长
Row=10;%松弛因子
fprintf('Update start, t=%6.3f\n',t)
t_d =u(3)*3.1415926/180;%CarSim输出的Yaw angle为角度,角度转换为弧度%    %直线路径
%     r(1)=5*t; %ref_x-axis
%     r(2)=5;%ref_y-axis
%     r(3)=0;%ref_heading_angle
%     vd1=5;% ref_velocity
%     vd2=0;% ref_steering% %     半径为25m的圆形轨迹, 圆心为(0, 35), 速度为5m/sr(1)=25*sin(0.2*t);r(2)=35-25*cos(0.2*t);r(3)=0.2*t;vd1=5;vd2=0.104;%     %半径为35m的圆形轨迹, 圆心为(0, 35), 速度为3m/s
%     r(1)=25*sin(0.12*t);
%     r(2)=25+10-25*cos(0.12*t);
%     r(3)=0.12*t;
%     vd1=3;
%     vd2=0.104;
%半径为25m的圆形轨迹, 圆心为(0, 35), 速度为10m/s
%      r(1)=25*sin(0.4*t);
%      r(2)=25+10-25*cos(0.4*t);
%      r(3)=0.4*t;
%      vd1=10;
%      vd2=0.104;
%半径为25m的圆形轨迹, 圆心为(0, 35), 速度为4m/s
% r(1)=25*sin(0.16*t);
% r(2)=25+10-25*cos(0.16*t);
% r(3)=0.16*t;
% vd1=4;
% vd2=0.104;%     t_d =  r(3);
kesi=zeros(Nx+Nu,1);
kesi(1) = u(1)-r(1);%u(1)==X(1),x_offset
kesi(2) = u(2)-r(2);%u(2)==X(2),y_offset
heading_offset = t_d - r(3); %u(3)==X(3),heading_angle_offsetif (heading_offset < -pi)heading_offset = heading_offset + 2*pi;
end
if (heading_offset > pi)heading_offset = heading_offset - 2*pi;
end
kesi(3)=heading_offset;%      U(1) = u(4)/3.6 - vd1; % vel, km/h-->m/s
%      steer_SW = u(5)*pi/180;
%      steering_angle = steer_SW/18.0;
%      U(2) = steering_angle - vd2;kesi(4)=U(1); % vel-vel_ref
kesi(5)=U(2); % steer_angle - steering_ref
fprintf('vel-offset=%4.2f, steering-offset, U(2)=%4.2f\n',U(1), U(2))T=0.05;
T_all=40;%临时设定,总的仿真时间,主要功能是防止计算期望轨迹越界
% Mobile Robot Parameters
L = 2.6; % wheelbase of carsim vehicle
% Mobile Robot variable%矩阵初始化
u_piao=zeros(Nx,Nu);
Q=eye(Nx*Np,Nx*Np);
R=5*eye(Nu*Nc);
a=[1    0   -vd1*sin(t_d)*T;0    1   vd1*cos(t_d)*T;0    0   1;];
b=[cos(t_d)*T        0;sin(t_d)*T        0;tan(vd2)*T/L      vd1*T/(cos(vd2)^2)];A_cell=cell(2,2);
B_cell=cell(2,1);
A_cell{1,1}=a;
A_cell{1,2}=b;
A_cell{2,1}=zeros(Nu,Nx);
A_cell{2,2}=eye(Nu);
B_cell{1,1}=b;
B_cell{2,1}=eye(Nu);A=cell2mat(A_cell);
B=cell2mat(B_cell);
C=[ 1 0 0 0 0;0 1 0 0 0;0 0 1 0 0];PHI_cell=cell(Np,1);
THETA_cell=cell(Np,Nc);
for j=1:1:NpPHI_cell{j,1}=C*A^j;for k=1:1:Ncif k<=jTHETA_cell{j,k}=C*A^(j-k)*B;elseTHETA_cell{j,k}=zeros(Nx,Nu);endend
end
PHI=cell2mat(PHI_cell);%size(PHI)=[Nx*Np Nx+Nu]
THETA=cell2mat(THETA_cell);%size(THETA)=[Nx*Np Nu*(Nc+1)]H_cell=cell(2,2);
H_cell{1,1}=THETA'*Q*THETA+R;
H_cell{1,2}=zeros(Nu*Nc,1);
H_cell{2,1}=zeros(1,Nu*Nc);
H_cell{2,2}=Row;
H=cell2mat(H_cell);
%     H=(H+H')/2;error=PHI*kesi;
f_cell=cell(1,2);
f_cell{1,1} = (error'*Q*THETA);
f_cell{1,2} = 0;
f=cell2mat(f_cell);%% 以下为约束生成区域
%不等式约束
A_t=zeros(Nc,Nc);%见falcone论文 P181
for p=1:1:Ncfor q=1:1:Ncif q<=pA_t(p,q)=1;elseA_t(p,q)=0;endend
end
A_I=kron(A_t,eye(Nu));%对应于falcone论文约束处理的矩阵A,求克罗内克积
Ut=kron(ones(Nc,1), U);%
umin=[-0.2;  -0.54];%[min_vel, min_steer]维数与控制变量的个数相同
umax=[0.05;   0.436]; %[max_vel, max_steer],%0.436rad = 25deg
delta_umin = [-0.5;  -0.0082]; % 0.0082rad = 0.47deg
delta_umax = [0.5;  0.0082];Umin=kron(ones(Nc,1),umin);
Umax=kron(ones(Nc,1),umax);
A_cons_cell={A_I zeros(Nu*Nc, 1); -A_I zeros(Nu*Nc, 1)};
b_cons_cell={Umax-Ut;-Umin+Ut};
A_cons=cell2mat(A_cons_cell);%(求解方程)状态量不等式约束增益矩阵,转换为绝对值的取值范围
b_cons=cell2mat(b_cons_cell);%(求解方程)状态量不等式约束的取值% 状态量约束
delta_Umin = kron(ones(Nc,1),delta_umin);
delta_Umax = kron(ones(Nc,1),delta_umax);
lb = [delta_Umin; 0];%(求解方程)状态量下界
ub = [delta_Umax; 10];%(求解方程)状态量上界%% 开始求解过程
%     options = optimset('Algorithm','active-set');
options = optimset('Algorithm','interior-point-convex');
warning off all  % close the warnings during computation
[X, fval,exitflag]=quadprog(H, f, A_cons, b_cons,[], [],lb,ub,[],options);fprintf('quadprog EXITFLAG = %d\n',exitflag);%% 计算输出
if ~isempty(X)u_piao(1)=X(1);u_piao(2)=X(2);
end;
U(1)=kesi(4)+u_piao(1);%用于存储上一个时刻的控制量
U(2)=kesi(5)+u_piao(2);
u_real(1) = U(1) + vd1;
u_real(2) = U(2) + vd2;sys=u_real % vel, steering, x, y
toc
% End of mdlOutputs.

新版的matlab没有active-set算法,需要更改为'interior-point-convex',但是会出现X出差索引范围,需要采用旧版的quadprog函数算法,这里更改为quadprog2011,具体代码如下;

function [X,fval,exitflag,output,lambda] = quadprog2011(H,f,A,B,Aeq,Beq,lb,ub,X0,options,varargin)
%QUADPROG Quadratic programming. 
%   X = QUADPROG(H,f,A,b) attempts to solve the quadratic programming 
%   problem:
%
%            min 0.5*x'*H*x + f'*x   subject to:  A*x <= b 
%             x    
%
%   X = QUADPROG(H,f,A,b,Aeq,beq) solves the problem above while 
%   additionally satisfying the equality constraints Aeq*x = beq.
%
%   X = QUADPROG(H,f,A,b,Aeq,beq,LB,UB) defines a set of lower and upper
%   bounds on the design variables, X, so that the solution is in the 
%   range LB <= X <= UB. Use empty matrices for LB and UB if no bounds 
%   exist. Set LB(i) = -Inf if X(i) is unbounded below; set UB(i) = Inf if 
%   X(i) is unbounded above.
%
%   X = QUADPROG(H,f,A,b,Aeq,beq,LB,UB,X0) sets the starting point to X0.
%
%   X = QUADPROG(H,f,A,b,Aeq,beq,LB,UB,X0,OPTIONS) minimizes with the 
%   default optimization parameters replaced by values in the structure 
%   OPTIONS, an argument created with the OPTIMSET function. See OPTIMSET 
%   for details. Used options are Display, Diagnostics, TolX, TolFun, 
%   HessMult, LargeScale, MaxIter, PrecondBandWidth, TypicalX, TolPCG, and 
%   MaxPCGIter. Currently, only 'final' and 'off' are valid values for the 
%   parameter Display ('iter' is not available).
%
%   X = QUADPROG(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a
%   structure with matrix 'H' in PROBLEM.H, the vector 'f' in PROBLEM.f,
%   the linear inequality constraints in PROBLEM.Aineq and PROBLEM.bineq,
%   the linear equality constraints in PROBLEM.Aeq and PROBLEM.beq, the
%   lower bounds in PROBLEM.lb, the upper bounds in PROBLEM.ub, the start
%   point in PROBLEM.x0, the options structure in PROBLEM.options, and 
%   solver name 'quadprog' in PROBLEM.solver. Use this syntax to solve at 
%   the command line a problem exported from OPTIMTOOL. The structure 
%   PROBLEM must have all the fields. 
%
%   [X,FVAL] = QUADPROG(H,f,A,b) returns the value of the objective 
%   function at X: FVAL = 0.5*X'*H*X + f'*X.
%
%   [X,FVAL,EXITFLAG] = QUADPROG(H,f,A,b) returns an EXITFLAG that 
%   describes the exit condition of QUADPROG. Possible values of EXITFLAG 
%   and the corresponding exit conditions are
%
%   All algorithms:
%     1  First order optimality conditions satisfied.
%     0  Maximum number of iterations exceeded.
%    -2  No feasible point found.
%    -3  Problem is unbounded.
%   Interior-point-convex only:
%    -6  Non-convex problem detected.
%   Trust-region-reflective only:
%     3  Change in objective function too small.
%    -4  Current search direction is not a descent direction; no further 
%         progress can be made.
%   Active-set only:
%     4  Local minimizer found.
%    -7  Magnitude of search direction became too small; no further 
%         progress can be made. The problem is ill-posed or badly 
%         conditioned.
%
%   [X,FVAL,EXITFLAG,OUTPUT] = QUADPROG(H,f,A,b) returns a structure
%   OUTPUT with the number of iterations taken in OUTPUT.iterations,
%   maximum of constraint violations in OUTPUT.constrviolation, the 
%   type of algorithm used in OUTPUT.algorithm, the number of conjugate
%   gradient iterations (if used) in OUTPUT.cgiterations, a measure of 
%   first order optimality (large-scale algorithm only) in 
%   OUTPUT.firstorderopt, and the exit message in OUTPUT.message.
%
%   [X,FVAL,EXITFLAG,OUTPUT,LAMBDA] = QUADPROG(H,f,A,b) returns the set of 
%   Lagrangian multipliers LAMBDA, at the solution: LAMBDA.ineqlin for the 
%   linear inequalities A, LAMBDA.eqlin for the linear equalities Aeq, 
%   LAMBDA.lower for LB, and LAMBDA.upper for UB.
%
%   See also LINPROG, LSQLIN.%   Copyright 1990-2010 The MathWorks, Inc.
%   $Revision: 1.1.6.14 $  $Date: 2010/11/01 19:41:32 $defaultopt = struct( ...'Algorithm','trust-region-reflective', ...'Diagnostics','off', ...'Display','final', ...'HessMult',[], ... 'LargeScale','on', ...'MaxIter',[], ...    'MaxPCGIter','max(1,floor(numberOfVariables/2))', ...   'PrecondBandWidth',0, ... 'TolCon',1e-8, ...'TolFun',[], ...'TolPCG',0.1, ...    'TolX',100*eps, ...'TypicalX','ones(numberOfVariables,1)' ...    );% If just 'defaults' passed in, return the default options in X
if nargin == 1 && nargout <= 1 && isequal(H,'defaults')X = defaultopt;return
endif nargin < 10options = [];if nargin < 9X0 = [];if nargin < 8ub = [];if nargin < 7lb = [];if nargin < 6Beq = [];if nargin < 5Aeq = [];if nargin < 4B = [];if nargin < 3A = [];endendendendendendend
end% Detect problem structure input
if nargin == 1if isa(H,'struct')[H,f,A,B,Aeq,Beq,lb,ub,X0,options] = separateOptimStruct(H);else % Single input and non-structure.error(message('optim:quadprog:InputArg'));end
endif nargin == 0 error(message('optim:quadprog:NotEnoughInputs'))
end% Check for non-double inputs
% SUPERIORFLOAT errors when superior input is neither single nor double;
% We use try-catch to override SUPERIORFLOAT's error message when input
% data type is integer.
trydataType = superiorfloat(H,f,A,B,Aeq,Beq,lb,ub,X0);
catch MEif strcmp(ME.identifier,'MATLAB:datatypes:superiorfloat')dataType = 'notDouble';end
endif ~strcmp(dataType,'double')error(message('optim:quadprog:NonDoubleInput'))
end% Set up constant strings
activeSet =  'active-set';
trustRegReflect = 'trust-region-reflective';
interiorPointConvex = 'interior-point-convex';if nargout > 4computeLambda = true;
else computeLambda = false;
end
if nargout > 3computeConstrViolation = true;computeFirstOrderOpt = true;
else computeConstrViolation = false;computeFirstOrderOpt = false;
end% Options setup
largescale = isequal(optimget(options,'LargeScale',defaultopt,'fast'),'on');
Algorithm = optimget(options,'Algorithm',defaultopt,'fast'); diagnostics = isequal(optimget(options,'Diagnostics',defaultopt,'fast'),'on');
display = optimget(options,'Display',defaultopt,'fast');
detailedExitMsg = ~isempty(strfind(display,'detailed'));
switch display
case {'off', 'none'}verbosity = 0;
case {'iter','iter-detailed'}verbosity = 2;
case {'final','final-detailed'}verbosity = 1;
case 'testing'verbosity = 3;
otherwiseverbosity = 1;
end% Determine algorithm user chose via options. (We need this now to set
% OUTPUT.algorithm in case of early termination due to inconsistent
% bounds.) This algorithm choice may be modified later when we check the
% problem type.
algChoiceOptsConflict = false;
if strcmpi(Algorithm,'active-set')output.algorithm = activeSet;
elseif strcmpi(Algorithm,'interior-point-convex')output.algorithm = interiorPointConvex;
elseif strcmpi(Algorithm,'trust-region-reflective')if largescaleoutput.algorithm = trustRegReflect;else% Conflicting options Algorithm='trust-region-reflective' and% LargeScale='off'. Choose active-set algorithm.algChoiceOptsConflict = true; % Warn later, not in case of early terminationoutput.algorithm = activeSet;end
elseerror(message('optim:quadprog:InvalidAlgorithm'));
end mtxmpy = optimget(options,'HessMult',defaultopt,'fast');
% Check for name clash
functionNameClashCheck('HessMult',mtxmpy,'hessMult_optimInternal','optim:quadprog:HessMultNameClash');
if isempty(mtxmpy)% Internal Hessian-multiply functionmtxmpy = @hessMult_optimInternal;usrSuppliedHessMult = false;     
elseusrSuppliedHessMult = true;
end% Set the constraints up: defaults and check size
[nineqcstr,numberOfVariablesineq] = size(A);
[neqcstr,numberOfVariableseq] = size(Aeq);
if isa(H,'double') && ~usrSuppliedHessMult% H must be square and have the correct size nColsH = size(H,2);if nColsH ~= size(H,1)error(message('optim:quadprog:NonSquareHessian'));end
else % HessMult in effect, so H can be anythingnColsH = 0;
end% Check the number of variables. The check must account for any combination of these cases:
% * User provides HessMult
% * The problem is linear (H = zeros, or H = [])
% * The objective has no linear component (f = [])
% * There are no linear constraints (A,Aeq = [])
% * There are no, or partially specified, bounds 
% * There is no X0
numberOfVariables = ...max([length(f),nColsH,numberOfVariablesineq,numberOfVariableseq]);if numberOfVariables == 0% If none of the problem quantities indicate the number of variables,% check X0, even though some algorithms do not use it.if isempty(X0)error(message('optim:quadprog:EmptyProblem'));else% With all other data empty, use the X0 input to determine% the number of variables.numberOfVariables = length(X0);end
endncstr = nineqcstr + neqcstr;if isempty(f)f = zeros(numberOfVariables,1);
else % Make sure that the number of rows/columns in H matches the length of% f under the following conditions:% * The Hessian is passed in explicitly (no HessMult)% * There is a non-empty Hessianif ~usrSuppliedHessMult && ~isempty(H)if length(f) ~= nColsHerror(message('optim:quadprog:MismatchObjCoefSize'));endend
end
if isempty(A)A = zeros(0,numberOfVariables);
end
if isempty(B)B = zeros(0,1);
end
if isempty(Aeq)Aeq = zeros(0,numberOfVariables); 
end
if isempty(Beq)Beq = zeros(0,1);
end% Expect vectors
f = f(:);
B = B(:);
Beq = Beq(:);if ~isequal(length(B),nineqcstr)error(message('optim:quadprog:InvalidSizesOfAAndB'))
elseif ~isequal(length(Beq),neqcstr)error(message('optim:quadprog:InvalidSizesOfAeqAndBeq'))
elseif ~isequal(length(f),numberOfVariablesineq) && ~isempty(A)error(message('optim:quadprog:InvalidSizesOfAAndF'))
elseif ~isequal(length(f),numberOfVariableseq) && ~isempty(Aeq)error(message('optim:quadprog:InvalidSizesOfAeqAndf'))
end[X0,lb,ub,msg] = checkbounds(X0,lb,ub,numberOfVariables);
if ~isempty(msg)exitflag = -2;X=X0; fval = []; lambda = [];output.iterations = 0;output.constrviolation = [];output.algorithm = ''; % Not known at this stageoutput.firstorderopt = [];output.cgiterations = []; output.message = msg;if verbosity > 0disp(msg)endreturn
end% Check that all data is real
if ~(isreal(H) && isreal(A) && isreal(Aeq) && isreal(f) && ...isreal(B) && isreal(Beq) && isreal(lb) && isreal(ub) && isreal(X0))error(message('optim:quadprog:ComplexData'))
endcaller = 'quadprog';
% Check out H and make sure it isn't empty or all zeros
if isa(H,'double') && ~usrSuppliedHessMultif norm(H,'inf')==0 || isempty(H)% Really a lp problemwarning(message('optim:quadprog:NullHessian'))[X,fval,exitflag,output,lambda]=linprog(f,A,B,Aeq,Beq,lb,ub,X0,options);returnelse% Make sure it is symmetricif norm(H-H',inf) > epsif verbosity > -1warning(message('optim:quadprog:HessianNotSym'))endH = (H+H')*0.5;endend
end% Determine which algorithm and make sure problem matches.
hasIneqs = (nineqcstr > 0);  % Does the problem have any inequalities?
hasEqsAndBnds = (neqcstr > 0) && (any(isfinite(ub)) || any(isfinite(lb))); % Does the problem have both equalities and bounds?
hasMoreEqsThanVars = (neqcstr > numberOfVariables); % Does the problem have more equalities than variables?
hasNoConstrs = (neqcstr == 0) && (nineqcstr == 0) && ...all(eq(ub, inf)) && all(eq(lb, -inf)); % Does the problem not have equalities, bounds, or inequalities?if (hasIneqs || hasEqsAndBnds || hasMoreEqsThanVars || hasNoConstrs) && ...strcmpi(output.algorithm,trustRegReflect) || strcmpi(output.algorithm,activeSet)% (has linear inequalites OR both equalities and bounds OR has no constraints OR% has more equalities than variables) then call active-set codeif algChoiceOptsConflict% Active-set algorithm chosen as a result of conflicting optionswarning('optim:quadprog:QPAlgLargeScaleConflict', ...['Options LargeScale = ''off'' and Algorithm = ''trust-region-reflective'' conflict. ' ...'Ignoring Algorithm and running active-set algorithm. To run trust-region-reflective, set ' ...'LargeScale = ''on''. To run active-set without this warning, set Algorithm = ''active-set''.']);endif strcmpi(output.algorithm,trustRegReflect)warning('optim:quadprog:SwitchToMedScale', ...['Trust-region-reflective algorithm does not solve this type of problem, ' ...'using active-set algorithm. You could also try the interior-point-convex ' ...'algorithm: set the Algorithm option to ''interior-point-convex'' ', ...'and rerun. For more help, see %s in the documentation.'], ...addLink('Choosing the Algorithm','choose_algorithm'))endoutput.algorithm = activeSet;Algorithm = 'active-set';if issparse(H)  || issparse(A) || issparse(Aeq) % Passed in sparse matriceswarning(message('optim:quadprog:ConvertingToFull'))endH = full(H); A = full(A); Aeq = full(Aeq);
else% Using trust-region-reflective or interior-point-convex algorithmsif ~usrSuppliedHessMultH = sparse(H);endA = sparse(A); Aeq = sparse(Aeq);
end
if ~isa(H,'double') || usrSuppliedHessMult &&  ...~strcmpi(output.algorithm,trustRegReflect)error(message('optim:quadprog:NoHessMult', Algorithm))
endif diagnostics % Do diagnostics on information so fargradflag = []; hessflag = []; line_search=[];constflag = 0; gradconstflag = 0; non_eq=0;non_ineq=0;lin_eq=size(Aeq,1); lin_ineq=size(A,1); XOUT=ones(numberOfVariables,1);funfcn{1} = [];ff=[]; GRAD=[];HESS=[];confcn{1}=[];c=[];ceq=[];cGRAD=[];ceqGRAD=[];msg = diagnose('quadprog',output,gradflag,hessflag,constflag,gradconstflag,...line_search,options,defaultopt,XOUT,non_eq,...non_ineq,lin_eq,lin_ineq,lb,ub,funfcn,confcn,ff,GRAD,HESS,c,ceq,cGRAD,ceqGRAD);
end% Trust-region-reflective
if strcmpi(output.algorithm,trustRegReflect)% Call sqpmin when just bounds or just equalities[X,fval,output,exitflag,lambda] = sqpmin(f,H,mtxmpy,X0,Aeq,Beq,lb,ub,verbosity, ...options,defaultopt,computeLambda,computeConstrViolation,varargin{:});if exitflag == -10  % Problem not handled by sqpmin at this time: dependent rowswarning(message('optim:quadprog:SwitchToMedScale'))output.algorithm = activeSet;if ~isa(H,'double') || usrSuppliedHessMulterror('optim:quadprog:NoHessMult', ...'H must be specified explicitly for active-set algorithm: cannot use HessMult option.')endH = full(H); A = full(A); Aeq = full(Aeq);end
end
% Call active-set algorithm
if strcmpi(output.algorithm,activeSet)if isempty(X0)X0 = zeros(numberOfVariables,1); end% Set default value of MaxIter for qpsubdefaultopt.MaxIter = 200;% Create options structure for qpsubqpoptions.MaxIter = optimget(options,'MaxIter',defaultopt,'fast');% A fixed constraint tolerance (eps) is used for constraint% satisfaction; no need to specify any valueqpoptions.TolCon = [];[X,lambdaqp,exitflag,output,~,~,msg]= ...qpsub(H,f,[Aeq;A],[Beq;B],lb,ub,X0,neqcstr,...verbosity,caller,ncstr,numberOfVariables,qpoptions); output.algorithm = activeSet; % have to reset since call to qpsub obliteratesendif strcmpi(output.algorithm,interiorPointConvex)defaultopt.MaxIter = 200;defaultopt.TolFun = 1e-8;% If the output structure is requested, we must reconstruct the% Lagrange multipliers in the postsolve. Therefore, set computeLambda% to true if the output structure is requested.flags.computeLambda = computeFirstOrderOpt; flags.detailedExitMsg = detailedExitMsg;flags.verbosity = verbosity;[X,fval,exitflag,output,lambda] = ipqpcommon(H,f,A,B,Aeq,Beq,lb,ub,X0, ...flags,options,defaultopt,varargin{:});% Presolve may have removed variables and constraints from the problem.% Postsolve will re-insert the primal and dual solutions after the main% algorithm has run. Therefore, constraint violation and first-order% optimality must be re-computed.%  % If no initial point was provided by the user and the presolve has% declared the problem infeasible or unbounded, X will be empty. The% lambda structure will also be empty, so do not compute constraint% violation or first-order optimality if lambda is missing.% Compute constraint violation if the output structure is requestedif computeFirstOrderOpt && ~isempty(lambda)output.constrviolation = norm([Aeq*X-Beq; max([A*X - B;X - ub;lb - X],0)],Inf);        end
end% Compute fval and first-order optimality if the active-set algorithm was
% run, or if the interior-point-convex algorithm was run (not stopped in presolve)
if (strcmpi(output.algorithm,interiorPointConvex) && ~isempty(lambda)) || ...strcmpi(output.algorithm,activeSet)% Compute objective function valuefval = 0.5*X'*(H*X)+f'*X;% Compute lambda and exit message for active-set algorithmif strcmpi(output.algorithm,activeSet)if computeLambda || computeFirstOrderOptllb = length(lb);lub = length(ub);lambda.lower = zeros(llb,1);lambda.upper = zeros(lub,1);arglb = ~isinf(lb); lenarglb = nnz(arglb);argub = ~isinf(ub); lenargub = nnz(argub);lambda.eqlin = lambdaqp(1:neqcstr,1);lambda.ineqlin = lambdaqp(neqcstr+1:neqcstr+nineqcstr,1);lambda.lower(arglb) = lambdaqp(neqcstr+nineqcstr+1:neqcstr+nineqcstr+lenarglb);lambda.upper(argub) = lambdaqp(neqcstr+nineqcstr+lenarglb+1: ...neqcstr+nineqcstr+lenarglb+lenargub);endif exitflag == 1normalTerminationMsg = sprintf('Optimization terminated.');if verbosity > 0disp(normalTerminationMsg)endif isempty(msg)output.message = normalTerminationMsg;else% append normal termination msg to current output msgoutput.message = sprintf('%s\n%s',msg,normalTerminationMsg);endelseoutput.message = msg;endend% Compute first order optimality if neededif computeFirstOrderOpt && ~isempty(lambda)output.firstorderopt = computeKKTErrorForQPLP(H,f,A,B,Aeq,Beq,lb,ub,lambda,X); elseoutput.firstorderopt = []; endoutput.cgiterations = [];  
end

结果:绿线为目标轨迹,红虚线为mpc控制车辆运行轨迹


http://chatgpt.dhexx.cn/article/AX52HEAS.shtml

相关文章

智能车辆路径跟踪控制:纯跟踪控制与Stanley控制算法,其他线相关算法

智能车辆路径跟踪控制&#xff1a;纯跟踪控制与Stanley控制算法&#xff0c;其他线相关算法 主要是MATLAB程序&#xff0c;可以根据需要的路径进行跟踪 ID:6920649147612984

LoRaWAN模块在车辆跟踪定位中的应用

目前 GPS已经在资产的管理中得到了越来越多的运用&#xff0c;如车辆跟踪、车队跟踪、资产监控等&#xff1b;人员跟踪&#xff0c;宠物跟踪&#xff0c;等等。在所有追踪装置中&#xff0c;最重要的是它的电池期望和监视距离。鉴于 LoRaWAN的功率消耗很小&#xff0c;而且能在…

无人驾驶车辆轨迹跟踪控制文献分享(1)

文献题目&#xff1a;Modelling and Control Strategies in Path Tracking Control for Autonomous Ground Vehicles: A Review of State of the Art and Challenges 作者&#xff1a;Noor Hafizah Amer Hairi Zamzuri Khisbullah Hudha Zulkiffli Abdul Kadir 论文类型&am…

无人驾驶之车辆检测与跟踪

整个项目源码&#xff1a;GitHub 整个项目数据集&#xff1a;车辆数据集、无车辆数据集 引言 本次分享主要介绍&#xff0c;如何对道路上的汽车进行识别与跟踪。这里我们实现一个简单的demo。后续我们还会对前面的代码及功能进行重构&#xff0c;从而进一步丰富我们的功能。 …

自动驾驶路径跟踪控制——车辆动力学建模基本概念

文章目录 1. 位姿自由度2. TDOFandCDOF3.运动学与动力学4. 运动控制问题描述5. 运动学建模6. 机器人位姿7. 跟踪误差8. 控制律设计声明 1. 位姿自由度 位姿自由度——系统在空间中的位姿描述所需变量的个数。任何一个没有受约束的物体&#xff0c;在空间均具有6个独立的运动&am…

车辆、行人跟踪一网打尽,超轻量、多类别、小目标跟踪系统开源了!

在琳琅满目的视觉应用中&#xff0c;对车辆、行人、飞行器等快速移动的物体进行实时跟踪及分析&#xff0c;可以说是突破安防、自动驾驶、智慧城市等炙手可热行业的利器~ 但要实现又快又准的持续跟踪&#xff0c;往往面临被检目标多、相互遮挡、图像扭曲变形、背景杂乱、视角差…

车辆轨迹跟踪算法---几何跟踪算法

车辆轨迹跟踪算法 车辆轨迹跟踪算法 车辆轨迹跟踪&#xff0c;目前的主流方法分为两类&#xff1a;基于几何追踪的方法和基于模型预测的方法&#xff1b; 几何追踪方法–pure-pursuit (纯跟踪)算法 阿克曼几何的简化版 – 车辆单轨模型&#xff08;自行车模型&#xff09;采…

MPC车辆轨迹跟踪----理论推导

MPC控制简介 众所周知&#xff0c;控制算法中&#xff0c;PID的应用占据了90&#xff05;&#xff0c;而另外10&#xff05;就是这次的主角MPC控制算法。 MPC控制算法全称模型预测控制&#xff0c;它相对比PID有着多输入&#xff0c;多输出以及更加平稳的特点。并且最重要的是…

车辆检测和跟踪技术的研究与实现

一、引言 车辆的持续跟踪对于肇事车辆的追捕以及减少交通事故具有重要意义。随着计算机技术的快速发展&#xff0c;推动着视频图像的智能化应用。当前对于视频图像的研究热点之一&#xff0c;就包括运动目标跟踪。当前对于运动目标的研究主要集中在单个摄像头的跟踪&#xff0…

自动驾驶:车道线检测、车速检测、实时通行跟踪、基于视频的车辆跟踪及流量统计

日萌社 人工智能AI&#xff1a;Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战&#xff08;不定时更新&#xff09; CNN&#xff1a;RCNN、SPPNet、Fast RCNN、Faster RCNN、YOLO V1 V2 V3、SSD、FCN、SegNet、U-Net、DeepLab V1 V2 V3、Mask RCNN 自动驾驶&…

yolo-车辆测距+前车碰撞预警(追尾预警)+车辆检测识别+车辆跟踪测速(原创算法-毕业设计)

目录 前言一、环境配置二、车辆检测、实时跟踪测速算法及代码解读1、主函数各参数含义2、算法实现3、核心代码4、效果展示 二、跟车距离测量算法及代码解读1、主函数各参数含义2、算法实现3、效果展示 三、前车碰撞预警&#xff08;追尾预警&#xff09;算法及代码解读1、算法实…

车辆跟踪技术概述zt

摘 要 基于视频的车辆检测器近年来在智能交通系统(ITS)中得到了越来越广泛的应用。本文介绍了近年来提出的一些主要的基于视频的车辆检测与跟踪技术&#xff0c;并对这些技术进行了分类。同时分析比较了各种方法的优缺点。最后&#xff0c;说明了这一领域仍然存在的问题和对可能…

车辆跟踪检测

这个是GUI的界面&#xff0c;我们分别对这个界面做介绍。 第一个窗口显示的是原始的视屏 第二个窗口是提取视屏的背景。 第三个窗口是汽车跟踪&#xff0c;将汽车跟踪效果显示。 第四个窗口是画线&#xff0c;将用户画线后的区域分为两个区间&#xff0c;通过检测区间后&…

【车辆行人检测和跟踪数据集及代码汇总】

车辆行人检测和跟踪数据集和代码汇总 1. 车辆检测和跟踪1.1 车辆检测数据集和训练权重1.2 车辆跟踪 2. 行人检测和跟踪2.1 行人检测数据集和训练权重2.2行人多目标跟踪 3. 车辆行人检测和跟踪3.1车辆行人检测数据集和训练权重3.2 车辆行人多目标跟踪 1. 车辆检测和跟踪 1.1 车…

自动驾驶路径跟踪控制——纯追踪控制

文章目录 1.自行车模型&#xff08;汽车二自由度模型&#xff09;注意点Point1Point2 2.纯追踪控制注意点Point1Point2Point3 3.相关代码参考文献声明 全局路径由一系列路径点构成&#xff0c;这些路径点只要包含 空间位置信息即可&#xff0c;也可以包含 姿态信息&#xff0…

超强实时跟踪系统首次开源!支持跨镜头、多类别、小目标跟踪!

在琳琅满目的视觉应用中&#xff0c;对车辆、行人、飞行器等快速移动的物体进行实时跟踪及分析&#xff0c;可以说是突破安防、自动驾驶、智慧城市等炙手可热行业的利器。 但要实现又快又准的持续跟踪&#xff0c;往往面临被检目标多、相互遮挡、图像扭曲变形、背景杂乱、视角差…

车辆路径跟踪算法及数学模型

一、纯追踪算法 基于当前车辆后轮中心位置&#xff0c;在参考路径上向Ld&#xff08;自定义&#xff09;的距离匹配一个瞄准点&#xff0c;假设车辆后轮中心点可以按照一定的转弯半径R行驶抵达该瞄准点&#xff0c;然后根据预瞄准距离Ld&#xff0c;转弯半径R&#xff0c;车辆…

五、车辆轨迹追踪的优化控制

5.1、车辆横向动力学模型 车辆动力学模型一般包括用于分析车辆平顺性的质量-弹簧-阻尼模型和分析车辆操纵稳定性的车辆-轮胎模型。两者研究的侧重点不同&#xff0c;平顺性分析的重点是车辆的悬架特性&#xff0c;而车辆的操纵稳定性分析的重点是车辆纵向和侧向力学特性。车辆…

PyCharm Professional 2016.1 破解 激活

接上一篇博文&#xff0c;尝试着激活一下 PyCharm Professional 2016.1&#xff0c; 居然也成功了。 方法同样来自 Rover12421 大神。 1.从官网下载 PyCharm Professional 2016.1 安装。 2.下载 破解补丁 并解压&#xff0c;记住路径 3.编辑 PyCharm 安装目录下 bin 文件夹中的…

Python:PyCharm 永久破解方法,真的超超超超超超超超超级简单!!!

准备工作&#xff1a; 1.破解包 >>>下载链接>>> 提取码&#xff1a;jjbf 2.注册码 .>>>获取地址>>> 第一步 进入PyCharm 的安装目录的bin文件夹下&#xff0c;把破解包放到该目录。 第二步 把bin 目录下的 pycharm.exe.vmoptions 和…