UnderAutomation
Eine Frage?

[email protected]

Kontakt
UnderAutomation
⌘Q
Diese Seite ist nur auf Englisch verfügbar.

REST API (PolyScope X)

Control a UR cobot with PolyScope X over HTTP: power, brakes, load, play and stop programs, read the program state. Replaces the Dashboard Server.

  • Prerequisites
  • Example
  • Connection parameters
  • Commands
  • State of the robot
  • Programs
  • Information on the robot
  • Popups
  • Program files
  • List the programs
  • Upload and download
  • Responses and errors
  • From the Dashboard Server to the REST API
  • Try it in the demo application
  • Reference
  • What to read next

The REST API of PolyScope X controls a Universal Robots cobot over HTTP: power, brakes, load and play programs, read the state of the robot, show popups, list, upload and download the program files. This page shows how to use it with the SDK. It replaces the Dashboard Server, which PolyScope X does not have.

For CB-Series and e-Series robots with PolyScope 5, use the Dashboard Server.

Prerequisites

  • The robot runs PolyScope X, or URSim PolyScope X: see Develop without a robot.
  • The robot is in remote control. Switch it with the icon at the top right of PolyScope X. The default password is operator.

Remote control on PolyScope X

Example

The REST API is disabled by default: set Rest.Enable in ConnectParameters.

static void Main(string[] args)
{
var robot = new UR();
var parameters = new ConnectParameters("192.168.0.1");
// The REST API is disabled by default
parameters.Rest.Enable = true;
// The Dashboard Server does not exist on PolyScope X
parameters.Dashboard.Enable = false;
robot.Connect(parameters);
robot.Rest.PowerOn();
robot.Rest.BrakeRelease();
robot.Rest.LoadProgram("my_program");
robot.Rest.Play();
// Playing, Paused, Stopped or Unknown
var state = robot.Rest.GetProgramState();
Console.WriteLine(state.Value.State);
robot.Disconnect();
}
Click to see the full code

The same client works without UR:

static void Main(string[] args)
{
// A REST client, without a UR instance
var client = new RestClient();
// Address of the robot. Optional: port, API version, timeout
client.Enable("192.168.0.1");
client.PowerOn();
client.LoadProgram("my_program");
client.Play();
client.Disable();
}
}
Click to see the full code

Connection parameters

PropertyDefaultRole
EnablefalseEnable the REST client
Port80HTTP port of the robot
VersionLatestVersion of the API (V1, Latest)
TimeoutMs5000Timeout of a request, in ms
{
var robot = new UR();
var parameters = new ConnectParameters("192.168.0.1");
parameters.Rest.Enable = true;
parameters.Rest.Port = 80; // HTTP port, 80 by default
parameters.Rest.Version = RestApiVersion.V1; // Latest by default
parameters.Rest.TimeoutMs = 10000; // 5000 ms by default
robot.Connect(parameters);
// The client is ready
if (robot.Rest.Initialized)
Console.WriteLine(robot.Rest.IP + ":" + robot.Rest.Port);
}
}
Click to see the full code

Commands

State of the robot

PowerOn, BrakeRelease, PowerOff, UnlockProtectiveStop and RestartSafety change the state of the robot. ChangeRobotState does the same with a RobotStateAction.

var robot = new UR();
robot.Connect(new ConnectParameters("192.168.0.1") { Rest = { Enable = true } });
// Power on, then release the brakes
robot.Rest.PowerOn();
robot.Rest.BrakeRelease();
// After a protective stop or a safety fault
robot.Rest.UnlockProtectiveStop();
robot.Rest.RestartSafety();
robot.Rest.PowerOff();
// The same actions, with one generic method
robot.Rest.ChangeRobotState(RobotStateAction.POWER_ON);
robot.Rest.ChangeRobotState(RobotStateAction.BRAKE_RELEASE);
}
}
Click to see the full code

Programs

LoadProgram loads a program by its name, with or without the .urp extension. Play, Pause, Resume and Stop control it, and GetProgramState returns its state: Playing, Paused, Stopped or Unknown. ChangeProgramState does the same with a ProgramStateAction.

var robot = new UR();
robot.Connect(new ConnectParameters("192.168.0.1") { Rest = { Enable = true } });
// The name of the program, with or without .urp
robot.Rest.LoadProgram("my_program");
// Name of the loaded program
RestApiResponse<string> loaded = robot.Rest.GetLoadedProgram();
robot.Rest.Play();
robot.Rest.Pause();
robot.Rest.Resume();
robot.Rest.Stop();
// The same actions, with one generic method
robot.Rest.ChangeProgramState(ProgramStateAction.play);
RestApiResponse<ProgramStateResponse> response = robot.Rest.GetProgramState();
if (response.Succeed)
{
RestProgramState state = response.Value.State; // Playing, Paused, Stopped, Unknown
}
}
}
Click to see the full code

