其他分享
首页 > 其他分享> > Simulink代码生成——多速率任务调度

Simulink代码生成——多速率任务调度

作者:互联网

Simulink代码生成——多速率任务调度

单任务统一采样时间生成代码

通过如下简单模型测试,设定离散步长0.01,模型中有三个out输出信号,设置采样全部为-1(继承),那么模型中所有的采样速率均为0.01。为了方便代码阅读,对模型进行了一些简单的设置,定义了输入BUS,AliasType。

在这里插入图片描述

直接生成代码如下:

#include "Demo1.h"
INPUT stInput;                         
ExtY_Demo1_T Demo1_Y;
RT_MODEL_Demo1_T Demo1_M_;
RT_MODEL_Demo1_T *const Demo1_M = &Demo1_M_;
void Demo1_step(void)
{
  Demo1_Y.Out1 = (U8)((U32)stInput.paraA + stInput.paraB);
  Demo1_Y.Out2 = (U8)(stInput.paraA - stInput.paraB);
  Demo1_Y.Out3 = (U8)((U32)stInput.paraA * stInput.paraB);
}
void Demo1_initialize(void)
{
  rtmSetErrorStatus(Demo1_M, (NULL));
}

单任务中设置不同采样时间生成代码

设置了三种不同采样速率,output2和output3采样速率分别设置为0.1和0.5

在这里插入图片描述

生成代码如下,产生了一个速率调度的函数rate_scheduler(),分别对应0.1和0.5。在step函数中根据速率来执行不同的逻辑。

#include "Demo1.h"
INPUT stInput;                         
B_Demo1_T Demo1_B;
ExtY_Demo1_T Demo1_Y;
RT_MODEL_Demo1_T Demo1_M_;
RT_MODEL_Demo1_T *const Demo1_M = &Demo1_M_;
static void rate_scheduler(void);
static void rate_scheduler(void)
{
  (Demo1_M->Timing.TaskCounters.TID[1])++;
  if ((Demo1_M->Timing.TaskCounters.TID[1]) > 9) {
    Demo1_M->Timing.TaskCounters.TID[1] = 0;
  }
  (Demo1_M->Timing.TaskCounters.TID[2])++;
  if ((Demo1_M->Timing.TaskCounters.TID[2]) > 49) {
    Demo1_M->Timing.TaskCounters.TID[2] = 0;
  }
}
void Demo1_step(void)
{
  if (Demo1_M->Timing.TaskCounters.TID[1] == 0) {
    Demo1_Y.Out1 = (U8)((U32)stInput.paraA + stInput.paraB);
    Demo1_Y.Out2 = (U8)(stInput.paraA - stInput.paraB);
    Demo1_B.output3 = (U8)((U32)stInput.paraA * stInput.paraB);
  }
  if (Demo1_M->Timing.TaskCounters.TID[2] == 0) {
    Demo1_Y.Out3 = Demo1_B.output3;
  }
  rate_scheduler();
}
void Demo1_initialize(void)
{
  (void) memset((void *)Demo1_M, 0,
                sizeof(RT_MODEL_Demo1_T));
  (void) memset(((void *) &Demo1_B), 0,
                sizeof(B_Demo1_T));
}

不同采样时间在生成代码时处理成独立的任务

在这里插入图片描述
生成代码如下:

#include "Demo1.h"
INPUT stInput;                         
B_Demo1_T Demo1_B;
ExtY_Demo1_T Demo1_Y;
RT_MODEL_Demo1_T Demo1_M_;
RT_MODEL_Demo1_T *const Demo1_M = &Demo1_M_;
void Demo1_step0(void)                 
{
}
void Demo1_step1(void)                 
{
  U8 rtb_output3;
  (Demo1_M->Timing.RateInteraction.TID1_2)++;
  if ((Demo1_M->Timing.RateInteraction.TID1_2) > 4) {
    Demo1_M->Timing.RateInteraction.TID1_2 = 0;
  }
  rtb_output3 = (U8)((U32)stInput.paraA + stInput.paraB);
  Demo1_Y.Out1 = rtb_output3;
  rtb_output3 = (U8)(stInput.paraA - stInput.paraB);
  Demo1_Y.Out2 = rtb_output3;
  rtb_output3 = (U8)((U32)stInput.paraA * stInput.paraB);
  if (Demo1_M->Timing.RateInteraction.TID1_2 == 1) {
    Demo1_B.output3 = rtb_output3;
  }
}
void Demo1_step2(void)                 
{
  Demo1_Y.Out3 = Demo1_B.output3;
}
void Demo1_initialize(void)
{
  (void) memset((void *)Demo1_M, 0,
                sizeof(RT_MODEL_Demo1_T));
  (void) memset(((void *) &Demo1_B), 0,
                sizeof(B_Demo1_T));
}

三种采样时间在生成代码时产生了三个step函数,在示例中给出了如下的任务调度方式:

#include <stddef.h>
#include <stdio.h>                     
#include "Demo1.h"                     
#include "rtwtypes.h"
void rt_OneStep(void);
void rt_OneStep(void)
{
  static boolean_T OverrunFlags[3] = { 0, 0, 0 };
  static boolean_T eventFlags[3] = { 0, 0, 0 };
  static int_T taskCounter[3] = { 0, 0, 0 };
  int_T i;
  if (OverrunFlags[0]) {
    rtmSetErrorStatus(Demo1_M, "Overrun");
    return;
  }
  OverrunFlags[0] = true;
  for (i = 1; i < 3; i++) {
    if (taskCounter[i] == 0) {
      if (eventFlags[i]) {
        OverrunFlags[0] = false;
        OverrunFlags[i] = true;
        rtmSetErrorStatus(Demo1_M, "Overrun");
        return;
      }
      eventFlags[i] = true;
    }
  }
  taskCounter[1]++;
  if (taskCounter[1] == 10) {
    taskCounter[1]= 0;
  }
  taskCounter[2]++;
  if (taskCounter[2] == 50) {
    taskCounter[2]= 0;
  }
  Demo1_step0();
  OverrunFlags[0] = false;
  for (i = 1; i < 3; i++) {
    if (OverrunFlags[i]) {
      return;
    }
    if (eventFlags[i]) {
      OverrunFlags[i] = true;
      switch (i) {
       case 1 :
        Demo1_step1();
        break;
       case 2 :
        Demo1_step2();
        break;
       default :
        break;
      }
      OverrunFlags[i] = false;
      eventFlags[i] = false;
    }
  }
}
int_T main(int_T argc, const char *argv[])
{
  (void)(argc);
  (void)(argv);
  Demo1_initialize();
  printf("Warning: The simulation will run forever. "
         "Generated ERT main won't simulate model step behavior. "
         "To change this behavior select the 'MAT-file logging' option.\n");
  fflush((NULL));
  while (rtmGetErrorStatus(Demo1_M) == (NULL)) {
  }
  return 0;
}

标签:代码生成,Simulink,U8,output3,void,stInput,Demo1,Timing,任务调度
来源: https://blog.csdn.net/aoyousihaiqiuqihuang/article/details/123605289