基于BECKHOFF TWINCAT 3的模块化C++实时性研究

基于BECKHOFF TWINCAT 3的模块化C++实时性研究摘要:随着信息化与工业自动化和智能制造领域深度融合,诸如C或C++等高级语言均在控制领域有了突破性应用。

然而,标准C或C++无法很好地适应工业自动化生产中针对实时性控制的需求。

德国BECKHOFF公司基于其集成自动化开发环境TwinCAT 3软件平台,提出了模块化C/C++的概念。

本文介绍代码封装成通用模块过程,如何分配相应的task周期地运行,从而解决了高级语言代码实时性问题,同时可以达到较高的软件开发灵活性。

关键词:智能制造;工业自动化;TwinCAT 3;模块化C/C++1.引言近年来,随着“工业4.0”和“中国制造2025”的提出,国内智能制造行业迅猛发展[1,2,3],运动控制设备和逻辑控制设备被广泛利用与新能源汽车制造业、金属加工、材料成型、食品生产与检测等行业中[4,5]。

在生产制造中常用的控制单元有可编程逻辑控制器(Programmable Logic Controller,PLC)、离散控制系统(Discrete Control System,DCS)等[6,7]。

这些设备为工业制造提供了安全、可靠的控制模式,然而随着机器人行业和智能制造领域的进一步深化,以上设备的不足之处逐渐暴露出来。

针对这种情况,德国BECKHOFF自动化公司在已有的开发套件TwinCAT 2基础上[8,9],研发了基于Microsoft Visual Studio的TwinCAT 3工业控制开发环境[10],可以通过模块封装的形式在面向PLC控制的程序开发中调用已经编译好的C/C++代码,较大地提高算法代码的运行效率,便于信息/计算机从业人员进行工业控制中特定功能的模块开发,降低代码的开发和维护成本,加快开发流程。

2.基于BECKHOFF TwinCAT 3模块化设计2.1 现有工业控制开发中的问题首先,PLC作为可靠的工业控制单元,其设计初衷是为工业控制提供高可靠的设备,然而计算能力却往往较低,无法实现高精度的运动位置插补算法与复杂度较高的信号处理算法。

在以往的工业应用中,以上问题往往通过具有较高计算能力的工控机或专门的运动控制卡实现,这就在无形中提升了生产成本、提高了系统开发难度,同时由于控制设备与运算设备之间需要进行通信,这就对总线通信的实时性带来了更高的要求。

其次,全球范围内,生产PLC生产厂家众多,例如美国Rockwell Automation公司、德国Siemens公司、日本的Omron与Mitsubishi公司等,国内生产PLC厂家如南大傲拓、无锡泊昂等。

针对PLC控制系统的开发语言有多种,例如梯形图(LD)、指令表(IL)、顺序功能图(SFC)、结构化文本语言(ST)等,这些语言均为IEC 61131标准下规定的PLC语言。

然而,针对不同的硬件版本,以Siemens公司为例,提供的开发环境也有区别,如Step 7和TIA Portal软件,这些软件不同的架构和开发流程无疑也带来了较高的开发成本。

最后,随着人工智能和机器学习向传统制造业和先进制造业的深入,计算机控制技术和高级语言正在工业控制中发挥着更大的作用。

然而由于PLC控制系统开发的封闭性,很难引入相应的高级语言编译及开发模块,同时难以解决高级语言执行相应控制任务时的实时性问题。

由于以上存在的问题,很难将高级语言编写程序时的灵活性与代码编译执行的高效性与工业实时控制相结合,局限了高级语言在工业现场中的应用。

2.2 BECKHOFF TwinCAT 3模块化设计机制随着工业产品设计复杂化、精细化和高精度的要求日益加剧,为减少开发成本,提高设计再利用率,制造企业在设计之初就对产品进行模块化设计。

这里的模块化设计分为两个部分,在硬件上根据产品的功能,设计实现不同功能的结构及结构原型,并保存设计信息,在其他具有同样或类似功能的结构上采用近似设计方案;其次,在软件开发中,将具有通用功能的算法或的控制流程封装成库文件或模块,以备之后调用,降低代码二次开发量,节约开发成本。

在BECKHOFF TwinCAT 3集成开发环境中,TwinCAT运行时(Runtime)为加载、应用和管理TwinCAT模块提供了必要的环境支持。

通过分配合理的系统和硬件资源,例如内存、硬件访问、任务优先级和总线通信资源等,实现了模块化设计的基础。

尽管不同的C/C++模块在编译时采用的编译器不同,在经过封装之后,可以脱开其原始编译执行环境在不同开发环境中调用,完成相同功能。

与PLC控制程序需要在TwinCAT运行时环境中运行不同,TwinCAT C++模块可以无需在宿主环境中运行,如图1所示,支持这个模块运行的是系统内核模块(.sys)。

图1 TwinCAT PLC和C/C++模块运行环境示意图然而,如何在依靠系统内核实现TwinCAT C/C++模块运行的实时性是Windows 操作系统面临的一大挑战。

众所周知,Windows操作系统是一个非实时系统,无法自发地完成具有实时性属性任务的调度。