Information on the robot

Read the robot mode, the safety mode, the operational mode, the remote control state, the version of PolyScope X and the version of the robot API. A robot mode or a safety mode that the SDK does not know gives Unknown. For an operational mode that the SDK does not know, Succeed is false.

param.Rest.Enable = true;
robot.Connect(param);
// Unknown if the robot answers a mode that the SDK does not know
RestApiResponse<RobotModes> robotMode = robot.Rest.GetRobotMode();
RestApiResponse<SafetyModes> safetyMode = robot.Rest.GetSafetyMode();
RestApiResponse<OperationalModes> operationalMode = robot.Rest.GetOperationalMode();
RestApiResponse<bool> remote = robot.Rest.IsInRemoteControl();
RestApiResponse<PolyscopeVersion> polyscope = robot.Rest.GetPolyscopeVersion();
RestApiResponse<Version> api = robot.Rest.GetRobotApiVersion();
Console.WriteLine($"Robot mode: {robotMode.Value}, safety mode: {safetyMode.Value}");
Console.WriteLine($"Operational mode: {operationalMode.Value}, remote control: {remote.Value}");
Console.WriteLine($"PolyScope X {polyscope.Value?.Description}, robot API {api.Value}");
robot.Disconnect();
}
Click to see the full code
Enum
SafetyModes
C#Python

Safety modes of a UR robot.

NameValueDescription
AutomaticModeSafeguardStop
12
Safeguard stop in automatic mode.
Fault
9
Safety fault.
ImmiEmergencyStop
15
Emergency stop from the IMMI interface.
ImmiSafeguardStop
16
Safeguard stop from the IMMI interface.
Normal
1
Normal operation.
ProfisafeAutomaticModeSafeguardStop
18
PROFIsafe safeguard stop in automatic mode.
ProfisafeEmergencyStop
20
PROFIsafe emergency stop.
ProfisafeSafeguardStop
19
PROFIsafe safeguard stop.
ProfisafeWaitingForParameters
17
PROFIsafe is waiting for its parameters.
ProtectiveStop
3
Protective stop.
Recovery
4
Recovery mode after a safety limit violation.
Reduced
2
Reduced mode: speed and force limits are lower.
RobotEmergencyStop
7
Robot emergency stop.
SafeguardStop
5
Safeguard stop.
SafetyApiSafeguardStop
22
Safeguard stop requested through the safety API.
SystemEmergencyStop
6
System emergency stop.
SystemThreePositionEnablingStop
13
System three-position enabling device stop.
TpThreePositionEnablingStop
14
Teach pendant three-position enabling device stop.
UndefinedSafetyMode
11
Undefined safety mode.
Unknown
-128
The safety mode is not known. Returned for a value this SDK does not recognize.
ValidateJointId
10
The safety system validates the joint identification.
Violation
8
Safety violation.

Popups

ShowPopup shows a popup with an empty title, ShowPopupWithTitle with a title. ClosePopup closes the popup, CloseSafetyPopup closes a safety popup.

param.Rest.Enable = true;
robot.Connect(param);
// Popup with an empty title
robot.Rest.ShowPopup("Load the parts");
// Popup with a title
robot.Rest.ShowPopupWithTitle("Cell 3", "Load the parts");
robot.Rest.ClosePopup();
robot.Rest.CloseSafetyPopup();
robot.Disconnect();
}
Click to see the full code

Program files

List the programs

GetPrograms returns a RestProgramInfo for each program of the robot: name, description, state, and the dates of creation, last modification and last save, in milliseconds since 1970-01-01 UTC.

Class
RestProgramInfo
C#Python

Description of a program stored on a PolyScope X robot, returned by GetPrograms.

MemberTypeDescription
RestProgramInfo()
Constructor
CreatedDate
Property
long
Creation date, in milliseconds since 1970-01-01 UTC. 0 if the robot gives none.
Description
Property
string
Description of the program. Empty if the robot gives none.
LastModifiedDate
Property
long
Date of the last modification, in milliseconds since 1970-01-01 UTC. 0 if the robot gives none.
LastSavedDate
Property
long
Date of the last save, in milliseconds since 1970-01-01 UTC. 0 if the robot gives none.
Name
Property
string
Name of the program.
ProgramState
Property
string
State of the program as text. Empty if the robot gives none.
Equals(object)
Method
bool
GetHashCode()
Method
int
ToString()
Method
string

Upload and download

  • UploadProgramFile and UploadProgramContent add a new program. The robot refuses a program that already exists.
  • UpdateProgramFile and UpdateProgramContent replace an existing program.
  • DownloadProgram returns the text of a program, DownloadProgramToFile writes it to a file of your computer. An existing file is replaced.
