【研发日记】Matlab/Simulink自动生成代码(二)——五种选择结构实现方法
Mr.Cssust 2024-06-13 10:35:02 阅读 84
文章目录
前言
Simulink实现选择结构
自动生成C代码
变式一
变式二
变式三
变式四
总结
前言
见《深入拆解Simulink自动生成代码(一)——数据流处理》
Simulink实现选择结构
用Simulink实现选择结构的一个最简单编程举例如下:
自动生成C代码
上述选择结构的Simulink自动生成的对应C代码如下:
/* Switch: '<S2>/Switch' incorporates: * UnitDelay: '<S3>/Output' */ if (CodeGenerate_DW.Output_DSTATE > 0) { /* Outport: '<Root>/y1' incorporates: * Constant: '<S2>/Constant' */ CodeGenerate_Y.y1 = 5.0; } else { /* Outport: '<Root>/y1' incorporates: * Constant: '<S2>/Constant2' */ CodeGenerate_Y.y1 = 10.0; } /* End of Switch: '<S2>/Switch' */ /* Switch: '<S5>/FixPt Switch' incorporates: * Constant: '<S4>/FixPt Constant' * Constant: '<S5>/Constant' * Sum: '<S4>/FixPt Sum1' * UnitDelay: '<S3>/Output' */ if ((uint8_T)(CodeGenerate_DW.Output_DSTATE + 1U) > 7) { CodeGenerate_DW.Output_DSTATE = 0U; } else { CodeGenerate_DW.Output_DSTATE++; } /* End of Switch: '<S5>/FixPt Switch' */
变式一
基于上述举例,增加选择结构的选项,生成的对应代码如下所示:
/* SwitchCase: '<S2>/Switch Case' incorporates: * UnitDelay: '<S3>/Output' */ switch (CodeGenerate_DW.Output_DSTATE) { case 1: /* Outputs for IfAction SubSystem: '<S2>/Switch Case Action Subsystem' incorporates: * ActionPort: '<S4>/Action Port' */ /* Outport: '<Root>/y1' incorporates: * Constant: '<S2>/Constant1' * Inport: '<S4>/In1' */ CodeGenerate_Y.y1 = 5.0; /* End of Outputs for SubSystem: '<S2>/Switch Case Action Subsystem' */ break; case 2: /* Outputs for IfAction SubSystem: '<S2>/Switch Case Action Subsystem1' incorporates: * ActionPort: '<S5>/Action Port' */ /* Outport: '<Root>/y1' incorporates: * Constant: '<S2>/Constant3' * Inport: '<S5>/In1' */ CodeGenerate_Y.y1 = 10.0; /* End of Outputs for SubSystem: '<S2>/Switch Case Action Subsystem1' */ break; case 3: /* Outputs for IfAction SubSystem: '<S2>/Switch Case Action Subsystem2' incorporates: * ActionPort: '<S6>/Action Port' */ /* Outport: '<Root>/y1' incorporates: * Constant: '<S2>/Constant4' * Inport: '<S6>/In1' */ CodeGenerate_Y.y1 = 15.0; /* End of Outputs for SubSystem: '<S2>/Switch Case Action Subsystem2' */ break; default: /* Outputs for IfAction SubSystem: '<S2>/Switch Case Action Subsystem3' incorporates: * ActionPort: '<S7>/Action Port' */ /* Outport: '<Root>/y1' incorporates: * Constant: '<S2>/Constant5' * Inport: '<S7>/In1' */ CodeGenerate_Y.y1 = 0.0; /* End of Outputs for SubSystem: '<S2>/Switch Case Action Subsystem3' */ break; } /* End of SwitchCase: '<S2>/Switch Case' */ /* Switch: '<S9>/FixPt Switch' incorporates: * Constant: '<S8>/FixPt Constant' * Constant: '<S9>/Constant' * Sum: '<S8>/FixPt Sum1' * UnitDelay: '<S3>/Output' */ if ((uint8_T)(CodeGenerate_DW.Output_DSTATE + 1U) > 7) { CodeGenerate_DW.Output_DSTATE = 0U; } else { CodeGenerate_DW.Output_DSTATE++; } /* End of Switch: '<S9>/FixPt Switch' */
变式二
基于上述举例,升级选择结构的判断条件,生成的对应代码如下所示:
real_T tmp; /* Math: '<S2>/Mod' incorporates: * Constant: '<S2>/Constant6' * DataTypeConversion: '<S2>/Data Type Conversion' * Math: '<S2>/Mod1' * Math: '<S2>/Mod2' * UnitDelay: '<S6>/Output' */ tmp = rt_modd_snf((real_T)CodeGenerate_DW.Output_DSTATE, 3.0); /* Outputs for Enabled SubSystem: '<S2>/Subsystem' incorporates: * EnablePort: '<S7>/Enable' */ /* RelationalOperator: '<S3>/Compare' incorporates: * Constant: '<S3>/Constant' * Math: '<S2>/Mod' */ if (tmp == 0.0) { /* Merge: '<S2>/Merge1' incorporates: * Constant: '<S2>/Constant7' * Inport: '<S7>/In1' */ CodeGenerate_B.Merge1 = 5.0; } /* End of RelationalOperator: '<S3>/Compare' */ /* End of Outputs for SubSystem: '<S2>/Subsystem' */ /* Outputs for Enabled SubSystem: '<S2>/Subsystem1' incorporates: * EnablePort: '<S8>/Enable' */ /* RelationalOperator: '<S4>/Compare' incorporates: * Constant: '<S4>/Constant' */ if (tmp == 1.0) { /* Merge: '<S2>/Merge1' incorporates: * Constant: '<S2>/Constant9' * Inport: '<S8>/In1' */ CodeGenerate_B.Merge1 = 10.0; } /* End of RelationalOperator: '<S4>/Compare' */ /* End of Outputs for SubSystem: '<S2>/Subsystem1' */ /* Outputs for Enabled SubSystem: '<S2>/Subsystem2' incorporates: * EnablePort: '<S9>/Enable' */ /* RelationalOperator: '<S5>/Compare' incorporates: * Constant: '<S5>/Constant' */ if (tmp == 2.0) { /* Merge: '<S2>/Merge1' incorporates: * Constant: '<S2>/Constant11' * Inport: '<S9>/In1' */ CodeGenerate_B.Merge1 = 15.0; } /* End of RelationalOperator: '<S5>/Compare' */ /* End of Outputs for SubSystem: '<S2>/Subsystem2' */ /* Switch: '<S11>/FixPt Switch' incorporates: * Constant: '<S10>/FixPt Constant' * Constant: '<S11>/Constant' * Sum: '<S10>/FixPt Sum1' * UnitDelay: '<S6>/Output' */ if ((uint8_T)(CodeGenerate_DW.Output_DSTATE + 1U) > 7) { CodeGenerate_DW.Output_DSTATE = 0U; } else { CodeGenerate_DW.Output_DSTATE++; } /* End of Switch: '<S11>/FixPt Switch' */ /* Outport: '<Root>/y1' incorporates: * MATLAB Function: '<Root>/MATLAB Function2' */ CodeGenerate_Y.y1 = CodeGenerate_B.Merge1;
变式三
基于上述举例,用MATLAB Function来实现,生成的对应代码如下所示:
#include "untitled.h"#include "untitled_private.h"/* Block states (default storage) */DW_untitled_T untitled_DW;/* External outputs (root outports fed by signals with default storage) */ExtY_untitled_T untitled_Y;/* Real-time model */static RT_MODEL_untitled_T untitled_M_;RT_MODEL_untitled_T *const untitled_M = &untitled_M_;/* Model step function */void untitled_step(void){ uint8_T rtb_Output; /* UnitDelay: '<S1>/Output' */ rtb_Output = untitled_DW.Output_DSTATE; /* Switch: '<S4>/FixPt Switch' incorporates: * Constant: '<S3>/FixPt Constant' * Constant: '<S4>/Constant' * Sum: '<S3>/FixPt Sum1' * UnitDelay: '<S1>/Output' */ if ((uint8_T)(untitled_DW.Output_DSTATE + 1U) > 7) { untitled_DW.Output_DSTATE = 0U; } else { untitled_DW.Output_DSTATE++; } /* End of Switch: '<S4>/FixPt Switch' */ /* MATLAB Function: '<Root>/MATLAB Function' */ switch (rtb_Output) { case 1: /* Outport: '<Root>/y' */ untitled_Y.y = 5.0; break; case 2: /* Outport: '<Root>/y' */ untitled_Y.y = 10.0; break; case 3: /* Outport: '<Root>/y' */ untitled_Y.y = 15.0; break; default: /* Outport: '<Root>/y' */ untitled_Y.y = 0.0; break; } /* End of MATLAB Function: '<Root>/MATLAB Function' */}
变式四
基于上述举例,用Stateflow配合Simulink Function来实现选择结构,参见《【研发日记】嵌入式处理器技能解锁(一)——多任务异步执行调度的三种方法》,示例如下:
总结
以上就是本人拆解Simulink模块自动生成代码时,讲解的第二部分。主要针对Simulink的选择结构,展示了这类模块的使用方法,并对比了相应的C代码。
后续还会更新Simulink其他的几种模块,欢迎评论区留言、点赞、收藏和关注,这些鼓励和支持都将成为笔者持续分享的动力。
另外,上述例程使用的Demo工程可以到笔者的主页查找和下载。
版权声明:原创文章,转载和引用请注明出处和链接,侵权必究!
上一篇: 华为OD机试C卷-- 字符串排序(Java & JS & Python)
本文标签
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。