如何通过编程方式获取Pepper Robot状态并使用Python API检测错误?
检测Pepper机器人错误状态及读取错误码的Python API实现方法
你可以通过NAOqi框架的ALDiagnostics和ALMemory两个核心模块实现错误状态的检测和错误码读取,操作步骤和代码示例如下:
前置条件
- 本地Python环境已安装naoqi依赖库
- 运行代码的设备和Pepper机器人处于同一局域网,可正常访问机器人IP和默认端口9559
方法1:主动拉取当前所有活跃错误
直接调用ALDiagnostics的接口拉取当前存在的所有错误告警,适合轮询检查的场景:
from naoqi import ALProxy # 替换为实际的Pepper机器人IP和端口 PEPPER_IP = "192.168.1.100" PEPPER_PORT = 9559 # 初始化诊断服务代理 diagnostics_proxy = ALProxy("ALDiagnostics", PEPPER_IP, PEPPER_PORT) # 获取所有当前未解除的错误列表 active_errors = diagnostics_proxy.getActiveErrors() # 遍历解析错误信息 for error in active_errors: error_code = error.getErrorID() error_desc = error.getMessage() error_severity = error.getSeverity() # 严重等级从低到高为INFO/WARNING/ERROR/FATAL print(f"错误码:{error_code},描述:{error_desc},严重等级:{error_severity}") # 示例:检测到过热错误时触发机器人休眠 if error_code == "RobotOverheatError": motion_proxy = ALProxy("ALMotion", PEPPER_IP, PEPPER_PORT) motion_proxy.rest()
方法2:实时监听错误事件
通过订阅ALMemory的系统事件,实现错误发生时的即时触发处理,适合需要实时响应的场景:
from naoqi import ALProxy, ALModule import time PEPPER_IP = "192.168.1.100" PEPPER_PORT = 9559 # 自定义错误监听模块 class PepperErrorMonitor(ALModule): def __init__(self, module_name): ALModule.__init__(self, module_name) self.memory_proxy = ALProxy("ALMemory", PEPPER_IP, PEPPER_PORT) # 订阅错误发生、错误解除两类事件 self.memory_proxy.subscribeToEvent("Diagnostics/ErrorOccured", self.getName(), "handle_new_error") self.memory_proxy.subscribeToEvent("Diagnostics/ErrorRemoved", self.getName(), "handle_error_resolved") def handle_new_error(self, event_name, error_obj, subscriber_id): error_code = error_obj.getErrorID() error_msg = error_obj.getMessage() print(f"触发新错误:{error_code} - {error_msg}") # 此处可添加对应错误码的处理逻辑 def handle_error_resolved(self, event_name, error_obj, subscriber_id): error_code = error_obj.getErrorID() print(f"错误已解除:{error_code}") if __name__ == "__main__": # 启动监听服务 error_monitor = PepperErrorMonitor("PepperErrorMonitor") # 保持进程运行 try: while True: time.sleep(1) except KeyboardInterrupt: # 退出前取消事件订阅 error_monitor.memory_proxy.unsubscribeToEvent("Diagnostics/ErrorOccured", error_monitor.getName()) error_monitor.memory_proxy.unsubscribeToEvent("Diagnostics/ErrorRemoved", error_monitor.getName())
补充说明
- 不同错误类型对应唯一的错误ID,你可以参考Pepper官方的用户通知文档完成错误码和故障场景的映射,比如过热、电机故障、电池低电量等都有专属的错误码。
- 如需检测特定维度的状态(比如具体部位温度),也可以直接读取
ALMemory中对应的状态键值,自行实现阈值告警逻辑。
内容的提问来源于stack exchange,提问作者whatsisname
相关产品推荐
相关产品推荐

