Script does not work automatically
Show older comments
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
Walter Roberson
on 3 Mar 2022
Doesn't setparam require that you set to character vectors, not numeric values?
Answers (1)
Ruben Dario Ramirez Serna
on 3 Mar 2022
Edited: Ruben Dario Ramirez Serna
on 3 Mar 2022
0 votes
3 Comments
Walter Roberson
on 3 Mar 2022
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.
Ruben Dario Ramirez Serna
on 3 Mar 2022
Walter Roberson
on 3 Mar 2022
Unfortunately, I do not have experience with Simulink Real-Time.
Categories
Find more on Multicore Processor Targets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!