概述
本文详细介绍了simulink连续模型离散化,代码生成以及最终运行到VS2010环境的详细过程,希望对大家有帮助。
第一步 打开模型

首先我们要确保模型仿真是正确的,即能够正常仿真。
第二步 将输入和需要的输出换成input和output模块。

第三步 离散化模型
如果你的模型中有连续模块,比如积分环节,那麽一定要离散化,否则即使生成C代码也会报错。
点击设置

设置求解器,采样时间。


最后点击Apply,点击OK。
点击Analysis->Control Designer->Model Discretizer。

设置和刚才一样的采样时间,点击转换。

转换完成后的状态空间模型和积分模块。


第四步 自定义变量名称
点击Model Explorer

选择Base Workspace,点击Add。


名称改为你想要的变量名称,类型选择mpt.singal(其他想要输出的信号也一样)。


关闭该窗口。
右键想要输出或者输入的信号,点击Properties。

输入刚才定义的变量名称,勾选第一项

其他信号同上。
第五步 生成C代码

剩下的全部默认就好
只有这里根据你代码运行的设备进行选择,我是在64位的windows系统上测试,运行环系是VS2010.

完成之后生成如下文件

第六步 在VS2010上运行
打开VS2010

点击文件->新建->从现有的代码中创建

选择刚才自动生成的文件夹

定义项目名称

选择控制台应用程序

然后就一直下一步,然后完成

打开ert_main.c这个文件

可以找到一个while循环

修改为一下代码(注意要定义整型变量i):
while (i<=2000) {
r=5;
System1xu_step();
printf("%f", x[1]);
printf("n");
i++;
/* Perform other application tasks here */
}
运行结果

最后
以上就是冷静信封为你收集整理的c语言代码大全和详细解释_最详细的Simulink代码生成(C语言)的全部内容,希望文章能够帮你解决c语言代码大全和详细解释_最详细的Simulink代码生成(C语言)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复