RPY_Euler_Quaternion_AngleAxis角度转化:Matlab、Python、ROS、Halcon版本

以你之姓@ 2022-12-02 01:21 21阅读 0赞

UR协作机器人和Franka机器人导出的位姿为angleVector,三个量表示,在Matlab中angleVector是四个量表示。如果是三个量的表示推荐使用Python的scipy库做转换。

在线欧拉角-四元数-齐次矩阵转换

https://www.andre-gaschler.com/rotationconverter/

Rotation vector介绍

https://en.wikipedia.org/wiki/Axis%E2%80%93angle_representation#Rotation_vector

一、RPY_Euler_Quaternion_AngleAxis角度转化:Matlab机器人工具箱

1.1 Quaternion转Matrix (带位置和姿态)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lha2Vk_size_16_color_FFFFFF_t_70

  1. % 位置及四元数xyzw转齐次矩阵
  2. robotHtool =[0.10345922, -0.48407779, 0.29668114, -0.03533355, 0.09830182, -0.86382214, 0.49284846];
  3. % w x y z
  4. robotHtool_qua = Quaternion([robotHtool(7), robotHtool(4), robotHtool(5) , robotHtool(6)])
  5. robotHtool_matrix = transl(robotHtool(1), robotHtool(2), robotHtool(3)) * robotHtool_qua.T
  6. % 位置及欧拉角xyz转齐次矩阵(默认单位:弧度)
  7. robotHtool =[0.400422, -0.0262847, 0.365594, 3.1342, 0.0193, -1.6122];
  8. robotHtool_rpy = rpy2tr([robotHtool(4), robotHtool(5) , robotHtool(6)]); % 'deg' or 'zyx'
  9. T61 = transl(robotHtool(1), robotHtool(2), robotHtool(3)) * robotHtool_rpy;

二、Python的scipy

2.1 UR机器人rotvec转换为RPY_rxryrz

UR机械臂通过30003端口发送过来的是rotation vector

https://www.universal-robots.com/articles/ur/explanation-on-robot-orientation/

https://www.universal-robots.com/articles/ur/rpy-tofrom-rotation-vector/

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lha2Vk_size_16_color_FFFFFF_t_70 1

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lha2Vk_size_16_color_FFFFFF_t_70 2

安装或升级scipy(≥1.2.0):

Python3:pip3 install scipy==1.2.0

python2:pip install scipy==1.2.0

  1. from scipy.spatial.transform import Rotation as R
  2. r = R.from_rotvec([-0.001220983, 3.1162765, 0.038891915])
  3. Euler_xyz = r.as_euler('xyz', degrees=False)

scipy spatial transform官方帮助:

https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.transform.Rotation.html

  1. r1 = R.from_euler('zyx', [0, 0, 180], degrees=True)
  2. r2 = R.from_quat(0, 0, 0, 1) (scalar-last formatx y z w模式)
  3. r3 = R.from_matrix([[1, 0, 0], [0, -1, 0], [0, 0, -1]])
  4. r1.as_matrix()
  5. r2.as_rotvec()
  6. r3.as_euler('xyz', degrees=True)
  7. r_array = np.asarray(r)

scipy中的四元数有时候会遇到正负号计算出来跟别的(matlab等)刚好相反的问题,按官方说法其实都是同一种姿态。(看具体平台,但是有时候确实会让结果出错,Just for your information.)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lha2Vk_size_16_color_FFFFFF_t_70 3

2.2 ROS tf坐标转换

http://docs.ros.org/en/kinetic/api/tf/html/python/transformations.html

  1. import tf.transformations
  2. R = tf.transformations.euler_matrix(3.14, 0, 0, axes='rxyz')
  3. R[0][3] = 1.0
  4. R[1][3] = 2.0
  5. R[2][3] = 3.0
  6. scale, shear, angles, trans, persp = tf.transformations.decompose_matrix(R)
  7. T0 = [1, 2, 3]
  8. R1 = tf.transformations.compose_matrix(scale, shear, angles, T0, persp)
  9. R3 = quaternion_matrix((1, 0, 0, 0))
  10. tf.transformations.quaternion_from_matrix(R)
  11. tf.transformations.inverse_matrix(R)
  12. tf.transformations.euler_from_matrix(R, axes='rzyx')

