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
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.*
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)
% ...
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 :
if ~NET.isNETSupporteddisp('The Matlab .NET environment is not fully configured')end% Load libraryNET.addAssembly(fullfile(pwd, '.\UnderAutomation.UniversalRobots.dll'));% Import library features in Matlabimport UnderAutomation.UniversalRobots.*% Create a robot instancerobot = UR();% Setup connection to th robotparam = ConnectParameters();param.IP = '192.168.0.56';% Enable primary interface to get variable valuesparam.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 registersparam.Rtde.InputSetup.Add(Rtde.RtdeInputData.StandardAnalogOutput0);param.Rtde.InputSetup.Add(Rtde.RtdeInputData.InputBitRegisters, 64);% Ask the robot to send me this dataparam.Rtde.OutputSetup.Add(Rtde.RtdeOutputData.ActualTcpPose)param.Rtde.OutputSetup.Add(Rtde.RtdeOutputData.ActualTcpForce);% Connect to the robotrobot.Connect(param);addlistener(robot.Rtde, 'OutputDataReceived', @OnOutputDataReceived);% Several other events are raised by primary interfaceaddlistener(robot.PrimaryInterface.GlobalVariables, 'ValuesUpdated', @OnValuesUpdated);addlistener(robot.PrimaryInterface, 'JointDataReceived', @OnJointDataReceived);addlistener(robot.PrimaryInterface, 'MasterboardDataReceived', @OnMasterboardDataReceived);addlistener(robot.PrimaryInterface, 'CartesianInfoReceived', @OnCartesianInfoReceived);input("Press enter to stop")robot.Disconnect();function OnOutputDataReceived(o, e)%%% Display robot TCP posepose = robot.Rtde.OutputDataValues.ActualTcpPose;% pose.X, pose.Y, pose.Z, pose.Rx, ...% Display robot TCP forceforce = robot.Rtde.OutputDataValues.ActualTcpForce;% Write data in robot controlerinputs = RtdeInputValues();inputs.StandardAnalogOutput0 = 0.2;inputs.InputBitRegisters.X64 = true;robot.Rtde.WriteInputs(inputs);%%end% Event raised when a program or installation variable changefunction OnMasterboardDataReceived(o, e)ai0 = e.AnalogInput0;di2 = e.DigitalInputs.Digital2;endfunction OnCartesianInfoReceived(o, e)x = e.X;tcpOffsetX = e.TCPOffsetX;endfunction OnJointDataReceived(o, e)shoulderAngle = e.Shoulder.Position;elbowMode = e.Elbow.JointMode;wrist3Speed = e.Wrist3.ActualSpeed;endfunction OnValuesUpdated(o, e)%%% Display all program and installation variablesvariables = robot.PrimaryInterface.GlobalVariables.GetAll();name = variables(0).Name;value = variables(0).ToPose();type = variables(0).Type;%%end
% Load libraryNET.addAssembly(fullfile(pwd, '.\UnderAutomation.UniversalRobots.dll'));% Import library features in Matlabimport UnderAutomation.UniversalRobots.*%%% Create a robot instancerobot = UR();% Connect to the robotrobot.Connect('192.168.0.56');% Power on the robot arm and release brakesrobot.Dashboard.PowerOn();robot.Dashboard.ReleaseBrake();% Load program file to polyscoperobot.Dashboard.LoadProgram('fast_bin_picking.urp');% Start the programrobot.Dashboard.Play();% Get program name and statestate = robot.Dashboard.GetProgramState();name = state.Value.Namestate = state.Value.State% Display a popup on the pendantrobot.Dashboard.ShowPopup('I just remote-controlled my robot!');%%
% Load libraryNET.addAssembly(fullfile(pwd, '.\UnderAutomation.UniversalRobots.dll'));% Import library features in Matlabimport UnderAutomation.UniversalRobots.*% Create a robot instancerobot = UR();% Setup connection to th robotparam = ConnectParameters();param.IP = '192.168.0.56';% Enable primary interface to get variable valuesparam.PrimaryInterface.Enable = true;% Enable SFTP file accessparam.Ssh.EnableSftp = true;% Connect to the robotrobot.Connect(param);%%% Ask robot to execute a movejrobot.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 folderitems = robot.Sftp.ListDirectory('/home/ur/ursim-current/programs/');% Download program file prg.urp to your local diskrobot.Sftp.DownloadFile('/home/ur/ursim-current/programs/prg.urp', 'C:\\temp\\prg.urp');% Send a local file to the robotrobot.Sftp.UploadFile('C:\temp\prg.urp', '/home/ur/ursim-current/programs/prg.urp');% Manipulate files and directoriesrobot.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 !');% ...%%
% Load libraryNET.addAssembly(fullfile(pwd, '.\UnderAutomation.UniversalRobots.dll'));% Import library features in Matlabimport UnderAutomation.UniversalRobots.*%%% Create a robot instancerobot = UR();% Setup connection to th robotparam = ConnectParameters();param.IP = '192.168.0.56';% Enable spcket server on port 50001param.SocketCommunication.Enable = true;param.SocketCommunication.Port = 50001;% Connect to the robotrobot.Connect(param)% Robot connects with URScipt function socket_open()addlistener(robot.SocketCommunication, 'SocketClientConnection', @OnSocketClientConnection)% Event raised when the robot sends a message with socket_write()addlistener(robot.SocketCommunication, 'SocketRequest', @OnSocketRequest)% Send a message to all connected clientsrobot.SocketCommunication.SocketWrite('123456')%%% List of all connected clientsconnectedClients = robot.SocketCommunication.ConnectedClients% IP address and remote port of a clientclientEndpoint = connectedClients(0).EndPoint% Send a message to a specific connected robotconnectedClients(0).SocketWrite('Hello robot [0] !')addlistener(connectedClients(0).SocketClientDisconnection, 'SocketClientDisconnection', @OnSocketClientDisconnection)function OnSocketRequest(o, e)robotMessage = e.Messageendfunction OnSocketClientDisconnection(o, e)% Handle client disconnectionendfunction OnSocketClientConnection(o, e)% Reply to the robote.Client.SocketWrite('Hello cobot <3')end
% Load libraryNET.addAssembly(fullfile(pwd, '.\UnderAutomation.UniversalRobots.dll'))% Import library features in Matlabimport UnderAutomation.UniversalRobots.*%%% Create X, Y, Z, RX, RY, RZ posepose = Common.Pose(0.1, 0.2, -0.1, 0, 0.05, 0.1)% Convert cartesian pose type to RPY or rotation vectorrpy = pose.FromRotationVectorToRPY()vectorPose = pose.FromRPYToRotationVector()% Decompile program and installation files and access inner XMLinstallation = Files.URInstallation.Load('C:\\temp\\default.installation')prg = Files.URProgram.Load('C:\\temp\\prg.urp')internalXml = prg.XML%%