一、简介
在使用SDK操作机器人时,机器人常常会遇到奇异点、碰撞告警等问题,导致运行暂停,这时候,可以使用回调处理这类问题
二、操作流程
1、实例化EliteDriverConfig类,指定robot_ip,以及script_file_path
2、将实例化的EliteDriverConfig对象,作为参数传递给EliteDriver类,实例化driver对象
3、创建异常处理函数,用于处理不同异常时的处理逻辑
4、调用EliteDriver的实例化对象的registerRobotExceptionCallback接口函数,将第三步创建的函数作为参数传递
三、常见问题
1、Q:异常处理逻辑里有哪些type?分别代表什么
A:elite_cs_sdk里有一个类RobotExceptionType,其中定义了三种类型的异常,分别是ROBOT_DISCONNECTED、ROBOT_ERROR、SCRIPT_RUNTIME。
ROBOT_DISCONNECTED: 表示与机器人断开了连接ROBOT_ERROR:表示机器人运行错误。SCRIPT_RUNTIME:表示运行时异常,如语法错误、脚本执行错误等。
可以在异常处理函数里,对参数进行getType方法获取异常类型,进行匹配后处理异常
2、Q:如何判断是否是我想要处理的异常呢?
A:异常类有各自的成员变量和成员函数。比如
ROBOT_ERROR提供了getErrorCode和getSubErrorCode接口,用来拿到对应的错误码和子错误码,然后查找下方的机器人状态报文手册里异常码章节,找到异常码和子码后,进行匹配和处理。再或者针对SCRIPT_RUNTIME类型,找到运行时异常章节,调用getMessage接口后进行匹配97.6 KB
四、代码和附件
DashboardClientInterface()构造dashboard.connect(ip, 29999)连 Dashboarddashboard.setSpeedScaling(75)设速度倍率 75%dashboard.powerOn()上电 →dashboard.brakeRelease()释放抱闸EliteDriverConfig()配置伺服参数:
servoj_time = 0.004(伺服周期 4ms)servoj_gain = 2000(伺服增益)servoj_lookahead_time = 0.3(前瞻时间 300ms)headless_mode = True- 自定义端口:
reverse_port=50011 / script_sender_port=50012 / trajectory_port=50013 / script_command_port=50014 - 脚本路径
./external_control.script
EliteDriver(config)构造 →sleep(1)→driver.registerRobotExceptionCallback(on_robot_exception)注册异常回调on_robot_exception异常回调里分三种情况处理:
SCRIPT_RUNTIME + "inv_kin_singularity"→closeSafetyDialog()→sendExternalControlScript重发脚本ROBOT_DISCONNECTED→primaryReconnect()重连Robot_Error + (errCode=12, subCode=101)→unlockProtectiveStop()→closeSafetyDialog()→sendExternalControlScript复位保护性停止- 其他 → 打印"未处理异常",返回
False
import os
import time
import sys
import elite_cs_sdk as cs
class CsRobot:
def __init__(self, ip, local_ip):
self.ip = ip
self.local_ip = local_ip
port = 29999
dashboard = cs.DashboardClientInterface()
res = dashboard.connect(self.ip, port)
new_scaling = 75 # percent
dashboard.setSpeedScaling(new_scaling)
dashboard.powerOn()
dashboard.brakeRelease()
config = cs.EliteDriverConfig()
config.robot_ip = self.ip
config.local_ip = self.local_ip
config.servoj_time = 0.004
config.servoj_gain = 2000
config.servoj_lookahead_time = 0.3
config.script_file_path = "./external_control.script"
config.headless_mode = True
config.reverse_port = 50011
config.script_sender_port = 50012
config.trajectory_port = 50013
config.script_command_port = 50014
self.driver = cs.EliteDriver(config)
time.sleep(1)
self.driver.registerRobotExceptionCallback(self.on_robot_exception)
def on_robot_exception(self, robot_exception):
"""机器人异常回调"""
if robot_exception.getType() == cs.RobotExceptionType.SCRIPT_RUNTIME:
if robot_exception.getMessage() == "inv_kin_singularity":
self.driver.closeSafetyDialog()
self.driver.sendExternalControlScript()
return True
if robot_exception.getType() == cs.RobotExceptionType.ROBOT_DISCONNECTED:
self.driver.primaryReconnect()
return True
if robot_exception.getType() == cs.RobotExceptionType.Robot_Error:
if robot_exception.getErrorCode() == 12 && robot_exception.getSubErrorCode() == 101:
self.driver.unlockProtectiveStop()
self.driver.closeSafetyDialog()
self.driver.sendExternalControlScript()
return True
print("机器人异常, 未处理异常")
return False
def get_driver(self):
return self.driver