classdef Robolink < handle
properties
APPLICATION_DIR = 'C:\RoboDK\bin\RoboDK.exe';
COM = 0;
end
properties(GetAccess = 'private', SetAccess = 'private')
SAFE_MODE = 1;
AUTO_UPDATE = 0;
TIMEOUT = 5;
PORT_START = 20500;
PORT_END = 20510;
PORT = -1;
end
methods
function connected = is_connected(this)
if strcmp(this.COM,'tcpip') ~= 0
connected = 0;
return
end
status = get(this.COM,'status');
if status(1) == 'o'
connected = 1;
end
end
function check_connection(this)
if ~is_connected(this) && Connect(this) ~= 1
error('Unable to connect');
end
flushinput(this.COM);
flushoutput(this.COM);
end
function status = check_status(this)
lastwarn('');
status = fread(this.COM, 1, 'int32');
if ~isempty(lastwarn)
error(lastwarn)
end
if status > 0 && status < 10
strproblems = 'Unknown error';
switch status
case 1
strproblems = 'Invalid item: The item identifier provided is not valid or it does not exist.';
case 2
strproblems = rec_line(this);
warning(strproblems);
status = 0;
return
case 3
strproblems = rec_line(this);
case 9
strproblems = 'Invalid license. Contact us at: www.robodk.org';
otherwise
end
fprintf([strproblems,'\n']);
error(strproblems);
elseif status == 0
else
error('problems running function');
fclose(this.COM);
fopen(this.COM);
validate_connection(this);
end
end
function send_line(this, string)
fprintf(this.COM, string);
end
function string = rec_line(this)
string = [];
chari = fread(this.COM, 1, 'char');
while chari ~= 10
string = [string, chari];
chari = fread(this.COM, 1, 'char');
end
string = char(string);
end
function send_item(this, item)
fwrite(this.COM, item.item(1), 'float64');
end
function item = rec_item(this)
itemnum = fread(this.COM, 1, 'float64');
item = RobolinkItem(this, itemnum);
end
function send_pose(this, pose)
fwrite(this.COM, pose(1:16), 'double');
end
function pose = rec_pose(this)
pose = fread(this.COM, 16, 'double');
pose = reshape(pose,4,4);
end
function send_xyz(this, pos)
fwrite(this.COM, pos(1:3), 'double');
end
function pos = rec_xyz(this)
pos = fread(this.COM, 3, 'double');
pos = reshape(pos,3,1);
end
function send_int(this, num)
if numel(num) > 1
num = num(1);
end
fwrite(this.COM, num, 'int32');
end
function num = rec_int(this)
num = fread(this.COM, 1, 'int32');
end
function send_array(this, values)
nval = numel(values);
send_int(this, nval);
if nval > 0
valok = reshape(values,nval,1);
fwrite(this.COM, valok, 'double');
end
end
function values = rec_array(this)
nvalues = rec_int(this);
if nvalues > 0
values = fread(this.COM, nvalues, 'double');
else
values = [];
end
end
function send_matrix(this, mat)
size1 = size(mat,1);
size2 = size(mat,2);
send_int(this,size1);
send_int(this,size2);
for j=1:size2
for i=1:size1
fwrite(this.COM,mat(i,j),'float64');
end
end
end
function mat = rec_matrix(this)
size1 = rec_int(this);
size2 = rec_int(this);
if size1*size2 > 0
mat = fread(this.COM, size1*size2, 'double');
mat = reshape(mat, size2, size1)';
else
mat = [];
end
end
function moveX(this, target, itemrobot, movetype, blocking)
if nargin < 5
blocking = 300;
end
itemrobot.WaitMove();
command = 'MoveX';
send_line(this, command);
send_int(this, movetype);
if numel(target) == 1
send_int(this,3);
send_array(this,[]);
send_item(this,target);
elseif size(target,1) == 1 || size(target,2) == 1
send_int(this, 1);
send_array(this, target);
send_item(this, RobolinkItem(this, 0));
elseif numel(target) == 16
send_int(this, 2);
send_array(this, target);
send_item(this, RobolinkItem(this, 0));
else
error('Invalid input values');
end
send_item(this, itemrobot);
check_status(this);
if blocking
itemrobot.WaitMove();
end
end
end
methods
function this = Robolink()
Connect(this);
end
function ok = Set_connection_params(this, safe_mode, auto_update, timeout)
if nargin > 1
this.SAFE_MODE = safe_mode;
end
if nargin > 2
this.AUTO_UPDATE = auto_update;
end
if nargin > 3
this.TIMEOUT = timeout;
end
fprintf(this.COM, 'CMD_START');
fprintf(this.COM, sprintf('%i %i', this.SAFE_MODE, this.AUTO_UPDATE));
response = rec_line(this);
if strcmp(response, 'READY')
ok = 1;
else
ok = 0;
end
end
function connected = Connect(this)
connected = 0;
for i=1:2
for port=this.PORT_START:this.PORT_END
this.COM = tcpip('localhost', port, 'Timeout', this.TIMEOUT, 'BytesAvailableFcnMode', 'byte', 'InputBufferSize', 4000);
try
fopen(this.COM);
connected = is_connected(this);
if connected
break
end
catch
end
end
if connected
this.PORT = port;
break;
else
try
winopen(this.APPLICATION_DIR);
pause(2);
catch
error(['application path is not correct or could not start: ',this.APPLICATION_DIR]);
end
end
end
if connected && ~Set_connection_params(this)
connected = 0;
end
end
end
methods
function item = Item(this, name)
check_connection(this);
command = 'G_Item';
send_line(this, command);
send_line(this, name);
item = rec_item(this);
check_status(this);
end
function list = ItemList(this)
check_connection(this);
command = 'G_List_Items';
send_line(this, command);
count = rec_int(this);
list = cell(1,count);
for i=1:count
namei = rec_line(this);
fprintf('%i\t->\t%s\n',i,namei);
list{i} = namei;
end
check_status(this);
end
function Copy(this, item)
if nargin < 2
item = RobolinkItem(this);
end
check_connection(this);
command = 'Copy';
send_line(this, command);
send_item(this, item);
check_status(this);
end
function newitem = Paste(this, toparent)
if nargin < 2
toparent = RobolinkItem(this);
end
check_connection(this);
command = 'Paste';
send_line(this, command);
send_item(this, toparent);
newitem = rec_item(this);
check_status(this);
end
function newitem = AddFile(this, filename, parent)
check_connection(this);
if nargin < 3
parent = RobolinkItem(this);
end
command = 'Add';
send_line(this, command);
send_line(this, filename);
send_item(this, parent);
newitem = rec_item(this);
check_status(this);
end
function newitem = AddTarget(this, name, itemparent, itemrobot)
if nargin < 3
itemparent = RobolinkItem(this);
end
if nargin < 4
itemrobot = RobolinkItem(this);
end
check_connection(this);
command = 'Add_TARGET';
send_line(this, command);
send_line(this, name);
send_item(this, itemparent);
send_item(this, itemrobot);
newitem = rec_item(this);
check_status(this);
end
function newitem = AddFrame(this, name, itemparent)
if nargin < 3
itemparent = RobolinkItem(this);
end
check_connection(this);
command = 'Add_FRAME';
send_line(this, command);
send_line(this, name);
send_item(this, itemparent);
newitem = rec_item(this);
check_status(this);
end
function newitem = AddProgram(this, name, itemrobot)
if nargin < 3
itemrobot = RobolinkItem(this);
end
check_connection(this);
command = 'Add_PROG';
send_line(this, command);
send_line(this, name);
send_item(this, itemrobot);
newitem = rec_item(this);
check_status(this);
end
function prog_status = RunProgram(this, fcn_param)
prog_status = this.RunCode(fcn_param, 1);
end
function prog_status = RunCode(this, code, code_is_fcn_call)
if nargin < 3
code_is_fcn_call = 0;
end
check_connection(this);
command = 'RunCode';
send_line(this, command);
send_int(this, code_is_fcn_call);
send_line(this, strrep(strrep(code,'\r\n','<<br>>'),'\n','<<br>>'))
prog_status = rec_int(this);
check_status(this);
end
function prog_status = RunMessage(this, message, message_is_comment)
if nargin < 3
message_is_comment = 0;
end
check_connection(this);
command = 'RunMessage';
send_line(this, command);
send_int(this, message_is_comment);
send_line(this, strrep(strrep(message,'\r\n','<<br>>'),'\n','<<br>>'))
prog_status = rec_int(this);
check_status(this);
end
function Render(this, always_render)
if nargin < 2
always_render = 0;
end
auto_render = int32(~always_render);
check_connection(this);
command = 'Render';
send_line(this, command);
send_int(this, auto_render);
check_status(this);
end
function ncollisions = Collision(this)
check_connection(this);
command = 'Collision';
send_line(this, command);
ncollisions = rec_int(this);
check_status(this);
end
function setSimulationSpeed(this, speed)
check_connection(this);
command = 'SimulateSpeed';
send_line(this, command);
send_int(this, speed*1000);
check_status(this);
end
function setRunMode(this, run_mode)
if nargin < 2
run_mode = 1;
end
check_connection(this);
command = 'S_RunMode';
send_line(this, command);
send_int(this, run_mode);
check_status(this);
end
function run_mode = RunMode(this)
check_connection(this);
command = 'G_RunMode';
send_line(this, command);
run_mode = rec_int(this);
check_status(this);
end
function value = getParam(this, param)
if nargin < 2
param = 'PATH_OPENSTATION';
end
check_connection(this);
command = 'G_Param';
send_line(this, command);
send_line(this, param);
value = rec_line(this);
check_status(this);
end
function ShowSequence(this, matrix)
RobolinkItem(this, 0).ShowSequence(matrix);
end
function xyz = LaserTracker_Measure(this, estimate, search)
if nargin < 2
estimate = [0; 0; 0];
end
if nargin < 3
search = 0;
end
check_connection(this);
command = 'MeasLT';
send_line(this, command);
send_xyz(this, estimate);
send_int(this, search);
xyz = rec_xyz(this);
check_status(this);
end
function [collision, itempicked, xyz] = Collision_Line(this, p1, p2, ref)
if nargin < 4
ref = eye(4);
end
p1abs = ref*[p1(1:3);1];
p2abs = ref*[p2(1:3);1];
check_connection(this);
command = 'CollisionLine';
send_line(this, command);
send_xyz(this, p1abs);
send_xyz(this, p2abs);
itempicked = rec_item(this);
xyz = rec_xyz(this);
collision = itempicked.Valid();
check_status(this);
end
end
end