我对SymPy,SciPy,NumPy等都很陌生,所以也许有更简单的方法来实现我的目标,即为4-定义一系列索引 4x4转换矩阵连杆。我对3 rd 维度没有任何迫切需要,只是为了便于迭代序列{A 0 ... A 3 < / sub>}在我的代码中。
以下代码会产生以下错误。
TypeError: unorderable types: slice() >= int()
另一方面,用下面的A[:,:,i]
替换A0
确实会产生一个4x4符号数组,所以也许我没有正确定义切片 - 但是,没有其他的切片排列似乎也有效。 (我在最后一行代码中也存在隐含行继续的问题,所以我把所有内容都放在内联。)
我很感激有关如何解决这个问题的任何建议。
from sympy import *
def plg_jac():
a_3, a_4, d_2, theta_1, theta_4 = symbols('a_3, a_4, d_2, theta_1, theta_4')
# The D-H table is defined by the following 4 vectors
a = Matrix([0, 0, a_3, -a_4])
alpha = Matrix([pi/2, pi/2, 0, 0])
d = Matrix([0, d_2, 0, 0])
theta = Matrix([theta_1, pi, 0, theta_4])
# Initialise a mutable 4x4x4 array
A = tensor.array.MutableDenseNDimArray(range(64), (4,4,4))
# Now define A0 ... A3, the transformation matrices between links i & i+1
for i in range(a.shape[0]):
A[:,:,i] = Array([[cos(theta[i]), -sin(theta[i])*cos(alpha[i]), sin(theta[i])*sin(alpha[i]), a[i]*cos(theta[i])], [sin(theta[i]), cos(theta[i])*cos(alpha[i]), -cos(theta[i])*sin(alpha[i]), a[i]*sin(theta[i])], [0, sin(alpha[i]), cos(alpha[i]), d[i]], [0, 0, 0, 1]])
答案 0 :(得分:1)
缺少对SymPy数组切片的分配,你可以通过使用 numpy 解决这个问题。
将NumPy导入为:
import numpy
将SymPy数组转换为NumPy数组(类型为object,以便包含SymPy表达式):
In [18]: A_numpy = numpy.array(A.tolist()).astype(object)
运行for循环的修改版本:
In [19]: for i in range(a.shape[0]):
...: A_numpy[:, :, i] = [[cos(theta[i]), -sin(theta[i])*cos(alpha[i]), s
...: in(theta[i])*sin(alpha[i]), a[i]*cos(theta[i])], [sin(theta[i]), cos(th
...: eta[i])*cos(alpha[i]), -cos(theta[i])*sin(alpha[i]), a[i]*sin(theta[i])
...: ], [0, sin(alpha[i]), cos(alpha[i]), d[i]], [0, 0, 0, 1]]
...:
将生成的NumPy数组转换回SymPy数组:
In [20]: A = Array(A_numpy)
打印它的内容:
In [21]: A
Out[21]:
⎡⎡cos(θ₁) -1 1 cos(θ₄) ⎤ ⎡sin(θ₁) 0 0 sin(θ₄) ⎤ ⎡0 0 0 0⎤
⎢⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢⎢ 0 0 0 -sin(θ₄) ⎥ ⎢ 0 0 1 cos(θ₄) ⎥ ⎢1 1 0 0⎥
⎢⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢⎢sin(θ₁) 0 0 0 ⎥ ⎢-cos(θ₁) 1 0 0 ⎥ ⎢0 0 1 1⎥
⎢⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎣⎣ 0 0 a₃ -a₄⋅cos(θ₄)⎦ ⎣ 0 0 0 -a₄⋅sin(θ₄)⎦ ⎣0 d₂ 0 0⎦
⎡0 0 0 0⎤⎤
⎢ ⎥⎥
⎢0 0 0 0⎥⎥
⎢ ⎥⎥
⎢0 0 0 0⎥⎥
⎢ ⎥⎥
⎣1 1 1 1⎦⎦