当前位置:网站首页>Simulink代码生成: 可变子系统及其代码
Simulink代码生成: 可变子系统及其代码
2022-07-24 03:08:00 【chhttty】
本文研究Simulink中的可变子系统,及其生成的条件编译的代码。
1 问题引入
在工作的不同项目回对应着不同的需求,然后不同的需求可能会需要用不同的模型策略实现。此时,就可以使用 Variant子系统进行建模,并通过参数控制某个项目通过哪个子系统输出结果。并且,生成的代码还可以表现为宏定义的形式。
2 建模示例
本章节演示可变子系统的建模方法。
1)首先,在Matlab工作空间新建一个Simulink.Parameter参数对象,将其作为后面控制可变子系统的变量参数;
其中,谁当StorageClass为Define,对应了C代码中的宏定义。这样做为了后面在生成代码时生成宏的形式。
2)在空白模型中添加一个可变子系统,将其属性设置为如下:
右键子系统,点击Block Parameters,可以打开子系统的配置界面,设置成如下配置;

在表格中的Name列,表示子系统中的不同的子系统名称,分别设置为Gain_2和Gain_3,后面的Variant control表示用参数控制可变子系统中运行哪个子系a统。当P_Condition2执行Gain_2子系统,当P_Condition3执行Gain_3子系统。保存并退出。
3)双击进入可变子系统中,可以看到有两个子系统中的子系统,并且仔细观察可以发现输入输出口都不太一样。

两个子系统的名字对应着上一步中的Name配置,输入输出口要和外面的port保持一致,并且不能连接信号线。
4)在两个子系统中分别用Gain模块,但是一个参数是2,另一个是3;

由于第一步中设置的P_Condition数值为2,所以上面的Gain_2子系统是实线,下面的Gain3子系统是阴影。
5)到了这一步,建模就已经完成了。可以验证一下,在模型最上层加一个输入为常数1,输出为display模块;
当P_Condition数值为2时,Update一下模型后,输出为2;
当P_Condition数值为3时,Update一下模型后,输出为3;
这就表示,可变子系统中究竟走哪一路,是根据参数P_Condition来控制的。在实际工作中,针对不同的项目,修改一下P_Condition参数的数值,就可以执行不同的控制逻辑和策略。
3 代码生成
将顶层模型换成Inport和Outport端口,Ctrl + B生成代码。
在P_Macro.h文件中,生成了P_Condition的宏定义,数值为2;

在step函数中,通过判断P_Condition的数值来决定执行哪个赋值语句,并且是#if形式的条件判断,而不是C语言中的if条件语句。这样有个好处为,在编译器编译这段代码时,可以跳过P_Condition == 3下面的语句,从而节省了控制器的flash资源。

4 总结
可变子系统可以通过判断条件,来决定内部执行不同的逻辑,生成代码也可以生成宏的形式。
边栏推荐
- JS 数组 isAarray() typeof
- All codes of selenium
- 如何获取步态能量图gei
- To forge ahead on a new journey, the city chain science and technology carnival was grandly held in Xiamen
- Unity 消息推送
- 二分查找
- The first edition of Niuke brush question series (automorphic number, return the number of prime numbers less than N, and the first character only appears once)
- Take you into the world of MySQL mvcc
- summernote富文本编辑器
- The implementation in unity determines whether missing or null
猜你喜欢

自定义kindeditor富文本默认的宽高

SSM family financial management personal financial management system accounting system

A simple and perfect WPF management system framework source code

Job hunting and recruitment system of SSM part-time job hunting

Analyze the overall planning of steam and maker education classroom

Dynamic programming-01 knapsack problem

JS small game running bear and cat source code

TCP data transmission and performance

Tweenmax+svg Pikachu transformation ball

Take you into the world of MySQL mvcc
随机推荐
Summernote rich text editor
go IO操作-文件读
Is it safe to open an account for Xiaobai stock? Can I apply online?
SSM family financial management personal financial management system accounting system
Some properties of differential array operation
Symbol類型
FTP服务与配置
二分查找
Liveqing live RTMP on demand video streaming platform how to carry the Sid and token returned by the login interface to call authentication streamtoken video streaming authentication
The implementation in unity determines whether missing or null
日常杂谈(一)
og seo
Go log package
Go IO operation - file write
Analyze the space practice field required by maker education activities
Lumberyard game engine of o3de
(六)装饰器扩展之[email protected]使用原理
summernote支持自定义视频上传功能
The process of solving a bug at work
C language exercises