The URScript Programming Language
Version 3.0
June 26, 2014
CONTENTS
CONTENTS
The information contained herein is the property of Universal Robots A/S and shall
not be reproduced in whole or in part without prior written approval of Universal
Robots A/S. The information herein is subject to change without notice and should
not be construed as a commitment by Universal Robots A/S. This manual is period-
ically reviewed and revised.
Universal Robots A/S assumes no responsibility for any errors or omissions in this doc-
ument.
Copyright c 2009-2014 by Universal Robots A/S
The Universal Robots logo is a registered trademark of Universal Robots A/S.
Contents
Contents
1 The URScript Programming Language
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Connecting to URControl
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3 Numbers, Variables and Types . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.4 Flow of Control
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.5 Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.6 Scoping rules
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.7 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.7.1 Threads and scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.7.2 Thread scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.8 Program Label Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
3
3
3
3
4
4
5
6
7
8
8
2 Module motion
8
2.1 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
2.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3 Module internals
18
3.1 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4 Module urmath
24
4.1 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5 Module interfaces
32
5.1 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2
URScript
The URScript Programming Language
1 The URScript Programming Language
1.1 Introduction
The Universal Robot can be controlled a three different levels: The Graphical User-
Interface Level, the Script Level and the C-API Level. URScript is the robot programming
languange used to control the robot at the Script Level. Like any other programming
language URScript has variables, types, flow of control statements, function etc.
In
addition URScript has a number of built-in variables and functions which monitors and
controls the I/O and the movements of the robot.
1.2 Connecting to URControl
URControl is the low-level robot controller running on the Mini-ITX PC in the controller
cabinet. When the PC boots up URControl starts up as a daemon (like a service) and
PolyScope User Interface connects as a client using a local TCP/IP connection.
Programming a robot at the Script Level is done by writing a client application (running
at another PC) and connecting to URControl using a TCP/IP socket.
• hostname: ur-xx (or the ip-adresse found in the about dialog-box in PolyScope if
the robot is not in dns.)
• port: 30002
When connected URScript programs or commands are sent i clear text on the socket.
Each line is terminated by “\n”.
1.3 Numbers, Variables and Types
The syntax of arithmetic expressions in URScript is very standard:
1+2-3
4*5/6
(1+2)*3/(4-5)
In boolean expressions the boolean operators are spelled out:
True or False and (1 == 2)
1 > 2 or 3 != 4 xor 5 < -6
not 42 >= 87 and 87 <= 42
Variable assignment is done using the equal sign “=”:
foo = 42
bar = False or True and not False
baz = 87-13/3.1415
hello = \q{Hello, World!}
3
URScript
Flow of Control
The URScript Programming Language
l = [1,2,4]
target = p[0.4,0.4,0.0,0.0,3.14159,0.0]
The fundamental type of a variable is deduced from the first assignment of the vari-
able.
In the example above foo is an int and bar is a bool. target is a pose, a
combination of a position and orientation.
The fundamental types are:
• none
• bool
• number - either int or float
• pose
• string
A pose is given as p[x,y,z,ax,ay,az], where x,y,z is the position of the TCP, and
ax,ay,az is the orientation of the TCP, given in axis-angle notation.
1.4 Flow of Control
The flow of control of a program is changed by if-statements:
if a > 3:
a = a + 1
elif b < 7:
b = b * a
else:
a = a + b
end
and while-loops:
l = [1,2,3,4,5]
i = 0
while i < 5:
l[i] = l[i]*2
end
To stop a loop prematurely the break statement can be used. Similarly the continue
statement can be used to pass control to the next iteration of the nearest enclosing
loop.
1.5 Function
A function is declared as follows:
4
URScript
Scoping rules
The URScript Programming Language
def add(a, b):
return a+b
end
The function can then be called like this:
result = add(1, 4)
It is also possible to give function arguments default values:
def add(a=0,b=0):
return a+b
end
URScript also supports named parameters. These will not be described here, as the
implementation is still somewhat broken.
1.6 Scoping rules
A urscript program is declared as a function without parameters:
def myProg():
end
Every variable declared inside a program exits at a global scope, except when they
are declared inside a function. I that case the variable are local to that function. Two
qualifiers are available to modify this behaviour. The local qualifier tells the runtime to
treat a variable inside a function, as being truly local, even if a global variable with the
same name exists. The global qualifier forces a variable declared inside a function,
to be globally accessible.
In the following example, a is a global variable, so the variable inside the function is
the same variable declared in the program:
def myProg():
a = 0
def myFun():
a = 1
return a
end
r = myFun()
end
In this next example, a is declared local inside the function, so the two variables are
different, even though they have the same name:
def myProg():
5
URScript
Threads
The URScript Programming Language
a = 0
def myFun():
local a = 1
return a
end
r = myFun()
end
Beware that the global variable is no longer accessible from within the function, as the
local variable masks the global variable of the same name.
1.7 Threads
Threads are supported by a number of special commands.
To declare a new thread a syntax similar to the declaration of functions are used:
thread myThread():
# Do some stuff
return
end
A couple of things should be noted. First of all, a thread cannot take any parameters,
and so the parentheses in the declaration must be empty. Second, although a return
statement is allowed in the thread, the value returned is discarded, and cannot be
accessed from outside the thread. A thread can contain other threads, the same
way a function can contain other functions. Threads can in other words be nested,
allowing for a thread hierarchy to be formed.
To run a thread use the following syntax:
thread myThread():
# Do some stuff
return
end
thrd = run myThread()
The value returned by the run command is a handle to the running thread. This handle
can be used to interact with a running thread. The run command spawns off the new
thread, and then goes off to execute the instruction following the run instruction.
To wait for a running thread to finish, use the join command:
6
URScript
Threads
The URScript Programming Language
thread myThread():
# Do some stuff
return
end
thrd = run myThread()
join thrd
This halts the calling threads execution, until the thread is finished
executing. If the thread is already finished, the statement has no effect.
To kill a running thread, use the kill command:
thread myThread():
# Do some stuff
return
end
thrd = run myThread()
kill thrd
After the call to kill, the thread is stopped, and the thread handle is no longer valid. If
the thread has children, these are killed as well.
To protect against race conditions and other thread related issues, support for critical
sections are provided. A critical section ensures that the code it encloses is allow to
finish, before another thread is allowed to run. It is therefore important that the critical
section is kept as short as possible. The syntax is as follows:
thread myThread():
enter_critical
# Do some stuff
exit_critical
return
end
1.7.1 Threads and scope
The scoping rules for threads are exactly the same, as those used for functions. See 1.6
for a discussion of these rules.
7
URScript
Program Label Messages
Module motion
1.7.2 Thread scheduling
Because the primary purpose of the urscript scripting language is to control the robot,
the scheduling policy is largely based upon the realtime demands of this task.
The robot must be controlled a frequency of 125 Hz, or in other words, it must be told
what to do every 0.008 second (each 0.008 second period is called a frame). To
achieve this, each thread is given a “physical” (or robot) time slice of 0.008 seconds
to use, and all threads in a runnable state is then scheduled in a round robin1 fashion.
Each time a thread is scheduled, it can use a piece of its time slice (by executing
instructions that control the robot), or it can execute instructions that do not control
the robot, and therefore do not use any “physical” time. If a thread uses up its entire
time slice, it is placed in a non-runnable state, and is not allowed to run until the next
frame starts.
If a thread does not use its time slice within a frame, it is expected to
switch to a non-runnable state before the end of the frame2. The reason for this state
switching can be a join instruction or simply because the thread terminates.
It should be noted that even though the sleep instruction does not control the robot,
it still uses “physical” time. The same is true for the sync instruction.
1.8 Program Label Messages
A special feature is added to the script code, to make it simple to keep track of which
lines are executed by the runtime machine. An example Program Label Message in
the script code looks as follows;
sleep(0.5)
$ 3 \q{AfterSleep}
set_standard_digital_out(7, True)
After the the Runtime Machine executes the sleep command, it will send a message
of type PROGRAM LABEL to the latest connected primary client. The message will hold
the number 3 and the text AfterSleep. This way the connected client can keep track
of which lines of codes are being executed by the Runtime Machine.
2 Module motion
This module contains functions and variables built into the URScript programming lan-
guage.
URScript programs are executed in real-time in the URControl RuntimeMachine (RTMa-
chine). The RuntimeMachine communicates with the robot with a frequency of 125hz.
1Before the start of each frame the threads are sorted, such that the thread with the largest remaining
time slice is to be scheduled first.
2If this expectation is not met, the program is stopped.
8
URScript