文章目录
前言
一、Simulink实现循环结构
二、自动生成C代码
三、变式1
四、变式2
五、变式3
六、变式4
总结
前言
《深入拆解Simulink自动生成代码(一)——数据流处理》
《深入拆解Simulink自动生成代码(二)——选择结构》
一、Simulink实现循环结构
用Simulink实现循环结构的一个最简单编程举例如下:
二、自动生成C代码
上述循环结构的Simulink自动生成的对应C代码如下:
int32_T s3_iter;
/* Outputs for Iterator SubSystem: '<S2>/For Iterator Subsystem' incorporates:
* ForIterator: '<S3>/For Iterator'
*/
for (s3_iter = 0; s3_iter < 5; s3_iter++) {
/* Outputs for Iterator SubSystem: '<S2>/For Iterator Subsystem' incorporates:
* ForIterator: '<S3>/For Iterator'
*/
CodeGenerate_Y.y2 = s3_iter + 1;
}
/* End of Outputs for SubSystem: '<S2>/For Iterator Subsystem' */
三、变式1
基于上述举例,把for循环改成while循环,生成的对应代码如下所示:
int32_T s3_iter;
/* Outputs for Iterator SubSystem: '<S2>/While Iterator Subsystem' incorporates:
* WhileIterator: '<S3>/While Iterator'
*/
s3_iter = 1;
/* End of Outputs for SubSystem: '<S2>/While Iterator Subsystem' */
do {
CodeGenerate_Y.y2 = s3_iter;
s3_iter++;
} while (s3_iter <= 5);
四、变式2
基于上述举例,再增加一个while的退出条件,当Counter>3时退出循环,生成的对应代码如下所示:
int32_T s3_iter;
boolean_T rtb_Compare;
/* Outputs for Iterator SubSystem: '<S2>/While Iterator Subsystem1' incorporates:
* WhileIterator: '<S3>/While Iterator'
*/
s3_iter = 1;
/* End of Outputs for SubSystem: '<S2>/While Iterator Subsystem1' */
do {
CodeGenerate_Y.y2 = s3_iter;
rtb_Compare = (CodeGenerate_DW.Output_DSTATE <= 3);
if ((uint8_T)(CodeGenerate_DW.Output_DSTATE + 1U) > 7) {
CodeGenerate_DW.Output_DSTATE = 0U;
} else {
CodeGenerate_DW.Output_DSTATE++;
}
s3_iter++;
} while (rtb_Compare && (s3_iter <= 5));
五、变式3
基于上述举例,把while的循环类型从do while改成while do,生成的对应代码如下所示:
int32_T s3_iter;
boolean_T loopCond;
/* Outputs for Iterator SubSystem: '<S2>/While Iterator Subsystem2' incorporates:
* WhileIterator: '<S3>/While Iterator'
*/
s3_iter = 1;
/* Constant: '<S2>/Constant' */
loopCond = true;
while (loopCond && (s3_iter <= 5)) {
/* Outputs for Iterator SubSystem: '<S2>/While Iterator Subsystem2' incorporates:
* WhileIterator: '<S3>/While Iterator'
*/
CodeGenerate_B.WhileIterator = s3_iter;
/* RelationalOperator: '<S4>/Compare' incorporates:
* Constant: '<S4>/Constant'
* UnitDelay: '<S5>/Output'
*/
loopCond = (CodeGenerate_DW.Output_DSTATE <= 3);
/* Switch: '<S7>/FixPt Switch' incorporates:
* Constant: '<S6>/FixPt Constant'
* Constant: '<S7>/Constant'
* Sum: '<S6>/FixPt Sum1'
* UnitDelay: '<S5>/Output'
*/
if ((uint8_T)(CodeGenerate_DW.Output_DSTATE + 1U) > 7) {
CodeGenerate_DW.Output_DSTATE = 0U;
} else {
CodeGenerate_DW.Output_DSTATE++;
}
/* End of Switch: '<S7>/FixPt Switch' */
s3_iter++;
}
/* End of Outputs for SubSystem: '<S2>/While Iterator Subsystem2' */
/* Outport: '<Root>/y2' incorporates:
* MATLAB Function: '<Root>/MATLAB Function3'
*/
CodeGenerate_Y.y2 = CodeGenerate_B.WhileIterator;
六、变式4
基于上述举例,把while循环体的执行内容,从每次退出循环再进入时保持前一次的状态,变成每次退出循环再进入时置初始状态,也即Start状态从Held改成Reset,生成的对应代码如下所示:
int32_T s3_iter;
uint8_T Output_DSTATE;
boolean_T loopCond;
/* Outputs for Iterator SubSystem: '<S2>/While Iterator Subsystem3' incorporates:
* WhileIterator: '<S3>/While Iterator'
*/
s3_iter = 1;
/* InitializeConditions for UnitDelay: '<S5>/Output' */
Output_DSTATE = 0U;
/* Constant: '<S2>/Constant1' */
loopCond = true;
while (loopCond && (s3_iter <= 5)) {
/* Outputs for Iterator SubSystem: '<S2>/While Iterator Subsystem3' incorporates:
* WhileIterator: '<S3>/While Iterator'
*/
CodeGenerate_B.WhileIterator = s3_iter;
/* RelationalOperator: '<S4>/Compare' incorporates:
* Constant: '<S4>/Constant'
* UnitDelay: '<S5>/Output'
*/
loopCond = (Output_DSTATE <= 3);
/* Switch: '<S7>/FixPt Switch' incorporates:
* Constant: '<S6>/FixPt Constant'
* Constant: '<S7>/Constant'
* Sum: '<S6>/FixPt Sum1'
* UnitDelay: '<S5>/Output'
*/
if ((uint8_T)(Output_DSTATE + 1U) > 7) {
Output_DSTATE = 0U;
} else {
Output_DSTATE++;
}
/* End of Switch: '<S7>/FixPt Switch' */
s3_iter++;
}
/* End of Outputs for SubSystem: '<S2>/While Iterator Subsystem3' */
/* Outport: '<Root>/y2' incorporates:
* MATLAB Function: '<Root>/MATLAB Function3'
*/
CodeGenerate_Y.y2 = CodeGenerate_B.WhileIterator;
总结
以上就是本人拆解Simulink模块自动生成代码时,讲解的第三部分。主要针对Simulink的循环模块,展示了这类模块的使用方法,并对比了相应的C代码。
后续还会更新Simulink其他的几种模块,欢迎评论区留言、点赞、收藏和关注,这些鼓励和支持都将成为笔者持续分享的动力。
另外,上述例程使用的Demo工程可以到笔者的主页查找和下载。文章来源:https://www.toymoban.com/news/detail-529704.html
版权声明:原创文章,转载和引用请注明出处和链接,侵权必究!文章来源地址https://www.toymoban.com/news/detail-529704.html
到了这里,关于深入拆解Simulink自动生成代码(三)——循环结构的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!