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.
% This example automatically loads the Example 1 installed by default in the "Library" folder
%
% 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 RL. This object interfaces with RoboDK.
RL = Robolink;

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

% Open example 1
RL.AddFile([path,'Example 1 - Pick and place.rdk']);

% Display a list of all items
RL.ItemList();

% Get one item by its name
program = RL.Item('Pick and place');

% Start "Pick and place" program
program.RunProgram();

% Alternative call to run the program
% program = RL.Item('Pick and place').RunProgram();

% Another alternative call to run the same program
% RL.RunProgram('Pick and place');

% return;
1	->	Example 1 - Pick and place
2	->	ABB IRB 1600-8/1.45 Base
3	->	ABB IRB 1600-8/1.45
4	->	Tool
5	->	Table 1
6	->	Approach
7	->	Target b1
8	->	Target b2
9	->	Target b3
10	->	Target b4
11	->	Target b5
12	->	base
13	->	ball 1
14	->	ball 2
15	->	ball 3
16	->	ball 4
17	->	ball 5
18	->	Table 2
19	->	base
20	->	Pick and place
21	->	Replace objects

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 = RL.Item('ABB IRB 1600-8/1.45');
fprintf('Robot selected:\t%s\n', robot.Name());
robot.setVisible(1);
% We can validate the type of each item by calling:
% robot.Type()
% We can retreive the item position with respect to the station with PoseAbs()
% robot.PoseAbs()

frameref = robot.Parent();
fprintf('Robot reference selected:\t%s\n', frameref.Name());

object = RL.Item('base');
fprintf('Object selected:\t%s\n', object.Name());

ball = RL.Item('ball');
fprintf('Ball selected:\t%s\n', ball.Name());

frametable = RL.Item('Table 1');
fprintf('Table selected:\t%s\n', frametable.Name());

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

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

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


% return
Robot selected:	ABB IRB 1600-8/1.45
Robot reference selected:	ABB IRB 1600-8/1.45 Base
Object selected:	base
Ball selected:	ball 1
Table selected:	Table 1
Tool selected:	Tool
Target 1 selected:	Target b1
Target 2 selected:	Target b3

How to generate a robot program

% Clean up previous items automatically generated by this script
% the keyword "macro" is used if we want to delete any items when the
% script is executed again.
tic()
while 1
    item = RL.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
    % if so, delete the item in the RoboDK station
    item.Delete();
end

% Set the home joints
jhome = [ 0, 0, 0, 0, 30, 0];

% Set the robot at the home position
robot.setJoints(jhome);

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

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

% Create a reference frame with respect to the robot base reference
ref = RL.AddFrame('Frame macro', frameref);
% Set the reference frame at coordinates XYZ, rotation of 90deg about Y plus rotation of 180 deg about Z
Hframe = transl(750,250,500)*roty(pi/2)*rotz(pi);
ref.setPose(Hframe);

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

% Get the position of the TCP wrt the robot base
Hhome = inv(Hframe)*robot.SolveFK(jhome)*Htcp;

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

% Create a joint target home
target = RL.AddTarget('Home', ref, robot);
target.setAsJointTarget();
target.setJoints(jhome)
% Add joint movement into the program
prog.addMoveJ(target);

% Generate a sequence of targets and move along the targets (linear move)
angleY = 0;
for dy=600:-100:100
    targetname = sprintf('Target TY=%i RY=%i',dy,angleY);
    target = RL.AddTarget(targetname,ref,robot);
    % Move along Z direction of the reference frame
    pose = transl(0,dy,0);
    % Keep the same orientation as home orientation
    pose(1:3,1:3) = Hhome(1:3,1:3);
    pose = pose*roty(angleY*pi/180);
    target.setPose(pose);
    prog.addMoveL(target);
    angleY = angleY + 20;
end

% Set automatic render on every call
RL.Render(1);

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

% Wait for the movement to finish
while robot.Busy()
    pause(1);
    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...
Running the program again...

How to change the parent that an item is attached to

% Change the support of a target
% The final result of the operations made to target1 and target2 is the same
Htarget = target1.Pose();
target1.setParentStatic(frameref);
target1.setPose(Htarget);

target2.setParent(frameref);

% We can list the items that depend on an item
childs = frametable.Childs();
for i=1:numel(childs)
    name = childs{i}.Name();
    newname = [name,' modified'];
    visible = childs{i}.Visible();
    childs{i}.setName(newname);
    fprintf('%s %i\n',newname, visible);