在Windows操作系统内进行高级语言的开发,以C#为例,当创建和编译完成程序之后,这段程序执行所需的资源的任务调度工作就交给了操作系统。

由于Windows操作系统多线程、多任务的特点,可能在任何时间点中断原有程序的运行,并且何时回复执行原程序也是未知的,如图2所示,这无法满足任务实时性要求。

图2 Windows操作系统的程序执行过程示意图因此在TwinCAT C/C++模块调用执行时采用类似PLC扫描周期的方式通过周期性函数回调管理实时性任务,在一个周期内必须完成程序的执行,如图3所示。

图3 实时性TwinCAT C/C++模块执行流程图4 并发多任务实现示意图在传统意义上的操作系统级代码开发过程中,并发特性是通过程序自身来控制的,也就是说通过程序自身控制在何处激活新的线程,在何时进行线程间的通信。

所有这些任务的实现都需要依靠一定的资源,这些资源的分配会进一步的危害到任务执行的实时性。

同时由于线程间的通信是事件驱动的,也就是说当有事件触发时才会在线程间开始或终止通信,这样就使得通信进程更加不可控。

为了解决这一问题,在TwinCAT中,通过任务(tasks)而非线程来调用模块。

在多核操作系统中,任务可以被分配给一个或多个CPU核,这些任务通常都带有任务周期和优先级属性(高优先级任务可以抢占低优先级任务的运行资源)。

当这些任务被分配到不同的CPU核上时,就实现了并发任务,如图4所示。

在内存管理方面,TwinCAT有自身的实时内存管理方式。

所有的内存资源均来自于操作系统分配的非页面内存池。

此外,在同样的内存区域内,TwinCAT提供了一种形式的“路由内存”,利用这些内存资源,TcCOM模块可以在实时性环境下动态的分配内存资源,如图5所示。

图5 TwinCAT内存管理示意图3.实例化TwinCAT 3 C/C++模块流程第一步,在安装有TwinCAT 3 XAE集成开发环境的计算机中打开Micro Visual Studio软件,并通过文件菜单新建一个TwinCAT工程。

之后在解决方案窗口中单击“C++”选择新建项目,并在接下来的窗口中选择TwinCAT驱动器项目,或者通过TwinCAT静态库项目新建静态TC-C++库文件。

在接下来的对话框中选择带有周期性I/O的TwinCAT模块类。

之后,带有驱动的TwinCAT 3 C++工程就创建完毕,如图6所示。

第二步,通过具体需求实现所需的TwinCAT 3 C++模块,并进行编译生成。

在编译生成时注意选择目标平台,如图7所示。

图7 目标平台选择第三步,实例化TwinCAT 3 C++模块。

在执行C++模块之前需要对模块进行实例化,同一个模块可以存在多个实例。

在C++ Configuration中通过添加新项目实例化一个对象,如图8所示。

图6 创建完成图8 实例化C++模块最后,将通过创建TwinCAT task并将其挂载实例化的模块上即可实现周期的调用模块对应的任务。

4.实验流程4.1 实验设备硬件:Thinkpad X270笔记本,CPU intel I7-7700,16G RAM,512G SSD。

软件:操作系统:WIN7 Ultimate,Microsoft Visual Studio 2012,TwinCAT 3 installation XAE Engineering,Microsoft Windows Driver Kit。

4.2 实验目标实现TwinCAT 3中实现PLC程序对C++模块的调用。

4.3 实验步骤(1)创建TwinCAT 3 C++模块,对其实例化,并将实例分配至某一任务。

(2)创建PLC项目:首先,在Microsoft Visual Studio中新建TwinCAT项目,并在其中创建标准PLC任务。

其次,在PLC文件中添加PLC代码,如图9所示。

并通过添加Function Block实现对Module实例对应的task的调用,结果如图10所示。

图9 PLC程序图10 调用结果5.结束语在工业控制集成开发环境中利用C/C++实现相应的算法,并利用模块化封装,可以大大提升代码利用率,在计算机控制环境模式下大大提升控制性能。

BECKHOFF公司提供的自动化控制集成开发环境TwinCAT 3,在Windows下实现C/C++模块的封装,并可通过PLC实现对C/C++模块的调用。

在满足实时性前提下,提高工业控制中算法的性能,为工业控制的实时性、可靠性和高性能提供了新的途径。

参考文献[1]胡振豪,左小五,万红平.工业以太网EtherCAT的实时性研究[J].软件导刊,2018,17(02):179-181+185.[2]闫高斌,王军,王金涛.面向工业无线控制网络的业务传输机制[J].计算机工程与设计,2017,38(11):3007-3010+3117.[3]郭秦.实时性容错以太网交换机的电路设计[D].北京交通大学,2017.[4]南扬.EtherCAT工业以太网控制系统主站设计与实现[D].西安电子科技大学,2017.[5]姚舜.工业机器人控制器实时多任务软件设计与实现[D].东南大学,2017.[6]赵新亚.网络实时通信技术在工业控制上的研究[J].科技经济导刊,2017(06):29.[7]李侃.工业无线控制网络中的实时性问题研究[J].科技视界,2015(26):332+341.[8]袁熹.高可靠网络中实时性保障技术研究与应用[D].南华大学,2015.[9]杨明.工业以太网实时任务调度算法的研究[D].浙江理工大学,2015.[10]邓尚伟.工业以太网实时通信技术及进展[J].中国新通信,2015,17(03):24.。

