UnderAutomation
Any question?

[email protected]

Contact us
UnderAutomation
⌘Q

Read and write variables

Read the program and installation variables of a UR cobot in C# or Python with the Primary Interface or the Dashboard Server, and how to change them.

  • Which way to choose
  • Read with the Primary Interface
  • Read with the Dashboard Server
  • Write a variable
  • Before the program
  • While the program runs
  • Try it in the demo application
  • Reference
  • What to read next

This article shows how to read the global variables of a Universal Robots cobot from a PC, in C# or Python, and how to change them. A global variable is a program variable or an installation variable of PolyScope. To exchange values with a running program at high speed, use registers instead.

Which way to choose

WayReadsWritesWhen
Primary InterfaceEvery variable, updated when its value changesnoFollow the variables of a running program
Dashboard ServerOne variable, by its name, when you asknoRead one value from time to time (PolyScope 5)
URScriptyes, the program stopsSet an installation variable before a program
Registers (RTDE)0 to 47 numbers and 0 to 127 bitsyes, while the program runsExchange values with a running program

In the SDK, a variable is a GlobalVariable: its Name, its Type and its Value, with typed conversions (ToInt, ToFloat, ToBool, ToPose, ToList, ToMatrix).

Read with the Primary Interface

The Primary Interface sends the list of the variables and their new values when they change. GlobalVariables keeps the last values received:

  • ListUpdated is raised when the list of the variables changes;
  • ValuesUpdated is raised when at least one value changes;
  • GetAll() and GetByName() return the last values received.
// The Primary Interface is enabled by default
robot.Connect("192.168.0.1");
// Raised when the list of variables changes
robot.PrimaryInterface.GlobalVariables.ListUpdated += (sender, e) =>
{
GlobalVariable[] variables = e.Variables;
};
// Raised when at least one value changes
robot.PrimaryInterface.GlobalVariables.ValuesUpdated += (sender, e) =>
{
GlobalVariable[] variables = e.Variables;
};
// Last values received
GlobalVariable[] all = robot.PrimaryInterface.GlobalVariables.GetAll();
GlobalVariable myVar = robot.PrimaryInterface.GlobalVariables.GetByName("myVar");
if (myVar != null)
{
// Typed value, according to myVar.Type
switch (myVar.Type)
{
case GlobalVariableTypes.Bool: bool b = myVar.ToBool(); break;
case GlobalVariableTypes.Int: int i = myVar.ToInt(); break;
case GlobalVariableTypes.Float: float f = myVar.ToFloat(); break;
case GlobalVariableTypes.String: string s = myVar.ToString(); break;
case GlobalVariableTypes.Pose: Pose p = myVar.ToPose(); break;
case GlobalVariableTypes.List: GlobalVariableValue[] l = myVar.ToList(); break;
case GlobalVariableTypes.Matrix: Array m = myVar.ToMatrix(); break;
}
}
}
}
Click to see the full code

The robot sends the variables when they change. If your application connects while a program runs, the list stays empty until a value changes. The decoding depends on the PolyScope version (up to 3.2, up to 5.9, later versions): the SDK selects it, and FirmwareVersion gives it.

Read with the Dashboard Server

GetVariable asks the robot for one variable of the running program. The type is deduced from the value.

// The Dashboard Server is enabled by default
robot.Connect("192.168.0.1");
// Read the variable "myVar" of the running program
var response = robot.Dashboard.GetVariable("myVar");
if (response.Succeed)
{
GlobalVariable variable = response.Value;
Console.WriteLine($"{variable.Name} = {variable.Value} ({variable.Type})");
// Typed conversions, according to variable.Type
if (variable.Type == GlobalVariableTypes.Pose)
{
Pose pose = variable.ToPose();
}
}
}
}
Click to see the full code

Write a variable

No interface of the robot writes a variable while a program runs. Two ways remain:

Before the program

Send URScript that sets the variable. The running program stops, so do it when no program runs, for example to initialize an installation variable. To make PolyScope save the new value of an installation variable, also set _hidden_verificationVariable to 0:

robot.Connect("192.168.0.1");
// Set the installation variable i_var_1 to 6.
// The running program stops: send it when no program runs.
// _hidden_verificationVariable = 0 makes PolyScope save the new value
robot.PrimaryInterface.Script.Send(
"def SetInstallationVariable():\n" +
" global _hidden_verificationVariable = 0\n" +
" global i_var_1 = 6\n" +
"end\n");
}
}
Click to see the full code

