RoboDK API for Matlab

Contents

% This is an example that uses the RoboDK API for Matlab.
% This is a .m file (Matlab file).
% The RoboDK API for Matlab requires the files in this folder.
% This example requires RoboDK to be running
% (otherwise, RoboDK will be started if it was installed in the default location)
% This example automatically loads the Example 01 installed by default in the "Library" folder

% Note: This program is not meant to accomplish a specific goal, only to
% show how to use the Matlab API
%
% RoboDK api Help:
% ->Type "doc Robolink"            for more help on the Robolink class
% ->Type "doc RobolinkItem"        for more help on the RobolinkItem item class
% ->Type "showdemo Example_RoboDK" for examples on how to use RoboDK's API using the last two classes

clc
clear
close all

% Generate a Robolink object RDK. This object interfaces with RoboDK.
RDK = Robolink;

% Get the library path
path = RDK.getParam('PATH_LIBRARY');

% Open example 1
RDK.AddFile([path, 'Tutorial-UR-Painting.rdk']);

% Display a list of all items
fprintf('Available items in the station:\n');
disp(RDK.ItemList());

% Get one item by name
program = RDK.Item('MainProg');

% Start simulating the "MainProg" program (not blocking call)
program.RunProgram();

% Optional: Wait until the program finished:
while program.Busy()
    fprintf('Working...\n')
    pause(0.5)
end
fprintf('Done!\n')

% Alternative call to run the program
% program = RDK.Item('MainProg').RunProgram();

% Another way to run the same program
% RDK.RunProgram('MainProg');

% return;
Available items in the station:
  Columns 1 through 5

    'Painting Test'    'UR10 Base'    'UR10'    'Paint gun'    'Frame 2'

  Columns 6 through 9

    'Object Inspection'    'Home'    'Approach'    'Top Paint 1'

  Columns 10 through 13

    'Top Paint 2'    'Top Paint 3'    'Top Paint 4'    'Top Paint 5'

  Columns 14 through 17

    'Top Paint 6'    'Top Paint 7'    'Top Paint 8'    'Retract'

  Columns 18 through 22

    'ApproachMove'    'PaintTop'    'Retract'    'MainProg'    'SprayOn'

Working...
Working...
Working...
Working...
Working...
Working...
Working...
Working...
Working...
Working...
Working...
Working...
Working...
Working...
Working...
Working...
Working...
Done!

Retrieving objects from the station and modifying them

% Get some items in the station by their name. Each item is visible in the
% current project tree

robot = RDK.Item('UR10');
fprintf('Robot selected: %s\n', robot.Name());
robot.setVisible(1);

% Retreive the item position with respect to the station with PoseAbs()
% robot.PoseAbs()

ref_base = robot.Parent();
fprintf('Robot base frame: %s\n', ref_base.Name());

object = RDK.Item('Object Inspection');
fprintf('Object selected: %s\n', object.Name());

ref_object = RDK.Item('Frame 2');
fprintf('Reference frame: %s\n', ref_object.Name());

tool = RDK.Item('Paint gun');
fprintf('Tool selected: t%s\n', tool.Name());

target1 = RDK.Item('Home');
fprintf('Target 1 selected:\t%s\n', target1.Name());

target2 = RDK.Item('Approach');
fprintf('Target 2 selected:\t%s\n', target2.Name());

% return
Robot selected: UR10
Robot base frame: UR10 Base
Object selected: Object Inspection
Reference frame: Frame 2
Tool selected: tPaint gun
Target 1 selected:	Home
Target 2 selected:	Approach

How to move a robot directly from Matlab code

% Get the joint values for the first target (home target):
% jhome = [ 0, 0, 0, 0, 30, 0];
jhome = target1.Joints();

% Set the simulation speed. This is a ratio, for example, simulation speed
% of 5 (default) means that 1 second of simulated time corresponds to 1
% second of real time.
RDK.setSimulationSpeed(5);

% Optionally, change the run mode and generate the program (ignores simulation to
% generate the robot program). This will generate a SCRIPT and URP file for
% a Universal Robot robot.
% RDK.setRunMode(RDK.RUNMODE_MAKE_ROBOTPROG)
% RDK.ProgramStart('MatlabProgram');

% Set the robot at the home position
robot.setJoints(jhome); % Immediate move
robot.MoveJ(jhome); % Joint move

% Make sure we are using the selected tool and reference frames with the
% robot
robot.setPoseTool(tool); % Set the tool frame (as item or as pose)
robot.setPoseFrame(ref_object); % Set the reference frame (as item or pose)
robot.setSpeed(100); % Set the TCP linear speed in mm/s

% Retrieve all the reference frame dependencies (items attached to it)
ref_object_items = ref_object.Childs();

