Zoay 2020-10-09 22:04 采纳率: 0%
浏览 89

想请问 matlab 使用 gcode 跑加工路径的问题

想请问 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.')
  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-25 08:58
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    clear all; close all; clc
    
    % Define some constants
    dist_res = 0.5; % Distance resolution
    angle_res = 0.5; % Angle resolution
    
    % Read the NC code file
    gcode_file = fopen('R.NC');
    gcode_str = fread(gcode_file);
    
    % Find the first instruction line
    instr_line = find(strcmp(gcode_str, 'G0'));
    
    % Set up the start position
    start_pos = [0,0,0]; % Start position
    
    % Calculate the total number of steps needed
    total_steps = instr_line - 1;
    
    % Loop through each step and calculate the corresponding position
    for step = 1:total_steps
        % Get the current position
        current_pos = str2num(fread(gcode_file, instr_line(step)));
    
        % Determine the current mode based on the current position
        switch current_pos(3)
            case 0
                current_mode = Linear_interpolation;
            case 1
                current_mode = Rapid_positioning;
            default
                current_mode = CCW_interpolation;
        end
    
        % If it's a circular interpolation mode, check the direction
        if current_mode == CCW_interpolation
            if current_pos(1) < 0 && current_pos(2) < 0
                current_dir = 1;
            elseif current_pos(1) < 0 && current_pos(2) > 0
                current_dir = -1;
            elseif current_pos(1) > 0 && current_pos(2) < 0
                current_dir = -1;
            else
                current_dir = 1;
            end
        end
    
        % If the current mode is not Linear or Rapid, skip this step
        if current_mode != Linear_interpolation && current_mode != Rapid_positioning
            continue;
        end
    
        % If the current mode is Linear or Rapid, calculate the next step
        if current_mode == Linear_interpolation
            next_pos = current_pos + current_dir * dist_res;
        elseif current_mode == Rapid_positioning
            next_pos = current_pos + current_dir * dist_res;
        end
    
        % Write the next position to the file
        fprintf(gcode_file, sprintf('G0 %f %f %f\n', next_pos(1), next_pos(2), next_pos(3)));
        instr_line = instr_line + 1;
    end
    
    fclose(gcode_file); % Close the file
    
    disp('Done!');
    

    这个脚本会从NC代码文件中读取指令,然后根据当前的路径模式和当前位置来计算下一个位置。请注意,你需要将R.NC替换为你的实际NC代码文件名。此外,这个脚本仅适用于线性或快速插值的循环轨迹。如果你的轨迹包含更多的控制点,你可能需要调整代码以适应这些变化。

    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