我是靠谱客的博主 温暖火龙果,最近开发中收集的这篇文章主要介绍simulink实现仿真时间与实际时间同步,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

我们都知道simulink仿真的时候可以设置仿真时间,默认的10s,但是这个时间不同于实际时间。对于一个简单的模型,比如显示一个正弦波,也许设置的仿真时间为10s而simulink仿真运行只用了零点几秒就完成了,而对于一个复杂的模型,也许仿真10s需要simulink计算十几分钟。也就是说simulink中设置的仿真时间可以看作是仿真步而不是实际的时间(仿真步的概念跟微分方程的数值解法有关,以simulink中求解器solver设为定步长为例,形象的说,仿真时间除以步长得到仿真步,例如10/0.01=1000步,matlab计算这1000个迭代步的时间才是模型运行的时间),但是有些情况下我们需要实现这样的效果:仿真时间等于实际时间,也就是我设置仿真时间为10s,模型就运行10s,与模型的复杂度无关(这里有一个前提,模型运行的足够快,不能要求一个超级复杂的模型仿真时间=实际时间从而“加快”仿真),在模型中加入一个clock模块,其显示的时间进度与真实时间进度相似。

simulink本身有自带的Real-Time Windows Target,可以自行参照帮助进行尝试,但是对于不同的版本,不同的操作系统,总是出现各种问题,有种感觉对于一般的简单实时性要求,matlab 的 RTW 有点大材小用。我在 32位 win7 + R2014a上可以部分运行matlab的demo,但是在64位win8.1 + R2014a就出现各种问题。于是在网上收罗了半天,终于找到一些简单的解决办法,用起来也比较方便,是国外一些比较厉害的人自己用s-function编的模块。

其具体实现的原理引用其中一个人的文档如下:

以下引至: http://www.mathworks.com/matlabcentral/fileexchange/3175-real-time-blockset-7-1-for-simulink


This new Simulink block allows the execution of Simulink models in real time. The blockset is actually composed by only one block, that has been realized using an S-function written in C++ language.

This blockset is based on the simple concept that, to make the Simulink run with a real-time temporisation, the cycle time (the time that Simulink needs to calculate a simulation step, that is function of the hardware and the OS in which Simulink is running) should be lower then the desired simulation step. If this assumption is not valid, no real-time simulation is possible, whichever is the applied scheduling method.

Obviously, this statement is not completely valid, for example if we suppose to work with a real-time OS, in which the scheduling can be fully controlled. Anyway, the Windows OS is not in this case, because it's a multitasking environment with a partial controllable scheduling approach. The only controllable feature of Windows is the possibility of assigning a greater priority to the running process. In this blockset, a block allows the selection of the preferred priority to assign to Simulink.

This blockset simply hold the execution of the Simulink simulation attached to the time flow, in the sense that, if the cycle time is lower then the simulation step, this block wait for the time needed to fill the simulation step, leaving the remaining CPU time to all the Windows Process that need it. This concept is very simple but effective.

Respect to the other products, the RT blockset doesn't use a separate OS or runs a RT kernel to provide an RT simulation. Another feature that may let you prefer this blockset is the lack of limitation that this block impose to the use of Win32 instruction in your C/C++ S-functions. This blockset may virtually cooperate with any Win32 or DirectX instruction. Personally, I tested the RTW version of this block (that will be soon available) even on WinNT Embedded, with interesting results.

The OS where this solution was first developed is Windows NT 4.0, then this blockset is based on the Win32 library timing functions and process control functions. The actual released blockset has been realized only for the Windows platform and has been tested on a Win2000 and WinME OS with different speed processors. 
This blockset can be compiled also with Real Time Workshop, offering a greater protection respect to all other process than a Simulink model that runs inside Windows NT can guarantee.

To see more about this blockset and download the previous version for Matlab 5.3, 6.0 and 6.5, visit the following web page: 
http://digilander.iol.it/LeoDaga, click on the "RT Blockset" link on the right of the page. 
This page contains also much more informations about the use of the RT block. 
The library is already on a tested stage, but debugging information from new users will be appreciated.

In the image, the behavior of the simulation during a delaying OS operation.


这个人写的模块我在32位win7 + R2014a上测试通过,64位电脑不行;

64位的帖几个链接   

http://www.mathworks.com/matlabcentral/fileexchange/25043-rtsync-blockset-64-bit

http://www.mathworks.com/matlabcentral/fileexchange/29107-real-time-pacer-for-simulink

可能下载需要注册,这个网站建议注册,资源相当丰富,国外matlab最大的社区

使用起来很简单,将文件夹解压到工作目录,将里面的simulink模块copy到自己的模型中就可以用了,然后就是像正常使用simulink一样搭建模型

最后

以上就是温暖火龙果为你收集整理的simulink实现仿真时间与实际时间同步的全部内容,希望文章能够帮你解决simulink实现仿真时间与实际时间同步所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部