合集下载

TwinCAT编程软件介绍

TwinCAT编程软件介绍

8 Bit
16 Bit 32 Bit 8 Bit 8 Bit 16 Bit 16 Bit 32 Bit 32 Bit
12
第三讲
实型
TwinCAT PLC数据类型
又称浮点型。
REAL: 占32Bit地址空间
字符串型
STRING: 字符型数据的宽度在变量声明时指定,若没有指定,系统默认宽度为80个字符。每个字符占一 个字节(8 Bit)的地址空间。 举例:str:STRING(35)=„This is a String‟;
T#60m0s0ms
T#8052m18s3ms 或T#5d14h12m18s3ms 举例 TIME_OF_DAY 或 TOD TOD#15:36:10.340 TOD#11:38 TOD#00:00 TOD#00:00:00.100
16#EE80,16#0036
16#19D3,16#1CCC
从00时00分00秒开始以毫秒计算,占32 Bit地址空间 低16 Bit,高16 Bit 16#1764,16#0359 16#09C0,16#09C0 16#0000,16#0000 16#0064,16#0000
程序的调用
在IL中调用: CAL PRGExample LD ST PRGExample.PAR ERG
在ST中调用: PRGEample; ERG:= PRGEample.PAR;
在FBD中调用: PRGEample
PAR
ERG
11
第三讲
TwinCAT PLC数据类型
布尔型
BOOL: 真( TRUE )与 假( FALSE ),占用1位(Bit)地址空间
整型
类型 下限 上限 占用地址空间
BYTE

Beckhoff测量模块

Beckhoff测量模块

Beckhoff 的自动化技术适用于各种信号和现场总线,可为所有常用的 I/O 信号、模拟量信号和现场总线系统提供全系列的现场总线组件。

EtherCAT 是用于实现工业自动化的以太网解决方案,具有性能优异和操作简单的特点。

本书详述了 Beckhoff 现场总线模块中的测量模块、高精度模块、XFC 模块和宽温模块。

目录现场总线端子模块简介测量模块的的应用• 只有 12mm 宽的测量仪器——基于现场总线的测量模块• 称重模块介绍• 三相电力模块介绍• 万用表介绍• 振动测量模块介绍• 温度测量模块介绍• 高精度模块• XFC 模块• 宽温模块SCOPE VIEW 2 软件介绍只有 12 mm 宽的测量仪器——基于现场总线的测量模块称重模块介绍三相电力测量模块介绍EL3356 和 EL3356-0010 称重测量端子模块可以直接连接一个电阻桥或者 1 个 6 线制的称重传感器,最终的重量值作为一个过程数据在模块内部计算,不需要在 PLC 上面进行额外的计算。

为了满足各种需求,这系列模块有以下特点:• 测量误差 <0.01%• 高分辨率:16 位(EL3356)或者 24 位(EL3356-0010)• 采样时间:10 ms(EL3356)或者 100 μs(EL3356-0010)• 电路自整定——消除温漂和时漂• 分布式时钟模式——可以获取采样数据的具体时间(仅 EL3356-0010)• 手动输入传感器的铭牌参数(如 KG 和 mV/V)以及通过整定程序来整定传感器的参数(如零平衡点)• 预制皮重功能,可以在测量值中去掉产品外包装的重量• 针对高速动态称重的特殊功能可以使用 EL3356 来实现高精度慢速称重测量任务,而 EL3356-0010 特别适合用于快速、准确监测扭矩或张力的传感器。

此类传感器的应用范围包括:• 称重任务:慢速料仓测量或快速装袋• 运动部件的张力测量• 静载荷下的变形测量和变形报警• 通过传感器变形进行压力测量EL3403、EL3413 和 EL3773 EtherCAT 电力测量端子模块可以直接通过现场总线对相连工厂或建筑物电网进行能耗分析,也可以对各个用电设备进行具体的能耗数据分析。

TwinCat软件基本使用教程

TwinCat软件基本使用教程

Beckhoff公司TwinCat软件基本使用教程协助:1、<能助信息论坛>:/bbs2、<倍福(Beckhoff)自动化QQ群>:463707183、<问题反馈邮箱>:thankchina@第二篇认识TwinCat软件主要分为2大部分:从图中可以看到,“System Manager”和“PLC Control”。

System Manager:系统管理器:进行配置整个控制系统。

下面请参考 ====System Manager 操作==== 部分内容;PLC Control:PLC控制器:编写控制程序。

下面请参考 ====PLC Control 操作==== 部分内容;====System Manager 操作====1、连接到目标系统:打开System Manager管理器窗口,注意此时窗口的右下脚标志为Local (192.168.0.100.1.1) |Config Mode如下图:下一步,选择需要控制的目标系统,按下Choose Target… 按钮,出现Choose Target System窗口:下一步,选择搜索整个以太网,按下Search (Ethernet) 按钮,并在出现的窗口中按下Broadcast Search按钮,显示出搜索到的TwinCat系统,显示为类似下面窗口:在Host Name中选择需要连接的工控机,点选左下脚的Add Route按钮,显示登陆密码输入窗口:输入正确的用户名和密码,(另:参考==== 远程桌面连接 ====进行设置该用户名和密码)点选OK按钮,并自动退回到Add Route Dialog窗口:点选窗口中的Close按钮返回到Choose Target System窗口:此时选择需要连接的工控机,并点选OK按钮,返回System Manager管理器主窗口,注意此时窗口的右下脚标志为CX_XXXXX (5.1.127.166.1.1) | RTime X%如下图:窗口的右下脚表示的是当前选择的系统和当前系统的状态。

