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

一个Scilab/Xcos模拟中的多个c_块实例,c,matlab,simulation,scilab,xcos,C,Matlab,Simulation,Scilab,Xcos,我遇到了一个关于Xcos c_块使用的问题。我发展了 具有以下c代码的c_块: #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 *

我遇到了一个关于Xcos c_块使用的问题。我发展了 具有以下c代码的c_块:

#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;

}
#包括
#包括
无效斜坡(标志、nevprt、t、xd、x、nx、z、nz、tvec、,
ntvec、rpar、nrpar、ipar、nipar
,u1,nu1,y1,ny1)
双*t,xd[],x[],z[],tvec[];
int*flag、*nevprt、*nx、*nz、*ntvec、*nrpar、ipar[]、*nipar、*nu1、*ny1;
双rpar[],u1[],y1[];
/*在这行下面修改*/
{
静态双目标=0;
静态双输入增量=0;
静态双输出=0;
如果(u1[0]!=目标)
{
目标=u1[0];
if(目标-y1[0]<0)
{
inputDelta=y1[0]-目标;
}
其他的
{
inputDelta=目标-y1[0];
}
}
如果(目标>y1[0])
{
out+=inputDelta*rpar[2]/rpar[0];
如果(输出>目标)
{
out=目标;
}
}
否则如果(目标
包含此块的Xcos模拟工作:

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

我的问题是,是否可能有一个的多个实例
一次模拟中的c_块?如果是这样的话,有人能给我一个建议吗?

答案是,在给定的模拟中,一个块可能有多个包含c代码的实例。我已经建立并运行了CBLOCK4块,并在
scicos_块
结构中使用了
work
指针。该指针包含堆上存储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));
  }

}
 
#包括“scicos_block4.h”
#定义U((双*)GetRealInPortPtrs(块,1))
#定义Y((双*)GetRealOutPortPtrs(块,1))
//参数
#定义Tu(GetRparPtrs(块)[0])
#定义Td(GetRparPtrs(块)[1])
#定义T(GetRparPtrs(块)[2])
类型定义结构
{
双重目标;
双输入增量;
加倍;
}斜坡工程;
无效渐变(scicos_块*块,int标志)
{
斜坡工作*工作;
如果(标志==4)
{
/*初始化*/
如果((*(块->工作)=(斜坡工作*)scicos(斜坡工作))==NULL
{
设置块错误(-16);
返回;
}
工作=*(块->工作);
工作->目标=0;
工作->输入增量=0;
工作->外出=0;
}
else if(标志==1)
{
工作=*(块->工作);
/*输出计算*/
如果(U[0]!=工作->目标)
{
工作->目标=U[0];
如果(工作->目标-Y[0]<0)
{
工作->输入增量=Y[0]-工作->目标;
}
其他的
{
工作->输入增量=工作->目标-Y[0];
}
}
如果(工作->目标>Y[0])
{
工作->输出+=工作->输入增量*T/Tu;
如果(工作->外出->工作->目标)
{
工作->外出=工作->目标;
}
}
否则如果(工作->目标输出-=工作->输入增量*T/Td;
如果(工作->外出<工作->目标)
{
工作->外出=工作->目标;
}
}
Y[0]=工作->外出;
} 
else if(标志==5)
{
/*结束*/
scicos_自由(*(块->工作));
}
}