This guide shows how to build custom Xcos block which can be used for code generation and host simulation mode
Before MicroDAQ Xcos block creation a requirements defining functionality and block interface has to be defined. Our new block will be described by the following properties:
Our custom block will be named 'My_step', it has three parameters 'Step time', 'Initial value', 'Final value' and 'Terminate value'. All block parameters are scalars, and has default values: 'Step time' - 1, 'Initial value' - 0, 'Final value' - 1, 'Terminate value' - 0. Block shall have one output port, output port size is 1. Block doesn't have input ports.
Custom 'Step' block step by stepSet block properties:
This step will generate block C code. Also block macros defining block properties and block behaviour in simulation mode ()'_sim' postfix) are generated. Functions generate following files:
Generated C code file is compiled, resulting DSP library will be used to build DSP executable created from Xcos diagram containing 'My_step' block.
Generated C source code contains 'init', 'inout' and 'end' functions. Function 'init' is executed when model is loaded on target. This function can initialize peripherals, allocate resources for block etc. The 'inout' function contains code which will be executed on every model step. The 'end' function is executed when model execution is done or it is stopped with Xcos 'Stop' button
The code uses get_scicos_time() function to get current simulation time. Depending on step_time block parameter block output (*y1) is set to final_value or initial_value.
If errors found during compilation correct them and run mdaqBlockBuild() again.
If block.use_sim_script is set to false (%F - by default) then C source will be compiled to shared library which will be used in simulation mode. It guarantees the same code on host (simulation mode) and target (code generation) side. In order to use this option the external C compiler is required. List of supported compilers can be found at: https://help.scilab.org/doc/5.5.2/en_US/supported_compilers.html. If compiler cannot be provided or user want to have different behaviour in simulation mode, the _sim.sci script can be used instead.
Generated block files can be used to implement block code for standard simulation mode. In order to add code for simulation mode generated mdaq_my_step_sim.sci file needs to be edited. The mdaq_my_step_sim.sci file contains code which will be executed in simulation mode. Function mdaqBlockAdd() generates file which contains block parameters variables which has to be used to change block output.
Generated mdaq_my_step.sci file:
// Generated with MicroDAQ toolbox ver: 1.1. function block=mdaq_my_step_sim(block, flag) global %microdaq if %microdaq.dsp_loaded == %F then step_time = block.rpar(1); initial_value = block.rpar(2); final_value = block.rpar(3); terminate_value = block.rpar(4); select flag case -5 // Error case 0 // Derivative State Update case 1 // Output Update case 2 // State Update case 3 // OutputEventTiming case 4 // Initialization case 5 // Ending case 6 // Re-Initialisation case 9 // ZeroCrossing else // Unknown flag break end end endfunction |
Similar to C source file we have to add Scilab code which will be performing 'step' function.
Add the following code to 'Output Update' case in generated mdaq_my_step_sim.sci file
if scicos_time() > step_time then block.outptr(1) = final_value; else block.outptr(1) = initial_value; end |