[go: up one dir, main page]

0% found this document useful (0 votes)
246 views31 pages

ScilabTec Xcos PDF

Xcos is a module in Scilab used for modeling and simulating hybrid dynamic systems using functional blocks, it allows combining continuous and discrete behaviors in models and includes standard blocks and the ability to create new custom blocks programmed in C, C++ or Scilab. Xcos provides a graphical interface based on JGraphX for modeling systems and includes features like an HDF5 data standard for exchanges and support for Modelica models through a free compiler.
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)
246 views31 pages

ScilabTec Xcos PDF

Xcos is a module in Scilab used for modeling and simulating hybrid dynamic systems using functional blocks, it allows combining continuous and discrete behaviors in models and includes standard blocks and the ability to create new custom blocks programmed in C, C++ or Scilab. Xcos provides a graphical interface based on JGraphX for modeling systems and includes features like an HDF5 data standard for exchanges and support for Modelica models through a free compiler.
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/ 31

Xcos

Hybrid dynamic systems modeler and


simulator
16th june 2010

Clément DAVID &


Yann COLLETTE

The Scilab Consortium

1
Xcos: Introduction

● Internal Scilab module

Freely available with Scilab (free


and open platform for numerical
computation)
● Purposes
Hybrid dynamic systems modeler
and simulator
Using functional black-boxes and
data/event links

2
Xcos: Features

● Graphically model, compile, and simulate dynamical systems


Combine continuous and discrete-time behaviors in the same model
Select model elements from Palettes of standard blocks
Program new blocks in C, Fortran, or Scilab Language

● Use standards
HDF5 standard which has been chosen to guarantee data exchanges
between Scilab and Xcos Editor
A free Modelica compiler which enables the simulation of implicit diagrams
A brand new graphical user interface based on JGraphX

3
Xcos: Overview

4
Xcos: Overview

5
Xcos: Overview

6
Xcos: Overview

7
Xcos: Overview

8
Xcos: Overview

9
Xcos: Overview

10
Xcos: Overview

11
Xcos: A simple block (1)
Controls

Inputs Xcos block Outputs

Commands

● Data links and data ports

Data communication is handled by data ports and data links (in black)
● Event links and event ports (discrete time)
Activation and control is performed through event ports and links (in red).

12
Xcos: A simple block (2)
Controls

BLOCK.sci block.c
Inputs runtime Outputs
parameters
evaluation
management
function

Commands

● An interface function (Scilab macro)

Used to configure the block when editing the diagram


● A functional implementation
Used to simulate the block in the simulation engine

13
Xcos: the new editor
Xcos editor

● Java-based diagram editor


● Ergonomic

● Run in parallel with Scilab interpreter

● Backward compatibility

Scicos compiler

Scicos Code
Scilab
simulator generation

14
Xcos: Reusing Scilab quality process

● Available in nightly builds


● Integrated in Scilab roadmap
● Use Scilab development flow
● Git revision system
● Code review
● Automatic nightly validation

15
Demo time,

any questions ?

www.scilab.org

16
ScilabTec 2010

Optimization of a PID regulator

17
Optimization of a PID regulator

The content of the xcos context:

if ~exists('w0') then w0 = 2*%pi*100; end


if ~exists('K0') then K0 = 0.01; end
if ~exists('m') then m = 0.5; end
if ~exists('P') then P = 1; end
if ~exists('I') then I = 1; end
block_output('values') = zeros(2000,2);
block_output('times') = zeros(2000,1);

18
Optimization of a PID regulator

importXcosDiagram('automatic_test.xcos');

