MATLAB在TwinCat 2.11的应用
使用MATLAB/simulink生成PLC的st语言功能函数的方法
本文统一以MATLAB demo中的plcdemo_simple.mdl为例。首先在MATLAB命令行中执行命令demo,选择simulink PLC coder->Demos->Basic Introductions and Examples->Generating Structured Text for a Simple Simulink Subsystem,然后打开该模型。
? 打开参数配置面板(Simulation->Configuration Parameters)
? 进入PLC Coder的选项页,将Targer IDE选为BeckhoffTwinCat 2.11。然后应用。
? 回到模型,右键点击SimpleSystem,选择PLC Coder->Generate Code For
Subsystem。如果没有错误,将在当前目录下生成plcdemo_simple.exp文件及诊断对话框。该文件即为ST语言的PLC文档。
打开TwinCat的PLC Control,新建一个工程,命名为testmatlab。下面我们将刚才生成的文件导入到TwinCat中,选择Project->Import,选择刚才生成的
plcdemo_simple_subsystem.exp文件。完成后TwinCat的POUs窗口中就有了功能函数文件。
!!!不是所有的MATLAB工程都能导出为PLC Code, 它的前提条件是支持的子系统必须是atomic subsystems. 以下种类的数据类型不能被PLC CODE生成工具接受,也就是说不能被转换为PLC代码。
? ? ? ? ? ? ? ? ? ? ? ?
Fixed-point data types Complex data types Model reference
Global data store memory (DSM)
Absolute time temporal logic in Stateflow charts Stateflow machine-parented data and events Exported graphical functions in Stateflow charts
Limited support for math functions. The coder does not support the following functions: tanh, cosh, sinh, atan2, rand. Triggered subsystems Merge block
Multi-rate models
Signal and state storage classes
以下案例是一个比较证明,用来表示不是所有的MATLAB组件都可以用来输出PLC代码,案例程序参见simulink PLC coder->Demos->Basic Introductions and Examples->plcdemo_pid_feedforward.mdl
只选择PID_feedforward 和 Memory,点击右键选择创建子系统
创建完子系统后,在上面点击右键
选择 Treat as atomic unit
点击OK, 然后在新生成的SUBSYSTEM上点击右键
当兼容性测试通过,则显示为
如果一开始选择了多个组件加入到子系统中比如样例中的PID_feedforward, Plant Actuator和 First Order Process, 那么在创建完子系统以后,点击PLC CODRE的兼容性检查,会出现以下错误
这代表该子系统某一个成分不能被PLC CODE工具接受,也意味着不能生成PLC代码。