Visit Zacobria Webshop

UR Script program by Socket connection

Host computer to UR robot #1.

The previous chapter used the “Script:” function available in the teaching pendant to import blocks of Script code or entire program into the robot and run from the teaching pendant.

In this chapter we will focus on making a script that entirely control the robot remotely from a host computer by Script programming.

The format is the same as in previous chapter and as documented in the Script manual, but since we are not using the teaching pendant we need to send the Script file to the Robot control through an already prepared open port on the Robot which is TCP port 30002.

In this case we are using the program language Python and the Python environment to communicate with the Robot. The Script file can still be edited in a Notepad.

The first program example is just to illustrate how to send a script file through via the Ethernet to the TCP port 300002 on the robot.

In this case the robot has already been configured to IP address 192.168.0.9 and our host must be in the same subnet e.g. defined as 255.255.255.0

The first Program 1 just turns digital output number 2 ON and the robot does not move at all and the program looks like this.

# Echo client program import socket HOST = “192.168.0.9”    # The remote host PORT = 30002              # The same port as used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) s.send (“set_digital_out(2,True)” + “\n”) data = s.recv(1024) s.close() print (“Received”, repr(data))

The program is run from the host computer and will only have one passage because there is no loop in the program.

The library “socket” in imported into the program so the Socket connection is available. The “Host” in this case is the Robot and “PORT” is the open port on the Robot that listens for Script code.

Then the Script code necessary to connect and send on the Socket. The “s.send” command send that actual script code for the robot to execute which in the case is (“set_digital_out(2,True)” Which is like previous chapter. The + “\n”) at the end of the line is a linefeed because the Robot need a linefeed after each command. The Socket connection needs to be closed again with the s.close() command. The print (“Received”, repr(data)) command prints the output from the robot on the Host or PC monitor – in this case  acknowledgement code.

The second Program 2 is similar and just turn digital out number 2 OFF again and the robot does not move at all.

# Echo client program import socket

HOST = “192.168.0.9”    # The remote host PORT = 30002              # The same port as used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) s.send (“set_digital_out(2,False)” + “\n”) data = s.recv(1024) s.close() print (“Received”, repr(data))

Script program for Socket connection

Host computer to UR robot #2.

This program send in Script mode via socket port runs through 7 Waypoints. See next page for comments.

# Echo client program import socket import time HOST = “192.168.0.9”    # The remote host PORT = 30002              # The same port as used by the server print “Starting Program” count = 0 while (count < 1): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) time.sleep(0.05) s.send (“set_digital_out(2,True)” + “\n”) time.sleep(0.1) print “0.2 seconds are up already” s.send (“set_digital_out(7,True)” + “\n”) time.sleep(2) s.send (“movej([-0.5405182705025187, -2.350330184112267, -1.316631037266588, -2.2775736604458237, 3.3528323423665642, -1.2291967454894914], a=1.3962634015954636, v=1.0471975511965976)” + “\n”) time.sleep(2) s.send (“movej([-0.7203210737806529, -1.82796919039503, -1.8248107684866093, -1.3401161163439792, 3.214294414832996, -0.2722986670990757], a=1.3962634015954636, v=1.0471975511965976)” + “\n”) time.sleep(2) s.send (“movej([-0.5405182705025187, -2.350330184112267, -1.316631037266588, -2.2775736604458237, 3.3528323423665642, -1.2291967454894914], a=1.3962634015954636, v=1.0471975511965976)” + “\n”) time.sleep(2) s.send (“movej([-0.720213311630304, -1.8280069071476523, -1.8247689994680725, -1.3396385689499288, 3.215063610324356, -0.27251695975573664], a=1.3962634015954636, v=1.0471975511965976)” + “\n”) time.sleep(2) s.send (“movej([-0.540537125683036, -2.2018732555807086, -1.0986348160112505, -2.6437150406384227, 3.352864759694935, -1.2294883935868013], a=1.3962634015954636, v=1.0471975511965976)” + “\n”) time.sleep(2) s.send (“movej([-0.5405182705025187, -2.350330184112267, -1.316631037266588, -2.2775736604458237, 3.3528323423665642, -1.2291967454894914], a=1.3962634015954636, v=1.0471975511965976)” + “\n”) time.sleep(2) s.send (“movej([-0.7203210737806529, -1.570750000000000, -1.570750000000000, -1.3401161163439792, 3.2142944148329960, -0.2722986670990757], t=4.0000000000000000, r=0.0000000000000000)” + “\n”) time.sleep(4) s.send (“set_digital_out(2,False)” + “\n”) time.sleep(0.05) print “0.2 seconds are up already” s.send (“set_digital_out(7,False)” + “\n”) time.sleep(1) count = count + 1 print “The count is:”, count time.sleep(1) data = s.recv(1024) s.close() print (“Received”, repr(data)) print “Program finish”

