I can’t make a C-block that implements something similar to a “state” of the system, where a variable persists across consecutive calls of the C-block. Is it possible to implement?
My first, initial and simple attempt was using persistent variables. Below is the code I used and I would appreciate any guidance or examples on how to properly use memory in a C-block within Scilab.
Please consider that the xcos model consists just on the C-block (with no input and just one output) connected to a scope to read its output.
Unfortunately, I’m encountering an error: “initialization problem: initial condition not converging.”
What am I doing wrong, and how can I fix it?
Thank you!
#include <machine.h>
#include <math.h>
void toto(flag,nevprt,t,xd,x,nx,z,nz,tvec,
ntvec,rpar,nrpar,ipar,nipar
,y1,ny1)
double *t,xd[],x[],z[],tvec[];
int *flag,*nevprt,*nx,*nz,*ntvec,*nrpar,ipar[],*nipar,*ny1;
double rpar[],y1[];
/* modify below this line */
{
static int count=0;
count++;
y1[0]=count;
}
Maybe @davidcl can continue the discussion, but to me, it is nonsense to yield this output as it will be completely dependent on the number of times the block is called by the solver. Moreover, for a continuous time simulation it will yield discrete values which will be interpreted as a singularity. So, please explain what you want to do.
I understand that the initial example I provided may not have a practical application, as the output would indeed depend on the number of times the solver calls the block. However, my intent was to illustrate a basic scenario to test the feasibility of using a persistent variable in a C-block to maintain state across consecutive calls.
Despite the example’s lack of practical sense, I expected to see the output reflect the number of times the block was called. The primary goal was to confirm whether the persistent variable could be used in a C-block to retain a variable’s value across multiple invocations.
For a more practical scenario, consider a C-block implementing an algorithm that needs to “remember” its state, such as tracking if an input has exceeded a specified threshold in the past. This requires the block to maintain state information between calls.
I would appreciate guidance on how to properly implement this state persistence in a C-block, regardless of the specific application where this feature might be used.
Thank you for your suggestion. I’ll try using the block->work field as you recommended. However, I have noticed that, unlike other blocks which have fairly detailed documentation and allow users to utilize them without knowing their implementation, the use of block4 is quite mysterious. I can’t find documentation in the official Scilab help, nor do I find examples, tutorials or templates on the internet. It seems like a function intended only for developers.
Nevertheless, I will try to deduce from your example how to use this block and will let you know
Additionally, it is difficult to understand without detailed knowledge what is the meaning of the various fields of the popup menu that appears when you double-click on the block. I couldn’t find templates for using the block in simpler cases, such as writing a simple C code block with memory, so where outputs are not solely dependent on instantaneous inputs but also on previous inputs, thanks to the ability to save information in variables that persist between block calls. I think that this would be a fairly common and useful use of the block, and it seems strange that achieving it requires such an in-depth study.
Each use-case is different and a block with memory is present in Xcos itself. I agree that Xcos documentation is sparse and requires you to play the examples to understand each block’s behaviour.
There might not be good documentation on writing block; I suggest you read the existing codebase to understand better how things work.