我有一个Jupyter-Lab笔记本,我一直在尝试使用它与Sympy一起开发机器人手臂的Jacobian。我有随后在后续表达式中使用的表达式,依此类推。尝试运行lambdified最终表达式时,我收到消息'Mul'对象没有属性'cos'
其他问题,例如
Sympy to numpy causes the AttributeError: 'Symbol' object has no attribute 'cos'
What causes this error (AttributeError: 'Mul' object has no attribute 'cos') in Python?
我确保使用以下方式调用sympy:
import sympy as sp
然后使用sp
前缀调用所有内容:
sp.cos()
sp.pi
这是我的代码(很抱歉)
# symbolic analysis
import sympy as sp
# Variables
theta1, theta2, theta3, theta4, theta5, theta6 = sp.symbols('theta1 theta2 theta3 theta4 theta5 theta6')
# Constants (undecided)
a1, a2, a3 = sp.symbols('a1 a2 a3')
d1, d4, d6 = sp.symbols('d1 d4 d6')
# Constants (predetermined)
d2, d3, d5, a4, a5, a6, alpha2, alpha6 = 0, 0, 0, 0, 0, 0, 0, 0
alpha1, alpha3, alpha5 = (sp.pi/2,)*3
alpha4 = -sp.pi/2
# rotation around x
def rotx(angle):
return sp.Matrix([[1, 0, 0, 0], [0, sp.cos(angle), -sp.sin(angle), 0], [0, sp.sin(angle), sp.cos(angle), 0], [0, 0, 0, 1]])
# rotation around y
def roty(angle):
return sp.Matrix([[sp.cos(angle), 0, sp.sin(angle), 0], [0, 1, 0, 0], [-sp.sin(angle), 0, sp.cos(angle), 0], [0, 0, 0, 1]])
# rotation around z
def rotz(angle):
return sp.Matrix([[sp.cos(angle), -sp.sin(angle), 0, 0], [sp.sin(angle), sp.cos(angle), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
# Translation in either x, y or z
def trans(x,y,z):
return sp.Matrix([[1, 0, 0, x], [0, 1, 0, y], [0, 0, 0, z], [0, 0, 0, 1]])
def dh_matrix(d, theta, a, alpha):
return trans(0,0,d)*rotz(theta)*trans(a,0,0)*rotx(alpha)
t_0_1 = dh_matrix(d1, theta1, a1, alpha1)
t_1_2 = dh_matrix(d2, theta2, a2, alpha2)
t_2_3 = dh_matrix(d3, theta3, a3, alpha3)
t_3_4 = dh_matrix(d4, theta4, a4, alpha4)
t_4_5 = dh_matrix(d5, theta5, a5, alpha5)
t_5_6 = dh_matrix(d6, theta6, a6, alpha6)
t_0_6 = t_0_1 * t_1_2 * t_2_3 * t_3_4 * t_4_5 * t_5_6
last_column = t_0_6.col(-1)
x = last_column.row(0)
y = last_column.row(1)
z = last_column.row(2)
# Partial derivatives of x
dxd1 = sp.diff(x,theta1)
dxd2 = sp.diff(x,theta2)
dxd3 = sp.diff(x,theta3)
dxd4 = sp.diff(x,theta4)
dxd5 = sp.diff(x,theta5)
dxd6 = sp.diff(x,theta6)
# Partial derivates of y
dyd1 = sp.diff(y,theta1)
dyd2 = sp.diff(y,theta2)
dyd3 = sp.diff(y,theta3)
dyd4 = sp.diff(y,theta4)
dyd5 = sp.diff(y,theta5)
dyd6 = sp.diff(y,theta6)
# Partial derivates of z
dzd1 = sp.diff(z,theta1)
dzd2 = sp.diff(z,theta2)
dzd3 = sp.diff(z,theta3)
dzd4 = sp.diff(z,theta4)
dzd5 = sp.diff(z,theta5)
dzd6 = sp.diff(z,theta6)
jacobian = sp.Matrix([[dxd1,dxd2,dxd3,dxd4,dxd5,dxd6], [dyd1,dyd2,dyd3,dyd4,dyd5,dyd6], [dzd1,dzd2,dzd3,dzd4,dzd5,dzd6]])
jacobian_numeric = sp.lambdify([theta1,theta2,theta3,theta4,theta5,theta6,a1,a2,a3,d1,d4,d6],jacobian)
jacobian_numeric(0,sp.pi/2,0,0,0,0,0,0,0,1,0.3,0.1)
包含数值的3x6矩阵
以下错误归因于最后一行:
AttributeError:“ Mul”对象没有属性“ cos”
我认为这与我如何将函数和多个表达式都扭曲在一起有关,但是我不确定如何找到失败的确切原因。
这是巨大的。我用...
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-21-d0f45c1787c2> in <module>
----> 1 current_jacobian = jacobian_numeric(0,sp.pi/2,0,0,0,0,0,0,0,1,0.3,0.1) # add pi/2 to theta2 because of the initial offset between x_2 and x_3
2 current_jacobian
<lambdifygenerated-1> in _lambdifygenerated(theta1, theta2, theta3, theta4, theta5, theta6, a1, a2, a3, d1, d4, d6)
1 def _lambdifygenerated(theta1, theta2, theta3, theta4, theta5, theta6, a1, a2, a3, d1, d4, d6):
----> 2 return (array([[array([[-a1*sin(theta1) - a2*sin(theta1)*cos(theta2) + a3*sin(theta1)*sin(theta2)*sin(theta3) - a3*sin(theta1)*cos(theta2)*cos(theta3) +...
AttributeError: 'Mul' object has no attribute 'cos'
答案 0 :(得分:1)
使用lambdify
创建可执行函数后,您将不再处于sympy
的“符号数学”世界中,而必须传递数字值,而不是符号值。
您可以将numpy
中的pass传递为np.pi
(如BenT的答案所述),也可以将sympy
中的?数值估算为sp.N(sp.pi)
。 N
函数将符号值转换为数字值,包括?之类的值。
答案 1 :(得分:0)
我并不完全确定为什么,但是当我在代码sp.pi
的最后一行中将np.pi
替换为jacobian_numeric(0,np.pi/2,0,0,0,0,0,0,0,1,0.3,0.1)
时,错误消失了。我已经看到其他几个问题类似的问题,但目前还没有好的答案。这可能是simpy
中的错误,或者希望其他人有更好的解释。