首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更新机器人和对象的位置而不重复其值

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

Stack Overflow用户
提问于 2012-09-26 11:52:51
回答 1查看 532关注 0票数 1

我想知道如何解决值被重复传递给我的activate(valueList) method.The程序的问题,它的工作方式是有一个机器人和一个球,并且主循环不断地传递值列表方法。我的目标是让机器人转动球的方向,然后朝it.The方向移动,问题是,如果球还在移动,这些值保持不变,直到它停止,这会导致机器人转向之前传递的角度。有什么具体的方法可以解决这个问题吗?请注意,即使机器人和球处于静止状态,向下传递的valueList中的值也会区分为+2或-2。PS。我使用的是lego nxt (nxt-python),它通过网络连接到一个向下传递值的摄像机

例如:

返回值的方法:

代码语言:javascript
复制
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

一个activate方法: Ps turn_to和move_to方法显示朝向对象的旋转和移动

代码语言:javascript
复制
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

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-26 12:13:48

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

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12594312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档