Skip to content

十、码垛工艺(nrc_craft_pallet.h)

码垛工艺接口提供码垛运行状态设置与查询、当前运行码垛工艺号获取等功能。支持单机器人和多机器人(_robot 后缀)两种调用方式。


10.1 运行状态控制

pallet_set_running_state / pallet_set_running_state_robot

设置码垛运行状态,指定当前码垛的工艺号、层数和工件数。调用后控制器将按照设定的参数执行码垛作业。

函数签名:

cpp
Result pallet_set_running_state(SOCKETFD socketFd, int craftID, int layerNum, int workpiecesNum);
Result pallet_set_running_state_robot(SOCKETFD socketFd, int robotNum, int craftID, int layerNum, int workpiecesNum);

参数说明:

参数类型输入/输出说明
socketFdSOCKETFD输入连接句柄
robotNumint输入机器人编号(仅 _robot 版本)
craftIDint输入码垛工艺号
layerNumint输入总层数
workpiecesNumint输入每层工件数

返回值: Result 枚举,0 表示成功,-1 表示等待消息失败,-2 表示找不到指定的客户端。

注意事项: 该接口用于启动或重置码垛运行状态,通常在码垛作业开始前调用。工艺号需与控制器中已配置的码垛工艺文件对应。

使用示例:

cpp
// 设置码垛运行状态:工艺号1,共5层,每层4个工件
Result result = pallet_set_running_state(fd, 1, 5, 4);
if (result == 0) {
    printf("码垛运行状态设置成功:工艺号=%d, 层数=%d, 每层工件数=%d\n", 1, 5, 4);
} else {
    printf("码垛运行状态设置失败,错误码: %d\n", result);
}

pallet_get_running_state / pallet_get_running_state_robot

获取码垛当前的运行状态,包括总工件数、总层数、当前层进度等详细信息。

函数签名:

cpp
Result pallet_get_running_state(SOCKETFD socketFd, int craftID, int& totalWpNum, int& totalLayerNum, int& curLayerWpSum, int& curLayerNum, int& curPalletedWpSum, int& curLayerPalletedWpNum);
Result pallet_get_running_state_robot(SOCKETFD socketFd, int robotNum, int craftID, int& totalWpNum, int& totalLayerNum, int& curLayerWpSum, int& curLayerNum, int& curPalletedWpSum, int& curLayerPalletedWpNum);

参数说明:

参数类型输入/输出说明
socketFdSOCKETFD输入连接句柄
robotNumint输入机器人编号(仅 _robot 版本)
craftIDint输入要查询的码垛工艺号
totalWpNumint&输出总工件数
totalLayerNumint&输出总层数
curLayerWpSumint&输出当前层总工件数
curLayerNumint&输出当前层数(正在执行的第几层)
curPalletedWpSumint&输出当前已码总工件数
curLayerPalletedWpNumint&输出当前层已码工件数

返回值: Result 枚举,0 表示成功,-1 表示等待消息失败,-2 表示找不到指定的客户端。

注意事项: 该接口可用于实时监控码垛进度,适合在生产管理系统中周期性查询以更新码垛完成进度。

使用示例:

cpp
// 查询工艺号1的码垛运行状态
int totalWpNum, totalLayerNum, curLayerWpSum;
int curLayerNum, curPalletedWpSum, curLayerPalletedWpNum;

Result result = pallet_get_running_state(fd, 1,
    totalWpNum, totalLayerNum, curLayerWpSum,
    curLayerNum, curPalletedWpSum, curLayerPalletedWpNum);

if (result == 0) {
    printf("码垛运行状态(工艺号1):\n");
    printf("  总工件数: %d\n", totalWpNum);
    printf("  总层数: %d\n", totalLayerNum);
    printf("  当前层总工件数: %d\n", curLayerWpSum);
    printf("  当前层数: %d\n", curLayerNum);
    printf("  已码总工件数: %d\n", curPalletedWpSum);
    printf("  当前层已码工件数: %d\n", curLayerPalletedWpNum);

    // 计算完成进度
    if (totalWpNum > 0) {
        float progress = (float)curPalletedWpSum / totalWpNum * 100;
        printf("  完成进度: %.1f%%\n", progress);
    }
}

