pythonABB读取ABB关节与坐标数据
hai_lin
最近做实验,其中有一个步骤是持续读取ABB当前的关节数据与世界坐标。
ChatGPT:
实时读取ABB机器人的关节数据和世界坐标有多种方式,包括:使用RAPID编程直接获取关节角度和位姿,通过RobotStudio仿真平台监控、利用Robot Web Services (RWS) 通过REST API访问、使用Robot Communication Runtime (RCR) 进行TCP/IP通信、通过PLC或Fieldbus工业总线协议读取,以及通过示教器手动监控数据。每种方式适用于不同的集成场景和需求。
不熟悉rapid编程,并且运行rapid程序则需要让机器人进入自动模式,那么就不能通过示教器灵活地操控机器人了。
不会RCR和PLC,也不会进行DLL开发。
因此选择了RWS,简单易实现。
ABB RWS的文档url:
https://developercenter.robotstudio.com/api/RWS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
   | 
  import requests import os import time import csv import json
  def get_robot_status(ip):          data_folder = 'test'     os.makedirs(data_folder, exist_ok=True)     csv_file = os.path.join(data_folder, 'robot_status.csv')               joint_target_url = f'http://{ip}/rw/motionsystem/mechunits/ROB_1/jointtarget?json=1'     rob_target_url = f'http://{ip}/rw/motionsystem/mechunits/ROB_1/robtarget?json=1'               auth = requests.auth.HTTPDigestAuth('Default User', 'robotics')                 headers = {'Accept': 'application/json'}               requests.packages.urllib3.disable_warnings()               response_joint = requests.get(joint_target_url, auth=auth, headers=headers, verify=False)     if response_joint.status_code == 200:         data_joint = response_joint.json()         state_joint = data_joint['_embedded']['_state'][0]         axes_data = [state_joint.get(f'rax_{i}', '0') for i in range(1, 7)]     else:         print(f"获取关节数据失败,状态码:{response_joint.status_code}")         axes_data = ['0'] * 6                 response_position = requests.get(rob_target_url, auth=auth, headers=headers, verify=False)     if response_position.status_code == 200:         data_position = response_position.json()         state_position = data_position['_embedded']['_state'][0]         coordinates = [state_position.get('x', '0'), state_position.get('y', '0'), state_position.get('z', '0')]     else:         print(f"获取机器人位置失败,状态码:{response_position.status_code}")         coordinates = ['0', '0', '0']                 csv_header = ["timestamp"] + [f'rax_{i}' for i in range(1, 7)] + ['x', 'y', 'z']     timestamp = time.strftime("%Y-%m-%d %H:%M:%S")     data_row = [timestamp] + axes_data + coordinates          file_exists = os.path.isfile(csv_file)     with open(csv_file, 'a', newline='') as csvfile:         writer = csv.writer(csvfile)         if not file_exists:             writer.writerow(csv_header)         writer.writerow(data_row)          print(f"机器人状态已保存到 {csv_file}")
  if __name__ == "__main__":     robot_ip = '192.168.125.1'     get_robot_status(robot_ip)
 
  | 
 
踩坑:
- 机器人ip地址要从示教器上读到。
 
- 认证方式要以如下方式 
auth = requests.auth.HTTPDigestAuth('Default User', 'robotics')