当前位置:   article > 正文

【研发日记】Matlab/Simulink自动生成代码(二)——五种选择结构实现方法_simulink选择性生成代码

simulink选择性生成代码

文章目录

前言

Simulink实现选择结构

自动生成C代码

变式一

变式二

变式三

变式四

总结


前言

        见《深入拆解Simulink自动生成代码(一)——数据流处理

Simulink实现选择结构

         用Simulink实现选择结构的一个最简单编程举例如下:

自动生成C代码

         上述选择结构的Simulink自动生成的对应C代码如下:

  1. /* Switch: '<S2>/Switch' incorporates:
  2. * UnitDelay: '<S3>/Output'
  3. */
  4. if (CodeGenerate_DW.Output_DSTATE > 0) {
  5. /* Outport: '<Root>/y1' incorporates:
  6. * Constant: '<S2>/Constant'
  7. */
  8. CodeGenerate_Y.y1 = 5.0;
  9. } else {
  10. /* Outport: '<Root>/y1' incorporates:
  11. * Constant: '<S2>/Constant2'
  12. */
  13. CodeGenerate_Y.y1 = 10.0;
  14. }
  15. /* End of Switch: '<S2>/Switch' */
  16. /* Switch: '<S5>/FixPt Switch' incorporates:
  17. * Constant: '<S4>/FixPt Constant'
  18. * Constant: '<S5>/Constant'
  19. * Sum: '<S4>/FixPt Sum1'
  20. * UnitDelay: '<S3>/Output'
  21. */
  22. if ((uint8_T)(CodeGenerate_DW.Output_DSTATE + 1U) > 7) {
  23. CodeGenerate_DW.Output_DSTATE = 0U;
  24. } else {
  25. CodeGenerate_DW.Output_DSTATE++;
  26. }
  27. /* End of Switch: '<S5>/FixPt Switch' */

变式一

         基于上述举例,增加选择结构的选项,生成的对应代码如下所示:

  1. /* SwitchCase: '<S2>/Switch Case' incorporates:
  2. * UnitDelay: '<S3>/Output'
  3. */
  4. switch (CodeGenerate_DW.Output_DSTATE) {
  5. case 1:
  6. /* Outputs for IfAction SubSystem: '<S2>/Switch Case Action Subsystem' incorporates:
  7. * ActionPort: '<S4>/Action Port'
  8. */
  9. /* Outport: '<Root>/y1' incorporates:
  10. * Constant: '<S2>/Constant1'
  11. * Inport: '<S4>/In1'
  12. */
  13. CodeGenerate_Y.y1 = 5.0;
  14. /* End of Outputs for SubSystem: '<S2>/Switch Case Action Subsystem' */
  15. break;
  16. case 2:
  17. /* Outputs for IfAction SubSystem: '<S2>/Switch Case Action Subsystem1' incorporates:
  18. * ActionPort: '<S5>/Action Port'
  19. */
  20. /* Outport: '<Root>/y1' incorporates:
  21. * Constant: '<S2>/Constant3'
  22. * Inport: '<S5>/In1'
  23. */
  24. CodeGenerate_Y.y1 = 10.0;
  25. /* End of Outputs for SubSystem: '<S2>/Switch Case Action Subsystem1' */
  26. break;
  27. case 3:
  28. /* Outputs for IfAction SubSystem: '<S2>/Switch Case Action Subsystem2' incorporates:
  29. * ActionPort: '<S6>/Action Port'
  30. */
  31. /* Outport: '<Root>/y1' incorporates:
  32. * Constant: '<S2>/Constant4'
  33. * Inport: '<S6>/In1'
  34. */
  35. CodeGenerate_Y.y1 = 15.0;
  36. /* End of Outputs for SubSystem: '<S2>/Switch Case Action Subsystem2' */
  37. break;
  38. default:
  39. /* Outputs for IfAction SubSystem: '<S2>/Switch Case Action Subsystem3' incorporates:
  40. * ActionPort: '<S7>/Action Port'
  41. */
  42. /* Outport: '<Root>/y1' incorporates:
  43. * Constant: '<S2>/Constant5'
  44. * Inport: '<S7>/In1'
  45. */
  46. CodeGenerate_Y.y1 = 0.0;
  47. /* End of Outputs for SubSystem: '<S2>/Switch Case Action Subsystem3' */
  48. break;
  49. }
  50. /* End of SwitchCase: '<S2>/Switch Case' */
  51. /* Switch: '<S9>/FixPt Switch' incorporates:
  52. * Constant: '<S8>/FixPt Constant'
  53. * Constant: '<S9>/Constant'
  54. * Sum: '<S8>/FixPt Sum1'
  55. * UnitDelay: '<S3>/Output'
  56. */
  57. if ((uint8_T)(CodeGenerate_DW.Output_DSTATE + 1U) > 7) {
  58. CodeGenerate_DW.Output_DSTATE = 0U;
  59. } else {
  60. CodeGenerate_DW.Output_DSTATE++;
  61. }
  62. /* End of Switch: '<S9>/FixPt Switch' */

