首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >机器人不会停止

机器人不会停止
EN

Stack Overflow用户
提问于 2012-10-10 16:00:59
回答 1查看 424关注 0票数 0

附件是一个代码,移动机器人到一个特定的距离,但我希望它停止移动,因为它接近和障碍。我该怎么做呢?我试着用超声波来检测障碍物。我使用的是nxt-python

代码语言:javascript
复制
def move_to(brick, bx, by ,rx, ry):
    wheel_circumference = (pi * wheel_diameter)
    distance_per_turn = (wheel_circumference / 360)
    distance = math.sqrt((math.pow((bx - rx),2)) + (math.pow((by - ry),2)))
    rotations = ((distance / distance_per_turn) / 360)
    tacho_units = (round((rotations) * 360))
    both.turn(power=power, tacho_units=tacho_units, brake=False)
    if(ultrasonic.get_sample() < 20):
        both.brake()

def activate2():

    update_coordinates()
    bx,by = get_ballxy()
    rx,ry,a = get_robotxya()

    if(ultrasonic.get_sample() < 15):
        both.turn(power=-65, tacho_units=380, brake= False)

    time.sleep(1)
    turn_to(brick,bx,by,rx,ry,a)
    time.sleep(0.5)
    move_to(brick,bx,by,rx,ry)
    kickBall(brick,by,ry)


Thread(target=update_coordinates).start()
connect()
update_coordinates()
while True:
    #activate()
    activate2()
    time.sleep(3)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-10 16:16:12

你的问题是,你只在你的机器人移动后检查一次障碍物。

代码语言:javascript
复制
both.turn(power=power, tacho_units=tacho_units, brake=False)
# the turn function blocks, so this check comes to late
if(ultrasonic.get_sample() < 20): 
    both.brake()

你应该在另一个线程中不断地检查障碍。

为了让事情变得更简单,你可以稍微调整一下nxc-python。

motor.pyBaseMotorturn方法更改为

代码语言:javascript
复制
def turn(self, power, tacho_units, brake=True, timeout=1, emulate=True, cancel_when=None):

并将以下代码添加到该方法中的while循环中:

代码语言:javascript
复制
        while True:

            # these lines are new
            if cancel_when and cancel_when():
                break

然后,您可以轻松地将您的代码编写为:

代码语言:javascript
复制
both.turn(power=power, tacho_units=tacho_units, brake=False, cancel_when=lambda: ultrasonic.get_sample() < 20)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12814656

复制
相关文章

相似问题

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