我遇到了一个关于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模拟工作:

我的问题是,我需要在一个Xcos模拟中拥有这个块的多个实例(每个实例都有不同的参数集)。我已经尝试制作了这个块的几个副本,并为每个副本设置了不同的参数值。这种天真的方法导致了所有实例的错误行为(所有实例都给出了完全相同的输出,但此输出并不对应于任何一组参数)。
我的问题是,是否有可能在一次模拟中拥有一个c_block的多个实例?如果是这样,有谁能给我一个如何做到这一点的建议?
发布于 2020-10-06 15:56:09
答案是,在给定的模拟中,有可能具有包含C代码的一个块的多个实例。我已经用CBLOCK4块和scicos_block结构中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));
}
}https://stackoverflow.com/questions/64074900
复制相似问题