Hello and I will explain how to create a OPCUA Client in RoboDK and communicate with TwinCAT OPCUA Server. The Robot will start a different program – depending on your command.Let’s create a station with 1 robot and 2 robot programs first.
RoboDK Side
Program1
This is program1.
Program2
This is program2.
Python Script
Click the python icon to create a python script .
Right click the script and press Edit Python script.
Code
We will use this library.
pip install opcua |
https://github.com/FreeOpcUa/python-opcua
from opcua import Client,ua from robodk import robolink # RoboDK API from robodk import robomath # Robot toolbox RDK = robolink.Robolink() def SetNodeValue(Node,value): DataType=Node.get_data_type_as_variant_type() DataValue=ua.DataValue(ua.Variant(value,DataType)) Node.set_data_value(DataValue) item = RDK.Item(‘UR3’) if item.Valid(): print(‘Robot is found..’) Programs=[‘Coke’,’Beer’] ENDPOINT=’opc.tcp://127.0.0.1:4840′ REGSNODE=’ns=4;s=GVL.Reg’ BUSYNODE=’ns=4;s=GVL.bBusy’ ENDNODE=’ns=4;s=GVL.bEnd’ GetProgram=False client=Client(ENDPOINT) client.connect() Regs=client.get_node(REGSNODE) Busy=client.get_node(BUSYNODE) End=client.get_node(ENDNODE) _busy=False SetNodeValue(Busy,False) while True: cmd=Regs.get_value()[0] if cmd is 0: GetProgram=False _busy=False SetNodeValue(Busy,False) SetNodeValue(End,False) _busy=False if cmd >0 : if not GetProgram and not _busy : prog=RDK.Item(Programs[cmd-1]) print(prog.Name()) GetProgram=True if not _busy: value = prog.RunProgram() SetNodeValue(Busy,True) SetNodeValue(End,False) _busy=True if not prog.Busy() : SetNodeValue(Busy,False) SetNodeValue(End,True) GetProgram=False |
TwinCAT Side
Here is the flow between RoboDK and TwinCAT.
The Operator can start a different program from the screen, and then TwinCAT will send a start command to RoboDK.
RoboDK will check the actual value of the command, if the value is greater than 0, python script will get the RoboDK robot program object and start it, otherwise rest all the Flags.
The Busy flag will remain at 1 until the program is completed. FInally, A End Flag will be sent from RoboDK to TwinCAT, TwinCAT will reset the command.
Then RoboDK will reset all the flags.
GVL
OPCUA NodeのGVLです。
{attribute ‘qualified_only’} VAR_GLOBAL {attribute ‘OPC.UA.DA’ := ‘1’} Reg:ARRAY[0..99]OF INT; {attribute ‘OPC.UA.DA’ := ‘1’} bBusy:BOOL; {attribute ‘OPC.UA.DA’ := ‘1’} bEnd:BOOL; END_VAR |
MAIN
These are the nodes of OPCUA Server.
VAR
PROGRAM MAIN VAR bBeer:BOOL; bCoke:BOOL; p1:R_TRIG; END_VAR |
Program
IF bBeer THEN bBeer:=FALSE; GVL.Reg[0]:=1; END_IF IF bCoke THEN bCoke:=FALSE; GVL.Reg[0]:=2; END_IF p1(clK:=GVL.bEnd); IF p1.Q THEN GVL.Reg[0]:=0; END_IF |
Screen
The operator can choose a Beer or Coke program.
Result
Soruce Code
https://github.com/soup01Threes/TwinCAT3/blob/main/Demo_Project_Play%20with%20OPCUA.zip