for i = 1:numel(ref_object_items)
    item_i = ref_object_items{i};
    if item_i.Type() ~= Robolink.ITEM_TYPE_TARGET
        fprintf('Skipping: %s\n', item_i.Name());
        continue
    end
    fprintf('Moving to: %s ...\n', item_i.Name());
    robot.MoveJ(item_i)

    % Alternatively, we can move the robot given a 4x4 pose:
%     robot.MoveL(item_i.Pose())

    % Alternatively, we can also move the robot given the joint values:
%     robot.MoveJ(item_i.Joints())

end
fprintf('Done!\n');

% Signal the end of generated program (when program generation is used)
RDK.Finish()
Skipping: Object Inspection
Moving to: Home ...
Moving to: Approach ...
Moving to: Top Paint 1 ...
Moving to: Top Paint 2 ...
Moving to: Top Paint 3 ...
Moving to: Top Paint 4 ...
Moving to: Top Paint 5 ...
Moving to: Top Paint 6 ...
Moving to: Top Paint 7 ...
Moving to: Top Paint 8 ...
Moving to: Retract ...
Done!

How to create program

Clean up previous items automatically generated by using this script the keyword "macro" is used if we want to delete any items when the script is executed again.

tic()
while 1
    item = RDK.Item('macro');
    if item.Valid() == 0
        % Iterate until there are no items with the "macro" name
        break
    end
    % if Valid() returns 1 it means that an item was found
    % in that case, delete the item in the RoboDK station
    item.Delete();
end

% Turn off rendering (faster)
RDK.Render(0);

% Get the tool pose
Htcp = tool.PoseTool();

% Create a reference frame with respect to the robot base reference
ref = RDK.AddFrame('Frame macro', ref_base);

% Set the reference frame at coordinates XYZ, rotation of 90deg about Y plus rotation of 180 deg about Z
Hframe = transl(0,-500,200)*rotz(pi/2);
ref.setPose(Hframe);

% Set the robot's reference frame as the reference we just cretaed
robot.setPoseFrame(ref);

% Set the robot tool frame (by item or by pose)
robot.setPoseTool(tool);
% robot.setPoseTool(Htcp);

% Add 90 deg to joint 1 to set a new home target
jhome2 = jhome;
jhome2(1) = jhome(1) + 90;
robot.setJoints(jhome2)

% Get the position of the TCP with respect to the robot base for the home
% position
Hhome2 = inv(Hframe)*robot.SolveFK(jhome2)*Htcp;

% This is the same as robot.Pose() because we just moved the robot there:
% Hhome = robot.Pose()

% Create a joint target home
target = RDK.AddTarget('Home 2', ref, robot);
target.setPose(Hhome2);
target.setJoints(jhome2);
target.setAsJointTarget();


% Create a new program "Prog"
prog = RDK.AddProgram('Prog macro');

% Specify the active reference frame and tool frame
prog.setPoseFrame(ref)
prog.setPoseTool(tool)

% Add joint movement into the program
prog.addMoveJ(target);

% Generate a sequence of targets and move along the targets (linear move)
angleY = 0;
for dx=-400:100:400
    targetname = sprintf('Target TX=%i RY=%i',dx,angleY);
    target = RDK.AddTarget(targetname,ref,robot);
    % Move along X direction with respect to the reference frame (pre multiply)
    % and rotate relative to the tool orientation (post multiply)
    pose = transl(dx,0,-100)*Hhome2*roty(angleY*pi/180);
    target.setPose(pose);
    prog.addMoveL(target);
    angleY = angleY + 5;
end

% Set automatic render after every modification
RDK.Render(1);

% Run the program we just created
prog.RunProgram();

% Wait for the movement to finish
while robot.Busy()
    pause(0.5);
    fprintf('Waiting for the robot to finish...\n');
end

% % Run the program once again
% fprintf('Running the program again...\n');
% prog.RunProgram();
Waiting for the robot to finish...
Waiting for the robot to finish...
Waiting for the robot to finish...
Waiting for the robot to finish...
Waiting for the robot to finish...
Waiting for the robot to finish...
Waiting for the robot to finish...
Waiting for the robot to finish...
Waiting for the robot to finish...
Waiting for the robot to finish...

Calculate forward and inverse kinematics of a robot

% Get the robot joints
fprintf('Current robot joints:\n');
joints = robot.Joints();
disp(joints);

% Get the position of the robot flange with respect to the robot base frame
fprintf('Calculated pose for current joints:\n');
H_tcp_wrt_frame = robot.SolveFK(joints);
disp(H_tcp_wrt_frame);

% Calculate the robot joints to reach this position. It returns the closes
% match to the current robot joint position (it should be the same as
% joints)
fprintf('Calculated robot joints from pose:\n');
joints2 = robot.SolveIK(H_tcp_wrt_frame);
disp(joints2);

