0% found this document useful (0 votes)
2K views

Xcos - Examples - PID - Scilab Wiki

This document summarizes an example of using Xcos API to optimize the parameters of a PID controller. It initializes Xcos, loads a diagram, and defines initial parameter values. It then defines an objective function to minimize error and an optimization wrapper. Optimization is performed to find better parameter values, and a final simulation is run with the optimized values.

Uploaded by

Almir Vinicius
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Xcos - Examples - PID - Scilab Wiki

This document summarizes an example of using Xcos API to optimize the parameters of a PID controller. It initializes Xcos, loads a diagram, and defines initial parameter values. It then defines an objective function to minimize error and an optimization wrapper. Optimization is performed to find better parameter values, and a final simulation is run with the optimized values.

Uploaded by

Almir Vinicius
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

04/01/2019 Xcos/Examples/PID - Scilab Wiki

Contents
RecentChanges
FindPage
HelpContents
Xcos/Examples/PID

Editar
Informação
Anexos
Mais Acções

Xcos Example: Optimization of a PID


This page presents an example of Xcos API use.

We will compute the optimal parameters of a PID controller.

All the necessary files are available in the archive PID.zip.

In particular, the 'automatic_test.zcos' file contains the schema used in this example:

First, we initialize Xcos by loading some important Xcos libraries. Then, we load our diagram using
importXcosDiagram, and set some initial parameters:

loadXcosLibs();

importXcosDiagram('automatic_test.zcos');

w0 = 2*%pi*100;
m = 0.5;
K0 = 0.1;

The goal here will be to find better values for w0, m, and K0 that give optimal results.

The goal here will be to find better values for w0, m, and K0 that give optimal results.

Let's define some initial parameters.

MaxEvalFunc = 10;
Pfact = 1;
Ifact = 1;
h_step = 1e-3; // the delta used to compute finite difference derivative
x0 = [1;5]; // Initial parameters

https://wiki.scilab.org/Xcos/Examples/PID 1/4
04/01/2019 Xcos/Examples/PID - Scilab Wiki
// P is proportionnal to Pfact
// I is proportionnal to Ifact
Lower = [0.01;0.01];
Upper = [1000; 100];

global Iter;
Iter = 0;

Once this is done, we define two functions:

the objective function (f_pid)


the wrapper between the objective function and the 'optim' Scilab function

The objective function (we minimize the error between the reference signal (the square generator) and
the output signal. We also try to minimize the derivative of the output signal (so as ta have some kind
of smooth command).

function y = f_pid(x)
global Iter;
%scicos_context.w0 = w0;
%scicos_context.m = m;
%scicos_context.K0 = K0;
%scicos_context.P = x(1)*Pfact;
%scicos_context.I = x(2)*Ifact;
Iter = Iter + 1;
Info = scicos_simulate(scs_m,list(),%scicos_context,flag='nw');
y_error = mean(abs((block_output('values')(:,1) - block_output('values')
(:,2))));
y_diff = mean(abs(diff(block_output('values')(:,2))));
y = 0.5*y_error + 0.5*1*y_diff; ...
printf('Evaluation %d - P = %f I = %f y = %f (y_error = %f, y_diff =
%f)\n',Iter,x(1),x(2),y,y_error,y_diff);
endfunction

For the optim wrapper, we compute the value of the objective function and the gradient (using the
Scilab derivative function).

function [f,df,ind] = my_optim_pid(x,ind)


f = f_pid(x);
df = derivative(f_pid,x,h=h_step,order=4);
endfunction

Once these functions are defined, we launch a first simulation to see the initial behavior.

%scicos_context = [];
%scicos_context.w0 = w0;
%scicos_context.m = m;
%scicos_context.K0 = K0;
%scicos_context.P = Pfact*x0(1);
%scicos_context.I = Ifact*x0(2);

Info = scicos_simulate(scs_m,list(),%scicos_context);

https://wiki.scilab.org/Xcos/Examples/PID 2/4
04/01/2019 Xcos/Examples/PID - Scilab Wiki

The behavior shown by Xcos.

Now, we launch the optimization

[f_opt, x_opt] =
optim(my_optim_pid,'b',Lower,Upper,x0,algo='qn','ar',MaxEvalFunc,MaxEvalFunc,1e-
3,1e-3,[1e-3;1e-3]);

Finally, we launch a simulation using the optimized parameters to see the behavior of the loop.

// We find a good solution, but it doesn't take into account the stability of the
system.
%scicos_context.w0 = w0; ...
%scicos_context.m = m; ...
%scicos_context.K0 = K0; ...
%scicos_context.P = x_opt(1)*Pfact;
%scicos_context.I = x_opt(2)*Ifact;

x_opt = [Pfact;Ifact].*x_opt;

Info = scicos_simulate(scs_m,Info,%scicos_context);

https://wiki.scilab.org/Xcos/Examples/PID 3/4
04/01/2019 Xcos/Examples/PID - Scilab Wiki

The behavior shown by Xcos.

CategoryXcos

public: Xcos/Examples/PID (última edição 2015-08-31 15:05:07 efectuada por [email protected])

https://wiki.scilab.org/Xcos/Examples/PID 4/4

You might also like