我想知道我该如何解决反复传递给我的activate(valueList)方法的值的问题。该程序的工作方式是有一个机器人和一个球,并且主循环不断传递值列表方法。我的目标是使机器人朝着球的方向旋转并朝着它的方向移动。问题是,如果球仍在移动,则数值会保持不变,直到停止为止,这会导致机器人转向之前的角度传下来。是否有解决此问题的特定方法?请注意,即使机械手和球静止不动,在valueList中向下传递的值也可以区分为+2或-2。PS。我正在使用通过网络连接到传递值的相机的乐高NXT(NXT-Python)
例如:
返回值的方法:
def updateBallx(valueList): # updates red ball x-axis position ballx = int(valueList[8]) return ballx def updateBally(valueList): # updates red ball y-axis position bally = int(valueList[9]) return bally def updateRobotx(valueList): # updates robot x-axis position robotx = int(valueList[12]) return robotx def updateRoboty(valueList): # updates robot x-axis position roboty = int(valueList[13]) return roboty def updateRobota(valueList): # updates robot angle position robota = int(valueList[14]) return robota
一个激活方法:Ps turn_to和move_to方法显示转向和朝对象移动
def activate(): new_x = updateBallx(valueList) print 'Ball x',new_x new_y = updateBally(valueList) print 'Ball y',new_y old_x = updateRobotx(valueList) print 'Robot x',old_x old_y = updateRoboty(valueList) print 'Robot y',old_y angle = updateRobota(valueList) print 'Robot angle',angle turn_to(brick,new_x, new_y, old_x, old_y, angle) #time.sleep(2) #move_to(brick,new_x, new_y, old_x, old_y) #time.sleep(3) #kickBall(brick,new_y, old_y) #time.sleep(3)
这个主循环不断将值传递给valueList
screenw = 0 screenh = 0 while 1: client_socket.send("loc\n") data = client_socket.recv(8192) valueList = data.split() if (not(valueList[-1] == "eom" and valueList[0] == "start")): #print "continuing.." continue if(screenw != int(valueList[2])): screenw = int(valueList[2]) screenh = int(valueList[3]) activate(valueList)