How can i fix Index exceeds the number of array elements (1) error

1 次查看(过去 30 天)
Hi,
i downloaded this simple g-code reader earlier from the site to plot a set of g-codes that i have created on SolidCAM. However, everytime i run it, it keeps giving this error
Index exceeds the number of array elements (1).
Error in gCodeReader_3 (line 75)
if splitLine{i}(2) == 'X'
function toolPath = gCodeReader_3(filepath, dist_res, angle_res, plot_path, verbose)
%gCodeReader Function that takes a G-Code file and outputs the tool path
% for plotting/analysis. Not a complete analysis of the whole file, but
% more or less the basic motions.
% Inputs:
% - path to G-Code file
% - point spacing for linear motion (mm or inches, I guess)
% - point spacing for arc motion (degrees)
% - Plot the current path (1 or 0)
% - Output raw G-Code to console
% Outputs:
% - The interpolated tool path
% Notes:
% - This is not at all complete, but should work well enough for
% simple CNC G-Code. If you need anything more complex, I'd suggest
% you implement it yourself, as this was more or less all I needed
% at the time.
% - I have also done zero optimization.
% - This comes with no guarantees or warranties whatsoever, but I
% hope it's useful for someone.
%
% Example usage:
% toolpath = gCodeReader('simplePart.NC',0.5,0.5,1,0);
%
% Tom Williamson
% 18/06/2018
raw_gcode_file = fopen(filepath);
% Modes
Rapid_positioning = 0;
Linear_interpolation = 1;
CW_interpolation = 2;
CCW_interpolation = 3;
current_mode = NaN;
% Initialize variables
current_pos = [0,0,0];
toolPath = [];
arc_offsets = [0,0,0];
interp_pos = [];
while ~feof(raw_gcode_file)
tline = fgetl(raw_gcode_file);
% Check if its an instruction line
if tline(1) == 'N'
arc_offsets = [0,0,0];
tline = tline(6:end);
splitLine = strsplit(tline,' ');
for i = 1:length(splitLine)
if verbose == 1
disp(splitLine{i});
end
% Check what the command is (only the main ones are
% implemented i.e. G0 - G3)
if strcmp(splitLine{i}, 'G0')
if verbose == 1
disp('Rapid positioning')
end
current_mode = Rapid_positioning;
elseif strcmp(splitLine{i}, 'G1')
if verbose == 1
disp('Linear interpolation')
end
current_mode = Linear_interpolation;
elseif strcmp(splitLine{i}, 'G2')
if verbose == 1
disp('Circular interpolation, clockwise')
end
current_mode = CW_interpolation;
elseif strcmp(splitLine{i}, 'G3')
if verbose == 1
disp('Circular interpolation, counterclockwise')
end
current_mode = CCW_interpolation;
else
if splitLine{i}(2) == 'X'
current_pos(1) = str2num(splitLine{i}(2:end));
elseif splitLine{i}(2) == 'Y'
current_pos(2) = str2num(splitLine{i}(2:end));
elseif splitLine{i}(2) == 'Z'
current_pos(3) = str2num(splitLine{i}(2:end));
elseif splitLine{i}(2) == 'I'
arc_offsets(1) = str2num(splitLine{i}(2:end));
elseif splitLine{i}(2) == 'J'
arc_offsets(2) = str2num(splitLine{i}(2:end));
end
end
end
% Check the current mode and calculate the next points along the
% path: linear modes
if current_mode == Linear_interpolation || current_mode == Rapid_positioning
if length(toolPath > 0)
interp_pos = [linspace(toolPath(end,1),current_pos(1),100)',linspace(toolPath(end,2),current_pos(2),100)',linspace(toolPath(end,3),current_pos(3),100)'];
dist = norm((current_pos - toolPath(end,:)));
if dist > 0
dire = (current_pos - toolPath(end,:))/dist;
interp_pos = toolPath(end,:) + dire.*(0:dist_res:dist)';
interp_pos = [interp_pos;current_pos];
end
else
interp_pos = current_pos;
end
% Check the current mode and calculate the next points along the
% path: arc modes, note that this assumes the arc is in the X-Y
% axis only
elseif current_mode == CW_interpolation
center_pos = toolPath(end,:) + arc_offsets;
v1 = (toolPath(end,1:2)-center_pos(1:2));
v2 = (current_pos(1:2)-center_pos(1:2));
r = norm(current_pos(1:2)-center_pos(1:2));
angle_1 = atan2d(v1(2),v1(1));
angle_2 = atan2d(v2(2),v2(1));
if angle_2 > angle_1
angle_2 = angle_2-360;
end
interp_pos = [center_pos(1:2) + [cosd(angle_1:-angle_res:angle_2)',sind(angle_1:-angle_res:angle_2)']*r, linspace(center_pos(3),current_pos(3),length(angle_1:-angle_res:angle_2))'];
interp_pos = [interp_pos;current_pos];
elseif current_mode == CCW_interpolation
center_pos = toolPath(end,:) + arc_offsets;
v1 = (toolPath(end,1:2)-center_pos(1:2));
v2 = (current_pos(1:2)-center_pos(1:2));
r = norm(current_pos(1:2)-center_pos(1:2));
angle_1 = atan2d(v1(2),v1(1));
angle_2 = atan2d(v2(2),v2(1));
if norm(v1) <0.1
angle_1 = 0;
end
if norm(v2) <0.1
angle_2 = 0;
end
if angle_2 < angle_1
angle_2 = angle_2+360;
end
interp_pos = [center_pos(1:2) + [cosd(angle_1:angle_res:angle_2)',sind(angle_1:angle_res:angle_2)']*r, linspace(center_pos(3),current_pos(3),length(angle_1:angle_res:angle_2))'];
interp_pos = [interp_pos;current_pos];
end
toolPath = [toolPath;interp_pos];
end
end
% Plot if requested
if plot_path
plot3(toolPath(:,1),toolPath(:,2),toolPath(:,3),'b-')
end
fclose(raw_gcode_file);
end
Can someone please help, very new to MATLAB so i don't know what's wrong
I've attached the gcode file i trying to plot

回答(1 个)

OKAN TOPCU
OKAN TOPCU 2021-2-4
I am very new to G Code files and i would like to learn how i can provide 'dist_res' and 'angle_res' values as input to the function. I am really confused. It would be really kind of you if you could help me.
  1 个评论
Shreyas Taware
Shreyas Taware 2021-4-27
angle_res is basically an angular step in the clockwise/ anticlockwise direction specified. If you notice variable 'dire' is a unit vector which is then multiplied by the distance that varies from 0 to d. If you go step-by-step, I think you should be able to get what the code is intending to say.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by