机器人不停

时间:2012-10-10 08:00:59

标签: python nxt lego nxt-python

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

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)

1 个答案:

答案 0 :(得分:1)

您的问题是在机器人移动后只检查障碍物

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。

turnBaseMotor的{​​{1}}方法更改为

motor.py

并将以下代码添加到该方法的def turn(self, power, tacho_units, brake=True, timeout=1, emulate=True, cancel_when=None): 循环中:

while

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

        while True:

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