BECKHOFF TwinCAT CNC简明操作手册

BECKHOFF TwinCAT CNC简明操作手册
3ads通讯四channel参数概述beckhoff金属加工cnc系统从入门到精通第四章第四章cnc工程概述plc程序介绍一main主程序二报警信息显示1报警设定2报警程序3报警显示画面三初始化程序四轴使能设置五手动操作1手动模式类型2手动初始化3选择手动轴4快慢速5手动速度六通道设置1cnc报警处理2倍率设定3blocksearch4手动操作5cnc通道接口第第五五章章cnc工程工程特殊功能介绍第五章
BECKHOFF 金属加工 CNC 系统从入门到精通
CNC 软件更新: CNC 部分版本在应用中需要更新补丁,譬如我们在使用 CNC 1506E 版本的时候,如果用到转为激光切割开
发的尖叫控制功能时,需要加装补丁文件。 更新补丁方法如下: 1、 解压缩 TcISG 1506e.01 文件。
2、 将解压文件夹中的【ahmi_ads.exe】复制到 TWINCAT 软件安装位置的 C:\TwinCAT\Cnc Tools。 将压缩文件内容替换到 C:\TwinCAT\Cnc Tools 中的【ahmi_ads.exe】文件。
1Hale Waihona Puke 模式相关 2)CNC 状态 3)特殊功能 第五章 CNC 工程特殊功能介绍 第五章:
BECKHOFF 金属加工 CNC 系统从入门到精通
第一章 调试准备
概述: 倍福 CNC 系统是建立在倍福的 TwinCAT NCI 软件基础上的一个应用。倍福 CNC 系统是一套标准的应用系统:G
代码格式遵循德国 DIN66025 标准,PLC 程序遵循 IEC61131-3 标准,配置界面在 C#程序下编写的。倍福 CNC 系统是 一套完整的 CNC 应用系统,功能涵盖了普通的 CNC 插补功能,到复杂的空间坐标转换功能,RTCP\TLC\KINEMIC 坐 标转换,可以实现多通道、多种类型 CNC 轴、高速切割、样条等标准 CNC 功能等;倍福 CNC 系统是一个完善的开 发平台,适合用于磨床、钻床、铣床、车床、特殊机械如等离子、激光机床等。 一、控制硬件要求

Twincat使用

Twincat使用

Beckhoff公司TwinCat软件基本使用教程协助:1、<能助信息论坛>:/bbs2、<倍福(Beckhoff)自动化QQ群>:463707183、<问题反馈邮箱>:thankchina@第二篇认识TwinCat软件主要分为2大部分:从图中可以看到,“System Manager”和“PLC Control”。

System Manager:系统管理器:进行配置整个控制系统。

下面请参考 ====System Manager 操作==== 部分内容;PLC Control:PLC控制器:编写控制程序。

下面请参考 ====PLC Control 操作==== 部分内容;====System Manager 操作====1、连接到目标系统:打开System Manager管理器窗口,注意此时窗口的右下脚标志为Local (192.168.0.100.1.1) |Config Mode如下图:下一步,选择需要控制的目标系统,按下Choose Target… 按钮,出现Choose Target System窗口:下一步,选择搜索整个以太网,按下Search (Ethernet) 按钮,并在出现的窗口中按下Broadcast Search按钮,显示出搜索到的TwinCat系统,显示为类似下面窗口:在Host Name中选择需要连接的工控机,点选左下脚的Add Route按钮,显示登陆密码输入窗口:输入正确的用户名和密码,(另:参考==== 远程桌面连接 ====进行设置该用户名和密码)点选OK按钮,并自动退回到Add Route Dialog窗口:点选窗口中的Close按钮返回到Choose Target System窗口:此时选择需要连接的工控机,并点选OK按钮,返回System Manager管理器主窗口,注意此时窗口的右下脚标志为CX_XXXXX (5.1.127.166.1.1) | RTime X%如下图:窗口的右下脚表示的是当前选择的系统和当前系统的状态。

基于TwinCAT的主减速器试车台监测系统设计

基于TwinCAT的主减速器试车台监测系统设计

