一个Scilab / Xcos模拟中的c_block的多个实例

问题描述

我遇到了有关Xcos c_block使用的问题。我开发了 具有以下C代码的c_block:

#include <machine.h>
#include <math.h>

void Ramp(flag,nevprt,t,xd,x,nx,z,nz,tvec,ntvec,rpar,nrpar,ipar,nipar,u1,nu1,y1,ny1)
 
 
      double *t,xd[],x[],z[],tvec[];
      int *flag,*nevprt,*nx,*nz,*ntvec,*nrpar,ipar[],*nipar,*nu1,*ny1;
      double rpar[],u1[],y1[];
/* modify below this line */
{

static double target     = 0;
static double inputDelta = 0;
static double out        = 0;

if(u1[0] != target)
{
        target = u1[0];

        if(target - y1[0] < 0)
        {
                inputDelta = y1[0] - target;
        }
        else
        {
                inputDelta = target - y1[0];
        }
}

if(target > y1[0])
{
        out += inputDelta*rpar[2]/rpar[0];
        if(out > target)
        {
                out = target;
        }
}
else if(target < y1[0])
{
        out -= inputDelta*rpar[2]/rpar[1];
        if(out < target)
        {
                out = target;
        }
}
       
y1[0] = out;

}

包含此块的Xcos仿真有效:

enter image description here

我的问题是,我需要在一个Xcos模拟中拥有该块的多个实例(每个实例具有不同的参数集)。我试图制作此块的多个副本 并为每个副本设置不同的参数值。这种幼稚的方法会导致所有实例的错误行为(所有实例给出的输出都完全相同,但该输出不对应于任何参数集)。

我的问题是,是否可能有一个实例的多个实例 一个模拟中的c_block?如果是这样,有人可以给我建议如何做吗?

解决方法

答案是,在给定的模拟中,可能有一个包含c代码的块的多个实例。我已经使用CBLOCK4块并在work结构中使用scicos_block指针来启动并运行它。该指针包含存储CBLOCK4的持久数据的堆上的某个地址。下面是上面代码的修改

#include "scicos_block4.h"

#define U  ((double *)GetRealInPortPtrs(block,1))
#define Y  ((double *)GetRealOutPortPtrs(block,1))

// parameters
#define Tu (GetRparPtrs(block)[0])
#define Td (GetRparPtrs(block)[1])
#define T  (GetRparPtrs(block)[2])

typedef struct
{
    double target;
    double inputDelta;
    double out;
}Ramp_work;

void Ramp(scicos_block *block,int flag)
{
 
  Ramp_work *work;

  if(flag == 4) 
  {
    /* init */
    if((*(block->work) = (Ramp_work*)scicos_malloc(sizeof(Ramp_work))) == NULL)
    {
        set_block_error(-16);
        return;
    }
    work = *(block->work);          
    work->target      = 0;
        work->inputDelta  = 0;
    work->out         = 0;

  }
  else if(flag == 1) 
  {

    work = *(block->work);  

    /* output computation */ 
    if(U[0] != work->target)
    {
        work->target = U[0];
        
        if(work->target - Y[0] < 0)
        {
            work->inputDelta = Y[0] - work->target;
        }
        else
        {
            work->inputDelta = work->target - Y[0];
        }
    }
    
    if(work->target > Y[0])
    {
        work->out += work->inputDelta*T/Tu;
        if(work->out > work->target)
        {
            work->out = work->target;
        }
    }
    else if(work->target < Y[0])
    {
        work->out -= work->inputDelta*T/Td;
        if(work->out < work->target)
        {
            work->out = work->target;
        }
    }
    
    Y[0] = work->out;  

  } 
  else  if (flag == 5) 
  {
    /* ending */
    scicos_free(*(block->work));
  }

}