Script does not work automatically

Hello. This Script allows me to simulate a PI control system but it should do it automatically, it is not doing it, I have to go to the Simulink diagram and change the PI values. In this Matlab script what do I have wrong? It doesn't change the PI values automatically. Thanks.
tg = xpc;
tg_P = getparamid(tg, 'PI Controller', 'P')
tg_I = getparamid(tg, 'PI Controller', 'I')
y = []; s = []; e = []; flag = 0; p=[1,0.5,0.1]; i=[0.01,0.05,0.1];
for k = 1 : 1 : 3
P=p(k);
setparam(tg,tg_P,P); % Set damping factor (Gain1/Gain)
for j = 1 : 1 : 3 % Loop over damping factor z
I=i(j);
setparam(tg,tg_I,I); % Set damping factor (Gain1/Gain)
start(tg); % Start model execution
pause(1.1*tg.StopTime);
outp = tg.OutputLog; % Upload output and
y = [y, outp(:, 1)]; % store in a matrix
s = [s, outp(:, 2)];
e = [e, outp(:, 3)];
t = tg.time; % Upload time vector
consigna=outp(:, 1);
posicion=outp(:, 2);
error=outp(:, 3);
SumErr=sum(abs(error));
save (['Control PI_',num2str(P),'_',num2str(I),'.mat'],'consigna','posicion','error','P','I','SumErr');
plot(t, outp(:, 1),'b',t, outp(:,2),'r'); % Plot data for current run
set(gca, 'XLim', [t(1), t(end)], 'YLim', [-50, 400]);
title(['DC Motor: Proportional Gain = ', num2str(P),' Integral Gain =',num2str(I)]);
xlabel('Time'); ylabel('Output');
drawnow;
saveas(gcf, ['P=', num2str(P),'_I=',num2str(I),'.fig'], 'fig') % Guarda las figuras
end
end
save Total.mat

1 Comment

Doesn't setparam require that you set to character vectors, not numeric values?

Sign in to comment.

Answers (1)

I'm sorry but I don't understand your question, you ask me if Setparam is ok?

3 Comments

I was getting confused between two functions with similar names.
You are using xpc which is Simulink Real-Time. For that product, using setparam is the correct way to set tunable parameters.
I was thinking about Simulink models not being used for real-time. When xpc is not being used, then set_param is the function used, and that function requires text values instead of numeric values. Very similar function name, different calling sequence.
I work in the control of a DC motor in real time by Simulink Real Time (Host-Target), the Script that I propose must make the automatic change of the PI control: P=1,I=0.01 after P=1,P=0.05 until P=0.1,I=0.1 but it is not doing it
Unfortunately, I do not have experience with Simulink Real-Time.

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!