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.
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
| Item | Supported |
|---|---|
| Windows | Matlab with the .NET Framework 4.6.2 or later (the default on Windows 10 and 11). Load the net48 DLL |
| Linux and macOS | Matlab R2024b or later with the .NET runtime, selected by dotnetenv("core"). Not tested by UnderAutomation yet |
| Controllers | CS8 and CS9, and the emulator of Staubli Robotics Suite |
Matlab documents its .NET interface in Call .NET from MATLAB.
Load the SDK
- Download UnderAutomation.Staubli.zip from the latest release of Staubli.NET.
- On Windows, unblock the zip before you extract it: right click,
Properties, checkUnblock. - Copy its
net48folder next to your script. - Load the DLL with
NET.addAssembly, then import the namespaces you use.
% The net48 folder of UnderAutomation.Staubli.zip, copied next to this scriptdll = fullfile(pwd, 'net48', 'UnderAutomation.Staubli.dll');if ~NET.isNETSupportederror('No supported .NET Framework found on this computer');end% Load the SDK once per Matlab sessionNET.addAssembly(dll);% Import the namespaces you useimport 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 connectionStaubliController.RegisterLicense('YourCompanyName', 'YOUR_LICENSE_KEY');info = StaubliController.LicenseInfo;% A readable description of the current statedisp(char(info.ToString()));% Check the license once at startupif ~info.IsLicensederror(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:
| .NET | Matlab |
|---|---|
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 tool | Pass every argument. [] passes null |
double[] returned | A .NET array: double(joints) converts it to a Matlab vector |
double[] expected | A Matlab vector, for example [0, 0, pi/2, 0, pi/2, 0] |
string[] expected | NET.createArray('System.String', n), then fill it |
robots[0] | robots(1): Matlab indexes .NET arrays from 1 |
robots.Length | robots.Length |
An enumeration value, like task.State | char(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 vectorjoints = double(position.JointsPosition);% Flange position in the world frame: X, Y, Z in meters, Rx, Ry, Rz in radiansflange = 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 jointsfk = controller.Soap.ForwardKinematics(0, joints);% Position of the frame originframe = fk.Position;fprintf('P = [%.4f, %.4f, %.4f]\n', frame.Px, frame.Py, frame.Pz);% Rotation matrix of the frame, columns N, O, AR = [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 controllerios = controller.Soap.GetAllPhysicalIos();% .NET arrays are indexed from 1 in Matlabfor i = 1:ios.Lengthio = ios(i);fprintf('%s [%s] %s\n', char(io.Name), char(io.TypeStr), char(io.Description));endcontroller.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 arraynames = NET.createArray('System.String', 2);names(1) = 'BasicIO-1\%I0';names(2) = 'BasicIO-1\%Q0';% One state per name, in the same orderstates = controller.Soap.ReadIos(names);for i = 1:states.Lengthfprintf('%s = %g\n', char(names(i)), states(i).Value);endcontroller.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 offvalues = [1, 0];responses = controller.Soap.WriteIos(names, values);for i = 1:responses.Lengthif ~responses(i).Found || ~responses(i).Successfprintf('%s: write refused\n', char(names(i)));endendcontroller.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.Lengthtask = 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 noneif task.RuntimeError ~= 0fprintf(' Error %d: %s\n', task.RuntimeError, char(task.RuntimeErrorDescription));endendcontroller.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.NetExceptionin Matlab. ItsExceptionObjectproperty holds the .NET exception, with itsMessage. - 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.