2.3 川崎机器人Euler_ZYZ转四元数Quaternion

  1. import sys
  2. import numpy as np
  3. from scipy.spatial.transform import Rotation as R
  4. f_result=open('/home/yake/Python_ws/kawasaki_convert_qua.txt','a')
  5. with open(r'/home/yake/Python_ws/kawasaki.txt','r') as f:
  6. while True:
  7. line = f.readline()
  8. if line:
  9. list = line.split (",")
  10. li = []
  11. for i in list:
  12. li.append(float(i))
  13. result = []
  14. result.append( li[0]/1000.0)
  15. result.append( li[1]/1000.0)
  16. result.append( li[2]/1000.0)
  17. r = R.from_euler('zyz', [li[3], li[4], li[5]], degrees=True)
  18. euler_xyz = r.as_quat()
  19. result.append( euler_xyz[0] )
  20. result.append( euler_xyz[1])
  21. result.append( euler_xyz[2])
  22. result.append( euler_xyz[3])
  23. np.savetxt( f_result, result, fmt='%0.8f', delimiter=',',newline=',')
  24. f_result.write("\n")
  25. else:
  26. break
  27. f_result.close()

三、Halcon姿态变换

3.1 Halcon 的姿态、齐次变换和四元数

create_pose函数是包含位置和姿态的,姿态格式为RPY_rx_ry_rz,注意输入为角度。pose_to_hom_mat3d是RPY_rx_ry_rz转为齐次矩阵。四元数的顺序是w, x, y, z

  1. robot_V_cam := [0.418906862152, 0.471104634456, 0.729862740299, 2.851, -1.241, -0.008]
  2. * Create pose use degrees.
  3. create_pose (robot_V_cam[0], robot_V_cam[1], robot_V_cam[2], deg(robot_V_cam[3]), deg(robot_V_cam[4]), deg(robot_V_cam[5]), 'Rp+T', 'gba', 'point', Pose)
  4. pose_to_hom_mat3d(Pose, robot_H_cam)
  5. pose_to_quat(Pose, robot_Q_cam)
  6. hom_mat3d_to_pose(robot_H_cam, pose_test1)
  7. ************************* xyz *********************************************
  8. hom_mat3d_identity (HomMat3DIdentity)
  9. hom_mat3d_translate (HomMat3DIdentity, robot_V_cam[0], robot_V_cam[1], robot_V_cam[2], HomMat3DTranslate)
  10. hom_mat3d_rotate_local (HomMat3DTranslate, robot_V_cam[3], 'x', HomMat3DT_Rl)
  11. hom_mat3d_rotate_local (HomMat3DT_Rl, robot_V_cam[4], 'y', HomMat3DT_Rl_Rm)
  12. hom_mat3d_rotate_local (HomMat3DT_Rl_Rm, robot_V_cam[5], 'z', HomMat3D)
  13. hom_mat3d_to_pose (HomMat3D, pose_test2)
  14. ********************* right-left old zyx **********************************
  15. hom_mat3d_identity (HomMat3DIdent)
  16. hom_mat3d_rotate (HomMat3DIdent, robot_V_cam[5], 'z', 0, 0, 0, HomMat3DRotZ)
  17. hom_mat3d_rotate (HomMat3DRotZ, robot_V_cam[4], 'y', 0, 0, 0, HomMat3DRotYZ)
  18. hom_mat3d_rotate (HomMat3DRotYZ, robot_V_cam[3], 'x', 0, 0, 0, HomMat3DXYZ3)
  19. hom_mat3d_translate(HomMat3DXYZ3, robot_V_cam[0], robot_V_cam[1], robot_V_cam[2], HomMat3Dzyx)
  20. hom_mat3d_to_pose(HomMat3Dzyx, pose_test3)

发表评论

表情:
评论列表 (有 0 条评论,21人围观)

还没有评论,来说两句吧...

相关阅读