Skip to content

Python SDK API 参考

Python 接口基于 C++ SDK 通过 SWIG 自动生成,所有函数名、参数和逻辑与 C++ 版本完全一致。详细参数说明请对照 C++ API 文档


导入方式

python
import nrc_interface as nrc

也可直接 import nrc_interface,不加别名。纳博特 Python Demo 中常用 nrc_interface as aa


Python 与 C++ 对照

类型映射

C++ 类型Python 类型说明
SOCKETFD (int)intsocket 文件描述符
Result (int)int返回状态码,0=成功
std::stringstr字符串
std::vector<double>VectorDouble须先创建再 append,或传入 list
std::vector<std::vector<double>>VectorVectorDouble二维数组
std::vector<int>VectorInt整数数组
std::vector<std::string>VectorString字符串数组

返回值差异(重要)

C++ 通过引用参数输出数据:

cpp
int status;
get_servo_state(fd, status);  // status 被填充

Python 通过元组同时返回结果码和输出值:

python
status = 0
result, status = nrc.get_servo_state(fd, status)  # 返回 (结果码, 输出值)

每次调用 getter 前必须重新初始化变量,因为 Python 端通过引用修改原始对象的值。

结构体

C++ 结构体在 Python 中为同名类,字段名保持一致:

python
# C++ MoveCmd → Python MoveCmd
cmd = nrc.MoveCmd()
cmd.coord = 0
cmd.velocity = 80
cmd.acc = 100
cmd.targetPosType = nrc.PosType_data
cmd.targetPosValue = pos   # VectorDouble

枚举常量

通过模块级常量访问:

python
nrc.PosType_data    # 等同于 C++ 的 PosType::data
nrc.SUCCESS         # 等同于 C++ 的 Result::SUCCESS

API 模块速查

所有 C++ 接口均可通过 nrc_interface. 直接调用。以下列出主要模块,详细参数说明参见 C++ 文档。

连接管理

函数说明C++ 参考
connect_robot(ip, port) → int连接控制器C++ connect_robot
disconnect_robot(fd) → Result断开连接同上
get_connection_status(fd) → Result获取连接状态同上
get_library_version() → str获取 SDK 版本同上

伺服控制

函数说明C++ 参考
clear_error(fd) → Result清错同上
set_servo_state(fd, state) → Result设置伺服状态 0=停止 1=就绪同上
get_servo_state(fd, status) → (Result, int)获取伺服状态同上
set_servo_poweron(fd) / poweroff(fd) → Result上电/下电同上
set_current_mode(fd, mode) → Result0=示教 1=远程 2=运行同上
get_current_mode(fd, mode) → (Result, int)获取当前模式同上
set_speed(fd, speed) → Result速度 1~100同上
get_speed(fd, speed) → (Result, int)获取速度同上

位置与状态

函数说明C++ 参考
get_current_position(fd, coord, pos) → Resultcoord: 0=关节 1=直角 2=工具 3=用户同上
get_current_extra_position(fd, pos) → Result外部轴位置同上
get_robot_running_state(fd, status) → (Result, int)0=停止 1=暂停 2=运行同上

工具手与标定

函数说明C++ 参考
get_tool_hand_number(fd, num) → (Result, int)获取工具手编号同上
get_tool_hand_param(fd, num, param) → Resultparam 为 ToolParam 对象同上
tool_hand_2_or_20_point_calibrate(fd, ...) → Result2/20 点标定同上

运动控制

函数说明C++ 参考
robot_movej(fd, cmd) → ResultMoveJ 关节运动同上
robot_movel(fd, cmd) → ResultMoveL 直线运动同上
robot_moves(fd, pos_list, vel, coord, acc, dec) → Result多段直线同上
robot_start_jogging(fd, axis, positive) → Result开始点动同上
robot_stop_jogging(fd, axis) → Result停止点动同上

队列运动

函数说明C++ 参考
queue_motion_set_status(fd, enable) → Result开启/关闭队列模式C++ 队列运动
queue_motion_push_back_moveJ(fd, cmd) → Result插入 MoveJ 指令同上
queue_motion_push_back_moveL(fd, cmd) → Result插入 MoveL 指令同上
queue_motion_send_to_controller(fd, count) → Result发送 count 条指令同上
queue_motion_clear_Data(fd) → Result清空队列同上

伺服跟踪 (7000端口)

函数说明C++ 参考
servo_move(fd, para) → Result需要单独连接 7000 端口同上
enable_servo_position_motion_control(fd, enable) → Result使能伺服位置控制同上

作业文件

函数说明C++ 参考
job_run_times(fd, times) → Result0=无限循环C++ 作业文件
job_upload_by_file(fd, path) → Result上传作业文件同上
job_download_by_directory(fd, path) → Result下载作业文件同上

消息回调

函数说明C++ 参考
set_receive_error_or_warnning_message_callback(fd, callback) → Result错误/警告回调同上
recv_message(fd, callback) → Result消息接收回调同上

IO / 工艺 / Modbus

参见 C++ IO 控制C++ 焊接 等,Python 函数名与 C++ 一致。


完整示例

参见 Python 接口示例渐入式教程