function [g,geq,dg,dgeq] = mpcCustomConstraintFcnWrapper(z,rseq,old_u,vseq,utarget,xQP,...
    I2Jm,SuJm,Su1,Sx,Hv,nu,ny,m,p,yoff,uoff,voff,uscale,yscale,vscale)
% Wrapper to custom constraint function to optimize by nonlinear programming

% Author:  A. Bemporad
% Copyright 2017 The MathWorks, Inc.

degrees = numel(z);
% Reconstruct sequence of Y,U,DU
[Y,U,DU,slack,vseq] = mpcCustomFcnMap(z,xQP,old_u,vseq,uscale,yscale,vscale,uoff,yoff,voff,p,m,nu,ny,I2Jm,SuJm,Hv,Sx,Su1,degrees);
% Evaluate constraints
[g,geq,dCdy,dCdu,dCddu,dCdslack,dCeqdy,dCeqdu,dCeqddu,dCeqdslack] = mpcCustomConstraintFcn(Y,rseq,U,utarget,DU,vseq(:,1:end-1),slack);
% Remap Jacobian in z-coordinates
if ~isempty(g) && ~isempty(dCdy) && ~isempty(dCdu) && ~isempty(dCddu) && ~isempty(dCdslack)
    dg=mpcCustomGradMap(dCdy,dCdu,dCddu,dCdslack,uscale,yscale,SuJm,I2Jm,p,m,nu,degrees);
else
    dg=[];
end
if ~isempty(geq) && ~isempty(dCeqdy) && ~isempty(dCeqdu) && ~isempty(dCeqddu) && ~isempty(dCeqdslack)
    dgeq=mpcCustomGradMap(dCeqdy,dCeqdu,dCeqddu,dCeqdslack,uscale,yscale,SuJm,I2Jm,p,m,nu,degrees);
else
    dgeq=[];
end