当前位置:   article > 正文

Simulink Embeded Coder生成的C代码解析

Simulink Embeded Coder生成的C代码解析

本文以一个简单的模型为例讲述Embeded Coder生成的C代码的架构。

模型

      模型如下图,名为test.

 其中Subsystem1中为与门,其它2个子系统的输入端和输出端直接相连。之后用Embeded Coder生成C代码,选择64位Windows系统作为运行环境。

生成的文件

     生成2个头文件: rtwtypes.h, test.h. 

     2个源文件: ert_main.c, test.c

      rtwtypes.h定义一些类型,如int16_T. 不细说这个头文件。下面讲解其它文件。

test.h

     

  1. typedef struct tag_RTM RT_MODEL;
  2. /* External inputs (root inport signals with default storage) */
  3. typedef struct {
  4. boolean_T In1; /* '<Root>/In1' */
  5. boolean_T In2; /* '<Root>/In2' */
  6. } ExtU;
  7. /* External outputs (root outports fed by signals with default storage) */
  8. typedef struct {
  9. boolean_T Out1; /* '<Root>/Out1' */
  10. boolean_T Out2; /* '<Root>/Out2' */
  11. } ExtY;
  12. /* Real-time Model Data Structure */
  13. struct tag_RTM {
  14. const char_T * volatile errorStatus;
  15. };

将输入、输出的数据用结构体封装起来。

test.c

      

  1. #include "test.h"
  2. /* External inputs (root inport signals with default storage) */
  3. ExtU rtU;
  4. /* External outputs (root outports fed by signals with default storage) */
  5. ExtY rtY;
  6. /* Real-time model */
  7. static RT_MODEL rtM_;
  8. RT_MODEL *const rtM = &rtM_;
  9. /* Model step function */
  10. void test_step(void)
  11. {
  12. boolean_T rtb_LogicalOperator;
  13. /* Logic: '<S1>/Logical Operator' incorporates:
  14. * Inport: '<Root>/In1'
  15. * Inport: '<Root>/In2'
  16. */
  17. rtb_LogicalOperator = (rtU.In2 && rtU.In1);
  18. /* Outport: '<Root>/Out1' */
  19. rtY.Out1 = rtb_LogicalOperator;
  20. /* Outport: '<Root>/Out2' */
  21. rtY.Out2 = rtb_LogicalOperator;
  22. }
  23. /* Model initialize function */
  24. void test_initialize(void)
  25. {
  26. /* (no initialization code required) */
  27. rtU.In1 = 1;
  28. rtU.In2 = 1;
  29. }

rtM_ 是运行时数据,这个例子中只存储了字符串errorStatus. initialize函数中原来是空的,里面的初始化的代码可以自己加上去,当作初始化输入。 step函数是运行中的每一步。

ert_main.c

     这是主函数所在源文件。代码如下

  1. #include <stddef.h>
  2. #include <stdio.h> /* This example main program uses printf/fflush */
  3. #include "test.h" /* Model's header file */
  4. void rt_OneStep(void);
  5. void rt_OneStep(void)
  6. {
  7. static boolean_T OverrunFlag = false;
  8. /* Disable interrupts here */
  9. /* Check for overrun */
  10. if (OverrunFlag) {
  11. rtmSetErrorStatus(rtM, "Overrun");
  12. return;
  13. }
  14. OverrunFlag = true;
  15. /* Save FPU context here (if necessary) */
  16. /* Re-enable timer or interrupt here */
  17. /* Set model inputs here */
  18. /* Step the model */
  19. test_step();
  20. /* Get model outputs here */
  21. /* Indicate task complete */
  22. OverrunFlag = false;
  23. /* Disable interrupts here */
  24. /* Restore FPU context here (if necessary) */
  25. /* Enable interrupts here */
  26. }
  27. /*
  28. * The example "main" function illustrates what is required by your
  29. * application code to initialize, execute, and terminate the generated code.
  30. * Attaching rt_OneStep to a real-time clock is target specific. This example
  31. * illustrates how you do this relative to initializing the model.
  32. */
  33. int_T main(int_T argc, const char *argv[])
  34. {
  35. /* Unused arguments */
  36. (void)(argc);
  37. (void)(argv);
  38. /* Initialize model */
  39. test_initialize();
  40. fflush((NULL));
  41. int i = 0;
  42. while (rtmGetErrorStatus(rtM) == (NULL) && i <= 5)
  43. {
  44. rt_OneStep();
  45. ++i;
  46. printf("%d %u\n", i, rtY.Out1);
  47. }
  48. /* Disable rt_OneStep here */
  49. return 0;
  50. }

代码中原本有很多注释,while循环是空的。现在我在while循环中加了控制循环次数的变量i,以及调用rt_OneStep函数,这个函数是test_step()函数的封装。函数中使用静态变量OverrunFlag,确保在多线程中,不会同时运行多个test_step().

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/744697
推荐阅读
相关标签
  

闽ICP备14008679号