G-Code Reader not work and say error
1 view (last 30 days)
Show older comments
i try to make it plot gcode not work
i think it not out loop or anther somthing because it say this error
i attached gcode file to make it work
code :
function toolPath = gCodeReader(filepath, dist_res, angle_res, plot_path, verbose)
filepath='Downloads\test (5).txt'
%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('test (5).txt',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}(1) == 'X'
current_pos(1) = str2num(splitLine{i}(2:end));
elseif splitLine{i}(1) == 'Y'
current_pos(2) = str2num(splitLine{i}(2:end));
elseif splitLine{i}(1) == 'Z'
current_pos(3) = str2num(splitLine{i}(2:end));
elseif splitLine{i}(1) == 'I'
arc_offsets(1) = str2num(splitLine{i}(2:end));
elseif splitLine{i}(1) == '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),'r-')
end
fclose(raw_gcode_file);
end
0 Comments
Answers (2)
Walter Roberson
on 15 Aug 2022
You need to comment out
toolpath = gCodeReader('test (5).txt',0.5,0.5,1,0);
That line is asking gCodeReader to call itself. Get rid of it.
Then in other code, or at the command line command
toolpath = gCodeReader('test (5).txt',0.5,0.5,1,0);
3 Comments
Jan
on 15 Aug 2022
The error message tells you, that there have been too many fopen() commands without an fclose() on the computer. If this concerns Matlab, this closes formerly opened files:
fclose('all')
Restarting Matlab is an option also.
3 Comments
Jan
on 15 Aug 2022
Your questions match the purpose of this forum exactly and are very welcome.
See Walter's answers: I've overseen, that the function gCodeReader calls itself recursively. Matlab limits the number of recursive calls to 500. But even then the line
raw_gcode_file = fopen(filepath);
is never reached. This means, that the shown error message cannot be produced by the posted code.
Please post the real code and a copy of the complete message, which should contain the line number of the failing command also.
Walter Roberson
on 15 Aug 2022
That error message about too many files open, can happen when MATLAB runs out of memory, or when MATLAB exceeds the call nesting depth. When you reach the recursion limit, you do not always get back a meaningful message.
... and of course it is possible that while the user was debugging previous versions of the routine that the used to have fopen() but their code crashed before fclose() was done, so possibly a one-time fclose('all') would be called for. It would also be a good thing to add in an onCleanup() to ensure that the file always got closed.
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!