param.Rest.Enable = true;
robot.Connect(param);
// List the programs of the robot
RestApiResponse<RestProgramInfo[]> programs = robot.Rest.GetPrograms();
foreach (RestProgramInfo program in programs.Value)
{
// Dates are in milliseconds since 1970-01-01 UTC
var modified = DateTimeOffset.FromUnixTimeMilliseconds(program.LastModifiedDate);
Console.WriteLine($"{program.Name}, modified on {modified:yyyy-MM-dd}");
}
// Add a new program. The robot refuses a program that already exists.
RestApiResponse<string> uploaded = robot.Rest.UploadProgramFile(@"C:\programs\pick_and_place.urpx");
Console.WriteLine($"Name on the robot: {uploaded.Value}");
// Replace an existing program
robot.Rest.UpdateProgramFile(@"C:\programs\pick_and_place.urpx");
// Same from a text content
string content = File.ReadAllText(@"C:\programs\pick_and_place.urpx");
robot.Rest.UpdateProgramContent("pick_and_place.urpx", content);
// Read a program as text, or save it in a file
RestApiResponse<string> downloaded = robot.Rest.DownloadProgram("pick_and_place");
robot.Rest.DownloadProgramToFile("pick_and_place", @"C:\backup\pick_and_place.urpx");
robot.Disconnect();
}
Click to see the full code

Responses and errors

Every method returns a RestApiResponse. GetProgramState returns a RestApiResponse<ProgramStateResponse>, with the state in Value.

PropertyRole
Succeedtrue when the robot answered HTTP 2xx
StatusCodeHTTP status of the answer
MessageMessage of success, or description of the error
RawResponseBody of the answer, in JSON
ValueTyped value (RestApiResponse<T> only)

The errors of the SDK, a timeout for example, raise InternalErrorOccured.

var robot = new UR();
robot.Connect(new ConnectParameters("192.168.0.1") { Rest = { Enable = true } });
RestApiResponse response = robot.Rest.PowerOn();
if (!response.Succeed)
{
// HTTP status, message and body of the answer of the robot
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Message);
Console.WriteLine(response.RawResponse);
}
// Errors of the SDK, for example a timeout
robot.Rest.InternalErrorOccured += (sender, e) =>
{
Console.WriteLine(e.Message);
Console.WriteLine(e.Exception);
};
}
}
Click to see the full code

From the Dashboard Server to the REST API

An application that supports both PolyScope 5 and PolyScope X enables the client of the robot:

{
var robot = new UR();
// PolyScope 5 and CB-Series: Dashboard Server
robot.Connect(new ConnectParameters("192.168.0.1") { Dashboard = { Enable = true } });
robot.Dashboard.PowerOn();
robot.Dashboard.LoadProgram("/programs/my_program.urp");
robot.Dashboard.Play();
// PolyScope X: REST API
robot.Connect(new ConnectParameters("192.168.0.1") { Rest = { Enable = true } });
robot.Rest.PowerOn();
robot.Rest.LoadProgram("my_program");
robot.Rest.Play();
}
}
Click to see the full code
  • The REST API uses HTTP (port 80), the Dashboard Server TCP (port 29999).
  • LoadProgram takes the name of the program, without its folder.
  • ReleaseBrake of the Dashboard Server is BrakeRelease in the REST API.

The interface IRobotDashboard hides these differences: one code for PolyScope 5 and PolyScope X. See the section "Same code for PolyScope and PolyScope X" of the Dashboard Server page.

Try it in the demo application

Everything on this page can be tried without writing code, in the REST (PolyscopeX) page of the demo application.

REST (PolyscopeX) page of the Universal Robots SDK demo application

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

Reference

Class
RestClientBaseinherits URServiceBase
C#Python

Base implementation of the REST API client for PolyscopeX robots

MemberTypeDescription
RestClientBase()
Constructor
IP
Property
read only
string
IP address of the robot
Initialized
Property
read only
bool
Indicates whether the REST client has been initialized
Port
Property
read only
int
HTTP port for REST API
TimeoutMs
Property
read only
int
Request timeout in milliseconds
Version
Property
read only
RestApiVersion
REST API version being used
BrakeRelease()
Method
RestApiResponse
Release the robot brakes.
ChangeProgramState(ProgramStateAction)
Method
RestApiResponse
Change the program state.
  • action : The program action to perform
ChangeRobotState(RobotStateAction)
Method
RestApiResponse
Change the robot's operational state.
  • action : The state action to perform
