How to implement discrete dynamic system in Xcos v2024.1.0?

I have been developing a Xcos simulation where I have a disturbance observer in discrete domain. My intention was to implement the observer itself as a Scilab function invoked in periodic manner from the scifunc_block_m block i.e. the block has an event port connected to the clock source. May I ask you for an advice how to properly handle the observer state. In other words I need to have some variables which values are persistent between the individual clock events.

I have noticed that the scifunc_block_m has some discrete state in the configuration dialog. Is it intended for such usage? If so, how can I access to the state in the function code? Thanks in advance for your answer.

I have decided to implement much more simple discrete system at the beginnig. I have attempted to implement the discrete first order low pass filter. So I have just defined the below given function in a separate .sci file

function [xnew, y1]=Digital_low_pass_filter(flag, x, u1, rpar, ipar)
    
    disp("ARGS: " + string(argn(2))); 
    disp("FLAG: " + string(flag));
    
    select flag
    case 1 then
    // system output update (occurs in every time step of the simulation)
    disp("Flag value: 1");
    y1 = x;
    xnew = x;
   
    case 2 then
    // system dicrete state update (occurs only at discrete event) 
    disp("Flag value: 2");
    T   = rpar(1);
    tau = rpar(2);
    a = T/(T + tau);
    b = 1 - a;
    xnew = a*u1 + b*x;
    y1 = [];
   
    case 3 then 
    // continuous state update
    y1 = [];
    xnew = x; 
   
    case 4 then
    // init
    disp("Flag value: 4");
    xnew = 0;
    y1 = [];
        
    case 5 then 
    // end of simulation 
    disp("Flag value: 5");
    y1 = []; 
    xnew = x; 
        
    case 6 then 
    // derivative computation
    disp("Flag value: 6");
    y1 = []; 
    xnew = x; 
        
    case 9 then 
    // zero-crossing
    disp("Flag value: 9");
    y1 = []; 
    xnew = x; 
        
    else
    // default
    disp("Unexpected flag value.");
    y1 = [];
    xnew = x;
    end
endfunction

Then I have prepared very simple Xcos model

The scifunc_block_m parameters have been set according to the attachment.

Unfortunately the simulation crashes. The diagnostic printing in the Console window occurs: “ARGS: 5“ and a warning occurs at the scifunc_block_m right down corner. Namely there is “Wrong number of input arguments.Expected 3.“ written.

May I ask you for an advice how to implement this simple discrete system via the scifunc_block_m?

Hello,

I think you are mixing up scifunc_block_m and generic_block3 (I recognize that generic_block3 is not sufficiently documented and misses examples). Maybe @davidcl can confirm ?

S.

Hello, thank you for your reaction. I have placed the scifunc_block_m block from the User defined functions category into my simulation. Why do you think I have used the generic_block3?

You have used a scifunc_block_m but your function prototype

[xnew,y1] = Digital_low_pass_filter(flag, x, u1, rpar, ipar)

has the argument list of functions used by the generic_block3 block. Inside a scifunc_block_m, if you call your function, there is no mecanism that would give you values to flag, x, u1, rpar, ipar (unless you pass actual arguments by defining them explicitely).

S.

I see. May I ask you for an advice how to modify the function prototype to be in accordance with the scifunc_block_m?

can you attach your diagram to your post (the one with the screenshots) ?

Digital_low_pass_filter.sci (1.2 KB)

Digital_low_pass.zcos (4.2 KB)

I have just attached the Xcos model along with the function definition.

Do you see anything wrong in the Xcos model or the function definition?

Hello,

After investiguation, it looks that flag is resolved to a Scilab macro. Calling string(flag) expect 3 output argument ; this is an issue.

To have a better error report, I enable proper call stack when debug is enabled. You will get the expected error and stacktrace (with lines). See Xcos: report Scilab intermediate errors in debug (!1680) · Merge requests · scilab / scilab · GitLab

In the dialog box of scifunc_block_m, the flag variable is not used by Xcos.

The flag variable you used here is the flag() function from colormaps. Thus the error.

Thank you very much for your reaction. Does it mean that I have to remove the flag parameter from the function header? If so what is the correct approach for control of the scifunc_block_m i.e. what is the mechanism how the solver interacts with the scifunc_block_m?