MATLAB_RTW__Engine
- 格式:pdf
- 大小:1.60 MB
- 文档页数:40
RTW技术在轮机模拟器中的应用高德基;欧镇;卢森微【摘要】This paper introduce a method of using the code generation of MATLAB/Simulink to convert Matlab/Simulink to theC++ code in the process of constructing the mathematical model of the marine main engine in the turbine simulator. You can modify the module parameters in real time and put the data generated by the simulink into the SQL database through further development for the calling of other parts of the turbine simulator . And the virtual operating system of the marine main engine based on the virtools , for example, I introduce the concrete application of this technology.This can not only avoid the problem of writing the code ,but also meet the demand of real-time in the turbine simulator and provide the convenience for the development of the turbine simulator.%文中介绍了轮机模拟器中船舶主机数学模型的构建过程中,利用MATLAB/Simulink中代码转换工具,将Simulink中搭建的船舶主机模型转换为C++代码。
基于MATLAB/ RTW的实时控制系统开发2004-4-30 14:33:38 来源: 点击次数: 4次新疆大学电气工程学院张宏立摘要:本文简要介绍了MATLAB的RTW工具箱的功能和作用,并用一实例着重介绍了使用RTW中XPC 开发实时控制系统的方法和步骤,为快速,高效开发实时控制系统提供有效手段。
关键词:MATLAB RTW XPC 实时控制一引言MATLAB是Mathworks 公司80年代推出的优秀科学计算和系统仿真软件,它以丰富的函数、强大的工具箱和简便的编程环境而深受广大用户欢迎,尤其是它的Simulink工具箱更是为系统仿真和建模提供强有力的支持。
但是说到MATLAB在实时控制方面的应用,就很少有人了解,关于这方面的资料也很少。
而RTW(Real Time Workshop)正是Mathworks 公司为MATLAB在实时控制应用方面开发的专门工具箱[1]。
二 RTW功能介绍RTW是对MATLAB和Simulink功能的一个重要补充,以往我们在设计控制系统中,先在Simulink中建模和仿真,直至仿真结果满意为止。
但是真正要开发实际控制器时,我们需要重新使用C或者其它语言重新编程,而当设计好的控制器与实际被控系统相联后,可能控制效果就不如仿真效果那么理想了,因为在仿真时我们可能对模型作了一些假设和近似或忽略了实际系统可能受到的扰动和噪声,这样造成仿真与实际应用相脱节。
RTW的产生解决了这个问题,RTW支两种类型的实时目标设计[2]:一种是快速原型化目标设计,另一种是嵌入式目标设计。
所谓快速原型化目标设计又叫半实物仿真,就是指可以用Simulink设计出来的控制器直接去控制实际的被控对象,通过半实物仿真过程来观察控制效果,如果控制效果不理想,则可以直接在Simulink上调整控制器的结构或参数,直至获得满意控制结果。
这样调试好的控制器可以认为是实际控制器的原型(Prototype) ,显然此时的控制器显然要好于纯数字仿真下的控制器。
关于如何使用VC调用matlab engine编程刚开始学使用VC调用matlab engine编程的时候,遇到很多问题,就去百度找解决方法,但发现根本解决不了我的问题,因为网上大部分实例和步骤都是在matlab6.5和vc++6.0环境下完成的,但是现在我用的是matlab2010a,所以会出现很多环境配置的问题,写本文的目的是总结用我在用VC调用matlab engine编程过程中出现的问题和解决办法。
Matlab Engine 采用Client/Server的方式,通过ActivcX通道和Matlab接口来实现在VC编程环境中直接调用matlab中的指令。
调用使用的函数是:engEvalSting。
下面是自己的实践过程:如果是第一次使用,则要设置一下Visual C++6.0 编程环境中的include 和lib的路径指向。
我的matlab2010安装在D盘目录D:\MATLAB 2010一、设置VC相关属性。
(1)Visual C++6.0 编译环境的设置:通过菜单 Tools/ Options,打开Visual C++6.0设置属性页,进入 Directorie页面,在 Show Directories for 下拉列表中选择Include Files,添加路径:D:\MATLAB 2010\extern\include (这是我电脑中Matlab的安装路径);再选择下拉列表中的 Library Files,添加路径:D:\MATLAB 2010\extern\lib\win32\microsoft(我电脑中Matlab 的安装路径,但是在我查看资料的时候看到很多是D:\MATLAB2010\extern\lib\win32\microsoft\msvc60这个路径,但是我用的是MatlabR2010a,msvc60是matlab6.5老版本才有的,10.0版本没有这个文件,所以我们把路径设到D:\MATLAB 2010\extern\lib\win32\microsoft 就可以了)。
1、搭建simulink模型输入幅度为3的阶跃信号进行测试得到输出为2、设置matlab新安装的matlab及vc,需要进行配置,在matlab窗口中输入mex –setup按提示进行默认配置即可。
3、设置仿真参数4、生成c代码生成文件在matlab当前目录下model_grt_rtw文件夹下5、新建vc工程(windows窗体程序)添加模型源文件(由simulink模型自动生成的)6、设置vc环境添加matlab相关文件引用7、项目属性设置对新增加的c源文件,修改预编译头工程属性8、文件修改model.h中增加//added by clh/////////////////////////////// extern void MdlOutputs(int_T tid);extern void MdlUpdate(int_T tid);extern void MdlInitializeSizes(void);extern void MdlInitializeSampleTimes(void); extern void MdlInitialize(void);extern void MdlStart(void);extern void MdlTerminate(void);extern RT_MODEL_model *model(void);///////////////////////////////////////////使用函数的文件(form1.h)中增加引用extern"C"{#include"model.h"#include"model_private.h"//#include "model_types.h"//#include "rtwtypes.h"//#include "rtmodel.h"//#include "rt_nonfinite.h" }Button按钮处理中增加得到输出结果结果一致。
Matlab代码生成(RTW)针对换挡省油提示系统项目代码生成过程如下:双击红色TCU0模块,进入TCU0子模块:选择Simulation/Configuration Parameters,打开配置参数界面:配置Solver,选择Fixed—step,因为是离散变量操作;解码器Solver选择ode1(Euler),Fixed-step size选择0。
004(4ms),与单片机执行周期对应;Tasking mode for periodic sample times:选择SingleTasking配置Hardware Implementation:选择Infineon C16xReal—time Workshop配置:系统目标文件选择ert.tlc;Language:选择C;使能生成文件Generate makefile;Make command:make_rtw;Template makefile:ert_default_tmfReport:配置为全部使能参数配置完成后,然后,主要对TCU模块进行定点数转换,右键TCU模块,选择Fixed-Point/Fixed-Point Advisor选择Run/Run to failure,会自动进行定点数转换检测定点数检测完成后,右键TCU模块,选择Real-Time Workshop/Build Subsystem点击Build后,会自动生成代码,产生代码生成报告:代码会保存在相应的目录下:双击打开TUC0_ert_rtw,将代码生成的C文件,HEX文件添加到单片机Keil程序中:最后6个文件为matlab代码生成的头文件和C文件.然后在单片机头文件中,调用一下四个头文件:那么Keil编译是肯定能通过的。
生成代码在程序中调用:首先在初始化程序中,调用TCU0_initialize(0),来完成生成代码的初始化过程。
在主程序中,每次给TCU0_U.Speed(车速),TCU0_U.throttle(油门开度),TCU0_U.Nee(转速)重新赋值,车辆上这三个值可以周期性的通过CAN采集到,然后通过生成代码TCU0_step()计算,便可得出期望档位TCU0_Y。
使用MATLAB Engine实现与C混合编程(五)引擎应用程序1、简介引擎应用程序的实质是把MATLAB做为一个引擎,它允许从你自己的C++程序调用这个引擎。
在运行时,引擎作为一个进程单独运行,你的C++程序也作为一个进程单独运行,二者可以通过进程间的通信机制进行交互。
2、引擎库MATLAB引擎库包含了若干个控制MATLAB引擎的函数,如下所示:engOpen 启动MATLAB引擎engClose 关闭MATLAB引擎engGetArray 从MATLAB引擎中获取一个MATLAB矩阵engPutArray 向MATLAB引擎发送一个MA TLAB矩阵engEvalString 执行于一个MATLAB命令engOutputBuffer 创建一个存储MATLAB文本输出的缓冲区同时,引擎应用程序还可以使用前面提到的API函数。
3、一个例子从这个示例中,我们看出引擎应用程序是如何编制的:/** engdemo.c* This is a simple program that illustrates how to call the* MATLAB engine functions from a C program.*/#include#include#include#include "engine.h"#define BUFSIZE 256int main(){Engine *ep;mxArray *T = NULL, *result = NULL;char buffer[BUFSIZE];double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,8.0, 9.0 };8.0, 9.0 };6-6/** Start the MATLAB engine locally by executing the string* "matlab".* To start the session on a remote host, use the name of* the host as the string rather than \0* For more complicated cases, use any string with whitespace,* and that string will be executed literally to start MATLAB.*/if (!(ep = engOpen("\0"))) {fprintf(stderr, "\nCan't start MATLAB engine\n");return EXIT_FAILURE;} /*启动MATLAB引擎*//** PART I* For the first half of this demonstration, we will send data* to MATLAB, analyze the data, and plot the result.* Create a variable for our data.*/T = mxCreateDoubleMatrix(1, 10, mxREAL); /*创建一个矩阵*/mxSetName(T, "T"); /*设置矩阵的名字为“T”*/memcpy((void *)mxGetPr(T), (void *)time, sizeof(time)); /*向矩阵“T”赋值*//** 把矩阵“T”置入MATLAB引擎*/engPutArray(ep, T)/** Evaluate a function of time, distance = (1/2)g.*t.^2* (g is the acceleration due to gravity).*/engEvalString(ep, "D = .5.*(–9.8).*T.^2;");/*执行MATLAB 命令:D = .5.*(–9.8).*T.^2;*//** 绘制图象.*/engEvalString(ep, "plot(T,D);"); /*执行MA TLAB命令:绘图*/engEvalString(ep, "title('Position vs. Time for a fallingobject');"); /*执行MATLAB命令:给图象加标题*/engEvalString(ep, "xlabel('Time (seconds)');"); /*执行MATLAB命令:设置X轴坐标*/ engEvalString(ep, "xlabel('Time (seconds)');"); /*执行MATLAB命令:设置X轴坐标*/ engEvalString(ep, "ylabel('Position (meters)');"); /*执行MATLAB命令:设置Y轴坐标*//** Use fgetc() to make sure that we pause long enough to be* able to see the plot.*/printf("Hit return to continue\n\n");fgetc(stdin);/** We're done for Part I! Free memory, close MATLAB engine.*/printf("Done for Part I.\n");mxDestroyArray(T); /*从内存中撤销矩阵“T”*/engEvalString(ep, "close;"); /*关闭刚才显示图象的窗口*//** PART II* For the second half of this demonstration, we will request* a MATLAB string, which should define a variable X. MATLAB* will evaluate the string and create the variable. We* will then recover the variable, and determine its type.*//** Use engOutputBuffer to capture MATLAB output, so we can* echo it back.*/engOutputBuffer(ep, buffer, BUFSIZE); /*构建MATLAB文本输入缓冲区*/while (result == NULL) {char str[BUFSIZE];/** Get a string input from the user.*/printf("Enter a MATLAB command to evaluate. Thiscommand should\n");printf("create a variable X. This program will thendetermine\n");printf("what kind of variable you created.\n");printf("For example: X = 1:5\n");printf(">> "); /*要求用户输入一个MATLAB命令*/fgets(str, BUFSIZE–1, stdin); /*获得用户输入*//** Evaluate input with engEvalString.*/engEvalString(ep, str); /*执行用户输入的MATLAB命令*/engEvalString(ep, str); /*执行用户输入的MATLAB命令*//** Echo the output from the command. First two characters* are always the double prompt (>>).*/printf("%s", buffer+2); /*显示该MATLAB命令的执行情况*//** Get result of computation.*/printf("\nRetrieving X...\n");if ((result = engGetArray(ep,"X")) == NULL)/*判断是否可以从MATLAB 引擎中获得矩阵“X”*/printf("Oops! You didn't create a variable X.\n\n");elseprintf("X is class %s\t\n", mxGetClassName(result));/*显示矩阵“X”的类型*/} /* while(result==NULL)*//** We're done! Free memory, close MATLAB engine and exit.*/printf("Done!\n");mxDestroyArray(result); /*从内存中撤销矩阵“T”*/engClose(ep); /*关闭MATLAB引擎*/return EXIT_SUCCESS; /*返回*/}4、引擎应用程序的编译对于象上例中的控制台程序,可以在MATLAB命令行中直接使用带-f参数的mex命令编译。