function y = f_pid(x)
context.w0 = w0;
context.m = m;
context.K0 = K0;
context.P = x(1)*Pfact;
context.I = x(2)*Ifact;
Info = scicos_simulate(scs_m,list(),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; .
endfunction

19
Optimization of a PID regulator

Before optimization After optimization

20
Demo time,

any questions ?

www.scilab.org

21
ScilabTec 2010

Programming new blocks

22
Definition of a new C block
0
Goal: 0,5
Store N samples of the input in a buffer Buffer size
1
Ouput the buffer as a vector output of size 0,5
0
N -0,5
-1
-0,5
Parameters: 0
Padding value (real parameter - rpar) 0,5
1
Buffer size (int parameter - ipar)

We will first interface our block through a GENERIC


block (In the user defined palette).

23
Definition of a new C block
The simulation function side
1 – Initialization
#include "scicos_block4.h"
2 – Simulation finalization
void buffer_vect_xcos(scicos_block *block, int flag) 3 – Update block internal state
{
int nsamples = GetIparPtrs(block)[0];
4 – Put the value on the output port
double padding_value = GetRparPtrs(block)[0];

switch(flag)
{

case Initialization:
GetWorkPtrs(block) = (double *)MALLOC(sizeof(double)*nsamples);
for(int i=0;i<nsamples;i++) ((double *)GetWorkPtrs(block))[i] = padding_value;
1
break;
case Ending:
FREE(GetWorkPtrs(block)); 2
break;
case StateUpdate:
for(int i=1;i<nsamples;i++)
((double *)GetWorkPtrs(block))[i-1] = ((double *)GetWorkPtrs(block))[i]; 3
((double *)GetWorkPtrs(block))[nsamples-1] = ((double *)GetInPortRows(block,1))[0];
break;
case OutputUpdate:
for(int i=0;i<nsamples;i++)
((double *)GetOutPortPtrs(block,1))[i] = ((double *)GetWorkPtrs(block))[i]; 4
break;

}
}

24
Definition of a new C block
The interface function side (1/2)

function
[x,y,typ]=BUFFERVECT_c(job,arg1,arg2)
...
select job
case 'plot' then
standard_draw(arg1)
case 'getinputs' then
[x,y,typ] = standard_inputs(arg1)
case 'getoutputs' then
[x,y,typ] = standard_outputs(arg1)
case 'getorigin' then
[x,y] = standard_origin(arg1)
case 'set' then

A
case 'define' then

B
end
endfunction

25
Definition of a new C block
The interface function side (2/2)

case 'define' then case 'set' then


buf_size = 1; …
pad_val = 0.0; while %t do

model = scicos_model(); [ok,buf_size,pad_val,exprs] =


model.sim = list('buffer_vect',4); scicos_getvalue('Set parameters Block',..
model.in = 1; ['Buffer size';'Padding value'],
model.in2 = 1; list('vec',1,'vec',1),
model.intyp = 1; label);
model.outtyp = 1; if ~ok then break, end
model.out = buf_size; if (buf_size<1) message ...
model.out2 = 1;
model.evtin = 1; if ok then
model.ipar = [buf_size]; In = [model.in model.in2];
model.rpar = [pad_val]; [model,graphics,ok] = set_io(model,
model.blocktype = 'd'; Graphics,
model.dep_ut = [%t %f]; list(in,1), list(in,1),1,[]);

label = string([buf_size; pad_val]); if ok then


... graphics.exprs = label;
end model.ipar = [buf_size];
model.rpar = [pad_val];

break;
end
end
end

26
Some more custom blocks
The FFT scope
This block is not yet available under Scilab.
(Scilab 5.3)

Signal 1

Signal 2

27
External module hierarchy
XcosMod Building the blocks and build the palette
builder.sce
Etc
XcosMod.start Loading macros, libs, gateways and adding
XcosMod.quit the palette to xcos
macros
help The interfacing functions of the blocks +
en_US others functions
fr_FR
sci_gateway
C The simulation functions of the blocks +
Cpp other functions
Ffortran
src
C
Cpp
Fortran
tests
unit_tests
nonreg_tests

28
Palette loading process
A part of XcosMod/builder.sce
// Build xcos palette
// =========================================================

xpal = xcosPal("Buffer");
xpal = xcosPalAddBlock(xpal, 'FFT_SCOPE', module_dir + '/macros/img/fft_scope.jpg');
xpal = xcosPalAddBlock(xpal, 'PS_SCOPE', module_dir + '/macros/img/ps_scope.jpg');
xpal = xcosPalAddBlock(xpal, 'VECTOR_SCOPE', module_dir + '/macros/img/vector_scope.jpg');
xpal = xcosPalAddBlock(xpal, 'BUFFER_VECT', module_dir + '/macros/img/buffer_vect.jpg');
xpal = xcosPalAddBlock(xpal, 'REAL_FFT', module_dir + '/macros/img/real_fft.jpg');
xpal = xcosPalAddBlock(xpal, 'INVERSE_FFT', module_dir + '/macros/img/inverse_fft.jpg');
xpal = xcosPalAddBlock(xpal, 'WINDOW_FUNC', module_dir + '/macros/img/window_func.jpg');

xcosPalExport(xpal, module_dir + '/Buffer.xpal');

A part of XcosMod/etc/XcosMod.start
// load palette
// ======================================

xcosPalAdd(module_dir + '/Buffer.xpal');

29
Demo time,

any questions ?

www.scilab.org

30
Thanks for your attention,

any questions ?

www.scilab.org

31

You might also like