ClosePopup()
Method
RestApiResponse
Close the popup of the screen of the robot.
CloseSafetyPopup()
Method
RestApiResponse
Close a safety popup of the screen of the robot.
Disable()
Method
void
Disable the REST client
DownloadProgram(string)
Method
RestApiResponse<string>
Download a program from the robot.
  • programName : Name of the program
DownloadProgramToFile(string, string)
Method
RestApiResponse
Download a program from the robot and save it in a file of this computer.
  • programName : Name of the program
  • savePath : Path of the file to write. An existing file is replaced.
EnableInternal(string, int, RestApiVersion, int)
Method
void
Enable the REST client
GetLoadedProgram()
Method
RestApiResponse<string>
Get the name of the loaded program.
GetOperationalMode()
Method
RestApiResponse<OperationalModes>
Get the operational mode of the robot.
GetPolyscopeVersion()
Method
RestApiResponse<PolyscopeVersion>
Get the version of PolyScope X.
GetProgramState()
Method
RestApiResponse<ProgramStateResponse>
Get the current program state.
GetPrograms()
Method
RestApiResponse<RestProgramInfo[]>
List the programs stored on the robot.
GetRobotApiVersion()
Method
RestApiResponse<Version>
Get the version of the robot API of the robot.
GetRobotMode()
Method
RestApiResponse<RobotModes>
Get the mode of the robot.
GetSafetyMode()
Method
RestApiResponse<SafetyModes>
Get the safety mode of the robot.
IsInRemoteControl()
Method
RestApiResponse<bool>
Indicate whether the robot is in remote control.
LoadProgram(string)
Method
RestApiResponse
Load a program by name.
  • programName : Name of the program to load (without .urp extension)
Pause()
Method
RestApiResponse
Pause the running program.
Play()
Method
RestApiResponse
Start playing the loaded program.
PowerOff()
Method
RestApiResponse
Power off the robot.
PowerOn()
Method
RestApiResponse
Power on the robot.
RestartSafety()
Method
RestApiResponse
Restart the safety system.
Resume()
Method
RestApiResponse
Resume a paused program.
ShowPopup(string)
Method
RestApiResponse
Show a popup on the screen of the robot, with an empty title.
  • message : Text of the popup
ShowPopupWithTitle(string, string)
Method
RestApiResponse
Show a popup on the screen of the robot.
  • title : Title of the popup
  • message : Text of the popup
Stop()
Method
RestApiResponse
Stop the running program.
UnlockProtectiveStop()
Method
RestApiResponse
Unlock the robot from protective stop state.
UpdateProgramContent(string, string)
Method
RestApiResponse<string>
Replace a program of the robot from its text content.
  • fileName : File name of the program
  • content : Text content of the program file
UpdateProgramFile(string)
Method
RestApiResponse<string>
Replace a program of the robot with a program file.
  • filePath : Path of the program file on this computer
UploadProgramContent(string, string)
Method
RestApiResponse<string>
Upload a new program to the robot from its text content. The robot refuses a program that already exists: use UpdateProgramContent(string%String) to replace it.
  • fileName : File name of the program
  • content : Text content of the program file
UploadProgramFile(string)
Method
RestApiResponse<string>
Upload a new program file to the robot. The robot refuses a program that already exists: use UpdateProgramFile(string) to replace it.
  • filePath : Path of the program file on this computer
Class
RestApiResponse
C#Python

Response from a REST API call

MemberTypeDescription
RestApiResponse()
Constructor
Message
Property
string
Response message or error description
RawResponse
Property
string
Raw JSON response body
StatusCode
Property
HttpStatusCode
HTTP status code returned by the API
Succeed
Property
bool
Indicates whether the API call succeeded (HTTP 200)
Equals(object)
Method
bool
GetHashCode()
Method
int
ToString()
Method
string
Enum
RobotStateAction
C#Python

Actions available for changing the robot's operational state via REST API

NameValueDescription
BRAKE_RELEASE
4
Releases the robot brakes
POWER_OFF
2
Powers off the robot
POWER_ON
3
Powers on the robot
RESTART_SAFETY
1
Restarts the safety system
UNLOCK_PROTECTIVE_STOP
0
Unlocks the robot from a protective stop state
Enum
ProgramStateAction
C#Python

Actions available for changing the program state via REST API

NameValueDescription
pause
1
Pause the running program
play
0
Start or resume playing the program
resume
3
Resume a paused program
stop
2
Stop the running program
Enum
RestProgramState
C#Python

Program state values returned by the REST API

NameValueDescription
Paused
3
Program is paused
Playing
2
Program is playing
Stopped
1
Program is stopped
Unknown
0
Unknown state

What to read next

  • Run a program: the steps from power on to the end of the program.
  • RTDE: the data of the robot, also on PolyScope X.