1. set_current_coord - 切换当前坐标系
方法: set_current_coord(socket_fd, coord)
参数:
socket_fd:连接句柄
coord:坐标系类型(0=关节,1=直角,2=工具,3=用户)
返回值:
使用示例:
Python
nrc.set_current_coord(fd, coord=0) # 切换到关节坐标系
2. get_current_coord - 获取当前坐标系
方法: get_current_coord(socket_fd)
参数:
返回值:
使用示例:
Python
coord = nrc.get_current_coord(fd)
print(f"当前坐标系: {coord}")
3. get_global_position - 查询全局点位
方法: get_global_position(socket_fd, pos_name)
参数:
socket_fd:连接句柄
pos_name:点位名称(如"GP0001")
返回值:
List[float]:14个值的点位数据,前7位为坐标属性,后7位为实际坐标值
使用示例:
Python
import nrc_interface as nrc
fd = nrc.connect_robot("192.168.1.13", "6001")
if fd < 0:
raise RuntimeError("连接失败")
# 查询全局点位 "GP0001"
pos = nrc.get_global_position(fd, "GP0001")
if pos:
print("点位数据(14个值):")
print(f" 坐标属性: {pos[:7]}") # 前7位:坐标系、角度/弧度、形态、工具号、用户号等
print(f" 关节/直角坐标: {pos[7:]}") # 后7位:实际坐标值
else:
print("获取点位失败,请检查点位是否存在或网络连接")
nrc.disconnect_robot(fd)
4. set_global_position - 设置全局点位
方法: set_global_position(socket_fd, pos_name, pos_info)
参数:
socket_fd:连接句柄
pos_name:点位名称(如"GP0001")
pos_info:14个值的点位数据列表
返回值:
使用示例:
Python
import nrc_interface as nrc
# 连接控制器
fd = nrc.connect_robot("192.168.1.13", "6001")
if fd < 0:
raise RuntimeError("连接失败")
# 构建点位数据:关节坐标,角度制,形态0,工具1,用户0
# 目标关节角度:J1=10°, J2=20°, J3=30°,其余为0
pos_info = [
0, # 坐标系:关节
0, # 角度制
0, # 形态
1, # 工具坐标序号
0, # 用户坐标序号
0, 0, # 预留
10.0, 20.0, 30.0, 0.0, 0.0, 0.0, 0.0 # 7个关节角度
]
# 写入全局点位 GP0001
ret = nrc.set_global_position(fd, "GP0001", pos_info)
if ret == nrc.SUCCESS:
print("全局点位设置成功")
else:
print(f"设置失败,错误码: {ret}")
nrc.disconnect_robot(fd)
5. set_global_variant / get_global_variant - 全局变量
方法: set_global_variant(socket_fd, var_name, value)、get_global_variant(socket_fd, var_name)
参数:
socket_fd:连接句柄
var_name:变量名称(如"GD001"、"GI001"、"GB001")
value:变量值(浮点数)
返回值:
使用示例:
Python
import nrc_interface as nrc
fd = nrc.connect_robot("192.168.1.13", "6001")
if fd < 0:
raise RuntimeError("连接失败")
# 1. 设置浮点变量 GD001 = 3.14159
ret = nrc.set_global_variant(fd, "GD001", 3.14159)
print("GD001 设置结果:", ret)
# 2. 设置整数变量 GI001 = 100
ret = nrc.set_global_variant(fd, "GI001", 100.0) # 传入浮点也可以,控制器会取整
print("GI001 设置结果:", ret)
# 3. 设置布尔变量 GB001 = True (1.0)
ret = nrc.set_global_variant(fd, "GB001", 1.0)
print("GB001 设置结果:", ret)
# 验证读取(使用 get_global_variant)
val = nrc.get_global_variant(fd, "GD001")
print(f"GD001 当前值: {val}")
nrc.disconnect_robot(fd)