![](/img/trans.png)
[英]How do I pass a variable between two functions in tkinter python?
[英]How do I pass an array between two functions in python?
我希望通過三個數組: xLinespace
, yLinespace
和zLinespace
從功能cubicSplineInterpolate
的功能trajectoryMover
。 但是我不確定如何使用python實現此目的。 將數組傳遞給第二個函數后,我打算同時遍歷每個數組以更改機器人的位置。 我是否需要在每個函數中將數組設置為參數?
class example_application:
def cubicSplineInterpolate(self, x_axis, y_axis, z_axis):
m=1
xLinespace=[]
yLinespace=[]
zLinespace=[]
while m<len(x_axis):
for t in np.arange(m-1,m,1/float(100)):
xLinespace.append(self.func(x_axis[m-1],x_axis[m],t,U[m-1],U[m],m-1,m))
yLinespace.append(self.func(y_axis[m-1],y_axis[m],t,V[m-1],V[m],m-1,m))
zLinespace.append(self.func(z_axis[m-1],z_axis[m],t,W[m-1],W[m],m-1,m))
m=m+1
def trajectoryMover(self):
newPose = Pose()
xLinespace=[]
yLinespace=[]
zLinespace=[]
x_axis = [0.01, 0.02, 0.033, 0.0044, 0.0001, 0.10]
y_axis = [0.002, 0.00033, 0.1014, 0.01512, 0.14316, 0.015143]
z_axis = [0.003, 0.2124, 0.15417, 0.15615, 0.01241, 0.151561]
self.cubicSplineInterpolate(x_axis,y_axis,z_axis)
print(self.cubicSplineInterpolate.xLinespace)
for x, y, z in zip(x_axis, y_axis, z_axis):
newPose.position.x = x
newPose.position.y = y
newPose.position.z = z
newPose.orientation.x = -0.907106781172
newPose.orientation.y = -0.0707106781191
newPose.orientation.z = 2.59734823723e-06
newPose.orientation.w = -2.59734823723e-06
self.set_position_cartesian.publish(newPose)
rospy.loginfo(newPose)
rospy.sleep(1)
從評論:如果trajectoryMover
調用cubicSplineInterpolate
類example_application:
def cubicSplineInterpolate(self, x_axis, y_axis, z_axis):
m=1
xLinespace=[]
yLinespace=[]
zLinespace=[]
while m<len(x_axis):
for t in np.arange(m-1,m,1/float(100)):
xLinespace.append(self.func(x_axis[m-1],x_axis[m],t,U[m-1],U[m],m-1,m))
yLinespace.append(self.func(y_axis[m-1],y_axis[m],t,V[m-1],V[m],m-1,m))
zLinespace.append(self.func(z_axis[m-1],z_axis[m],t,W[m-1],W[m],m-1,m))
m=m+1
return(xLinespace, yLinespace, zLinespace)
def trajectoryMover(self):
newPose = Pose()
x_axis = [0.01, 0.02, 0.033, 0.0044, 0.0001, 0.10]
y_axis = [0.002, 0.00033, 0.1014, 0.01512, 0.14316, 0.015143]
z_axis = [0.003, 0.2124, 0.15417, 0.15615, 0.01241, 0.151561]
xLinespace, yLinespace, zLinespace = self.cubicSplineInterpolate(x_axis,y_axis,z_axis)
print(self.cubicSplineInterpolate.xLinespace)
for x, y, z in zip(x_axis, y_axis, z_axis):
newPose.position.x = x
newPose.position.y = y
newPose.position.z = z
newPose.orientation.x = -0.907106781172
newPose.orientation.y = -0.0707106781191
newPose.orientation.z = 2.59734823723e-06
newPose.orientation.w = -2.59734823723e-06
self.set_position_cartesian.publish(newPose)
rospy.loginfo(newPose)
rospy.sleep(1)
還要注意(盡管不是很重要):我相信PEP8表示函數應使用下划線分隔,即trajectory_mover和cubic_spline_interpolate()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.