I am trying to implement a Software in the loop simulation, I do not find the issue here. I am currently debugging using constant input vectors. Appears the problem is given by the allocation and definition of u1, the output variable.
What am I doing wrong?
The code works in a Function MATLAB evnironment, but the same does not work in simulink MATLAB Function Block.
function [u1,deltau] = Controller(Omega,Psi,Lzerot,xm,y,sp,m)
persistent Xf
if isempty(Xf)
Xf = [xm;(y-sp)];
end
persistent xm_old
if isempty(xm_old)
xm_old = xm;
end
persistent u
if isempty(u)
u = zeros(m,1);
end
persistent deltau_old
if isempty(deltau_old)
deltau_old = zeros(m,1);
end
% O P T I M I Z A T I O N
eta = -(Omega\Psi)*(Xf); % (L. Wang pg. 99/100)
deltau = Lzerot*eta; % Kmpc = L(0)'*(Omega\Psi) & Du = - Kmpc * x(k)
if deltau == deltau_old
else
u = u + deltau;
deltau_old = deltau;
end
u1 = u;
% U P D A T E
if xm == xm_old
% elseif y == y_old
% Xf = [xm-xm_old;(y-sp)];
else
Xf = [xm-xm_old;(y-sp)];
xm_old = xm;
end
end
Thank you for your help.
I tried to pre-allocate the u1 variable, as (m,1) vector but it does not affect the problem. Cleaning the function and optimizing is part of the task I want to achieve. Is it just that I need the zero allocation of u, for that reason I am using the persistant type of variable.
I do not want my variable to change size. It has not to.
Even if I change to "variable size" type of output, it does not solve the problem.