如何更新机器人和对象的位置而不重复它的值

时间:2012-09-26 03:52:51

标签: python lego nxt

我想知道如何解决重复传递给我的activate(valueList)方法的值的问题。程序以一种机器人和一个球的方式工作,主循环传递值列表方法不断。我的目标是将机器人转向球的方向并朝向它移动。问题是,假设球仍在移动,值保持不变直到它停止,这导致机器人转向之前的角度传了下来。有没有具体的解决方法?请注意,即使机器人和球处于静止状态,valueList中传递的值也会在+2或-2之间进行区分。 PS。我正在使用通过网络连接到传递值的摄像机的lego 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)

1 个答案:

答案 0 :(得分:1)

所以听起来你只想继续改变。在这种情况下,您可能只想保留以前的值并进行比较。然后,仅在检测到更改时调用activate()

last_valueList = []
while True:
    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])
    if valueList != last_valueList
        activate(valueList)
    last_valueList = valueList[:] # copy list