simulink仿真demo臨摹筆記之在stateflow中使用總線(結構體)&集成自定義代碼

臨摹對象

sfbus_demo

 

模型簡介

先看一下整體外觀:

再看看stateflow的chart:

 

功能說明

重點在於,stateflow中能使用總線信號作爲輸入。圖形函數的參數可以設置數據類型。

 

重要步驟

1,PreLoadFcn的回調函數中,要加載bus_objects:

加載後,工作區中會有這3個總線的定義:

 

2,設置輸入、輸出和局部變量的數據類型爲總線型:

 

3,設置圖形函數的輸入輸出變量的數據類型爲總線(SIGNALBUS, COUNTERBUS)

 

4,在Simulation Target的配置中,添加頭文件(#include "counterbus.h")和C文件(counterbus.c)

這兩個文件裏面定義了總線所對應的結構體,以及counterbusFcn函數。

 

counterbus.h頭文件:


#ifndef _COUNTER_BUS_H_
#define _COUNTER_BUS_H_

#include "rtwtypes.h"

typedef struct {
  int input;
} SIGNALBUS;

typedef struct {
  int upper_saturation_limit;
  int lower_saturation_limit;
} LIMITBUS;

typedef struct {
  SIGNALBUS inputsignal;
  LIMITBUS limits;
} COUNTERBUS;

extern void counterbusFcn(COUNTERBUS *u1, int u2, COUNTERBUS *y1, int *y2);

#endif

 

counterbus.c文件:

#include "counterbus.h"

void counterbusFcn(COUNTERBUS *u1, int32_T u2, COUNTERBUS *y1, int32_T *y2)
{
  int32_T limit;
  boolean_T inputGElower;  
  
  limit = u1->inputsignal.input + u2;

  inputGElower = (limit >= u1->limits.lower_saturation_limit);

  if((u1->limits.upper_saturation_limit >= limit) && inputGElower) {
    *y2 = limit;
  } else {

    if(inputGElower) {
      limit = u1->limits.upper_saturation_limit;
    } else {
      limit = u1->limits.lower_saturation_limit;
    }
    *y2 = limit;
  }

  y1->inputsignal.input = *y2;
  y1->limits = u1->limits;

}

 

 

參考資料

Integrate Custom Structures in Stateflow Charts

https://ww2.mathworks.cn/help/stateflow/ug/integrating-custom-structures-in-stateflow-charts.html

 

Access Bus Signals Through Stateflow Structures

https://ww2.mathworks.cn/help/stateflow/ug/about-stateflow-structures.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章