end
Approach modified 1
Target b2 modified 1
Target b4 modified 1
Target b5 modified 1
base modified 1
ball 1 modified 1
ball 2 modified 1
ball 3 modified 1
ball 4 modified 1
ball 5 modified 1

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');
Attached: base modified
Detached all objects

How to scale an object and how to detect collisions

% Replace objects (we call the program previously set in example 1)
RL.Item('Replace objects').RunProgram();

% Verify if a joint movement from j1 to j2 is free of colllision
j1 = [-100, -50, -50, -50, -50, -50];
j2 = [100, 50, 50, 50, 50, 50];
collision = robot.MoveJ_Collision(j1, j2, 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
pairs = RL.Collision();
fprintf('Pairs collided: %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; 0; 8000];
p2 = [1000; 0;    0];
[collision, itempicked, xyz] = RL.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
    ball.Copy();
    newball = RL.Paste();
    % Set this ball at the collision point
    newball.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
     1

Pairs collided: 1
Line from p1 to p2 collides with base modified

How to move the robot programmaticall without creating a program

% Replace objects (we call the program previously set in example 1)
RL.Item('Replace objects').RunProgram();

% RL.setRunMode(1); % this performs a quick validation without showing the dynamic movement
% (1 = RUNMODE_QUICKVALIDATE)

fprintf('Moving by target item...\n');
robot.setFrame(frametable);
RL.setSimulationSpeed(10);
for i=1:2
    robot.setSpeed(10000,1000);
    robot.MoveJ(target1);
    robot.setSpeed(100,200);
    robot.MoveL(target2);

end

fprintf('Moving by joints...\n');
J1 = [0,0,0,0,50,0];
J2 = [40,30,-30,0,50,0];
for i=1:2
    robot.MoveJ(J1);
    robot.MoveL(J2);
end

fprintf('Moving by pose...\n');
% Follow these steps to retreive a pose:
% 1-Double click a robot
% 2-Copy the pose of the Tool frame with respect to the User Frame (as a Matrix)
% 3-Paste it here
H1 = [    -0.492404,    -0.642788,    -0.586824,  -101.791308 ;
     -0.413176,     0.766044,    -0.492404,  1265.638417 ;
      0.766044,     0.000000,    -0.642788,   117.851733 ;
      0.000000,     0.000000,     0.000000,     1.000000 ];

H2 = [    -0.759717,    -0.280123,    -0.586823,  -323.957442 ;
      0.060192,     0.868282,    -0.492405,   358.739694 ;
      0.647462,    -0.409410,    -0.642787,   239.313006 ;
      0.000000,     0.000000,     0.000000,     1.000000 ];

for i=1:2
    robot.MoveJ(H1);
    robot.MoveL(H2);
end
Moving by target item...
Moving by joints...
Moving by pose...

Calculate forward and inverse kinematics of a robot

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

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

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

% Calculate all 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
RL.ShowSequence(joints3_all);

pause(1);
% Make joints 4 the solution to reach the target off by 100 mm in Z
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:
  -56.8091
  -22.9202
   33.5814
   47.6220
   54.6902
 -104.7797

Calculated pose for current joints:
   -0.5868   -0.2801    0.7597  305.0477
   -0.4924    0.8683   -0.0602 -394.7465
   -0.6428   -0.4094   -0.6475  978.1476
         0         0         0    1.0000

Calculated robot joints from pose:
  -56.8091
  -22.9202
   33.5814
   47.6220
   54.6902
 -104.7797

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

  -56.8091  -56.8091  123.1909  123.1909  -56.8091  -56.8091  123.1909
  -22.9202  -22.9202   -3.2918   -3.2918  -22.9202  -22.9202   -3.2918
   33.5814   33.5814 -192.6814 -192.6814   33.5814   33.5814 -192.6814
   47.6220 -132.3780 -129.3428   50.6572   47.6220 -132.3780 -129.3428
   54.6902  -54.6902   51.2124  -51.2124   54.6902  -54.6902   51.2124
 -104.7797   75.2203 -109.8131   70.1869  255.2203 -284.7797  250.1869

  Column 8

  123.1909
   -3.2918
 -192.6814
   50.6572
  -51.2124
 -289.8131