【问题标题】:Multiple instances of c_block in one Scilab/Xcos simulation一个 Scilab/Xcos 仿真中的多个 c_block 实例
【发布时间】:2023-03-21 07:31:01
【问题描述】:

我遇到了关于 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 在一个模拟中?如果是这样,任何人都可以给我建议如何做到这一点?

【问题讨论】:

    标签: c matlab simulation scilab xcos


    【解决方案1】:

    答案是,在给定的模拟中,一个包含 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));
      }
    
    }
     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      • 2014-08-01
      相关资源
      最近更新 更多