以下是将欧拉角转为旋转矩阵的 Python 代码:

import numpy as np

def euler_to_rotation_matrix(euler_angles):
    'Converts Euler angles to a rotation matrix.'
    'param euler_angles: A tuple of three angles in radians (roll, pitch, yaw)' 
    'return: A 3x3 rotation matrix'
    roll, pitch, yaw = euler_angles

    # Calculate the rotation matrix elements
    c_r, s_r = np.cos(roll), np.sin(roll)
    c_p, s_p = np.cos(pitch), np.sin(pitch)
    c_y, s_y = np.cos(yaw), np.sin(yaw)

    # Build the rotation matrix
    # R = Rz(yaw) * Ry(pitch) * Rx(roll)
    Rz = np.array([[c_y, -s_y, 0], [s_y, c_y, 0], [0, 0, 1]])
    Ry = np.array([[c_p, 0, s_p], [0, 1, 0], [-s_p, 0, c_p]])
    Rx = np.array([[1, 0, 0], [0, c_r, -s_r], [0, s_r, c_r]])
    R = Rz.dot(Ry).dot(Rx)

    return R

其中,欧拉角的顺序为 Z-Y-X(yaw-pitch-roll),也就是先绕 Z 轴旋转 yaw 角度,再绕新的 Y 轴旋转 pitch 角度,最后绕新的 X 轴旋转 roll 角度。

Python 代码:将欧拉角转换为旋转矩阵

原文地址: https://www.cveoy.top/t/topic/lS79 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录