I was trying out the MatlabAPI to test keyboard input for simulated robot Control. I was getting
I used the try and catch method to handle the exception, but the robot does not move.
The only way the above code works and the robot moves is when under Matlab/RoboLinkItem.m, I comment the Timeout lines under the function WaitMove():
But then, when I register a very frequent keypresses, I get a Unknown error as follows:
The Unknown error is more of an warning than an error, as the robot jitters/jerks and continues with next keypresses.
Can I request a solution rather than this workaround?
Quote:"No public property 'TIMEOUT' for class 'Robolink'."error when implementing the python version in Matlab. Before running the below code, a robot must be in the Station in RoboDK. I use the latest version of RoboDK v5.8.1.24728 and Matlab R2024b.
Code:
% Initialize RoboDK API
RDK = Robolink();
% Get a robot
robot = RDK.Item('', RDK.ITEM_TYPE_ROBOT);
if ~robot.Valid()
disp('No robot in the station. Load a robot first, then run this program.');
pause(5);
error('No robot in the station!');
end
disp(['Using robot: ', robot.Name()]);
disp('Use the arrows (left, right, up, down), Q and A keys to move the robot');
disp('Note: This works on console mode only, you must run the MATLAB file separately');
% Define keypress callback function
function keyPressCallback(~, event, robot)
move_direction = [0, 0, 0];
switch event.Key
case 'leftarrow'
disp('arrow left (Y-)');
move_direction = [0, -1, 0];
case 'rightarrow'
disp('arrow right (Y+)');
move_direction = [0, 1, 0];
case 'uparrow'
disp('arrow up (X-)');
move_direction = [-1, 0, 0];
case 'downarrow'
disp('arrow down (X+)');
move_direction = [1, 0, 0];
case 'q'
disp('Q (Z+)');
move_direction = [0, 0, 1];
case 'a'
disp('A (Z-)');
move_direction = [0, 0, -1];
end
% Make sure that a movement direction is specified
if norm(move_direction) <= 0
return;
end
% Calculate the movement in mm according to the movement speed
xyz_move = move_direction * 10; % move_speed;
disp(['xyz_move: ', mat2str(xyz_move)]);
% Get the robot joints
robot_joints = robot.Joints();
if isempty(robot_joints)
disp('Failed to retrieve robot joints.');
return;
end
disp(['robot_joints: ', mat2str(robot_joints)]);
% Get the robot position from the joints (calculate forward kinematics)
robot_position = robot.SolveFK(robot_joints);
if isempty(robot_position)
disp('Failed to calculate robot position.');
return;
end
disp(['robot_position: ', mat2str(robot_position)]);
% Get the robot configuration (robot joint state)
robot_config = robot.JointsConfig(robot_joints);
if isempty(robot_config)
disp('Failed to retrieve robot configuration.');
return;
end
disp(['robot_config: ', mat2str(robot_config)]);
% Calculate the new robot position
new_robot_position = transl(xyz_move(1), xyz_move(2), xyz_move(3)) * robot_position;
disp(['new_robot_position: ', mat2str(new_robot_position)]);
% Calculate the new robot joints
new_robot_joints = robot.SolveIK(new_robot_position);
if isempty(new_robot_joints)
disp('No robot solution!! The new position is too far, out of reach or close to a singularity');
return;
end
disp(['new_robot_joints: ', mat2str(new_robot_joints)]);
% Calculate the robot configuration for the new joints
new_robot_config = robot.JointsConfig(new_robot_joints);
if isempty(new_robot_config)
disp('Failed to retrieve new robot configuration.');
return;
end
disp(['new_robot_config: ', mat2str(new_robot_config)]);
try
% Move the robot joints to the new position
robot.MoveL(new_robot_joints);
catch ME
disp(['Error moving robot: ', ME.message]);
end
% Process any pending callbacks
drawnow;
end
% Set up figure to capture keypress events
fig = figure('KeyPressFcn', @(src, event) keyPressCallback(src, event, robot));
% Keep the script running without blocking indefinitely
while ishandle(fig)
drawnow;
end
The only way the above code works and the robot moves is when under Matlab/RoboLinkItem.m, I comment the Timeout lines under the function WaitMove():
Code:
function WaitMove(this, timeout)
% Waits (blocks) until the robot finished its move.
% In 1 (optional) timeout -> Max time to wait for robot to finish its movement (in seconds)
if nargin < 2
timeout = 300;
end
this.link.check_connection();
command = 'WaitMove';
this.link.send_line(command);
this.link.send_item(this);
this.link.check_status();
% Commenting the following lines
%set(this.link.COM, 'Timeout', timeout);
%this.link.check_status();
%set(this.link.COM, 'Timeout', this.link.TIMEOUT);
end
But then, when I register a very frequent keypresses, I get a Unknown error as follows:
Code:
Unknown error
Error using Robolink/check_status (line 139)
Unknown error
Error in RobolinkItem/Joints (line 639)
this.link.check_status();
^^^^^^^^^^^^^^^^^^^^^^^^
Error in keypress>keyPressCallback (line 50)
robot_joints = robot.Joints();
^^^^^^^^^^^^^^
Error in keypress>@(src,event)keyPressCallback(src,event,robot) (line 105)
fig = figure('KeyPressFcn', @(src, event) keyPressCallback(src, event, robot));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error using keypress>keyPressCallback (line 101)
Error while evaluating Figure KeyPressFcn.
The Unknown error is more of an warning than an error, as the robot jitters/jerks and continues with next keypresses.
Can I request a solution rather than this workaround?