Files

83 lines
2.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""诊断左臂IK问题:测试目标位姿是否可达"""
import sys
sys.path.insert(0, 'scripts')
from real_arm import RealArm
import numpy as np
from scipy.spatial.transform import Rotation as Rot
import json
print("==== 左臂IK诊断 ====\n")
# 读取目标抓取姿态
g = json.load(open('data/real_grasp_manual/grasp_l20_target_fixed2_0.json'))
wrist_pos = np.array(g['wrist_pos'])
q = np.array(g['wrist_quat_wxyz'])
R = Rot.from_quat([q[1], q[2], q[3], q[0]])
wrist_euler = R.as_euler('xyz')
print("目标抓取姿态:")
print(f" 位置: {wrist_pos}")
print(f" 欧拉角(弧度): {wrist_euler}")
print(f" 欧拉角(度): {np.degrees(wrist_euler)}")
print()
# 预抓取点
approach = np.array(g['approach'])
clearance = 0.10
pre_pos = wrist_pos - clearance * approach / (np.linalg.norm(approach) + 1e-9)
print("预抓取点:")
print(f" 位置: {pre_pos}")
print()
# 初始化左臂
print("初始化左臂...")
arm = RealArm(side='left', hand_rotation_offset_deg=45.0)
# 读取当前位姿
current = arm.read()
print("当前左臂位姿:")
print(f" 位置: {current['pos']}")
print(f" 欧拉角(度): {np.degrees(current['euler'])}")
print()
# 测试IK(预抓取点)
print("测试预抓取点IK...")
ik_result = arm.ik_wrist(pre_pos, wrist_euler)
if ik_result is not None:
print(f"✓ IK成功: {len(ik_result)} 个解")
print(f" 关节角(前7个): {ik_result[0][:7]}")
else:
print("✗ IK失败")
print()
print("尝试诊断:")
# 测试仅位置是否可达(忽略姿态)
print("\n1. 测试更高的位置(z+0.1):")
test_pos = pre_pos.copy()
test_pos[2] += 0.1
ik = arm.ik_wrist(test_pos, wrist_euler)
print(f" 结果: {'✓ 可达' if ik else '✗ 不可达'}")
print("\n2. 测试更近的位置(x-0.1):")
test_pos = pre_pos.copy()
test_pos[0] -= 0.1
ik = arm.ik_wrist(test_pos, wrist_euler)
print(f" 结果: {'✓ 可达' if ik else '✗ 不可达'}")
print("\n3. 测试当前位置(应该可达):")
ik = arm.ik_wrist(current['pos'], current['euler'])
print(f" 结果: {'✓ 可达' if ik else '✗ 不可达'}")
print("\n4. 检查目标位置是否在机械臂范围内:")
dist = np.linalg.norm(pre_pos[:2]) # xy平面距离
print(f" xy平面距离base: {dist:.3f}m")
print(f" 高度: {pre_pos[2]:.3f}m")
print(f" A7 Lite手臂reach: 约0.7m")
if dist > 0.7:
print(" ⚠️ 距离过远,超出手臂reach")
if pre_pos[2] > 1.2:
print(" ⚠️ 高度过高")
arm.close()
print("\n诊断完成。")