How to develop own block in Scilab/Xcos with C language usage

886 Views Asked by At

I have been developing my own block in Scilab/Xcos with the CBLOCK4 usage. The C language code inside the block is following:

#include <stdio.h>
#include "scicos_block4.h"

#define U  ((SCSREAL_COP *)GetRealInPortPtrs(block, 1))
#define Y  ((SCSREAL_COP *)GetRealOutPortPtrs(block, 1))
#define X  ((SCSREAL_COP *)GetState(block))
#define dX ((SCSREAL_COP *)GetDerState(block))
#define Xk ((SCSREAL_COP *)GetDstate(block))
#define W  ((SCSREAL_COP *)GetWorkPtrs(block))

// parameters
#define N (GetIparPtrs(block)[0])

typedef struct
{
    double *buffer;
    double *sum;
    int    *index;
}MovingAverage_t;

FILE *f;

void MovingAverage(scicos_block *block,int flag)
{

  MovingAverage_t *ptr;
  int bufferPos;

  if(flag == 4) 
  {
    /* init */

    f = fopen("Debug.txt", "w");

    if((*(block->work) = (MovingAverage_t *)scicos_malloc(sizeof(MovingAverage_t))) == NULL)
    {
        set_block_error(-16);
        return;
    }
    ptr = (MovingAverage_t*)*block->work;       

    fprintf(f, "ptr: %ld \n", ptr);

    if((ptr->buffer = (double*)scicos_malloc(sizeof(double)*N)) == NULL)
    {
        scicos_free(*(block->work));
        fclose(f);
        set_block_error(-16);
        return;
    }
    
    if((ptr->sum = (double*)scicos_malloc(sizeof(double))) == NULL)
    {
        scicos_free(*(ptr->buffer));
        scicos_free(*(block->work));
        fclose(f);
        set_block_error(-16);
        return;
    }

    if((ptr->index  = (int*)scicos_malloc(sizeof(int))) == NULL)
    {
        scicos_free(*(ptr->buffer));
        scicos_free(*(ptr->sum));
        scicos_free(*(block->work));
        fclose(f);
        set_block_error(-16);
        return;
    }

    fprintf(f, "ptr->buffer: %ld \n", ptr->buffer);
    fprintf(f, "ptr->sum: %ld \n", ptr->sum);
    fprintf(f, "ptr->index: %ld \n", ptr->index);

    int i;
    for(i = 0; i < N; i++)
    {
        ptr->buffer[i] = 0;
    }
    *(ptr->sum) = 0;
    *(ptr->index) = 0;

    for(i = 0; i < N; i++)
    {
        fprintf(f, "buffer[%d]: %f\n", i, ptr->buffer[i]);
    }

    fprintf(f, "*(ptr->sum): %f\n", *(ptr->sum));
    fprintf(f, "*(ptr->index): %d\n", *(ptr->index));
        
  }
  else if(flag == 1) 
  {
    fprintf(f, "In: %f \n", U[0]);  

    ptr->buffer[0] = U[0];

    int i;
    for(i = 0; i < N; i++)
    {
        fprintf(f, "buffer[%d]: %f\n", i, ptr->buffer[i]);
    }
    
  } 
  else  if (flag == 5) 
  {
    /* ending */
    scicos_free(*(block->work));
    fclose(f);
  }
}

I also attach the scicos_block structure for the completeness:

typedef struct
{
    int nevprt;
    voidg funpt;
    int type;
    int scsptr;
    int nz;
    double *z;
    int noz;
    int *ozsz;
    int *oztyp;
    void **ozptr;
    int nx;
    double *x;
    double *xd;
    double *res;
    int *xprop;
    int nin;
    int *insz;
    void **inptr;
    int nout;
    int *outsz;
    void **outptr;
    int nevout;
    double *evout;
    int nrpar;
    double *rpar;
    int nipar;
    int *ipar;
    int nopar;
    int *oparsz;
    int *opartyp;
    void **oparptr;
    int ng;
    double *g;
    int ztyp;
    int *jroot;
    char *label;
    void **work;
    int nmode;
    int *mode;
    char *uid;
}scicos_block;

I am able to successfully compile the code but in case I run the simulation containing my CBLOCK4 block I always receive following error message:

Warning !!! Scilab has found a critical error (EXCEPTION_ACCESS_VIOLATION) with "scicosim" function. Save your data and restart Scilab.

and the simulation does not either start. I have found that the simulation starts working in case I remove following lines of code inside the CBLOCK4 block:

ptr->buffer[0] = U[0];

int i;
for(i = 0; i < N; i++)
{
    fprintf(f, "buffer[%d]: %f\n", i, ptr->buffer[i]);
}

I have doubts that I have some bug inside my C code (probably regarding the memory allocation) but I can't find it. Does anybody have experience with the Scilab/Xcos CBLOCK4 usage?

1

There are 1 best solutions below

0
On

I experienced it can be quite hard to get good info/examples on cblock4, but after a lot of googling, I now use the following for a buffer implementation (takes a real in, outputs a vector of N last samples):

#include "scicos_block4.h"

#define r_IN(n, i)  ((GetRealInPortPtrs(block, n+1))[(i)]) 
#define r_OUT(n, i) ((GetRealOutPortPtrs(block, n+1))[(i)]) 

#define in          (r_IN(0, 0))                // input, single real
#define out(n)      (r_OUT(0, n))               // output, vector(1,N) real
#define nSamples    (GetOutPortSize(block, 1, 2))

#define setting0    (GetIparPtrs(block)[0])
#define X           (GetState(block)[0])        // state 
#define Xdot        (GetDerState(block)[0])     // derivative of the state 
#define surface0    (GetGPtrs(block)[0]) 
#define mode0       (GetModePtrs(block)[0]) 
#define wrk(i)      (((double*)GetWorkPtrs(block))[i])

void buffer(scicos_block *block, int flag)
{
    int i;
    switch(flag){
        case 0:            // calculate derivative
            break;
        case 1:            // calculate output
            for(i=1; i<nSamples; i++) out(i-1)=out(i);
            out(nSamples-1)=in;
            break;
        case 2:            // update states
            break;
        case 3:            // event
            break;
        case 4:            // init
            break;
        case 5:            // done, deinit
            break;
        case 6:            // re-initialization
            break;
        case 9:            // calculate surfaces and modes
            break;
    }
}

notice there is no malloc, it just uses the output as the buffer...