0

ロボットを特定の距離まで動かすコードを添付しているのですが、近づくと動きを止めて障害物を出したいです。どうすればいいですか?超音波を追加して障害物を検出しようとしました。私は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)
4

1 に答える 1

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 を少し調整することができます。

turnのメソッドをBaseMotorに変更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

次に、コードを次のように簡単に記述できます。

both.turn(power=power, tacho_units=tacho_units, brake=False, cancel_when=lambda: ultrasonic.get_sample() < 20)
于 2012-10-10T08:16:12.223 に答える