Xcos Hybrid dynamic systems modeler and simulator
Transcrição
Xcos Hybrid dynamic systems modeler and simulator
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 Inputs BLOCK.sci block.c parameters management runtime evaluation 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 Outputs Xcos: the new editor Xcos editor Java-based diagram editor ● Ergonomic ● Run in parallel with Scilab interpreter ● Backward compatibility ● Scicos compiler Scilab 14 Scicos simulator Code generation Xcos: Reusing Scilab quality process ● Available in nightly builds ● Integrated in Scilab roadmap ● Use Scilab development flow 15 ● Git revision system ● Code review ● Automatic nightly validation 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 if ~exists('K0') then K0 if ~exists('m') then m if ~exists('P') then P if ~exists('I') then I block_output('values') = block_output('times') = 18 = 2*%pi*100; end = 0.01; end = 0.5; end = 1; end = 1; end zeros(2000,2); zeros(2000,1); 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 20 After optimization Demo time, any questions ? www.scilab.org 21 ScilabTec 2010 Programming new blocks 22 Definition of a new C block Goal: Store N samples of the input in a buffer Ouput the buffer as a vector output of size N Parameters: Padding value (real parameter - rpar) Buffer size (int parameter - ipar) Buffer size 0 0,5 1 0,5 0 -0,5 -1 -0,5 0 0,5 1 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 #include "scicos_block4.h" void buffer_vect_xcos(scicos_block *block, int flag) { int nsamples = GetIparPtrs(block)[0]; double padding_value = GetRparPtrs(block)[0]; 1 – Initialization 2 – Simulation finalization 3 – Update block internal state 4 – Put the value on the output port switch(flag) { case Initialization: GetWorkPtrs(block) = (double *)MALLOC(sizeof(double)*nsamples); for(int i=0;i<nsamples;i++) ((double *)GetWorkPtrs(block))[i] = padding_value; break; 1 case Ending: FREE(GetWorkPtrs(block)); break; 2 case StateUpdate: for(int i=1;i<nsamples;i++) ((double *)GetWorkPtrs(block))[i-1] = ((double *)GetWorkPtrs(block))[i]; ((double *)GetWorkPtrs(block))[nsamples-1] = ((double *)GetInPortRows(block,1))[0]; break; 3 case OutputUpdate: for(int i=0;i<nsamples;i++) ((double *)GetOutPortPtrs(block,1))[i] = ((double *)GetWorkPtrs(block))[i]; break; } } 24 4 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 buf_size = 1; pad_val = 0.0; model = scicos_model(); model.sim = list('buffer_vect',4); model.in = 1; model.in2 = 1; model.intyp = 1; model.outtyp = 1; model.out = buf_size; model.out2 = 1; model.evtin = 1; model.ipar = [buf_size]; model.rpar = [pad_val]; model.blocktype = 'd'; model.dep_ut = [%t %f]; label = string([buf_size; pad_val]); ... end case 'set' then … while %t do [ok,buf_size,pad_val,exprs] = scicos_getvalue('Set parameters Block',.. ['Buffer size';'Padding value'], list('vec',1,'vec',1), label); if ~ok then break, end if (buf_size<1) message ... if ok then In = [model.in model.in2]; [model,graphics,ok] = set_io(model, Graphics, list(in,1), list(in,1),1,[]); if ok then graphics.exprs = label; 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 builder.sce Etc XcosMod.start XcosMod.quit macros help en_US fr_FR sci_gateway C Cpp Ffortran src C Cpp Fortran tests unit_tests nonreg_tests 28 Building the blocks and build the palette Loading macros, libs, gateways and adding the palette to xcos The interfacing functions of the blocks + others functions The simulation functions of the blocks + other functions Palette loading process A part of XcosMod/builder.sce // Build xcos palette // ========================================================= xpal xpal xpal xpal xpal xpal xpal xpal = = = = = = = = xcosPal("Buffer"); xcosPalAddBlock(xpal, xcosPalAddBlock(xpal, xcosPalAddBlock(xpal, xcosPalAddBlock(xpal, xcosPalAddBlock(xpal, xcosPalAddBlock(xpal, xcosPalAddBlock(xpal, 'FFT_SCOPE', 'PS_SCOPE', 'VECTOR_SCOPE', 'BUFFER_VECT', 'REAL_FFT', 'INVERSE_FFT', 'WINDOW_FUNC', module_dir module_dir module_dir module_dir module_dir module_dir module_dir xcosPalExport(xpal, module_dir + '/Buffer.xpal'); A part of XcosMod/etc/XcosMod.start // load palette // ====================================== xcosPalAdd(module_dir + '/Buffer.xpal'); 29 + + + + + + + '/macros/img/fft_scope.jpg'); '/macros/img/ps_scope.jpg'); '/macros/img/vector_scope.jpg'); '/macros/img/buffer_vect.jpg'); '/macros/img/real_fft.jpg'); '/macros/img/inverse_fft.jpg'); '/macros/img/window_func.jpg'); Demo time, any questions ? www.scilab.org 30 Thanks for your attention, any questions ? www.scilab.org 31