Get started with Matlab

The library has been developed to provide functions compatible with the Matlab environment.

The use of this library is based on the interoperability between Matlab and .NET

For more information, please refer to the official Matlab documentation: https://mathworks.com/help/matlab/using-net-libraries-in-matlab.html

Load Library

Please download the library from the download page and put the .NET 3.5 DLL in your Matlab project folder. Make sure the DLL file is not locked (see its properties in Windows).

To ensure that your installation is ready to use the library, please ensure that NET.isNETSupported is true.

if ~NET.isNETSupported
    disp('The Matlab .NET environment is not fully configured')
end

To load the library, please specify its path as an argument to the function NET.addAssembly.

Its path must be absolute, but you can also calculate it by concatenating the path of the current pwd folder.

Finally, it is necessary to import the namespaces of the library with import UnderAutomation.UniversalRobots.*

% Load library
NET.addAssembly('C:\path\to\my\workspace\UnderAutomation.UniversalRobots.dll');

% You can also use relative path
% NET.addAssembly(fullfile(pwd, '.\UnderAutomation.UniversalRobots.dll'));

% Import library features in Matlab
import UnderAutomation.UniversalRobots.*

Connect and interact with your robot

Once the library is loaded correctly, you can create instances of the objects implemented by this library, please refer to the rest of the documentation which describes them.

To react to events, you must use addlistener() which executes a function passed as a parameter when the event is raised.

NET.addAssembly('C:\path\to\my\workspace\UnderAutomation.UniversalRobots.dll');
import UnderAutomation.UniversalRobots.*

% Setup your connection
param = ConnectParameters('192.168.0.1')
param.Rtde.Enable = true

% Instanciate robot
robot = UR()

% Connect to robot
robot.Connect(param)

% ...

Limitations

Not all the features of .NET are available in Matlab, but this library has been developed to work around these limitations and allow you to make full use of it.

However, please ensure that the Framework 3.5 is enabled in the optional Windows features.

For more information on the limitations of Matlab :

Examples

Data exchange

% Load library
NET.addAssembly(fullfile(pwd, '.\UnderAutomation.UniversalRobots.dll'))
% Import library features in Matlab
import UnderAutomation.UniversalRobots.*
% Create a robot instance
robot = UR()
% Setup connection to th robot
param = ConnectParameters()
param.IP = '192.168.0.1'
% Enable primary interface to get variable values
param.PrimaryInterface.Enable = true
% Enable RTDE at 5Hz (500Hz possible)
param.Rtde.Enable = true
param.Rtde.Frequency = 5
% Ask the robot for permission to write these registers
param.Rtde.InputSetup.Add(RtdeInputData.StandardAnalogOutput0)
param.Rtde.InputSetup.Add(RtdeInputData.InputBitRegisters, 64)
% Ask the robot to send me this data
param.Rtde.OutputSetup.Add(RtdeOutputData.ActualTcpPose)
param.Rtde.OutputSetup.Add(RtdeOutputData.ActualTcpForce)
% Connect to the robot
robot.Connect(param)
function OnOutputDataReceived(o, e)
%%
% Display robot TCP pose
pose = robot.Rtde.OutputDataValues.ActualTcpPose
% pose.X, pose.Y, pose.Z, pose.Rx, ...
% Display robot TCP force
force = robot.Rtde.OutputDataValues.ActualTcpForce
% Write data in robot controler
inputs = RtdeInputValues()
inputs.StandardAnalogOutput0 = 0.2
inputs.InputBitRegisters.X64 = true
robot.Rtde.WriteInputs(inputs)
%%
end
addlistener(robot.PrimaryInterface.GlobalVariables, 'OutputDataReceived', @OnOutputDataReceived)
function OnValuesUpdated(o, e)
%%
% Display all program and installation variables
variables = robot.PrimaryInterface.GlobalVariables.GetAll()
name = variables[0].Name
value = variables[0].ToPose()
type = variables[0].Type
%%
end
% Event raised when a program or installation variable change
addlistener(robot.PrimaryInterface.GlobalVariables, 'ValuesUpdated', @OnValuesUpdated)
function OnMasterboardDataReceived(o, e)
ai0 = e.AnalogInput0
di2 = e.DigitalInputs.Digital2
end
% Several other events are raised by primary interface
addlistener(robot.PrimaryInterface, 'MasterboardDataReceived', @OnMasterboardDataReceived)
function OnCartesianInfoReceived(o, e)
x = e.X
tcpOffsetX = e.TCPOffsetX
end
addlistener(robot.PrimaryInterface, 'CartesianInfoReceived', @OnCartesianInfoReceived)
function OnJointDataReceived(o, e)
shoulderAngle = e.Shoulder.Position
elbowMode = e.Elbow.JointMode
wrist3Speed = e.Wrist3.ActualSpeed
end
addlistener(robot.PrimaryInterface, 'JointDataReceived', @OnJointDataReceived)