Design of monitoring system for a main gearbox bench based on TwinCAT
Li Xiongbin,Xia Jiqiang,Man Qingfeng ( School of Mechanical Engineering & Automation,Beihang University,Beijing 100191,China)
根据 5 类传感器输出信号,搭建现场设备层总线 结构,现场设备层硬件拓扑图如图 2 所示。
图 1 系统总体构成图
2 系统详细设计
2. 1 PLC 数据实时采集 根据传感器信号的类型,构建现场总线、EtherCAT
总线二级总线结构,利用 TwinCAT PC 作为 EtherCAT 软主站,以实现可编程逻辑控制器( PLC) 对数据的实 时采集。 2. 1. 1 传感器信号的分类
* 国家科技重大专项项目( 2010ZX04014-017)
123
2015 年第 6 期
现代制造工程( Modern Manufacturing Engineering)
1 系统总体构成
试车台监测系统基于 TwinCAT 软 PLC 技术,采用 分层的结构模型,由数据层、HMI 层、TwinCAT 层和现 场设备层 4 大部分组成。现场设备层由 EtherCAT 及 现场总线二级总线结构组成,将现场底层传感器信号 通过现场总线 ( PRPFIBUS-DP、CANopen 及 EtherCAN 总线) 构成分布式的采集网络,并将不同的现场总线 信号转换为高速实时的 EtherCAT 信号。TwinCAT 层 构成 EtherCAT 软主站,通过 TwinCAT System Manager 实现对各总线的访问,TwinCAT PLC Control 进行数据 的处理。HMI 层采用 C #高级语言完成 C #程序的编 制,实 现 采 集 量 的 监 测 和 报 警、用 户 管 理,以 及 与 TwinCAT 的通信及对数据层的访问。数据层采用 Microsoft SQL Server 2008 构建数据库,实现试车数据的 管 理。 HMI 层 C # 程 序 通 过 ADS ( Automatic Device Specification) 与 TwinCAT 进 行 通 信,通 过 ADO. NET 技术访问数据库。图 1 所示为系统总体构成图。ຫໍສະໝຸດ 传感器类型 1 2 3 4 5

BECKHOFF技术_TwinCat的特点


TwinCAT特点 BECKHOFF技术特点
TwinCAT PLC/NC 性能参数 (安装在标准 P4 主板上 主板上): 安装在标准
1000 条PLC 命令所需执行时间 0,9 微秒 20 微秒 30 微秒 50 微秒 100 个伺服轴所需执行时间 更新1000 更新1000 个 I/O (EtherCAT) 所需通信时间
只要以太网能够到达的地方, 只要以太网能够到达的地方 TwinCAT通讯就能到达 通讯就能到达
丰富的调试诊断工具
TwinCAT特点 BECKHOFF技术特点
• 无须仿真,PLC程序可直接在开发环境所在的机器上运行。 无须仿真, 程序可直接在开发环境所在的机器上运行。 程序可直接在开发环境所在的机器上运行 • 类似高级语言的调试功能,支持断点功能。 类似高级语言的调试功能,支持断点功能。 • BECKHOFF另外提供一些免费的工具软件, BECKHOFF另外提供一些免费的工具软件, 另外提供一些免费的工具软件 示波器软件: 示波器软件:scope view, , 可视化组件: 可视化组件:Visulization, , 设备仿真功能库 …… • TwinCAT Freerun模式具有硬件诊断功能。 模式具有硬件诊断功能。 模式具有硬件诊断功能
支持源代码下载, 支持源代码下载 上传源代码时所 有注释均可保留
只要计算的CPU够快 够快, 只要计算的 够快 TwinCAT的任务周期最短 的任务周期最短 可至50微秒 可至 微秒
丰富的功能库
TwinCAT特点 BECKHOFF技术特点
运行速度快
TwinCAT PLC/NC:
Execution time / µs
1200 1000 800 600 400 200

EtherCAT系统搭建与测试指导(采用Beckhoff 公司的测试套件)

EtherCAT系统搭建与测试指导(采用 Beckhoff公司的 TC9910-B112测试套件)Version 0.1Date: 2010-10-09版权申明TrademarksBeckhoff®, TwinCAT®, EtherCAT®,Safety over EtherCAT®,TwinSAFE® andXFC® are registered trademarks of and licensed by Beckhoff Automation GmbH. Other designations used in this publication may be trademarks whose use by third parties for their own purposes could violate the rights of the owners.Patent PendingThe EtherCAT Technology is covered, including but not limited to the following German patent applications and patents: DE10304637, DE102004044764, DE102005009224, DE102007017835 with corresponding applications or registrations in various other countries.DisclaimerThe documentation has been prepared with care. The products described are, however, constantly under development. For that reason the documentation is not in every case checked for consistency with performance data, standards or other characteristics. In the event that it contains technical or editorial errors, we retain the right to make alterations at any time and without warning. No claims for the modification of products that have already been supplied may be made on the basis of the data, diagrams and descriptions in this documentation.Copyright© Beckhoff Automation GmbH 05/2009.The reproduction, distribution and utilization of this document as well as the communication of its contents to others without express authorization are prohibited. Offenders will be held liable for the payment of damages.All rights reserved in the event of the grant of a patent, utility model or design.1 EtherCAT 协议介绍EtherCAT 是德国倍福(Beckhoff)公司提出的开放式实时以太网,由独立的技术小组ETG(EtherCAT Technology Group)负责管理和推广。

twincat3 指针

Twincat3 指针1. 什么是 Twincat3?Twincat3 是由德国贝加莱(Beckhoff)公司开发的一款工业自动化软件平台。

它提供了一个集成的开发环境,用于编写、调试和测试各种自动化控制系统。

