正向运动学分析DH编程实例

在机器人运动学中,正向运动学(Forward kinematics)是指利用机器人的运动方程,根据关节参数的指定值计算末端执行器的位置。

在机械工程中,Denavit–Hartenberg参数(也称为 DH 参数)是与特定惯例相关的四个参数,用于将参考系附加到空间运动链或机器人操纵器的链接上。

Jacques Denavit和Richard Hartenberg于 1955 年引入了这一惯例,以标准化空间连杆的坐标系。Richard Paul于 1981 年证明了其对机器人系统运动分析的价值。虽然已经开发了许多用于连接参考系的惯例,但Denavit–Hartenberg惯例仍然是一种流行的方法。

下面我们通过一个Python的实例来更深入了解这一方法在机械臂设计中的应用。这里用到的Python库是Robotics Toolbox for Python。还是用回之前的运动学图示:

图片

根据之前的DH参数表:

图片

假设a1=a2=a3=a4=0.1m;d3为关节的位移量也设为0.1m。

初始角度θ都设为0°。可以生成以下表格:

θαr(a)d
10π/20.10.1
2π/2π/200
3π/2000.3

这里角度要换算成弧度的形式。

使用Claude AI通过提示词生成以下代码:

import roboticstoolbox as rtb
import numpy as np
import matplotlib.pyplot as plt

# Define the robot using DH parameters
# Parameters: (theta, d, a, alpha)
link1 = rtb.RevoluteDH(d=0.1, a=0.1, alpha=np.pi/2)
link2 = rtb.RevoluteDH(d=0, a=0, alpha=np.pi/2)
link3 = rtb.PrismaticDH(theta=np.pi/2, a=0, alpha=0)

# Create the robot model
demo = rtb.DHRobot([link1, link2, link3], name="Demo Robot")

# Print the robot's DH parameter table
print("Denavit-Hartenberg Parameter Table:")
print(demo)

# Define joint angles and extension (in SI units: radians and meters)
q1 = 0  # First revolute joint
q2 = np.pi/2   # Second revolute joint
d3 = 0.3        # Prismatic joint extension


# Perform forward kinematics
T = demo.fkine([q1, q2, d3])

# Print the result
print("\nForward Kinematics Result:")
print(T)

# Extract and print the end-effector position
position = T.t
print("\nEnd-effector position:")
print(f"X: {position[0]:.4f}, Y: {position[1]:.4f}, Z: {position[2]:.4f}")

# Visualize the robot
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Plot the robot
demo.plot([q1, q2, d3], backend='pyplot')

# Set the plot limits and labels
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_zlim(0, 1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Demo Robot Visualization')

# Show the plot and keep the window open
plt.tight_layout()
plt.show(block=True)

# This line is only reached after the plot window is closed
print("Plot window closed.")

解释一下各段代码的作用:

import roboticstoolbox as rtb
import numpy as np
import matplotlib.pyplot as plt

这些行导入了必要的库:Robotics Toolbox、用于数值运算的 NumPy 和用于可视化的 Matplotlib。

link1 = rtb.RevoluteDH(d=0.1, a=0.1, alpha=np.pi/2)
link2 = rtb.RevoluteDH(d=0, a=0, alpha=np.pi/2)
link3 = rtb.PrismaticDH(theta=np.pi/2, a=0, alpha=0)

这里使用 Denavit-Hartenberg 参数定义了三个连杆:

  • 连杆 1 和 2 是旋转关节(绕轴旋转)。
  • 连杆 3 是移动关节(线性运动)。
  • 参数 (d、a、alpha) 定义每个连杆的几何形状。
q1 = 0
q2 = np.pi/2
d3 = 0.3

这为关节变量设置了特定的值:旋转关节的角度(q1、q2)和移动关节的延伸(d3)。

T = demo.fkine([q1, q2, d3])

这段计算给定关节配置的末端执行器姿势(位置和方向)。

输出结果:

DH参数表

图片

齐次变换矩阵和终端位置:

图片

可视化结果:

图片
图片

将第一和第二个关节都旋转30°

图片
图片