变式二

         基于上述举例,升级选择结构的判断条件,生成的对应代码如下所示:

  1. real_T tmp;
  2. /* Math: '<S2>/Mod' incorporates:
  3. * Constant: '<S2>/Constant6'
  4. * DataTypeConversion: '<S2>/Data Type Conversion'
  5. * Math: '<S2>/Mod1'
  6. * Math: '<S2>/Mod2'
  7. * UnitDelay: '<S6>/Output'
  8. */
  9. tmp = rt_modd_snf((real_T)CodeGenerate_DW.Output_DSTATE, 3.0);
  10. /* Outputs for Enabled SubSystem: '<S2>/Subsystem' incorporates:
  11. * EnablePort: '<S7>/Enable'
  12. */
  13. /* RelationalOperator: '<S3>/Compare' incorporates:
  14. * Constant: '<S3>/Constant'
  15. * Math: '<S2>/Mod'
  16. */
  17. if (tmp == 0.0) {
  18. /* Merge: '<S2>/Merge1' incorporates:
  19. * Constant: '<S2>/Constant7'
  20. * Inport: '<S7>/In1'
  21. */
  22. CodeGenerate_B.Merge1 = 5.0;
  23. }
  24. /* End of RelationalOperator: '<S3>/Compare' */
  25. /* End of Outputs for SubSystem: '<S2>/Subsystem' */
  26. /* Outputs for Enabled SubSystem: '<S2>/Subsystem1' incorporates:
  27. * EnablePort: '<S8>/Enable'
  28. */
  29. /* RelationalOperator: '<S4>/Compare' incorporates:
  30. * Constant: '<S4>/Constant'
  31. */
  32. if (tmp == 1.0) {
  33. /* Merge: '<S2>/Merge1' incorporates:
  34. * Constant: '<S2>/Constant9'
  35. * Inport: '<S8>/In1'
  36. */
  37. CodeGenerate_B.Merge1 = 10.0;
  38. }
  39. /* End of RelationalOperator: '<S4>/Compare' */
  40. /* End of Outputs for SubSystem: '<S2>/Subsystem1' */
  41. /* Outputs for Enabled SubSystem: '<S2>/Subsystem2' incorporates:
  42. * EnablePort: '<S9>/Enable'
  43. */
  44. /* RelationalOperator: '<S5>/Compare' incorporates:
  45. * Constant: '<S5>/Constant'
  46. */
  47. if (tmp == 2.0) {
  48. /* Merge: '<S2>/Merge1' incorporates:
  49. * Constant: '<S2>/Constant11'
  50. * Inport: '<S9>/In1'
  51. */
  52. CodeGenerate_B.Merge1 = 15.0;
  53. }
  54. /* End of RelationalOperator: '<S5>/Compare' */
  55. /* End of Outputs for SubSystem: '<S2>/Subsystem2' */
  56. /* Switch: '<S11>/FixPt Switch' incorporates:
  57. * Constant: '<S10>/FixPt Constant'
  58. * Constant: '<S11>/Constant'
  59. * Sum: '<S10>/FixPt Sum1'
  60. * UnitDelay: '<S6>/Output'
  61. */
  62. if ((uint8_T)(CodeGenerate_DW.Output_DSTATE + 1U) > 7) {
  63. CodeGenerate_DW.Output_DSTATE = 0U;
  64. } else {
  65. CodeGenerate_DW.Output_DSTATE++;
  66. }
  67. /* End of Switch: '<S11>/FixPt Switch' */
  68. /* Outport: '<Root>/y1' incorporates:
  69. * MATLAB Function: '<Root>/MATLAB Function2'
  70. */
  71. CodeGenerate_Y.y1 = CodeGenerate_B.Merge1;