Twincat3 是基于 Windows 操作系统的,它的设计目标是提供高效、可靠和灵活的自动化解决方案。

2. 指针在 Twincat3 中的作用在 Twincat3 中,指针是一种特殊的变量类型,它存储了一个内存地址,可以用来访问和操作内存中的数据。

指针在编程中扮演着非常重要的角色,它可以提供灵活性和效率,使程序能够更好地管理和利用内存资源。

指针在 Twincat3 中的主要作用有以下几个方面:2.1 内存管理指针可以用来动态分配和释放内存。

在 Twincat3 中,可以使用指针来创建动态数组或者动态数据结构,这样可以根据实际需要在运行时分配和释放内存,提高内存利用率。

2.2 数据传递指针可以用来传递大型数据结构或对象,避免了数据的复制和传输过程中产生的额外开销。

通过传递指针,可以直接访问和操作内存中的数据,提高了程序的效率。

2.3 数据共享指针可以用来实现数据的共享和同步。

在多个任务或线程之间共享数据时,可以使用指针来访问和修改共享内存区域,实现数据的同步和共享。

2.4 数据结构的操作指针可以用来操作复杂的数据结构,如链表、树等。

通过指针,可以方便地遍历和操作数据结构,实现各种高级的数据操作算法。

2.5 外部设备的访问指针可以用来访问外部设备的内存映射区域,实现对外部设备的控制和通信。

通过指针,可以直接读写外部设备的寄存器或内存区域,实现与外部设备的高效交互。

3. Twincat3 中指针的使用在 Twincat3 中,使用指针需要注意以下几点:3.1 指针的定义和初始化在 Twincat3 中,可以使用VAR_POINTER关键字来定义一个指针变量,并使用ADR 函数来初始化指针变量。

Beckhoff TwinCAT 3 系统开发指南说明书

