Advanced Techniques: Custom Functions and Macros in Scicos Block Editor
Overview
Custom functions (Scilab or C) and macros let you extend Scicos/Xcos with user-defined blocks that implement tailored computations, discrete/continuous dynamics, event handling, and optimized performance.
When to use
- Implement specialized dynamics or control laws not available in palettes
- Improve simulation speed with compiled C functions
- Expose parameters, states, zero-crossings, or multiple outputs not supported by standard blocks
- Create reusable blocks and palettes for team workflows
Block types
- Scilab computational blocks (sci*): easier to write and debug; run interpreted.
- C computational blocks (C): higher performance; require a C compiler and correct Scicos block API.
- Modelica/Compiled/External S-functions: for complex or third-party code integration.
Key concepts and APIs
- scicos_block structure: contains pointers for inputs, outputs, states, parameters, modes, work vectors.
- Macros/utilities ©: GetRealInPortPtrs, GetRealOutPortPtrs, GetRparPtrs, GetState, GetDstate, GetGPtrs, GetModePtrs.
- Supervisor utilities (Scilab): curblock, getblocklabel, getscicosvars, phase_simulation, scicos_time, set_blockerror.
- Simulation phases (flag values): initialization, outputs, derivatives, zero-crossing computation, termination — implement behavior per flag.
Implementation pattern (concise)
- Define block prototype in Scicos editor: set number/types of ports, states, parameters, and event surfaces.
- Provide computational function:
- Scilab: write a .sci function matching scicos_block API (use sci_struct conventions).
- C: implement function void blockname(scicosblockblk, int flag) using macros to access data and switch(flag) for phases.
- Handle modes/zero-crossings: compute surf[] and set mode[] appropriately to guide solver.
- Register outputs, derivatives, and update states according to phase.
- Create .sci or XML descriptor and include icon/graphics settings for palette.
- Compile (if C) and add to custom palette; test with unit models.
Practical tips
- Start with a Scilab implementation to validate logic, then port to C for speed.
- Keep parameter vectors (rpar/ipar) compact and document ordering.
- Use zero-crossing surfaces for discontinuities to ensure solver accuracy.
- Use getblocklabel/curblock for runtime introspection during debugging.
- Provide meaningful block icons and default parameter values for usability.
Example skeleton (C computational block)
c
#include “scicos_block.h” void myblock(scicos_block blk, int flag) { double u = GetRealInPortPtrs(blk,1); double y = GetRealOutPortPtrs(blk,1); double rpar = GetRparPtrs(blk); double state = GetState(blk); int *mode = GetModePtrs(blk); switch(flag) { case 1: // initialization // allocate/init state break; case 2: // derivatives // compute state derivatives break; case 3: // outputs // compute outputs break; case 4: // zero-crossings // compute surf[] break; case 5: // termination break; } }
Resources
- Scilab/Xcos online help: “Programming xcos Blocks” and xcos manual.
- Tutorials: “Customizing Xcos with new Blocks and Palette” and example C-block tutorials.
If you want, I can provide a full Scilab example block implementing a PID with anti-windup (Scilab code) or a C skeleton adapted to a specific model — tell me which.
Leave a Reply