Connect Matlab - TwinCAT trough ADS.dll , via ethernet
48 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hey fellow programmers
For my Bachelor Thesis as an Mechanical Engineer, i built a machine, which plays "Pong" autonomous. I used a Beckhoff XTS with twinCAT3 to control the PLC. TwinCAT was installed on a industrial PC, which was part of the Beckhoff XTS set. On my personal Laptop, i installed matlab. A webcam (connected to an extern laptop, using matlab) filmed the playcourt and the ball. After image processing, detecting of the ball, and Calculations about the trajectory of the ball, i send the movers to the expected position to catch and throught the ball back to the other side. To make use of the calculations in matlab in twincat, i had to build a connection "Matlab-TwinCAT" using an ethernet cable. i ll explain it step by step, and will put the whole code together at the end.
1) TwinCAT.Ads.dll : you can get this dll from TwinCAT website and save it wherever you want (path required afterwards)
asm = NET.addAssembly('D:\BFH\Thesis\TwinCAT\TC ADS DLL\TwinCAT.Ads.dll');
2) import the dll.
import TwinCAT.Ads.*;
3) open a asm-Client.
adsClt=TwinCAT.Ads.TcAdsClient;
4) Now you need to find the NetID & Port nr. in twinCAT.
- NetID can be found under "SYSTEM/Route -> NetID Management". i.e. 192.168.1.50.1.1
- Port under "PLC/ PLC Thesis/Project". i.e. 851
connect client with these specifications to twincat:
adsClt.Connect('192.168.1.50.1.1',851);
5) Now you can define variable in matlab, which will be connected to a variable in twinCAT. CAUTION: The variable in twinCAT must already exist and its path known. i.e
info_startPos=adsClt.ReadSymbolInfo('MAIN.startPos');
startPos is the variable, which defines the starting position of a mover. This variable is defined in the MAIN script. info_startPost contains now all information about the startPos, and can now be used to send data, specifically the start position of the mover.
6) send data to specific variable:
adsClt.WriteAny(info_startPos.IndexGroup,info_startPos.IndexOffset,200);
Now i can draw the information from the matlab-variable "info_startPos" to send data. dont worry about the IndexGroup or IndexOffset. 200 is now the sent starting position of the mover.
Thats basicly it. Same holds true to all data types. i.e boolean:
adsClt.WriteAny(info_HubMag_01.IndexGroup,info_HubMag_01.IndexOffset,false);
dont worry about the names. HubMag_01 is a magnetic bolt, which extends when its true, and goes back when false.
---------------------------------------------------------------------------
Now to the full code. To give my matlab code more overview and flexibility, i had my Main file "Thesis_06" , in which i called the file "Init_Comm_01". "Init_Comm_01" is initiating the whole communication and defines all necessary variable now, which i want to use later.
%Init Communication with TwinCAT
% and reading necessary Info of variable for later write
asm = NET.addAssembly('D:\BFH\Thesis\TwinCAT\TC ADS DLL\TwinCAT.Ads.dll');
import TwinCAT.Ads.*;
adsClt=TwinCAT.Ads.TcAdsClient;
adsClt.Connect('192.168.1.50.1.1',851);
info_startPos_01=adsClt.ReadSymbolInfo('MAIN.startPos[1]');
info_startPos_02=adsClt.ReadSymbolInfo('MAIN.startPos[2]');
info_startPos_03=adsClt.ReadSymbolInfo('MAIN.startPos[3]');
info_targetPos_01=adsClt.ReadSymbolInfo('MAIN.targetPos[1]');
info_targetPos_02=adsClt.ReadSymbolInfo('MAIN.targetPos[2]');
info_targetPos_03=adsClt.ReadSymbolInfo('MAIN.targetPos[3]');
info_Velo_01=adsClt.ReadSymbolInfo('MAIN.velo[1]');
info_Velo_02=adsClt.ReadSymbolInfo('MAIN.velo[2]');
info_Velo_03=adsClt.ReadSymbolInfo('MAIN.velo[3]');
info_Acc_01=adsClt.ReadSymbolInfo('MAIN.Acc[1]');
info_Acc_02=adsClt.ReadSymbolInfo('MAIN.Acc[2]');
info_Acc_03=adsClt.ReadSymbolInfo('MAIN.Acc[3]');
info_TOI=adsClt.ReadSymbolInfo('MAIN.TOI');
info_HubMag_01=adsClt.ReadSymbolInfo('MAIN.HubMag_01');
info_HubMag_02=adsClt.ReadSymbolInfo('MAIN.HubMag_02');
info_HubMag_03=adsClt.ReadSymbolInfo('MAIN.HubMag_03');
info_getReady=adsClt.ReadSymbolInfo('MAIN.getReady');
info_StartKnopf=adsClt.ReadSymbolInfo('MAIN.StartKnopf');
info_stop=adsClt.ReadSymbolInfo('MAIN.stop');
info_Notfall_AUS=adsClt.ReadSymbolInfo('MAIN.Notfall_AUS');
info_Free=adsClt.ReadSymbolInfo('MAIN.Free');
you can see that there are different types of data. Some variables are referring to a value in an array, some to a variable.
Now i can send data to TwinCAT from the main file "Thesis_06". I dont bore you with the whole, detailed code. but just some parts/exaples to get the idea:
%%Initialization
clear all
close all
clc
Init_Comm_01
adsClt.WriteAny(info_getReady.IndexGroup,info_getReady.IndexOffset,true);
targetPosVal_01 = 1000;
//This value would normaly be the result of a complicated
//calculation from another file and is changing continuously
//sending mover to starting
adsClt.WriteAny(info_targetPos_01.IndexGroup,info_targetPos_01.IndexOffset,targetPosVal_01);
I hope i could help you with this explaination and my knowledge will make your work/thesis easier! If you have any questions regarding the code or configuration of twinCAT, i m glad trying to help. Send an email n ask.
Rotem
PS: Another Option would be to install TwinCAT directly on your PC. I didnt test, if the communication beweet matlab-twinCAT is the same, if they are on the same computer. but i m quiet sure it is. if you happen to test it, i d be glad to hear from you guys and learn from you=)
1 comentario
Daniel Gray
el 28 de Dic. de 2016
Could you please reach out to me further on this, I am attempting to follow your example, but I can only get the Bool variables to change within TwinCat. If possible could you pleae contact me directly at dagray2015@outlook.com or give me some more direction on here as to changing the numerical values.
Thanks
Respuestas (2)
Tomas Rompotl
el 6 de Abr. de 2021
Thank you man, this is pretty awesome!
I would also make searching easier for others. You can easily read value of variable this way:
info = adsClt.ReadSymbolInfo('GVL.temperature');
value = adsClt.ReadSymbol(info)
0 comentarios
Daniel Gray
el 28 de Dic. de 2016
Hello, I have the interface set, but am I able to change variables within TwinCat that are mapped directly? (Can I force an output") or do I need to toggle another variable to toggle the linked variable.
Thanks
0 comentarios
Ver también
Categorías
Más información sobre Install Products en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!