Beckhoff and TwinCAT 3 System Development GuideDecember 2015Steven BlairUniversity of StrathclydeThe place of useful learningContents1Introduction (2)2TwinCAT 3 Installation on Development PC (3)3Connecting to a Hardware Controller (4)4TwinCAT 3 – Typical C++ Project Configuration (5)4.1C++ Module (5)4.2Assigning Module Instances to Processor Cores (9)4.3Using a TMC File (11)4.4Real-Time Monitoring (13)5Integrating Simulink Models (16)5.1Manual Compilation of Generated C++ Code within TwinCAT (16)5.2Using Simulink-TwinCAT 3 Integration (18)6References (20)AbbreviationsCPU Central Processing UnitIO Input or OutputPC Personal ComputerPMU Phasor Measurement UnitTMC TwinCAT Module ClassWDK Windows Driver Kit1 IntroductionThis document is a guide for setting up a Beckhoff hardware controller and a development PC. It is assumed that the development PC runs Windows 7 or Window 8/8.1, although other versions of Windows (including 32-bit and 64-bit) should also work. In particular, guidance is given on using C++ modules and integrating MATLAB Simulink models within TwinCAT 3.This guide supplements existing Beckhoff documentation:∙Webinar on TwinCAT 3 C++ integration:/webinar/en/Webinar_TwinCAT_3_C_plusplus_Integra tion/default.htm∙C++ Quick Start Guide:https:///english.php?content=../content/1033/tc3_c/7205759414 8625035.html&id=∙The comprehensive manual is available here:ftp:///Document/automation/twincat3/TC1300_C_EN.pdf ∙The Beckhoff Information System (available at: /index_en.htm) contains general documentation and some code examples; however, many examples are for TwinCAT 2 and cannot be used in TwinCAT 3.2 TwinCAT3 Installation on Development PC1. Install Visual Studio. There are several versions which are compatible with TwinCAT3, and provide full functionality:a. Free download: Visual Studio Community 2013 (available here:https:///en-us/news/vs2013-community-vs.aspx). Notethat TwinCAT 3 does not yet support the 2015 version.b. Commercial: Visual Studio 2010, 2012, or 2013 (Professional, Premium, orUltimate).c. Within Strathclyde, the 2013 Professional edition can obtained from theMicrosoft DreamSpark agreement, for teaching and non-commercial researchpurposes.Note that if you install TwinCAT 3 without Visual Studio already installed, it will install “Visual Studio 2010 Shell”. This version is not suitable for C++ development or debugging.2. Install Microsoft Windows Driver Kit (WDK) 7 and set up a Windows environmentvariable, as described by the following instructions: /english.php?content=../content/1033/tc3_c/54043195639 122187.html&id=3. Download TwinCAT 3 from /english.asp?download/tc3-downloads.htm?id=1905053019883865a. You need to create an account on the Beckhoff website to access downloads.b. The specific download is found in the “Software” > “TE1xxx | Engineering”section, and is called “TwinCAT 3.1 –eXtended Automation Engineering(XAE)”.4. When using TwinCAT 3, you might be asked to create or renew licenses. This isnormal. Licenses last for one week, but can be simply renewed, perpetually, when needed. Licenses only need to be purchased for commercial work.5. For 64-bit targets, two additional steps must be done:a. On the development PC, set up digital driver signing. The process is givenhere:/english.php?content=../content/1033/tc3_c/63050394893879947.html&id=. Note that step 3 is only required for executing thesystem on the local development machine.b. On the hardware target, execute the “bcdedit /set testsigning yes” command(step 3 from the link above) and reboot the device.6. Visual Studio with TwinCAT 3 can be opened form the Windows system tray icon,and a new TwinCAT 3 project can be created as described here: /english.php?content=../content/1033/tc3_c/54043195639 143947.html&id=17135.3 Connecting to a Hardware ControllerA TwinCAT “target” is the hardware device which will ultimately execute a control system. Typically, the target will be one of the Beckhoff hardware controllers (such as the CX1020, etc.), but the local development PC can also be used as the target for testing purposes.To use a hardware controller:1. Connect one of the Ethernet interfaces on the Beckhoff controller to the developmentPC. This can be a direct connection, or via a switch. Set the PC's network interface IP address to be on the same subnet as the control. For example, if the controller’s IP address is 192.168.2.10 with a subnet mask of 255.255.255.0, then the PC’s IP address could be set to 192.168.2.20. Note that Beckhoff controllers must not be added to a corporate Windows domain, because this conflicts with the functionality.2. Create a new TwinCAT 3 project (or use an existing project) in Visual Studio.3. In the Solution Explorer panel, double-click on the “SYSTEM” for your project andselect “C hoose target...”. Select“S earch (Ethernet)”. Press "Broadcast Search" and select the appropriate network interface (if there is more than one). Select the appropriate controller device in the list. You may need to select the "IP address"radio box before adding the route. Enter the device username and password (by default: "Administrator" and "1"). This process is described in further detail here: /english.php?content=../content/1033/tc3_system/html/tcs ysmgr_addroutedialog.htm&id=.4 TwinCAT 3 – Typical C++ Project ConfigurationThis section describes adding a C++ module to a TwinCAT 3 project.4.1 C++ ModuleIn the Solution Explorer panel, right click on “C++”and “Add New Item...”. Select “TwinCAT Driver Project”, choose a name, and click on “Add”.Figure 1: Initial C++ module creationThere are several C++ module templates to choose from:Figure 2: C++ module templateSelect the option with “Cyclic IO” and click on “Add”. Select a name for the Module Class:Figure 3: Module Class nameThis results in a “Module Project” with a TwinCAT Module Class (TMC) file and some pre-generated C++ code. For example a template CycleUpdate() function has been created within the main module C++ file (SingleCoreModule.cpp in this example):Figure 4: Example of initial generated codeHowever, this Module Class is merely “template”, and you must still create an instance (or “object”) of the Module Class to assign to a processor core on a target controller.Right click on the C++ project, select “Add New Item”, and find your “published” module, as shown below:Figure 5: Creating an instance of a Module ClassThis means that you can create multiple object instances from the same Module Class template.4.2 Assigning Module Instances to Processor CoresIn “SYSTEM”> “Real-Time”, enable the required CPU cores (you may first need to click “Read from Target”) and set the required Base Time:Figure 6: Assigning module instances to processor coresThen create a task for each C++ module instance:Figure 7: Task configurationFor each module instance, select the “Context” tab and assign the module to a task:Figure 8: Task context mappingMore details on task configuration are available here:/english.php?content=../content/1033/tc3_system/html/tcsysmgr_ systemnode_subnodes_tasks.htm&id=15611.4.3 Using a TMC FileThe TMC file, with its associated editor GUI, defines the functionality of a C++ module. For example, Outputs – which are visible to other TwinCAT modules – can be defined:Figure 9: Defining a module output using the TMC editorIn order to be able to monitor variables in real-time, select “Create symbol” in the editor:Figure 10: “Create symbol” optionAfter making changes to a TMC file, save the file, right click on the module, and select “TwinCAT TMC Code Generator”. This updates the generated code. The changes should automatically appear for all module instances:Figure 11: Two Module Class objects, with a common definitionThe definitions can be accessed from C++ code. For example, two output variables can be assigned values as follows:Figure 12: Assignment of output valuesOnce all required definitions have been added to the TMC file and the required C++ code has been added, the module can now be built, although this will also be done automatically when transferring to a target. Select “Activate Configuration” in the toolbar to run the project on the TwinCAT target.Further TMC documentation is available here: https:///english.php?content=../content/1033/tc3_c/27021598485651723.html&id=.4.4 Real-Time MonitoringData in TwinCAT projects can be monitored with high granularity in real-time. Go to “File” > “Add” > “New Project...” > “TwinCAT Measurement”, then select the appropriate scope type:Figure 13: Creating a new TwinCAT Measurement projectOpen the Target Browser by right-clicking on the newly-created “Axis” element:Add the relevant signals to your plot:Press the Record button on the toolbar to capture data:Figure 16: Viewing plots of module dataFor the trial license of TwinCAT 3 (which can be renewed perpetually), up to one hour of data, per signal, can be captured.5 Integrating Simulink ModelsThere are two approaches for integrating an algorithm or control system implemented in Simulink (which may include performance-critical components written directly in C) into the Beckhoff platform, and these approaches are described in the following subsections. Both approaches involve the automatic generation of a C or C++ code implementation of a Simulink model, using Simulink Coder [1], [2].5.1 Manual Compilation of Generated C++ Code within TwinCAT Simulink Coder can be used to create a C or C++ implementation of a Simulink model, which is intended to be optimised for embedded, real-time applications. For integration with TwinCAT 3, Simulink Coder should be configured to generate C++ code for a 32-bit x86 platform (unless a different controller unit architecture is being targeted). Double-precision floating-point numbers can be used.The generated C++ code can be included in a TwinCAT 3 project within a C++ Module Class, using the template for “cyclic” tasks(see Figure 2). The code from Simulink Coder should wrap the Simulink model in a C++ class. An instance of this class should be defined as a protected member in the header file of the Module Class (i.e., not simply declared arbitrarily in the C++ code). The Simulink model should be initialised within the SetObjStatePS() function within the main Module Class C++ file, for example: // State transition from PREOP to SAFEOP//// Initialize input parameters// Allocate memoryHRESULT CPMU_M_class::SetObjStatePS(PTComInitDataHdr pInitData){m_Trace.Log(tlVerbose, FENTERA);HRESULT hr = S_OK;IMPLEMENT_ITCOMOBJECT_EVALUATE_INITDATA(pInitData);// Simulink model initializationPMU_Beckhoff_Test_Obj.initialize();m_Trace.Log(tlVerbose, FLEAVEA "hr=0x%08x", hr);return hr;}The task should be configured to execute on a particular time-base, such as 1 ms or 100 µs, which matches the time-step of the Simulink model. For example, the CX1020 controller unit –a relatively basic controller with a 1 GHz single-core CPU –can execute the phasor measurement (PMU) algorithm documented in [3] in real-time with a 100 µs time-base, requiring 30-40% of the CPU time1. This is illustrated in Figure 17.1 This only includes the CPU time for the PMU algorithm; however, the additional CPU time requirements for communications processing are not significant, and can be allocated to a lower priority task than the time-critical algorithm processing.Figure 17: PMU algorithm CPU execution timeThere are two caveats with this approach:1. In TwinCAT 3, precompiled headers must be manually disabled for the importedSimulink source files. This is a simple change within the C++ compiler settings.2. It is required to use platform-specific library calls for mathematical functions, whichare better suited for real-time operation, rather than those defined in the standard C “math.h” library. A simple method for achieving this is to replace the “#include <math.h>” line in the main C++ header file generated by Simulink Coder with:#include <fpu87.h>#include "math_h_defines.h"where “math_h_defines.h” provides a mapping of the standard C versions to the TwinCAT versions:#define sqr sqr_#define sqrt sqrt_#define sin sin_#define cos cos_#define tan tan_#define atan atan_#define atan2 atan2_#define asin asin_#define acos acos_#define exp exp_#define log log_#define log10 log10_#define fabs fabs_#define fmod fmod_#define ceil ceil_#define floor floor_#define pow pow_#define sincos sincos_#define fmodabs fmodabs_#define round round_#define rounddigits rounddigits_#define coubic coubic_#define ldexp ldexp_#define ldexpf ldexpf_#define sinh sinh_#define cosh cosh_#define tanh tanh_#define finite finite_#define isnan isnan_#define rands rands_Note that this will result in compiler warnings, but allows the module to be built and executed.5.2 Using Simulink-TwinCAT 3 IntegrationThere is a Beckhoff software tool for directly integrating Simulink models with TwinCAT 3. This is achieved simply by selecting the TwinCAT 3 “target” with Simulink Coder (rather than the normal embedded target); once the code is generated, the module is automatically available within TwinCAT 3. A simple example, which illustrates the integration of a Simulink “block” model into the T winCAT 3 user interface, is shown in Figure 18.Figure 18: Simple example of Simulink-TwinCAT 3 integrationThis tool is intended to be more convenient and to allow easier debugging than direct integration of C++ code as described in Section 5.1. Simulink model inputs and outputs are automatically identified in TwinCAT 3, as illustrated in the left of Figure 18. Signals in the Simulink model can be monitored in real-time whilst the project is executed on the target hardware. However, this approach requires a separate software licence and the code generation process typically takes significantly longer than for manual C++ code integration. Furthermore, the process in Section 5.1 allows additional “wrapper” C++ code to be added to the Module Class, without needing to add an additional task. Conversely, the direct Simulink-TwinCAT integration forces all task functionality to be implemented within Simulink.6 References[1] MathWorks, “Simulink Coder - Generate C and C++ code from Simulink and Stateflowmodels,”2012. [Online]. Available: /products/simulink-coder/.[2] A. J. Roscoe, S. M. Blair, and G. M. Burt, “Benchmarking and optimisation of Simulinkcode using Real-Time Workshop and Embedded Coder for inverter and microgrid control applications,”in Universities Power Engineering Conference (UPEC), 2009, pp. 1–5.[3] A. J. Roscoe, “Exploring the Relative Performance of Frequency-Tracking and Fixed-Filter Phasor Measurement Unit Algorithms Under C37.118 Test Procedures, the Effects of Interharmonics, and Initial Attempts at Merging P-Class Response With M-Class Filtering,”IEEE Trans. Instrum. Meas., vol. 62, no. 8, pp. 2140–2153, Aug.2013.20。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档