% Retrieve all possible solutions
fprintf('All solutions available for the selected position:\n');
joints3_all = robot.SolveIK_All(H_tcp_wrt_frame);
disp(joints3_all);

% Show the sequence in the slider bar in RoboDK
RDK.ShowSequence(joints3_all);

pause(1);
% Calculate a new solution to reach the target off by 100 mm in Z along the
% robot flange
joints4 = robot.SolveIK(H_tcp_wrt_frame*transl(0,0,-100));
% Set the robot at the new position calculated
robot.setJoints(joints4);
Current robot joints:
  -58.2186  -74.4992  -80.3729 -111.8020   89.0605  119.7580

Calculated pose for current joints:
   -0.0343    0.9993    0.0166  109.3584
    0.9977    0.0353   -0.0579 -490.5706
   -0.0585    0.0146   -0.9982  865.8185
         0         0         0    1.0000

Calculated robot joints from pose:
  -58.2186  -74.4992  -80.3729 -111.8020   89.0605  119.7580

All solutions available for the selected position:
  Columns 1 through 7

  -58.2186   83.2759   83.2759   83.2759  -58.2186   83.2759  -58.2186
  -74.4992  -26.7665 -105.4910  -83.9443  -96.0666  -28.5114 -153.2036
  -80.3729  -59.4481   80.3355   59.4481  -59.4052  -80.3355   59.4052
 -111.8020  173.0263  -68.0327  111.3080   68.7977   15.6586    7.1242
   89.0605   88.6658  -88.6658   88.6658  -89.0605  -88.6658  -89.0605
  119.7580  -98.6831   81.3169  -98.6831  -60.2420   81.3169  -60.2420

  Columns 8 through 14

  -58.2186   83.2759   83.2759  -58.2186  -58.2186   83.2759   83.2759
 -151.5140  -26.7665  -83.9443  -96.0666 -151.5140  -26.7665  -83.9443
   80.3729  -59.4481   59.4481  -59.4052   80.3729  -59.4481   59.4481
  164.4670 -186.9737 -248.6920 -291.2023 -195.5330  173.0263  111.3080
   89.0605   88.6658   88.6658  -89.0605   89.0605   88.6658   88.6658
  119.7580  -98.6831  -98.6831  -60.2420  119.7580  261.3169  261.3169

  Columns 15 through 19

  -58.2186  -58.2186   83.2759   83.2759  -58.2186
  -96.0666 -153.2036  -26.7665  -83.9443  -96.0666
  -59.4052   59.4052  -59.4481   59.4481  -59.4052
   68.7977    7.1242 -186.9737 -248.6920 -291.2023
  -89.0605  -89.0605   88.6658   88.6658  -89.0605
  299.7580  299.7580  261.3169  261.3169  299.7580

How to scale an object and how to detect collisions

% Verify if a joint movement from j1 to j2 is free of colllision
collision = robot.MoveJ_Test(jhome, jhome2, 1);
disp(collision)
% Activate the trace to see what path the robot tries to make
% To activate the trace: Tools->Trace->Active (ALT+T)

% Detect collisions: returns the number of pairs of objects in a collision state
% Tip: Set the collision map in: Tools-Collision Map
pairs = RDK.Collisions();
fprintf('Pairs of objects in a collision state: %i\n', pairs);

% Scale the geometry of an object, scale can be one number or a scale per axis
% object.Scale([10, 10, 0.5]);

% Detect the intersection between a line and any object
p1 = [-1000; 300; 5000];
p2 = [-1000; 300; -5000];
[collision, itempicked, xyz] = RDK.Collision_Line(p1, p2);
if itempicked.Valid()
    fprintf('Line from p1 to p2 collides with %s\n', itempicked.Name());
    % Create a point in the intersection to display collision
    new_ref = RDK.AddFrame('Collision');
    % Set this ball at the collision point
    new_ref.setPose(transl(xyz(1),xyz(2),xyz(3)));
%     newball.Scale(0.5); % Make this ball 50% of the original size
%     newball.Recolor([1 0 0]); % Make it a red ball
end
   0

Pairs of objects in a collision state: 2
Line from p1 to p2 collides with Object Inspection

How to Attach/Detach an object to the robot tool

% Attach the closest object to the tool
attached = tool.AttachClosest();
% If we know what object we want to attach, we can use this function
% instead: object.setParentStatic(tool);
if attached.Valid()
    attachedname = attached.Name();
    fprintf('Attached: %s\n', attachedname);
else
    % The tolerance can be changed in:
    % Tools->Options->General tab->Maximum distance to attach an object to
    % a robot tool (default is 1000 mm)
    fprintf('No object is close enough\n');
end
pause(2);
tool.DetachAll();
fprintf('Detached all objects\n');
No object is close enough
Detached all objects