Same program as above i.e. send in Script mode via socket port runs through 7 Waypoints. Here with comments.

# Echo client program import socket
(Import the library for Socket connections)
import time
(Import the library for Time function)
HOST = “192.168.0.9”    # The remote host
(IP address of the robot)
PORT = 30002              # The same port as used by the server
(The port socket open on the robot for script programming)
print “Starting Program”
(This is printed on the PC Computer from where the script program is run).
count = 0
(set the variable “count” to value 0)
while (count < 1):
(Loop unitl count is smaller than 1, but not 0)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
(All line that is indented under the While statement is executed in the loop).
s.connect((HOST, PORT))
(Connect to the robot port)
time.sleep(0.05)
(Wait 0.05 seconds because the robot communication speed is 125 Hz = 0.008 seconds)
s.send (“set_digital_out(2,True)” + “\n”)
(Set digital output 2 high )True))
time.sleep(0.1)
(Wait for communication speed)
print “0.2 seconds are up already”                                                             (Print messages on PC Computer screen)
s.send (“set_digital_out(7,True)” + “\n”) time.sleep(2) s.send (“movej([-0.5405182705025187, -2.350330184112267, -1.316631037266588, -2.2775736604458237, 3.3528323423665642, -1.2291967454894914], a=1.3962634015954636, v=1.0471975511965976)” + “\n”) a = joint accleration      v = joint speed time.sleep(2)
(Wait for robot to move to Waypoint)
s.send (“movej([-0.7203210737806529, -1.82796919039503, -1.8248107684866093, -1.3401161163439792, 3.214294414832996, -0.2722986670990757], a=1.3962634015954636, v=1.0471975511965976)” + “\n”) time.sleep(2) s.send (“movej([-0.5405182705025187, -2.350330184112267, -1.316631037266588, -2.2775736604458237, 3.3528323423665642, -1.2291967454894914], a=1.3962634015954636, v=1.0471975511965976)” + “\n”) time.sleep(2) s.send (“movej([-0.720213311630304, -1.8280069071476523, -1.8247689994680725, -1.3396385689499288, 3.215063610324356, -0.27251695975573664], a=1.3962634015954636, v=1.0471975511965976)” + “\n”) time.sleep(2) s.send (“movej([-0.540537125683036, -2.2018732555807086, -1.0986348160112505, -2.6437150406384227, 3.352864759694935, -1.2294883935868013], a=1.3962634015954636, v=1.0471975511965976)” + “\n”) time.sleep(2) s.send (“movej([-0.5405182705025187, -2.350330184112267, -1.316631037266588, -2.2775736604458237, 3.3528323423665642, -1.2291967454894914], a=1.3962634015954636, v=1.0471975511965976)” + “\n”) time.sleep(2) s.send (“movej([-0.7203210737806529, -1.570750000000000, -1.570750000000000, -1.3401161163439792, 3.2142944148329960, -0.2722986670990757], t=4.0000000000000000, r=0.0000000000000000)” + “\n”)
(Second joint at -90 deg                                                                                                                                      t = (time to reach this     r = (Blend radius is set to 0)    .

waypoint is 4 sec)                                                            .

(In robot Move Screen the jopint positions expressed in degrees i.e. -360 to 360 degrees. Here the joint positions are expressed as radians where -360 = -6.283, -180= -3.14,

-190 = -1.57075, 0 = 0, 90 = 1.57075, 180 = 3.1415, 360 = 6.283)
time.sleep(4)
(Program waits 4 second because it takes 4 seonds for the robot to move to the waypoint).

s.send (“set_digital_out(2,False)” + “\n”) time.sleep(0.05) print “0.2 seconds are up already” s.send (“set_digital_out(7,False)” + “\n”) time.sleep(1) count = count + 1 print “The count is:”, count time.sleep(1) data = s.recv(1024) s.close() print (“Received”, repr(data)) print “Program finish”


 

Contribute moderator



Disclaimer: While the Zacobria Pte. Ltd. believes that information and guidance provided is correct, parties must rely upon their skill and judgement when making use of them. Zacobria Pte. Ltd. assumes no liability for loss or damage caused by error or omission, whether such an error or omission is the result of negligence or any other cause. Where reference is made to legislation it is not to be considered as legal advice. Any and all such liability is disclaimed.

If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.

Author:
By Zacobria Lars Skovsgaard
Accredited 2015-2018 Universal Robots support Centre and Forum.


Tweet

Follow @zacobria