示例#1
1
def main(robot):
    # Définition des moteurs / capteurs
    m_left = Motor(robot, PORT_B)
    m_right = Motor(robot, PORT_A)

    touch_right = Touch(robot, PORT_2)
    touch_left = Touch(robot, PORT_1)
    ultrason = Ultrasonic(robot, PORT_3)

    DEFAULT_POWER = 80
    DISTANCE_LIMIT = 20
    TURN_TIME = 1.5

    # Début
    while True:
        m_left.run(power=DEFAULT_POWER)
        m_right.run(power=DEFAULT_POWER)

        while not touch_right.is_pressed() and not touch_left.is_pressed() and ultrason.get_distance() > DISTANCE_LIMIT:
            sleep(0.01)

        print("Aieee")
        if touch_right.is_pressed():
            m_left.run(power=DEFAULT_POWER)
            m_left.run(power=-DEFAULT_POWER)
        else:
            m_left.run(power=-DEFAULT_POWER)
            m_left.run(power=DEFAULT_POWER)

        sleep(TURN_TIME)
示例#2
0
#######################################################################
## Then, you can specify what you want the NXT to do
#######################################################################

from time import sleep

from nxt.motor import Motor, PORT_A, PORT_B, PORT_C
from nxt.sensor import Touch, PORT_4, PORT_3

turningMotor = Motor(brick, PORT_B)
walkingMotor = Motor(brick, PORT_C)
turnerSwitch = Touch(brick, PORT_4)
legPosition = Touch(brick, PORT_3)

while True:
    if turnerSwitch.is_pressed() == False:
        turningMotor.run(power=0)
        a = 0
        walkingMotor.run(power=120)

    else:
        if legPosition.is_pressed() == False:
            walkingMotor.run(power=100)
        else:
            a = 1
        if a == 1:
            walkingMotor.run(power=0)
            walkingMotor.brake()
            turningMotor.run(power=120)