Remote control

% Load library
NET.addAssembly(fullfile(pwd, '.\UnderAutomation.UniversalRobots.dll'))
% Import library features in Matlab
import UnderAutomation.UniversalRobots.*
%%
% Create a robot instance
robot = UR();
% Connect to the robot
robot.Connect('192.168.0.1');
% Power on the robot arm and release brakes
robot.Dashboard.PowerOn();
robot.Dashboard.ReleaseBrake();
% Load program file to polyscope
robot.Dashboard.LoadProgram('fast_bin_picking.urp');
% Start the program
robot.Dashboard.Play();
% Get program name and state
state = robot.Dashboard.GetProgramState();
disp(strcat('Program name : ', state.Value.Name));
disp(strcat('Stopped, Playing or Paused ? : ', state.Value.State));
% Display a popup on the pendant
robot.Dashboard.ShowPopup('I just remote-controlled my robot!');
%%

Scripts

% Load library
NET.addAssembly(fullfile(pwd, '.\UnderAutomation.UniversalRobots.dll'))
% Import library features in Matlab
import UnderAutomation.UniversalRobots.*
% Create a robot instance
robot = UR()
% Setup connection to th robot
param = ConnectParameters()
param.IP = '192.168.0.1'
% Enable primary interface to get variable values
param.PrimaryInterface.Enable = true
% Enable SFTP file access
param.Ssh.EnableSftp = true
% Connect to the robot
robot.Connect(param)
%%
% Ask robot to execute a movej
robot.PrimaryInterface.Script.Send('movej([-1.5,-1.5,-2,-0.5,1.8,0], a=1.4, v=1.05, t=0, r=0)')
% Enumerates files and folder
items = robot.Sftp.ListDirectory('/home/ur/ursim-current/programs/')
% Download program file prg.urp to your local disk
robot.Sftp.DownloadFile('/home/ur/ursim-current/programs/prg.urp', 'C:\\temp\\prg.urp')
% Send a local file to the robot
robot.Sftp.UploadFile(@'C: \ temp \ prg.urp', '/home/ur/ursim-current/programs/prg.urp')
% Manipulate files and directories
robot.Sftp.RenameFile('/home/ur/prg.urp', '/home/ur/prg2.urp')
robot.Sftp.Delete('/home/ur/prg.urp')
exists = robot.Sftp.Exists('/home/ur/prg.urp')
robot.Sftp.WriteAllText('/home/ur/file.txt', 'Hello robot !')
% ...
%%

Sockets

% Load library
NET.addAssembly(fullfile(pwd, '.\UnderAutomation.UniversalRobots.dll'))
% Import library features in Matlab
import UnderAutomation.UniversalRobots.*
% Create a robot instance
robot = UR()
% Setup connection to th robot
param = ConnectParameters()
param.IP = '192.168.0.1'
% Enable spcket server on port 50001
param.SocketCommunication.Enable = true
param.SocketCommunication.Port = 50001
% Connect to the robot
robot.Connect(param)
%%
function OnSocketClientConnection(o, e)
% Reply to the robot
e.Client.SocketWrite('Hello cobot <3')
end
% Robot connects with URScipt function socket_open()
addlistener(robot.SocketCommunication, 'SocketClientConnection', @OnSocketClientConnection)
function OnSocketRequest(o, e)
robotMessage = e.Message
end
% Event raised when the robot sends a message with socket_write()
addlistener(robot.SocketCommunication, 'SocketRequest', @OnSocketRequest)
% Send a message to all connected clients
robot.SocketCommunication.SocketWrite('123456')
%%
% List of all connected clients
connectedClients = robot.SocketCommunication.ConnectedClients
% IP address and remote port of a client
clientEndpoint = connectedClients[0].EndPoint
% Send a message to a specific connected robot
connectedClients[0].SocketWrite('Hello robot [0] !')
function OnSocketClientDisconnection(o, e)
% Handle client disconnection
end
addlistener(connectedClients[0].SocketClientDisconnection, 'SocketClientDisconnection', @OnSocketClientDisconnection)

Toolbox

% Load library
NET.addAssembly(fullfile(pwd, '.\UnderAutomation.UniversalRobots.dll'))
% Import library features in Matlab
import UnderAutomation.UniversalRobots.*
%%
% Create X, Y, Z, RX, RY, RZ pose
pose = Pose(0.1, 0.2, -0.1, 0, 0.05, 0.1)
% Convert cartesian pose type to RPY or rotation vector
rpy = pose.FromRotationVectorToRPY()
vectorPose = pose.FromRPYToRotationVector()
% Decompile program and installation files and access inner XML
installation = URInstallation.Load('C:\\temp\\default.installation')
prg = URProgram.Load('C:\\temp\\prg.urp')
internalXml = prg.XML
%%

Read more