【发布时间】:2015-02-04 10:35:44
【问题描述】:
我在一个类中有一个函数,它在一个 while 循环中被调用。 此函数获取 2 个 6x6 矩阵并返回一个按 np.linspace 中指定的维度放大的向量,即 6x91。我想通过将这三个数组连接为浮点数将它们存储在一个文件中(使用 savetxt)。下面是一个与类分开运行的函数示例,它运行良好。 当我在循环之前声明列表输出并通过 Tkinter 窗口更新 EulArr 时,问题就出现了,将数组附加到列表中。我只能用 %s 格式保存数组,但不能再用 %f 保存。为什么?
LegForces(self, Li, ti)
import numpy as np
from math import pi
M=100. #total estimated mass to move (upper platform+upper legs+payload)
g=9.81
nsteps= 91
Li=np.matrix([[-10.64569774, -93.1416122, 116.35191853],\
[-10.68368329, 93.17236065, 116.35542498],\
[85.985087,37.35196994, 116.20350534],[-75.34703551,-55.83790049, 115.44528196],\
[-75.33938926, 55.78964226, 115.44457613],[86.0307188,-37.33446016, 116.19929305]])
ti=np.matrix([[88.15843159,88.04450508,50.10006323, -138.28903445, -138.26610178,50.2369224],\
[-108.75675186, 108.84408749, 130.72504635, 21.82594871, -21.97569549,-130.6774372],\
[ 119.40585161, 119.40170883, 119.23577854, 118.41560138, 118.41643529,119.24075525]])
ti=ti.T #transpose the ti for compatible format
x_cm= 1.
y_cm= -1.
z_cm=87.752
z=np.linspace(0,90,nsteps)
Fx=-M*g*np.cos(pi*z/180)
Fy= M*g*np.sin(pi*z/180)
Fz=50 # including braking forces [N]
#specify here the centre of mass coordinates retrieved from Inventor
Mx=Fz*y_cm-Fy*z_cm
My=-Fz*x_cm+Fx*z_cm
Mz=Fy*x_cm-Fx*y_cm
mc=np.zeros((6,3),'float')
ex_forces=np.array([Fx,Fy,Fz,Mx,My,Mz])
for i in range(6):
mc[i,:]=np.cross(ti[i,:],Li[i,:])
r1=[Li[0,0],Li[1,0],Li[2,0],Li[3,0],Li[4,0],Li[5,0]]
r2=[Li[0,1],Li[1,1],Li[2,1],Li[3,1],Li[4,1],Li[5,1]]
r3=[Li[0,2],Li[1,2],Li[2,2],Li[3,2],Li[4,2],Li[5,2]]
r4=[mc[0,0],mc[1,0],mc[2,0],mc[3,0],mc[4,0],mc[5,0]]
r5=[mc[0,1],mc[1,1],mc[2,1],mc[3,1],mc[4,1],mc[5,1]]
r6=[mc[0,2],mc[1,2],mc[2,2],mc[3,2],mc[4,2],mc[5,2]]
DMatrix=np.vstack([r1,r2,r3,r4,r5,r6])
print 'DMatrix form:\n', DMatrix
invD=np.linalg.inv(DMatrix)
print ' inv(DMatrix) is:\n', invD
legF=np.dot(ex_forces,invD)
#slice it!
legF=legF.tolist()
a,b=np.shape(legF)
print 'check identity matrix:\n', np.dot(invD,DMatrix)
print 'leg forces:\n',legF, type(legF)
newlegF=np.reshape(legF,(1,a*b))
strokeValues= np.array([[-0.3595, .1450483, -0.3131,0.4210,-0.0825,.19124]])
print 'strokeValues shape:\n', np.shape(strokeValues)
print 'leg forces vector shape:', np.shape(newlegF)
EulArr=np.array([[0.12,0.2,0,-3.,-1.,15.]])
output=np.concatenate((strokeValues,EulArr,newlegF),axis=1)
np.savetxt('leg_forces.dat', output,fmt=' %f')
print output ,np.shape(output)
该类将如下所示:
class Hexapod(self,....):
output=[]
while 1:
...
LegAxialF=self.LegForces(Li,ti)
#create a list for EUl parameters which can be refreshed
EulArr=np.zeros((6,1),'float')
EulArr[0]=180/pi*self.EulPhi
EulArr[1]=180/pi*self.EulTheta
EulArr[2]=180/pi*self.EulPsi
EulArr[3]=self.EulX
EulArr[4]=self.EulY
EulArr[5]=self.EulZ-self.height
#print meaningful values to the specified file
EulArr=np.reshape(EulArr,(1,6))
EulArrList=EulArr.tolist()
strokes=np.reshape(strokeValues,(1,6))
strokeList=strokes.tolist()
output.append(np.concatenate((strokeList,\
EulArrList,LegFList),axis=1))
np.savetxt('act_lengths.dat', output, fmt='%f')
def LegForces(self, Li, ti):
我收到以下错误:
np.savetxt('act_lengths.dat', output, fmt='%f')
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 1047, in savetxt
fh.write(asbytes(format % tuple(row) + newline))
TypeError: float argument required, not numpy.ndarray
【问题讨论】: