| from rtde_control import RTDEControlInterface
|
| from rtde_receive import RTDEReceiveInterface
|
| import time
|
| import math
|
| import os
|
| from datetime import datetime
|
|
|
|
|
| ROBOT_HOST = "192.168.1.14"
|
| RTDE_PORT = 8000
|
| DATA_DIR = "video2"
|
|
|
|
|
| os.makedirs(DATA_DIR, exist_ok=True)
|
|
|
|
|
| current_time_str = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| OUTPUT_FILE = f"{DATA_DIR}/robot_pose_data_{current_time_str}.txt"
|
|
|
| print("连接中 ")
|
|
|
|
|
| rtde_r = RTDEReceiveInterface(ROBOT_HOST, RTDE_PORT)
|
|
|
|
|
| with open(OUTPUT_FILE, 'w') as f:
|
| f.write("time,x,y,z,rx,ry,rz\n")
|
|
|
| print("连接成功,开始实时读取末端位姿数据并保存到文件...")
|
| print(f"数据将保存到: {OUTPUT_FILE}")
|
|
|
| try:
|
| while True:
|
|
|
| current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
|
|
|
|
|
| tcp_pose = rtde_r.getActualTCPPose()
|
|
|
|
|
| x = tcp_pose[0] * 1000
|
| y = tcp_pose[1] * 1000
|
| z = tcp_pose[2] * 1000
|
| rx = tcp_pose[3]
|
| ry = tcp_pose[4]
|
| rz = tcp_pose[5]
|
|
|
|
|
| print(f"[{current_time}] Position: x={x:.2f}mm, y={y:.2f}mm, z={z:.2f}mm")
|
| print(f"[{current_time}] Orientation (Rx, Ry, Rz): {rx:.3f}, {ry:.3f}, {rz:.3f}")
|
| print("-" * 80)
|
|
|
|
|
| with open(OUTPUT_FILE, 'a') as f:
|
| f.write(f"{current_time},{x:.4f},{y:.4f},{z:.4f},{rx:.4f},{ry:.4f},{rz:.4f}\n")
|
|
|
| time.sleep(0.1)
|
|
|
| except KeyboardInterrupt:
|
| print("读取已停止。")
|
| print(f"数据已保存到 {OUTPUT_FILE}")
|
|
|