UnderAutomation
Eine Frage?

[email protected]

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

Get started with Matlab

Load the .NET DLL of the Staubli SDK in Matlab with NET.addAssembly, then read positions, I/O and tasks of a CS8 or CS9 controller from a Matlab script.

  • Requirements
  • Load the SDK
  • First program
  • From .NET to Matlab
  • Examples
  • Read the position
  • Forward kinematics
  • List, read and write I/O
  • VAL 3 tasks
  • Known limits
  • What to read next

This page shows how to use the Staubli SDK from Matlab, to read and control a Staubli CS8 or CS9 controller from a Matlab script. Matlab loads the .NET DLL of the SDK with its .NET interface: nothing else is installed, on the PC or on the controller.

Requirements

ItemSupported
WindowsMatlab with the .NET Framework 4.6.2 or later (the default on Windows 10 and 11). Load the net48 DLL
Linux and macOSMatlab R2024b or later with the .NET runtime, selected by dotnetenv("core"). Not tested by UnderAutomation yet
ControllersCS8 and CS9, and the emulator of Staubli Robotics Suite

Matlab documents its .NET interface in Call .NET from MATLAB.

Load the SDK

  1. Download UnderAutomation.Staubli.zip from the latest release of Staubli.NET.
  2. On Windows, unblock the zip before you extract it: right click, Properties, check Unblock.
  3. Copy its net48 folder next to your script.
  4. Load the DLL with NET.addAssembly, then import the namespaces you use.
% The net48 folder of UnderAutomation.Staubli.zip, copied next to this script
dll = fullfile(pwd, 'net48', 'UnderAutomation.Staubli.dll');
if ~NET.isNETSupported
error('No supported .NET Framework found on this computer');
end
% Load the SDK once per Matlab session
NET.addAssembly(dll);
% Import the namespaces you use
import UnderAutomation.Staubli.*
import UnderAutomation.Staubli.Soap.Data.*

NET.addAssembly needs an absolute path: fullfile(pwd, ...) builds one that works on every operating system. Matlab cannot unload a .NET assembly: restart Matlab to load a new version of the DLL.

On Linux and macOS, Matlab R2024b and later can use the .NET runtime instead of the .NET Framework. Set the environment variable DOTNET_ROOT to the folder of the .NET runtime, call dotnetenv("core") before NET.addAssembly, and load the DLL of the net8.0 folder. See dotnetenv in the Matlab documentation.

First program

NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Staubli.dll'));
import UnderAutomation.Staubli.*
controller = StaubliController();
% Default parameters: SOAP port 851, user "default", password "default"
controller.Connect('192.168.0.254');
connected = controller.Enabled;
controller.Disconnect();

The SDK runs for 30 days without a key. After that, register your license once per Matlab session:

NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Staubli.dll'));
import UnderAutomation.Staubli.*
% Register the license once, before the first connection
StaubliController.RegisterLicense('YourCompanyName', 'YOUR_LICENSE_KEY');
info = StaubliController.LicenseInfo;
% A readable description of the current state
disp(char(info.ToString()));
% Check the license once at startup
if ~info.IsLicensed
error(char(info.ToString()));
end

From .NET to Matlab

The names of the classes, methods and properties are the .NET names of the other pages. The C# samples of the documentation translate line by line, with these rules:

.NETMatlab
new StaubliController()StaubliController(), after import UnderAutomation.Staubli.*
"192.168.0.254"'192.168.0.254', a char vector
GetCurrentJointPosition(robot: 0)GetCurrentJointPosition(0): no named arguments
An optional argument, like toolPass every argument. [] passes null
double[] returnedA .NET array: double(joints) converts it to a Matlab vector
double[] expectedA Matlab vector, for example [0, 0, pi/2, 0, pi/2, 0]
string[] expectedNET.createArray('System.String', n), then fill it
robots[0]robots(1): Matlab indexes .NET arrays from 1
robots.Lengthrobots.Length
An enumeration value, like task.Statechar(task.State) gives its name

Examples

Read the position

NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Staubli.dll'));
import UnderAutomation.Staubli.*
controller = StaubliController();
controller.Connect('192.168.0.254');
% Joints and flange position of the first robot, in one request.
% Pass [] for the optional tool and frame.
position = controller.Soap.GetCurrentCartesianJointPosition(0, [], []);
% Joint values in radians, as a Matlab row vector
joints = double(position.JointsPosition);
% Flange position in the world frame: X, Y, Z in meters, Rx, Ry, Rz in radians
flange = position.CartesianPosition;
fprintf('X=%.4f Y=%.4f Z=%.4f\n', flange.X, flange.Y, flange.Z);
fprintf('Rx=%.4f Ry=%.4f Rz=%.4f\n', flange.Rx, flange.Ry, flange.Rz);
controller.Disconnect();

Forward kinematics

NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Staubli.dll'));
import UnderAutomation.Staubli.*
controller = StaubliController();
controller.Connect('192.168.0.254');
% Joint values in radians. A Matlab vector is passed as a .NET array.
joints = [0, 0, pi/2, 0, pi/2, 0];
% The controller computes the flange frame for these joints
fk = controller.Soap.ForwardKinematics(0, joints);
% Position of the frame origin
frame = fk.Position;
fprintf('P = [%.4f, %.4f, %.4f]\n', frame.Px, frame.Py, frame.Pz);
% Rotation matrix of the frame, columns N, O, A
R = [frame.Nx frame.Ox frame.Ax; frame.Ny frame.Oy frame.Ay; frame.Nz frame.Oz frame.Az];
controller.Disconnect();

List, read and write I/O

NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Staubli.dll'));
import UnderAutomation.Staubli.*
controller = StaubliController();
controller.Connect('192.168.0.254');
% Every physical I/O of the controller
ios = controller.Soap.GetAllPhysicalIos();
% .NET arrays are indexed from 1 in Matlab
for i = 1:ios.Length
io = ios(i);
fprintf('%s [%s] %s\n', char(io.Name), char(io.TypeStr), char(io.Description));
end
controller.Disconnect();
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Staubli.dll'));
import UnderAutomation.Staubli.*
controller = StaubliController();
controller.Connect('192.168.0.254');
% Names as returned by GetAllPhysicalIos, in a .NET string array
names = NET.createArray('System.String', 2);
names(1) = 'BasicIO-1\%I0';
names(2) = 'BasicIO-1\%Q0';
% One state per name, in the same order
states = controller.Soap.ReadIos(names);
for i = 1:states.Length
fprintf('%s = %g\n', char(names(i)), states(i).Value);
end
controller.Disconnect();
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Staubli.dll'));
import UnderAutomation.Staubli.*
controller = StaubliController();
controller.Connect('192.168.0.254');
names = NET.createArray('System.String', 2);
names(1) = 'BasicIO-1\%Q0';
names(2) = 'BasicIO-1\%Q1';
% One value per name. Digital outputs: 1 is on, 0 is off
values = [1, 0];
responses = controller.Soap.WriteIos(names, values);
for i = 1:responses.Length
if ~responses(i).Found || ~responses(i).Success
fprintf('%s: write refused\n', char(names(i)));
end
end
controller.Disconnect();

VAL 3 tasks

NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Staubli.dll'));
import UnderAutomation.Staubli.*
controller = StaubliController();
controller.Connect('192.168.0.254');
tasks = controller.Soap.GetTasks();
for i = 1:tasks.Length
task = tasks(i);
% char() gives the name of an enumeration value: Idle, Running, Stopped...
fprintf('%s (%s), created by %s\n', char(task.Name), char(task.State), char(task.CreatedBy));
% Runtime error of the task, 0 when there is none
if task.RuntimeError ~= 0
fprintf(' Error %d: %s\n', task.RuntimeError, char(task.RuntimeErrorDescription));
end
end
controller.Disconnect();

Known limits

  • Matlab has no named arguments: pass all the arguments of a .NET method, in order.
  • An exception of the SDK is a NET.NetException in Matlab. Its ExceptionObject property holds the .NET exception, with its Message.
  • Matlab keeps the DLL loaded until it closes.

See also the Matlab pages on the limits of .NET arrays, .NET methods and .NET enumerations.

What to read next

  • Connect to your robot: connection parameters and errors.
  • SOAP overview: the functions of the SDK, one page per topic.
  • Licensing: the 30 day trial and the license key.

Integrieren Sie Roboter von Universal Robots, Fanuc, Yaskawa, ABB oder Staubli ganz einfach in Ihre .NET-, Python-, LabVIEW- oder Matlab-Anwendungen

UnderAutomation
KontaktLegal

© All rights reserved.