CBLOCK2模块

示例下载
实现一个无需外接触发信号,就能够自我输出50Hz正弦波信号的模型
该模型内置两个固定的放大增益参数,浮点型参数5.0和整形参数2,因此输出的正弦波最大幅值为10
分别由模型中的参数Real parameters vector和Integer parameters vector来代表

所需元件:
Sources -> CLOCK_c
User-Defined Functions -> CBLOCK
Sinks -> CSCOPE

CBLOCK2示例

各模块参数
尤其需要注意的是,Time dependence必须设置为y,以确保CBLOCK2的代码在每个步长的仿真周期都会被调用执行

Simulation->Setup参数

CBLOCK2代码

int toto_bloc_outputs(scicos_block *block,int flag)
{
  double current_time = get_scicos_time();
  double* output_wave = *block->outptr;
  output_wave[0] = (*block->rpar) * (*block->ipar) * sin(2*M_PI*50*current_time);
return 0;}

代码中,有几个地方需要特别注意:

  1. 确保默认的#include <scicos_block.h>已经被自动添加,该头文件包含用来获取当前仿真时间函数的声明double get_scicos_time();或者也可以添加#include <scicos.h>,同样包含函数double get_scicos_time()的声明。
  2. CBLOCK2目前有Bug,需要在CBLOCK2代码的头部,手动添加下面三个函数的声明。否则当编译时,编译器发现无法引用,会报错。

  1. flag == 1代表了每次步长时执行的代码;flag == 4代表了初始化时执行的代码;flag == 5代表了仿真结束时执行的代码;其分别各自调用了三个不同的函数名供用户编程使用,以便阅读:

flag==4 -> int toto_bloc_init(scicos_block *block,int flag)
flag==1 -> int toto_bloc_outputs(scicos_block *block,int flag)
flag==5 -> int toto_bloc_ending(scicos_block *block,int flag)
我们只需要将每个步长需要执行的代码添加到上述函数toto_bloc_outputs中即可
本例没有设计启动和终止阶段的代码,因此对应部分留空

  1. 函数入参*block为一个结构体指针,flag为事件类型

结构体

CBLOCK2在#scilab#path/include/scilab/scicos_block.h中定义了一个结构体

typedef struct
{
    int nevprt;
    voidg funpt ;
    int type;         函数类型,c语言函数总是为4
    void* 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;
    double **inptr;   输入信号数据
    int nout;         输出信号数量
    int *outsz;
    double **outptr;  输出信号数据
    int nevout;
    double *evout;
    int nrpar;        模型入参Real parameters vector数量
    double *rpar;     模型入参Real parameters vector数据
    int nipar;        模型入参Integer parameters vector数量
    int *ipar;        模型入参Integer parameters vector数据
    int nopar;
    int *oparsz;
    int *opartyp;
    void **oparptr;
    int ng;           过零函数数量,即为模块入参Number of zero crossing surfaces
    double *g;        过零函数指针,例如block->g = myfunction();
    int ztyp;
    int *jroot;       过零检测,jroot[i] = 0:第i个过零函数没有发生过零;1:从负变正;-1从正变负
    char *label;
    void **work;
    int nmode;
    int *mode;
    char *uid;
} scicos_block;

事件类型

CBLOCK2支持预先定义的各类事件,以flag数值进行识别

flag == 1(输出更新)
flag == 2(状态更新)
flag == 4(初始化)
flag == 5(结束)
flag == 9(自定义过零面)

上述示例基于版本Scilab 2026.0.1

最后修改:2026 年 05 月 10 日
如果觉得我的文章对你有用,请随意赞赏