A secondary program (sec) does not stop the program, but it cannot write a global variable.

While the program runs

Use the registers: the program reads them with read_input_integer_register, read_input_float_register or read_input_boolean_register, and your application writes them with RTDE up to 500 Hz. See Read and write registers.

Try it in the demo application

Everything on this page can be tried without writing code, in the Running program and variables page of the demo application.

Running program and variables page of the Universal Robots SDK demo application

The demo application is open source. The C# source of this page is ProgramControl.cs.

Reference

Class
GlobalVariableinherits GlobalVariableValue
C#Python

Describes a global variable

MemberTypeDescription
GlobalVariable()
Constructor
Name
Property
read only
string
Variable name
Time
Property
read only
TimeSpan
Last time the variable was sampled
Equals(object)
Method
bool
Determines whether the specified object is a GlobalVariable with the same name, type, and value.
GetHashCode()
Method
int
Returns a hash code based on the variable name, type, and value.
ToString()
Method
string
Returns the variable name followed by its string representation.
Class
GlobalVariableValue
C#Python

Describes a typed variable value

MemberTypeDescription
GlobalVariableValue()
Constructor
Type
Property
read only
GlobalVariableTypes
Type of a variable
Value
Property
read only
object
Value of the variable
Equals(object)
Method
bool
Determines whether the specified object is a GlobalVariableValue with the same type and value.
  • obj : The object to compare with.
GetHashCode()
Method
int
Returns a hash code based on the variable type and value.
Parse(string)
Method
static
GlobalVariableValue
Estimate variable value from its string representation
ToBool()
Method
bool
Returns variable value if type is Bool. Il type is Float or Int, it returns True if value is not 0. Else, it returns false
ToFloat()
Method
float
Returns variable value if type is Float. Il type is int, it casts it to float. If Type is bool, it returns 1 or 0. Else it returns NaN
ToInt()
Method
int
Returns variable value if type is Int. Il type is Float, it tries to cast it to int. If Type is bool, it returns 1 or 0. Else it returns 0
ToList()
Method
GlobalVariableValue[]
Returns an array of GlobalVariableValue if Type is List. Else, null is returned
ToMatrix()
Method
Array
Return variable value GlobalVariable[,] if variable is a matrix. First dimension is row index and second dimension is column index. Use GetLength(0) to get row number and GetLength(1) to get column count
ToPose()
Method
Pose
Returns a Pose if Type is Pose. Else, null is returned
ToString()
Method
string
Returns a string that describes the variable, regardless of its type
Enum
GlobalVariableTypes
C#Python

Possible types of a variable

NameValueDescription
Bool
4
Variable value is bool
Float
6
Variable value is float
Int
5
Variable value is int
List
2
Variable value is an array : GlobalVariableValue[]
Matrix
7
Variable value is a matrix
None
0
Variable value is null, the value has not been assigned yet
Pose
3
Variable value is a UnderAutomation.UniversalRobots.Pose
String
1
Variable value is a System.String
Class
GlobalVariables
C#Python

List of all global variables

MemberTypeDescription
FirmwareVersion
Property
read only
GlobalVariablesFirmwareVersion
Indicates which decoder is used used to read variables according to firmware version
ListUpdated
Event
EventHandler<GlobalVariablesEventArgs>
Event raised whan the variable list changed. For example, after a program starts
ValuesUpdated
Event
EventHandler<GlobalVariablesEventArgs>
Event raised at 10Hz when variable values are updated
GetAll()
Method
GlobalVariable[]
Returns a list of all variables declared in the robot
GetByName(string)
Method
GlobalVariable
Get a variable by its name. Null is returned if the variable doesn't exist
  • name : Variable name, not case sensitive
ToString()
Method
string
Return a string where each line contains type, name and variable value

What to read next

  • Read and write registers: exchange values with a running program.
  • Send URScript: the forms of URScript and their effect on the program.

Easily integrate Universal Robots, Fanuc, Yaskawa, ABB or Staubli robots into your .NET, Python, LabVIEW or Matlab applications

UnderAutomation
Contact usLegal

© All rights reserved.