10.2 工艺号查询

pallet_get_current_run_craftid / pallet_get_current_run_craftid_robot

获取当前运行的作业文件中所包含的所有码垛工艺号列表。

函数签名:

cpp
Result pallet_get_current_run_craftid(SOCKETFD socketFd, std::vector<int>& craftIds);
Result pallet_get_current_run_craftid_robot(SOCKETFD socketFd, int robotNum, std::vector<int>& craftIds);

参数说明:

参数类型输入/输出说明
socketFdSOCKETFD输入连接句柄
robotNumint输入机器人编号(仅 _robot 版本)
craftIdsstd::vector<int>&输出码垛工艺号列表

返回值: Result 枚举,0 表示成功,-1 表示等待消息失败,-2 表示找不到指定的客户端。

注意事项: 一个作业文件中可能包含多个码垛工艺,该接口返回当前作业文件中所有已配置的码垛工艺号。可用于动态识别当前作业需要执行哪些码垛任务。

使用示例:

cpp
// 获取当前运行的码垛工艺号列表
std::vector<int> craftIds;
Result result = pallet_get_current_run_craftid(fd, craftIds);
if (result == 0) {
    printf("当前作业包含 %zu 个码垛工艺:\n", craftIds.size());
    for (size_t i = 0; i < craftIds.size(); i++) {
        printf("  工艺号 %d: %d\n", (int)(i + 1), craftIds[i]);
    }
} else {
    printf("获取码垛工艺号失败,错误码: %d\n", result);
}

10.3 完整码垛流程示例

cpp
#include "nrc_craft_pallet.h"
#include "nrc_interface.h"
#include <stdio.h>

int main() {
    // 1. 连接机器人
    SOCKETFD fd = connect_robot("192.168.1.15", "6001");
    if (fd <= 0) {
        printf("连接失败\n");
        return -1;
    }

    // 2. 获取当前作业包含的码垛工艺号
    std::vector<int> craftIds;
    Result ret = pallet_get_current_run_craftid(fd, craftIds);
    if (ret == 0 && !craftIds.empty()) {
        printf("检测到 %zu 个码垛工艺\n", craftIds.size());
    }

    // 3. 设置码垛运行状态
    int craftID = 1;       // 工艺号
    int layers = 3;        // 3层
    int wpPerLayer = 6;    // 每层6个工件
    ret = pallet_set_running_state(fd, craftID, layers, wpPerLayer);
    if (ret != 0) {
        printf("码垛状态设置失败\n");
        return -1;
    }
    printf("码垛任务启动:%d层 x %d件/层 = %d\n", layers, wpPerLayer, layers * wpPerLayer);

    // 4. 启动码垛作业文件
    // run_job_file(fd, "pallet_job_001");

    // 5. 监控码垛进度
    bool running = true;
    while (running) {
        int totalWpNum, totalLayerNum, curLayerWpSum;
        int curLayerNum, curPalletedWpSum, curLayerPalletedWpNum;

        ret = pallet_get_running_state(fd, craftID,
            totalWpNum, totalLayerNum, curLayerWpSum,
            curLayerNum, curPalletedWpSum, curLayerPalletedWpNum);

        if (ret == 0) {
            printf("\r进度: 第%d/%d层, 已码%d/%d件",
                   curLayerNum, totalLayerNum,
                   curPalletedWpSum, totalWpNum);
            fflush(stdout);

            // 判断是否全部完成
            if (curPalletedWpSum >= totalWpNum && totalWpNum > 0) {
                running = false;
                printf("\n码垛全部完成!\n");
            }
        }
        usleep(500000);  // 500ms 查询一次
    }

    printf("码垛流程结束\n");
    return 0;
}