变式三

         基于上述举例,用MATLAB Function来实现,生成的对应代码如下所示:

  1. #include "untitled.h"
  2. #include "untitled_private.h"
  3. /* Block states (default storage) */
  4. DW_untitled_T untitled_DW;
  5. /* External outputs (root outports fed by signals with default storage) */
  6. ExtY_untitled_T untitled_Y;
  7. /* Real-time model */
  8. static RT_MODEL_untitled_T untitled_M_;
  9. RT_MODEL_untitled_T *const untitled_M = &untitled_M_;
  10. /* Model step function */
  11. void untitled_step(void)
  12. {
  13. uint8_T rtb_Output;
  14. /* UnitDelay: '<S1>/Output' */
  15. rtb_Output = untitled_DW.Output_DSTATE;
  16. /* Switch: '<S4>/FixPt Switch' incorporates:
  17. * Constant: '<S3>/FixPt Constant'
  18. * Constant: '<S4>/Constant'
  19. * Sum: '<S3>/FixPt Sum1'
  20. * UnitDelay: '<S1>/Output'
  21. */
  22. if ((uint8_T)(untitled_DW.Output_DSTATE + 1U) > 7) {
  23. untitled_DW.Output_DSTATE = 0U;
  24. } else {
  25. untitled_DW.Output_DSTATE++;
  26. }
  27. /* End of Switch: '<S4>/FixPt Switch' */
  28. /* MATLAB Function: '<Root>/MATLAB Function' */
  29. switch (rtb_Output) {
  30. case 1:
  31. /* Outport: '<Root>/y' */
  32. untitled_Y.y = 5.0;
  33. break;
  34. case 2:
  35. /* Outport: '<Root>/y' */
  36. untitled_Y.y = 10.0;
  37. break;
  38. case 3:
  39. /* Outport: '<Root>/y' */
  40. untitled_Y.y = 15.0;
  41. break;
  42. default:
  43. /* Outport: '<Root>/y' */
  44. untitled_Y.y = 0.0;
  45. break;
  46. }
  47. /* End of MATLAB Function: '<Root>/MATLAB Function' */
  48. }

变式四

        基于上述举例,用Stateflow配合Simulink Function来实现选择结构,参见《【研发日记】嵌入式处理器技能解锁(一)——多任务异步执行调度的三种方法》,示例如下:

总结

         以上就是本人拆解Simulink模块自动生成代码时,讲解的第二部分。主要针对Simulink的选择结构,展示了这类模块的使用方法,并对比了相应的C代码。

         后续还会更新Simulink其他的几种模块,欢迎评论区留言、点赞、收藏和关注,这些鼓励和支持都将成为笔者持续分享的动力。

         另外,上述例程使用的Demo工程可以到笔者的主页查找和下载。


         版权声明:原创文章,转载和引用请注明出处和链接,侵权必究!

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

闽ICP备14008679号