想请问 matlab 使用 gcode 跑加工路径的问题
最近在研究如何将NC代码在matlab上计算,并生成座标
当读取NC代码时,疑似在读取资料发生错误
由于已知前两行X,Y资讯(50.52行)的时候从未发生任何错误,
错误发生在读取Z轴座标(54行,与前者相同),
有试着将Z轴不读取,直接尝试执行程式,结果成功了,
想请问大家在读取Z轴座标时发生了什么错误?
clc; clear; close All
% initialization
filepath = 'R.NC';
dist_res = 0.5;
angle_res = 0.5;
plot_path = 1;
verbose = 0;
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)
% get line from NC code
tline = fgetl(raw_gcode_file);
% Check if its an instruction line
if tline(1) == 'N'
arc_offsets = [0,0,0];
% read from the 6th place
tline = tline(6:end);
% split the string to the vector
splitLine = strsplit(tline,' ');
else
arc_offsets = [0,0,0];
% read from the 6th place
tline = tline(1:end);
% split the string to the vector
splitLine = strsplit(tline,' ');
end
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));
elseif splitLine{i}(1) == 'K'
arc_offsets(3) = 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
% Plot if requested
plot3(toolPath(:,1),toolPath(:,2),toolPath(:,3),'b.')