Python新手求助:Euler2quat函数调用出现TypeError问题
解决欧拉角转四元数函数的两个错误
第一个错误:TypeError: Euler2quat() missing 2 required positional arguments: 'theta' and 'psi'
错误原因
原函数Euler2quat定义时要求接收三个独立的标量参数phi, theta, psi,但你调用时传入的是[ptp0]——这是一个仅包含一个二维数组的列表,函数无法从中提取出theta和psi参数,因此触发错误。
修复方案
ptp0是形状为(3,1)的二维数组,先将其展平为一维,再通过*操作符解包成三个独立参数传递给函数:
# 原调用代码修改为 q0123_0 = Euler2quat(*ptp0.flatten())
第二个错误:TypeError: 'list' object is not callable
错误原因
修改函数为接收单个列表参数后出现此错误,大概率是你不小心将函数名Euler2quat重定义成了列表(比如写了Euler2quat = [...]这类代码),导致原本的函数对象被覆盖成列表,调用时自然报错(列表不能像函数一样被调用)。
修复方案
- 检查并删除重定义代码:确保代码中没有将
Euler2quat赋值为列表或其他非可调用对象的语句。 - 正确修改函数接收单个数组/列表参数:
def Euler2quat(euler_angles): # 将输入的二维数组展平为一维,提取三个欧拉角 phi, theta, psi = euler_angles.flatten() q0 = np.cos(phi/2) * np.cos(theta/2) * np.cos(psi/2) + np.sin(phi/2) * np.sin(theta/2) * np.sin(psi/2) q1 = np.sin(phi/2) * np.cos(theta/2) * np.cos(psi/2) - np.cos(phi/2) * np.sin(theta/2) * np.sin(psi/2) q2 = np.cos(phi/2) * np.sin(theta/2) * np.cos(psi/2) + np.sin(phi/2) * np.cos(theta/2) * np.sin(psi/2) q3 = np.cos(phi/2) * np.cos(theta/2) * np.sin(psi/2) - np.sin(phi/2) * np.sin(theta/2) * np.cos(psi/2) return [q0, q1, q2, q3] # 调用时直接传入ptp0 q0123_0 = Euler2quat(ptp0)
内容的提问来源于stack exchange,提问作者saib salih
相关产品推荐
相关产品推荐

