找回密码
 立即注册

QQ登录

只需一步,快速开始

本帖最后由 lou 于 2020-4-27 11:28 编辑

Geek专栏:LejuRoban-1特别专题:双连杆机械臂运动学正向和逆向 Python 仿真




今天Geek专栏为大家带来
乐聚机器人王松博士的
LejuRoban-1特别专题
“ 双连杆机械臂运动学正向和逆向 Python 仿真”

包括LejuRoban-1在内的机器人中前向运动学主要关心机械臂各关节的变量与机械臂末段机构位置与方向的关系。

DH(Denavit-Hartenberg)方法
DH 方法描述了相邻坐标系之间的旋转与移动的关系,只需要找到 4 个参数,以基座坐标(x0,y0,z0)为参考,就可以依次求出相连接的连杆坐标系(xi,yi,zi)和末端执行器坐标系(xn,yn,zn)的远点与方向。

78.png

DH 方法要求相邻坐标系之间满足下面约定:

  • xi和zi-1轴垂直
  • xi和zi-1轴相交
4 个 DH 参数分别为:
  • 关节转角(joint angle) θi
  • 连杆扭角(link twist) αi
  • 连杆距离(link offset) di
  • 连杆长度(link length) ai

正运动学公式

79.png

逆运动学公式

80.png

平面连杆正运动学仿真

81.png

  1. 1  import numpy as np
  2. 2  from numpy import sin, cos
  3. 3  import matplotlib.pyplot as plt
  4. 4
  5. 5  def dhmat(delta, d, a, alpha):
  6. 6      return np.array([[cos(delta), -sin(delta)*cos(alpha), sin(delta)*sin(alpha), a*cos(delta)],
  7. 7                       [sin(delta), cos(delta)*cos(alpha), -cos(delta)*sin(alpha), a*sin(delta)],
  8. 8                       [0, sin(alpha), cos(alpha), d],
  9. 9                       [0, 0, 0, 1]])
  10. 10
  11. 11  def main():
  12. 12      theta_1 = np.radians(np.arange(20, 50, 1))
  13. 13      theta_2 = np.radians(np.arange(0, 30, 1))
  14. 14      d_1 = 0
  15. 15      d_2 = 0
  16. 16      l_1 = 0.1
  17. 17      l_2 = 0.2
  18. 18      alpha_1 = 0
  19. 19      alpha_2 = 0
  20. 20
  21. 21      P_0 = np.array([0, 0, 0, 1]).T
  22. 22      P_1 = np.array([0, 0, 0, 1]).T
  23. 23      P_2 = np.array([0, 0, 0, 1]).T
  24. 24
  25. 25      fig, ax = plt.subplots()
  26. 26
  27. 27      for i in range(len(theta_1)):
  28. 28          P1_0 = dhmat(theta_1[i], d_1, l_1, alpha_1).dot(P_1)
  29. 29          P2_0 = dhmat(theta_1[i], d_1, l_1, alpha_1).dot(dhmat(theta_2[i], d_2, l_2, alpha_2)).dot(P_2)
  30. 30          ax.plot([P_0[0], P1_0[0], P2_0[0]], [P_0[1], P1_0[1], P2_0[1]], '-bo')
  31. 31      ax.set_xlabel('x (m)')
  32. 32      ax.set_ylabel('y (m)')
  33. 33      fig.savefig('./twp_link_planar_robot.png')
  34. 34      plt.show()
  35. 35
  36. 36  if __name__ == '__main__':
  37. 37      main()

复制代码

平面连杆逆运动学仿真

83.png

  1. 1  import numpy as np
  2. 2  from numpy import linalg as LA
  3. 3  from numpy import sin, cos, arctan
  4. 4  import matplotlib.pyplot as plt
  5. 5
  6. 6
  7. 7  def dhmat(delta, d, a, alpha):
  8. 8      return np.array([[cos(delta), -sin(delta)*cos(alpha), sin(delta)*sin(alpha), a*cos(delta)],
  9. 9                       [sin(delta), cos(delta)*cos(alpha), -cos(delta)*sin(alpha), a*sin(delta)],
  10. 10                       [0, sin(alpha), cos(alpha), d],
  11. 11                       [0, 0, 0, 1]])
  12. 12
  13. 13  def main():
  14. 14      d_1 = 0
  15. 15      d_2 = 0
  16. 16      l_1 = 0.1
  17. 17      l_2 = 0.2
  18. 18      alpha_1 = 0
  19. 19      alpha_2 = 0
  20. 20
  21. 21      theta_1 = np.radians(np.arange(20, 50, 1))
  22. 22      theta_2 = np.zeros(len(theta_1))
  23. 23
  24. 24      st = 1
  25. 25
  26. 26      des_P2_0 = np.array([0.22, 0.2, 0, 1]).T
  27. 27      err_dist = np.zeros(len(theta_1))
  28. 28
  29. 29      P_0 = np.array([0, 0, 0, 1]).T
  30. 30      P_1 = np.array([0, 0, 0, 1]).T
  31. 31      P_2 = np.array([0, 0, 0, 1]).T
  32. 32
  33. 33      fig, ax = plt.subplots()
  34. 34
  35. 35      for i in range(len(theta_1)):
  36. 36          P1_0 = dhmat(theta_1[i], d_1, l_1, alpha_1).dot(P_1)
  37. 37          theta_2[i] = arctan((-sin(theta_1[i]) * des_P2_0[0] + cos(theta_1[i]) * des_P2_0[1])
  38. 38                              / (cos(theta_1[i]) * des_P2_0[0] + sin(theta_1[i]) * des_P2_0[1] - l_1))
  39. 39          P2_0 = dhmat(theta_1[i], d_1, l_1, alpha_1).dot(dhmat(theta_2[i], d_2, l_2, alpha_2)).dot(P_2)
  40. 40          err_dist[i] = LA.norm(des_P2_0 - P2_0)
  41. 41          ax.plot([P_0[0], P1_0[0], P2_0[0]], [P_0[1], P1_0[1], P2_0[1]], '-bo')
  42. 42
  43. 43      # 双连杆平面机械运动逆向运动学仿真
  44. 44      ax.plot(des_P2_0[0], des_P2_0[1], '-rx')
  45. 45      ax.set_xlabel('x (m)')
  46. 46      ax.set_ylabel('y (m)')
  47. 47      fig.savefig('./ik_two_link_planar_robot_0.png')
  48. 48      plt.show()
  49. 49
  50. 50      # theta_1, theta_2 角度变化轨迹
  51. 51      fig, ax = plt.subplots(nrows=2)
  52. 52      ax[0].plot(theta_1, 'k')
  53. 53      ax[0].set_ylabel('theta_1')
  54. 54      ax[1].plot(theta_2, 'k')
  55. 55      ax[1].set_ylabel('theta_2')
  56. 56      ax[1].set_xlabel('time (s)')
  57. 57      fig.savefig('./ik_two_link_planar_robot_1.png')
  58. 58      plt.show()
  59. 59
  60. 60      # 目标点与实际点之间的距离误差
  61. 61      fig, ax = plt.subplots()
  62. 62      ax.plot(err_dist, 'k')
  63. 63      ax.set_ylabel('distance error (m)')
  64. 64      ax.set_xlabel('time (s)')
  65. 65      fig.savefig('./ik_two_link_planar_robot_2.png')
  66. 66      plt.show()
  67. 67
  68. 68  if __name__ == '__main__':
  69. 69      main()
复制代码

参考

机器人运动学中的D-H变换(https://zhuanlan.zhihu.com/p/20944328)


分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册