コード例 #1
0
class Gripp3r(IRBeaconRemoteControlledTank):
    def __init__(self,
                 left_motor_port: str = OUTPUT_B,
                 right_motor_port: str = OUTPUT_C,
                 grip_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        super().__init__(left_motor_port=left_motor_port,
                         right_motor_port=right_motor_port,
                         ir_sensor_port=ir_sensor_port,
                         ir_beacon_channel=ir_beacon_channel)

        self.grip_motor = MediumMotor(address=grip_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.speaker = Sound()

    def grip_or_release_by_ir_beacon(self, speed: float = 50):
        if self.ir_sensor.beacon(channel=self.ir_beacon_channel):
            if self.touch_sensor.is_pressed:
                self.speaker.play_file(
                    wav_file='/home/robot/sound/Air release.wav',
                    volume=100,
                    play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)

                self.grip_motor.on_for_seconds(speed=speed,
                                               seconds=1,
                                               brake=True,
                                               block=True)

            else:
                self.speaker.play_file(
                    wav_file='/home/robot/sound/Airbrake.wav',
                    volume=100,
                    play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)

                self.grip_motor.on(speed=-speed, brake=False, block=False)

                self.touch_sensor.wait_for_pressed()

                self.grip_motor.off(brake=True)

            while self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                pass

    def main(self, speed: float = 100):
        self.grip_motor.on_for_seconds(speed=-50,
                                       seconds=1,
                                       brake=True,
                                       block=True)

        while True:
            self.drive_once_by_ir_beacon(speed=speed)

            self.grip_or_release_by_ir_beacon()
class Sweep3r(IRBeaconRemoteControlledTank):
    def __init__(self,
                 left_foot_motor_port: str = OUTPUT_B,
                 right_foot_motor_port: str = OUTPUT_C,
                 medium_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        super().__init__(left_motor_port=left_foot_motor_port,
                         right_motor_port=right_foot_motor_port,
                         ir_sensor_port=ir_sensor_port,
                         ir_beacon_channel=ir_beacon_channel)

        self.medium_motor = MediumMotor(address=medium_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.speaker = Sound()

    def drill(self, speed: float = 100):
        while True:
            if self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                self.medium_motor.on_for_rotations(speed=speed,
                                                   rotations=2,
                                                   block=True,
                                                   brake=True)

    def move_when_touched(self):
        while True:
            if self.touch_sensor.is_pressed:
                self.tank_driver.on_for_seconds(left_speed=100,
                                                right_speed=-100,
                                                seconds=2,
                                                brake=True,
                                                block=True)

    def move_when_see_smothing(self):
        while True:
            if self.color_sensor.reflected_light_intensity > 30:
                self.tank_driver.on_for_seconds(left_speed=-100,
                                                right_speed=100,
                                                seconds=2,
                                                brake=True,
                                                block=True)

    def main(self, speed: float = 100):
        Thread(target=self.move_when_touched).start()

        Thread(target=self.move_when_see_smothing).start()

        Thread(target=self.drill).start()

        self.keep_driving_by_ir_beacon(speed=speed)
コード例 #3
0
class Remote():
    def __init__(self):
        self.ir = InfraredSensor()
        self.ir.mode = 'IR-REMOTE'
        self.drive = Driver()
        self.ir.on_channel1_beacon = self.beacon_channel_1_action
        self.ir.on_channel1_top_left = self.top_left_channel_1_action
        self.ir.on_channel1_bottom_left = self.bot_left_channel_1_action
        self.ir.on_channel1_top_right = self.top_right_channel_1_action
        self.ir.on_channel1_bottom_right = self.bot_right_channel_1_action

    def beacon_channel_1_action(self, state):
        print(self.ir.beacon())
        if state:
            print("Beacon pressed, now stopping")
            self.drive.stop()

    def top_left_channel_1_action(self, state):
        print(self.ir.top_left())
        while state:
            self.drive.move_cm(5)
        self.drive.stop()

    def bot_left_channel_1_action(self, state):
        print(self.ir.bottom_left())
        while state:
            self.drive.move_neg_cm(5)
        self.drive.stop()

    def top_right_channel_1_action(self, state):
        print(self.ir.top_right())
        while state:
            self.drive.turn_degrees(10)
        self.drive.stop()

    def bot_right_channel_1_action(self, state):
        print(self.ir.bottom_right())
        while state:
            self.drive.turn_neg_degrees(10)
        self.drive.stop()

    def remote(self):
        try:
            while True:
                self.ir.process()
                time.sleep(0.01)
        except Exception as e:
            print(e)
            self.drive.stop()
コード例 #4
0
class Ev3rstorm:
    def __init__(self,
                 left_foot_motor_port: str = OUTPUT_B,
                 right_foot_motor_port: str = OUTPUT_C,
                 bazooka_blast_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        self.tank_driver = MoveTank(left_motor_port=left_foot_motor_port,
                                    right_motor_port=right_foot_motor_port,
                                    motor_class=LargeMotor)

        self.bazooka_blast_motor = MediumMotor(
            address=bazooka_blast_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.leds = Leds()
        self.speaker = Sound()
        self.screen = Display()

    def drive_once_by_ir_beacon(self, speed: float = 100):
        # forward
        if self.ir_sensor.top_left(
                channel=self.ir_beacon_channel) and self.ir_sensor.top_right(
                    channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=speed, right_speed=speed)

        # backward
        elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel
                                        ) and self.ir_sensor.bottom_right(
                                            channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=-speed, right_speed=-speed)

        # turn left on the spot
        elif self.ir_sensor.top_left(channel=self.ir_beacon_channel
                                     ) and self.ir_sensor.bottom_right(
                                         channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=-speed, right_speed=speed)

        # turn right on the spot
        elif self.ir_sensor.top_right(
                channel=self.ir_beacon_channel) and self.ir_sensor.bottom_left(
                    channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=speed, right_speed=-speed)

        # turn left forward
        elif self.ir_sensor.top_left(channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=0, right_speed=speed)

        # turn right forward
        elif self.ir_sensor.top_right(channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=speed, right_speed=0)

        # turn left backward
        elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=0, right_speed=-speed)

        # turn right backward
        elif self.ir_sensor.bottom_right(channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=-speed, right_speed=0)

        # otherwise stop
        else:
            self.tank_driver.off(brake=False)

    def dance_if_ir_beacon_pressed(self):
        while self.ir_sensor.beacon(channel=self.ir_beacon_channel):
            self.tank_driver.on_for_seconds(left_speed=randint(-100, 100),
                                            right_speed=randint(-100, 100),
                                            seconds=1,
                                            brake=False,
                                            block=True)

    def detect_object_by_ir_sensor(self):
        if self.ir_sensor.proximity < 25:
            self.leds.animate_police_lights(color1=Leds.ORANGE,
                                            color2=Leds.RED,
                                            group1=Leds.LEFT,
                                            group2=Leds.RIGHT,
                                            sleeptime=0.5,
                                            duration=5,
                                            block=False)

            self.speaker.play_file(wav_file='/home/robot/sound/Object.wav',
                                   volume=100,
                                   play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

            self.speaker.play_file(wav_file='/home/robot/sound/Detected.wav',
                                   volume=100,
                                   play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

            self.speaker.play_file(
                wav_file='/home/robot/sound/Error alarm.wav',
                volume=100,
                play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

        else:
            self.leds.all_off()

    def blast_bazooka_if_touched(self):
        if self.touch_sensor.is_pressed:
            if self.color_sensor.ambient_light_intensity < 5:  # 15 not dark enough
                self.speaker.play_file(wav_file='/home/robot/sound/Up.wav',
                                       volume=100,
                                       play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

                self.bazooka_blast_motor.on_for_rotations(speed=100,
                                                          rotations=-3,
                                                          brake=True,
                                                          block=True)

                self.speaker.play_file(
                    wav_file='/home/robot/sound/Laughing 1.wav',
                    volume=100,
                    play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

            else:
                self.speaker.play_file(wav_file='/home/robot/sound/Down.wav',
                                       volume=100,
                                       play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

                self.bazooka_blast_motor.on_for_rotations(speed=100,
                                                          rotations=3,
                                                          brake=True,
                                                          block=True)

                self.speaker.play_file(
                    wav_file='/home/robot/sound/Laughing 2.wav',
                    volume=100,
                    play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

            self.touch_sensor.wait_for_released()

    def main(self, driving_speed: float = 100):
        self.screen.image_filename(filename='/home/robot/image/Target.bmp',
                                   clear_screen=True)
        self.screen.update()

        while True:
            self.drive_once_by_ir_beacon(speed=driving_speed)

            self.dance_if_ir_beacon_pressed()

            # DON'T use IR Sensor in 2 different modes in the same program / loop
            # - https://github.com/pybricks/support/issues/62
            # - https://github.com/ev3dev/ev3dev/issues/1401
            # self.detect_object_by_ir_sensor()

            self.blast_bazooka_if_touched()
コード例 #5
0
class Remote():
    def __init__(self):
        self.ir = InfraredSensor()
        self.ir.mode = 'IR-REMOTE'
        self.drive = Driver()
        self.ir.on_channel1_beacon = self.beacon_channel_1_action
        self.ir.on_channel1_top_left = self.top_left_channel_1_action
        self.ir.on_channel1_bottom_left = self.bot_left_channel_1_action
        self.ir.on_channel1_top_right = self.top_right_channel_1_action
        self.ir.on_channel1_bottom_right = self.bot_right_channel_1_action
        self.name = sys.argv[1]
        self.ghost = []
        self.now = 0

    def beacon_channel_1_action(self, state):
        print(self.ir.beacon())
        if state:
            print("Beacon pressed, now stopping")
            with open(self.name + '.pkl', 'wb') as f:
                pickle.dump(self.ghost, f)

    def top_left_channel_1_action(self, state):
        print(self.ir.top_left())
        if state:
            self.now = time.time()
            self.drive.move()
        else:
            self.ghost.append(('forward', time.time() - self.now))
            self.drive.stop()

    def bot_left_channel_1_action(self, state):
        print(self.ir.bottom_left())
        if state:
            self.now = time.time()
            self.drive.reverse()
        else:
            self.ghost.append(('backward', time.time() - self.now))
            self.drive.stop()

    def top_right_channel_1_action(self, state):
        print(self.ir.top_right())
        if state:
            self.now = time.time()
            self.drive.turn(100)
        else:
            self.ghost.append(('right', time.time() - self.now))
            self.drive.stop()

    def bot_right_channel_1_action(self, state):
        print(self.ir.bottom_right())
        if state:
            self.now = time.time()
            self.drive.turn(-100)
        else:
            self.ghost.append(('left', time.time() - self.now))
            self.drive.stop()

    def remote(self):
        try:
            while True:
                self.ir.process()
                time.sleep(0.01)
        except Exception as e:
            print(e)
            self.drive.stop()
class Kraz33Hors3:
    def __init__(self,
                 back_foot_motor_port: str = OUTPUT_B,
                 front_foot_motor_port: str = OUTPUT_C,
                 gear_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        self.tank_driver = MoveTank(left_motor_port=back_foot_motor_port,
                                    right_motor_port=front_foot_motor_port,
                                    motor_class=LargeMotor)

        self.gear_motor = MediumMotor(address=gear_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

    def drive_once_by_ir_beacon(self, speed: float = 100):
        # forward
        if self.ir_sensor.top_left(
                channel=self.ir_beacon_channel) and self.ir_sensor.top_right(
                    channel=self.ir_beacon_channel):
            self.tank_driver.on_for_seconds(left_speed=speed,
                                            right_speed=-speed,
                                            seconds=1,
                                            brake=False,
                                            block=True)

        # backward
        elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel
                                        ) and self.ir_sensor.bottom_right(
                                            channel=self.ir_beacon_channel):
            self.tank_driver.on_for_seconds(left_speed=-speed,
                                            right_speed=speed,
                                            seconds=1,
                                            brake=False,
                                            block=True)

        # move crazily
        elif self.ir_sensor.beacon(channel=self.ir_beacon_channel):
            self.gear_motor.on(speed=speed, brake=False, block=False)

            self.tank_driver.on_for_seconds(left_speed=-speed / 3,
                                            right_speed=-speed / 3,
                                            seconds=1,
                                            brake=False,
                                            block=True)

        else:
            self.gear_motor.off(brake=False)

    def keep_driving_by_ir_beacon(self, speed: float = 100):
        while True:
            self.drive_once_by_ir_beacon(speed=speed)

    def back_whenever_touched(self, speed: float = 100):
        while True:
            if self.touch_sensor.is_pressed:
                self.tank_driver.on_for_seconds(left_speed=-speed,
                                                right_speed=speed,
                                                seconds=1,
                                                brake=False,
                                                block=True)

    def main(self):
        Process(target=self.back_whenever_touched, daemon=True).start()

        self.keep_driving_by_ir_beacon()
コード例 #7
0
class Ev3rstorm:
    def __init__(self,
                 left_foot_motor_port: str = OUTPUT_B,
                 right_foot_motor_port: str = OUTPUT_C,
                 bazooka_blast_motor_port: str = OUTPUT_A,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        self.tank_driver = MoveTank(left_motor_port=left_foot_motor_port,
                                    right_motor_port=right_foot_motor_port,
                                    motor_class=LargeMotor)

        self.bazooka_blast_motor = MediumMotor(
            address=bazooka_blast_motor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.leds = Leds()
        self.speaker = Sound()

    def seek_wheeler(self):
        self.leds.animate_rainbow(group1=Leds.LEFT,
                                  group2=Leds.RIGHT,
                                  increment_by=0.1,
                                  sleeptime=0.5,
                                  duration=5,
                                  block=True)

        if self.ir_sensor.beacon(channel=self.ir_beacon_channel):
            heading_difference = self.ir_sensor.heading(
                channel=self.ir_beacon_channel) - (-3)

            proximity_difference = self.ir_sensor.distance(
                channel=self.ir_beacon_channel) - 70

            if (heading_difference == 0) and (proximity_difference == 0):
                self.tank_driver.off(brake=True)

                self.leds.animate_rainbow(group1=Leds.LEFT,
                                          group2=Leds.RIGHT,
                                          increment_by=0.1,
                                          sleeptime=0.5,
                                          duration=5,
                                          block=True)

                self.bazooka_blast_motor.on_for_rotations(speed=100,
                                                          rotations=3,
                                                          brake=True,
                                                          block=True)

                self.speaker.play_file(
                    wav_file='/home/robot/sound/Laughing 2.wav',
                    volume=100,
                    play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

            else:
                # FIXME: make it work
                self.tank_driver.on(left_speed=max(
                    min((3 * proximity_difference + 4 * heading_difference) /
                        5, 100), -100),
                                    right_speed=max(
                                        min((3 * proximity_difference -
                                             4 * heading_difference) / 5, 100),
                                        -100))

        else:
            self.tank_driver.off(brake=True)

    def main(self):
        while True:
            self.seek_wheeler()
コード例 #8
0
from ev3dev2.sensor import INPUT_4
from ev3dev2.sensor.lego import InfraredSensor
from ev3dev2.sound import Sound

MEDIUM_MOTOR = MediumMotor(address=OUTPUT_A)

IR_SENSOR = InfraredSensor(address=INPUT_4)

SPEAKER = Sound()

is_gripping = False

MEDIUM_MOTOR.on_for_seconds(speed=50, seconds=1, brake=True, block=True)

while True:
    if IR_SENSOR.beacon(channel=1):
        if is_gripping:
            MEDIUM_MOTOR.on_for_seconds(speed=100,
                                        seconds=2,
                                        brake=True,
                                        block=True)

            SPEAKER.play_file(wav_file='/home/robot/sound/Air release.wav',
                              volume=100,
                              play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)

            is_gripping = False

        else:
            MEDIUM_MOTOR.on_for_seconds(speed=-100,
                                        seconds=2,
コード例 #9
0
class Spik3r:
    def __init__(self,
                 claw_motor_port: str = OUTPUT_A,
                 move_motor_port: str = OUTPUT_B,
                 sting_motor_port: str = OUTPUT_D,
                 touch_sensor_port: str = INPUT_1,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        self.claw_motor = MediumMotor(address=claw_motor_port)
        self.move_motor = LargeMotor(address=move_motor_port)
        self.sting_motor = LargeMotor(address=sting_motor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.touch_sensor = TouchSensor(address=touch_sensor_port)

        self.speaker = Sound()

    def snap_claw_if_touched(self):
        if self.touch_sensor.is_pressed:
            self.claw_motor.on_for_seconds(speed=50,
                                           seconds=1,
                                           brake=True,
                                           block=True)

            self.claw_motor.on_for_seconds(speed=-50,
                                           seconds=0.3,
                                           brake=True,
                                           block=True)

    def move_by_ir_beacon(self):
        if self.ir_sensor.top_left(channel=self.ir_beacon_channel) and \
                self.ir_sensor.top_right(channel=self.ir_beacon_channel):
            self.move_motor.on(speed=100, block=False, brake=False)

        elif self.ir_sensor.top_right(channel=self.ir_beacon_channel):
            self.move_motor.on(speed=-100, brake=False, block=False)

        else:
            self.move_motor.off(brake=False)

    def sting_by_ir_beacon(self):
        if self.ir_sensor.beacon(channel=self.ir_beacon_channel):
            self.sting_motor.on_for_degrees(speed=-75,
                                            degrees=220,
                                            brake=True,
                                            block=False)

            self.speaker.play_file(wav_file='Blip 3.wav',
                                   volume=100,
                                   play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

            self.sting_motor.on_for_seconds(speed=-100,
                                            seconds=1,
                                            brake=True,
                                            block=True)

            self.sting_motor.on_for_seconds(speed=40,
                                            seconds=1,
                                            brake=True,
                                            block=True)

            while self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                pass

    def main(self):
        while True:
            self.snap_claw_if_touched()
            self.move_by_ir_beacon()
            self.sting_by_ir_beacon()
コード例 #10
0
class Dinor3x:
    FAST_WALK_SPEED = 80
    NORMAL_WALK_SPEED = 40
    SLOW_WALK_SPEED = 20

    def __init__(self,
                 jaw_motor_port: str = OUTPUT_A,
                 left_motor_port: str = OUTPUT_B,
                 right_motor_port: str = OUTPUT_C,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        self.jaw_motor = MediumMotor(address=jaw_motor_port)

        self.left_motor = LargeMotor(address=left_motor_port)
        self.right_motor = LargeMotor(address=right_motor_port)
        self.tank_driver = MoveTank(left_motor_port=left_motor_port,
                                    right_motor_port=right_motor_port,
                                    motor_class=LargeMotor)
        self.steer_driver = MoveSteering(left_motor_port=left_motor_port,
                                         right_motor_port=right_motor_port,
                                         motor_class=LargeMotor)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.speaker = Sound()

        self.roaring = False
        self.walk_speed = self.NORMAL_WALK_SPEED

    def roar_by_ir_beacon(self):
        """
        Dinor3x roars when the Beacon button is pressed
        """
        if self.ir_sensor.beacon(channel=self.ir_beacon_channel):
            self.roaring = True
            self.open_mouth()
            self.roar()

        elif self.roaring:
            self.roaring = False
            self.close_mouth()

    def change_speed_by_color(self):
        """
        Dinor3x changes its speed when detecting some colors
        - Red: walk fast
        - Green: walk normally
        - White: walk slowly
        """
        if self.color_sensor.color == ColorSensor.COLOR_RED:
            self.speaker.speak(text='RUN!',
                               volume=100,
                               play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)
            self.walk_speed = self.FAST_WALK_SPEED
            self.walk(speed=self.walk_speed)

        elif self.color_sensor.color == ColorSensor.COLOR_GREEN:
            self.speaker.speak(text='Normal',
                               volume=100,
                               play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)
            self.walk_speed = self.NORMAL_WALK_SPEED
            self.walk(speed=self.walk_speed)

        elif self.color_sensor.color == ColorSensor.COLOR_WHITE:
            self.speaker.speak(text='slow...',
                               volume=100,
                               play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)
            self.walk_speed = self.SLOW_WALK_SPEED
            self.walk(speed=self.walk_speed)

    def walk_by_ir_beacon(self):
        """
        Dinor3x walks or turns according to instructions from the IR Beacon
        - 2 top/up buttons together: walk forward
        - 2 bottom/down buttons together: walk backward
        - Top Left / Red Up: turn left on the spot
        - Top Right / Blue Up: turn right on the spot
        - Bottom Left / Red Down: stop
        - Bottom Right / Blue Down: calibrate to make the legs straight
        """

        # forward
        if self.ir_sensor.top_left(channel=self.ir_beacon_channel) and \
                self.ir_sensor.top_right:
            self.walk(speed=self.walk_speed)

        # backward
        elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel) and \
                self.ir_sensor.bottom_right(channel=self.ir_beacon_channel):
            self.walk(speed=-self.walk_speed)

        # turn left on the spot
        elif self.ir_sensor.top_left(channel=self.ir_beacon_channel):
            self.turn(speed=self.walk_speed)

        # turn right on the spot
        elif self.ir_sensor.top_right(channel=self.ir_beacon_channel):
            self.turn(speed=-self.walk_speed)

        # stop
        elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel):
            self.tank_driver.off(brake=True)

        # calibrate legs
        elif self.ir_sensor.bottom_right(channel=self.ir_beacon_channel):
            self.calibrate_legs()

    def calibrate_legs(self):
        self.tank_driver.on(left_speed=10, right_speed=20)

        self.touch_sensor.wait_for_released()

        self.tank_driver.off(brake=True)

        self.left_motor.on(speed=40)

        self.touch_sensor.wait_for_pressed()

        self.left_motor.off(brake=True)

        self.left_motor.on_for_rotations(rotations=-0.2,
                                         speed=50,
                                         brake=True,
                                         block=True)

        self.right_motor.on(speed=40)

        self.touch_sensor.wait_for_pressed()

        self.right_motor.off(brake=True)

        self.right_motor.on_for_rotations(rotations=-0.2,
                                          speed=50,
                                          brake=True,
                                          block=True)

        self.left_motor.reset()
        self.right_motor.reset()

    def walk(self, speed: float = 100):
        self.calibrate_legs()

        self.steer_driver.on(steering=0, speed=-speed)

    def turn(self, speed: float = 100):
        self.calibrate_legs()

        if speed >= 0:
            self.left_motor.on_for_degrees(degrees=180,
                                           speed=speed,
                                           brake=True,
                                           block=True)

        else:
            self.right_motor.on_for_degrees(degrees=180,
                                            speed=-speed,
                                            brake=True,
                                            block=True)

        self.tank_driver.on(left_speed=speed, right_speed=-speed)

    def close_mouth(self):
        self.jaw_motor.on_for_seconds(speed=-20,
                                      seconds=1,
                                      brake=False,
                                      block=False)

    def open_mouth(self):
        self.jaw_motor.on_for_seconds(speed=20,
                                      seconds=1,
                                      block=False,
                                      brake=False)

    def roar(self):
        self.speaker.play_file(wav_file='T-rex roar.wav',
                               volume=100,
                               play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)

        self.jaw_motor.on_for_degrees(speed=40,
                                      degrees=-60,
                                      block=True,
                                      brake=True)

        for i in range(12):
            self.jaw_motor.on_for_seconds(speed=-40,
                                          seconds=0.05,
                                          block=True,
                                          brake=True)

            self.jaw_motor.on_for_seconds(speed=40,
                                          seconds=0.05,
                                          block=True,
                                          brake=True)

        self.jaw_motor.on_for_seconds(speed=20,
                                      seconds=0.5,
                                      brake=False,
                                      block=True)

    def main(self):
        self.close_mouth()

        while True:
            self.roar_by_ir_beacon()
            self.change_speed_by_color()
            self.walk_by_ir_beacon()
コード例 #11
0
class SuperTurtle:
    def __init__(self,
                 left_leg_motor_port: str = OUTPUT_B,
                 right_leg_motor_port: str = OUTPUT_C,
                 shooting_motor_port: str = OUTPUT_A,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1,
                 touch_sensor_port: str = INPUT_1):
        self.tank_driver = MoveTank(left_motor_port=left_leg_motor_port,
                                    right_motor_port=right_leg_motor_port,
                                    motor_class=LargeMotor)

        self.shooting_motor = MediumMotor(address=shooting_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.speaker = Sound()

    def drive_once_by_ir_beacon(self, channel: int = 1, speed: float = 100):
        if self.ir_sensor.top_left(
                channel=self.ir_beacon_channel) and self.ir_sensor.top_right(
                    channel=self.ir_beacon_channel):
            # go forward
            self.tank_driver.on_for_seconds(left_speed=-speed,
                                            right_speed=0,
                                            seconds=1,
                                            brake=True,
                                            block=True)

            self.tank_driver.on_for_seconds(left_speed=0,
                                            right_speed=-speed,
                                            seconds=1,
                                            brake=True,
                                            block=True)

        elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel
                                        ) and self.ir_sensor.bottom_right(
                                            channel=self.ir_beacon_channel):
            # go backward
            self.tank_driver.on_for_seconds(left_speed=speed,
                                            right_speed=0,
                                            seconds=1,
                                            brake=True,
                                            block=True)

            self.tank_driver.on_for_seconds(left_speed=0,
                                            right_speed=speed,
                                            seconds=1,
                                            brake=True,
                                            block=True)

        elif self.ir_sensor.top_left(channel=self.ir_beacon_channel
                                     ) and self.ir_sensor.bottom_right(
                                         channel=self.ir_beacon_channel):
            # turn around left
            self.tank_driver.on_for_seconds(left_speed=0,
                                            right_speed=-speed,
                                            seconds=1,
                                            brake=True,
                                            block=True)

            self.tank_driver.on_for_seconds(left_speed=speed,
                                            right_speed=0,
                                            seconds=1,
                                            brake=True,
                                            block=True)

        elif self.ir_sensor.top_right(
                channel=self.ir_beacon_channel) and self.ir_sensor.bottom_left(
                    channel=self.ir_beacon_channel):
            # turn around right
            self.tank_driver.on_for_seconds(left_speed=-speed,
                                            right_speed=0,
                                            seconds=1,
                                            brake=True,
                                            block=True)

            self.tank_driver.on_for_seconds(left_speed=0,
                                            right_speed=speed,
                                            seconds=1,
                                            brake=True,
                                            block=True)

        elif self.ir_sensor.top_left(channel=self.ir_beacon_channel):
            # turn left
            self.tank_driver.on(left_speed=0, right_speed=-speed)

        elif self.ir_sensor.top_right(channel=self.ir_beacon_channel):
            # turn right
            self.tank_driver.on(left_speed=-speed, right_speed=0)

        elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel):
            # left backward
            self.tank_driver.on(left_speed=0, right_speed=speed)

        elif self.ir_sensor.bottom_right(channel=self.ir_beacon_channel):
            # right backward
            self.tank_driver.on(left_speed=speed, right_speed=0)

        else:
            self.tank_driver.off(brake=False)

    def shoot_objects_by_ir_beacon(self, channel: int = 1, speed: float = 1):
        if self.ir_sensor.beacon(channel=channel):
            self.shooting_motor.on_for_rotations(speed=speed,
                                                 rotations=6,
                                                 block=True,
                                                 brake=True)

            while self.ir_sensor.beacon(channel=channel):
                pass

        else:
            self.shooting_motor.off(brake=False)

    def seek_the_fruit(self, distance: float = 10):
        if self.ir_sensor.proximity <= distance:
            self.speaker.play_file(wav_file='/home/robot/sound/Fanfare.wav',
                                   volume=100,
                                   play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

    def run_if_chased(self, speed: float = 100, how_many_steps: int = 3):
        if self.touch_sensor.is_pressed:
            # go forward
            for i in range(how_many_steps):
                self.tank_driver.on_for_seconds(left_speed=-speed,
                                                right_speed=0,
                                                seconds=1,
                                                brake=True,
                                                block=True)

                self.tank_driver.on_for_seconds(left_speed=0,
                                                right_speed=-speed,
                                                seconds=1,
                                                brake=True,
                                                block=True)
コード例 #12
0
class Ev3rstorm(RemoteControlledTank):
    def __init__(self,
                 left_foot_motor_port: str = OUTPUT_B,
                 right_foot_motor_port: str = OUTPUT_C,
                 bazooka_blast_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        super().__init__(left_motor_port=left_foot_motor_port,
                         right_motor_port=right_foot_motor_port,
                         polarity=Motor.POLARITY_NORMAL,
                         speed=1000,
                         channel=ir_beacon_channel)

        self.tank_driver = MoveTank(left_motor_port=left_foot_motor_port,
                                    right_motor_port=right_foot_motor_port,
                                    motor_class=LargeMotor)

        self.bazooka_blast_motor = MediumMotor(
            address=bazooka_blast_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.leds = Leds()
        self.speaker = Sound()

    def dance_whenever_ir_beacon_pressed(self):
        while True:
            while self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                self.tank_driver.on_for_seconds(left_speed=randint(-100, 100),
                                                right_speed=randint(-100, 100),
                                                seconds=1,
                                                brake=False,
                                                block=True)

    def keep_detecting_objects_by_ir_sensor(self):
        while True:
            if self.ir_sensor.proximity < 25:
                self.leds.animate_police_lights(color1=Leds.ORANGE,
                                                color2=Leds.RED,
                                                group1=Leds.LEFT,
                                                group2=Leds.RIGHT,
                                                sleeptime=0.5,
                                                duration=5,
                                                block=False)

                self.speaker.play_file(wav_file='/home/robot/sound/Object.wav',
                                       volume=100,
                                       play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

                self.speaker.play_file(
                    wav_file='/home/robot/sound/Detected.wav',
                    volume=100,
                    play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

                self.speaker.play_file(
                    wav_file='/home/robot/sound/Error alarm.wav',
                    volume=100,
                    play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

            else:
                self.leds.all_off()

    def blast_bazooka_whenever_touched(self):
        while True:
            if self.touch_sensor.is_pressed:
                if self.color_sensor.ambient_light_intensity < 5:  # 15 not dark enough
                    self.speaker.play_file(
                        wav_file='/home/robot/sound/Up.wav',
                        volume=100,
                        play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

                    self.bazooka_blast_motor.on_for_rotations(speed=100,
                                                              rotations=-3,
                                                              brake=True,
                                                              block=True)

                else:
                    self.speaker.play_file(
                        wav_file='/home/robot/sound/Down.wav',
                        volume=100,
                        play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

                    self.bazooka_blast_motor.on_for_rotations(speed=100,
                                                              rotations=3,
                                                              brake=True,
                                                              block=True)

                self.touch_sensor.wait_for_released()

    def main(self):
        Thread(target=self.dance_whenever_ir_beacon_pressed,
               daemon=True).start()

        # DON'T use IR Sensor in 2 different modes in the same program / loop
        # - https://github.com/pybricks/support/issues/62
        # - https://github.com/ev3dev/ev3dev/issues/1401
        # Thread(target=self.keep_detecting_objects_by_ir_sensor).start()

        Thread(target=self.blast_bazooka_whenever_touched, daemon=True).start()

        super().main()  # RemoteControlledTank.main()
コード例 #13
0
class Sweep3r(IRBeaconRemoteControlledTank):
    def __init__(self,
                 left_foot_motor_port: str = OUTPUT_B,
                 right_foot_motor_port: str = OUTPUT_C,
                 medium_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        super().__init__(left_motor_port=left_foot_motor_port,
                         right_motor_port=right_foot_motor_port,
                         ir_sensor_port=ir_sensor_port,
                         ir_beacon_channel=ir_beacon_channel)

        self.medium_motor = MediumMotor(address=medium_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.screen = Display()
        self.speaker = Sound()

    def drill(self, speed: float = 100):
        if self.ir_sensor.beacon(channel=self.ir_beacon_channel):
            self.medium_motor.on_for_rotations(speed=speed,
                                               rotations=2,
                                               block=True,
                                               brake=True)

    def move_when_touched(self):
        if self.touch_sensor.is_pressed:
            self.tank_driver.on_for_seconds(left_speed=100,
                                            right_speed=-100,
                                            seconds=2,
                                            brake=True,
                                            block=True)

    def move_when_see_smothing(self):
        if self.color_sensor.reflected_light_intensity > 30:
            self.tank_driver.on_for_seconds(left_speed=-100,
                                            right_speed=100,
                                            seconds=2,
                                            brake=True,
                                            block=True)

    def main(self, speed: float = 100):
        self.screen.image_filename(filename='/home/robot/image/Neutral.bmp',
                                   clear_screen=True)
        self.screen.update()

        while True:
            self.drive_once_by_ir_beacon(speed=speed)

            self.move_when_touched()

            self.move_when_see_smothing()

            self.drill(speed=speed)
class Spik3r:
    def __init__(self,
                 sting_motor_port: str = OUTPUT_D,
                 go_motor_port: str = OUTPUT_B,
                 claw_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        self.sting_motor = LargeMotor(address=sting_motor_port)
        self.go_motor = LargeMotor(address=go_motor_port)
        self.claw_motor = MediumMotor(address=claw_motor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.dis = Display()
        self.speaker = Sound()

    def sting_by_ir_beacon(self):
        while True:
            if self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                self.sting_motor.on_for_degrees(speed=-75,
                                                degrees=220,
                                                brake=True,
                                                block=False)

                self.speaker.play_file(wav_file='/home/robot/sound/Blip 1.wav',
                                       volume=100,
                                       play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

                self.sting_motor.on_for_seconds(speed=-100,
                                                seconds=1,
                                                brake=True,
                                                block=True)

                self.sting_motor.on_for_seconds(speed=100,
                                                seconds=1,
                                                brake=True,
                                                block=True)

            while self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                pass

    def be_noisy_to_people(self):
        while True:
            if self.color_sensor.reflected_light_intensity > 30:
                for i in range(4):
                    self.speaker.play_file(
                        wav_file='/home/robot/sound/Blip 2.wav',
                        volume=100,
                        play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

    def pinch_if_touched(self):
        while True:
            if self.touch_sensor.is_pressed:
                self.claw_motor.on_for_seconds(speed=50,
                                               seconds=1,
                                               brake=True,
                                               block=True)

                self.claw_motor.on_for_seconds(speed=-50,
                                               seconds=0.3,
                                               brake=True,
                                               block=True)

    def keep_driving_by_ir_beacon(self):
        while True:
            if self.ir_sensor.top_left(channel=self.ir_beacon_channel) and \
                    self.ir_sensor.top_right(channel=self.ir_beacon_channel):
                self.go_motor.on(speed=91, block=False, brake=False)

            elif self.ir_sensor.top_right(channel=self.ir_beacon_channel):
                self.go_motor.on(speed=-100, brake=False, block=False)

            else:
                self.go_motor.off(brake=True)

    def main(self):
        self.dis.image_filename(filename='/home/robot/image/Evil.bmp',
                                clear_screen=True)
        self.dis.update()

        # FIXME: .sting_by_ir_beacon stops responding after a while
        Process(target=self.be_noisy_to_people, daemon=True).start()

        Process(target=self.sting_by_ir_beacon, daemon=True).start()

        Process(target=self.pinch_if_touched, daemon=True).start()

        self.keep_driving_by_ir_beacon()
コード例 #15
0
class R3ptar:
    def __init__(self,
                 turn_motor_port: str = OUTPUT_A,
                 move_motor_port: str = OUTPUT_B,
                 scare_motor_port: str = OUTPUT_D,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        self.turn_motor = MediumMotor(address=turn_motor_port)
        self.move_motor = LargeMotor(address=move_motor_port)
        self.scare_motor = LargeMotor(address=scare_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.noise = Sound()

    def keep_driving_by_ir_beacon(self, speed: float = 100):
        while True:
            if self.ir_sensor.top_left(channel=self.ir_beacon_channel) and \
                    self.ir_sensor.top_right(channel=self.ir_beacon_channel):
                self.move_motor.on(speed=speed, brake=False, block=False)

            elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel) and \
                    self.ir_sensor.bottom_right(channel=self.ir_beacon_channel):
                self.move_motor.on(speed=-speed, brake=False, block=False)

            elif self.ir_sensor.top_left(channel=self.ir_beacon_channel):
                self.turn_motor.on(speed=-50, brake=False, block=False)

                self.move_motor.on(speed=speed, brake=False, block=False)

            elif self.ir_sensor.top_right(channel=self.ir_beacon_channel):
                self.turn_motor.on(speed=50, brake=False, block=False)

                self.move_motor.on(speed=speed, brake=False, block=False)

            elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel):
                self.turn_motor.on(speed=-50, brake=False, block=False)

                self.move_motor.on(speed=-speed, brake=False, block=False)

            elif self.ir_sensor.bottom_right(channel=self.ir_beacon_channel):
                self.turn_motor.on(speed=50, brake=False, block=False)

                self.move_motor.on(speed=-speed, brake=False, block=False)

            else:
                self.turn_motor.off(brake=True)

                self.move_motor.off(brake=False)

    def bite_by_ir_beacon(self, speed: float = 100):
        while True:
            if self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                self.scare_motor.on_for_seconds(speed=speed,
                                                seconds=1,
                                                brake=True,
                                                block=False)

                self.noise.play_file(
                    wav_file='/home/robot/sound/Snake hiss.wav',
                    volume=100,
                    play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)

                self.scare_motor.on_for_seconds(speed=-speed,
                                                seconds=1,
                                                brake=True,
                                                block=True)

                while self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                    pass

    def run_away_if_chased(self):
        while True:
            if self.color_sensor.reflected_light_intensity > 30:
                self.move_motor.on_for_seconds(speed=50,
                                               seconds=4,
                                               brake=True,
                                               block=False)

                for i in range(2):
                    self.turn_motor.on_for_seconds(speed=50,
                                                   seconds=1,
                                                   brake=False,
                                                   block=True)

                    self.turn_motor.on_for_seconds(speed=-50,
                                                   seconds=1,
                                                   brake=False,
                                                   block=True)

    def bite_if_touched(self):
        while True:
            if self.touch_sensor.is_pressed:
                self.scare_motor.on_for_seconds(speed=100,
                                                seconds=1,
                                                brake=True,
                                                block=False)

                self.noise.play_file(
                    wav_file='/home/robot/sound/Snake hiss.wav',
                    volume=100,
                    play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)

                self.scare_motor.on_for_seconds(speed=-10,
                                                seconds=10,
                                                brake=True,
                                                block=True)

    def main(self, speed: float = 100):
        Thread(target=self.bite_by_ir_beacon).start()

        Thread(target=self.bite_if_touched).start()

        Thread(target=self.run_away_if_chased).start()

        self.keep_driving_by_ir_beacon(speed=speed)
class MarsRov3r(IRBeaconRemoteControlledTank):
    is_gripping = False

    def __init__(self,
                 left_motor_port: str = OUTPUT_B,
                 right_motor_port: str = OUTPUT_C,
                 grip_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        super().__init__(left_motor_port=left_motor_port,
                         right_motor_port=right_motor_port,
                         ir_sensor_port=ir_sensor_port,
                         ir_beacon_channel=ir_beacon_channel)

        self.grip_motor = MediumMotor(address=grip_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.speaker = Sound()

    def grip_or_release_claw_by_ir_beacon(self):
        while True:
            if self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                if self.is_gripping:
                    self.grip_motor.on_for_seconds(speed=100,
                                                   seconds=2,
                                                   brake=True,
                                                   block=True)

                    self.speaker.play_file(
                        wav_file='/home/robot/sound/Air release.wav',
                        volume=100,
                        play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)

                    self.is_gripping = False

                else:
                    self.grip_motor.on_for_seconds(speed=-100,
                                                   seconds=2,
                                                   brake=True,
                                                   block=True)

                    self.speaker.play_file(
                        wav_file='/home/robot/sound/Airbrake.wav',
                        volume=100,
                        play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)

                    self.is_gripping = True

                while self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                    pass

    def main(self):
        self.grip_motor.on_for_seconds(speed=50,
                                       seconds=1,
                                       brake=True,
                                       block=True)

        Process(target=self.grip_or_release_claw_by_ir_beacon,
                daemon=True).start()

        self.keep_driving_by_ir_beacon(speed=100)
class Kraz33Hors3:
    def __init__(self,
                 back_foot_motor_port: str = OUTPUT_B,
                 front_foot_motor_port: str = OUTPUT_C,
                 gear_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        self.tank_driver = MoveTank(left_motor_port=back_foot_motor_port,
                                    right_motor_port=front_foot_motor_port,
                                    motor_class=LargeMotor)

        self.gear_motor = MediumMotor(address=gear_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

    def drive_once_by_ir_beacon(self, speed: float = 100):
        # forward
        if self.ir_sensor.top_left(
                channel=self.ir_beacon_channel) and self.ir_sensor.top_right(
                    channel=self.ir_beacon_channel):
            self.tank_driver.on_for_seconds(left_speed=speed,
                                            right_speed=-speed,
                                            seconds=1,
                                            brake=False,
                                            block=True)

        # backward
        elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel
                                        ) and self.ir_sensor.bottom_right(
                                            channel=self.ir_beacon_channel):
            self.tank_driver.on_for_seconds(left_speed=-speed,
                                            right_speed=speed,
                                            seconds=1,
                                            brake=False,
                                            block=True)

        # move crazily
        elif self.ir_sensor.beacon(channel=self.ir_beacon_channel):
            self.gear_motor.on(speed=speed, brake=False, block=False)

            self.tank_driver.on_for_seconds(left_speed=-speed / 3,
                                            right_speed=-speed / 3,
                                            seconds=1,
                                            brake=False,
                                            block=True)

        else:
            self.gear_motor.off(brake=False)

    def keep_driving_by_ir_beacon(self, speed: float = 100):
        while True:
            self.drive_once_by_ir_beacon(speed=speed)

    def back_whenever_touched(self, speed: float = 100):
        while True:
            if self.touch_sensor.is_pressed:
                self.tank_driver.on_for_seconds(left_speed=-speed,
                                                right_speed=speed,
                                                seconds=1,
                                                brake=False,
                                                block=True)

    def main(self):
        # FIXME: when this thread is activated, the program encounters OSError after a while:
        # Traceback (most recent call last):
        #   File "/home/robot/Kraz33-Mov3r/Kraz33-Mov3r.EV3Dev2.MicroPython.Threading.FIXME.py", line 92, in <module>
        #   File "/home/robot/Kraz33-Mov3r/Kraz33-Mov3r.EV3Dev2.MicroPython.Threading.FIXME.py", line 86, in main
        #   File "/home/robot/Kraz33-Mov3r/Kraz33-Mov3r.EV3Dev2.MicroPython.Threading.FIXME.py", line 68, in keep_driving_by_ir_beacon
        #   File "/home/robot/Kraz33-Mov3r/Kraz33-Mov3r.EV3Dev2.MicroPython.Threading.FIXME.py", line 43, in drive_once_by_ir_beacon
        #   File "ev3dev2/motor.py", line 1957, in on_for_rotations
        #   File "ev3dev2/motor.py", line 1945, in on_for_degrees
        #   File "ev3dev2/motor.py", line 1803, in _block
        #   File "ev3dev2/motor.py", line 1787, in wait_until_not_moving
        #   File "ev3dev2/motor.py", line 928, in     wait_until_not_moving
        #   File "ev3dev2/motor.py", line 908, in wait
        # OSError: 4
        Thread(target=self.back_whenever_touched).start()

        self.keep_driving_by_ir_beacon()
コード例 #18
0
class Gripp3r:
    def __init__(self,
                 left_motor_port: str = OUTPUT_B,
                 right_motor_port: str = OUTPUT_C,
                 grip_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        self.tank_driver = MoveTank(left_motor_port=left_motor_port,
                                    right_motor_port=right_motor_port,
                                    motor_class=LargeMotor)

        self.grip_motor = MediumMotor(address=grip_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.speaker = Sound()

    def drive_once_by_ir_beacon(self, speed: float = 100):
        if self.ir_sensor.top_left(channel=self.ir_beacon_channel) and \
                self.ir_sensor.top_right(channel=self.ir_beacon_channel):
            # go forward
            self.tank_driver.on(left_speed=speed, right_speed=speed)

        elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel) and \
                self.ir_sensor.bottom_right(channel=self.ir_beacon_channel):
            # go backward
            self.tank_driver.on(left_speed=-speed, right_speed=-speed)

        elif self.ir_sensor.top_left(channel=self.ir_beacon_channel) and \
                self.ir_sensor.bottom_right(channel=self.ir_beacon_channel):
            # turn around left
            self.tank_driver.on(left_speed=-speed, right_speed=speed)

        elif self.ir_sensor.top_right(channel=self.ir_beacon_channel) and \
                self.ir_sensor.bottom_left(channel=self.ir_beacon_channel):
            # turn around right
            self.tank_driver.on(left_speed=speed, right_speed=-speed)

        elif self.ir_sensor.top_left(channel=self.ir_beacon_channel):
            # turn left
            self.tank_driver.on(left_speed=0, right_speed=speed)

        elif self.ir_sensor.top_right(channel=self.ir_beacon_channel):
            # turn right
            self.tank_driver.on(left_speed=speed, right_speed=0)

        elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel):
            # left backward
            self.tank_driver.on(left_speed=0, right_speed=-speed)

        elif self.ir_sensor.bottom_right(channel=self.ir_beacon_channel):
            # right backward
            self.tank_driver.on(left_speed=-speed, right_speed=0)

        else:
            self.tank_driver.off(brake=False)

    def grip_or_release_by_ir_beacon(self, speed: float = 50):
        if self.ir_sensor.beacon(channel=self.ir_beacon_channel):
            if self.touch_sensor.is_pressed:
                self.speaker.play_file(
                    wav_file='/home/robot/sound/Air release.wav',
                    volume=100,
                    play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)

                self.grip_motor.on_for_seconds(speed=speed,
                                               seconds=1,
                                               brake=True,
                                               block=True)

            else:
                self.speaker.play_file(
                    wav_file='/home/robot/sound/Airbrake.wav',
                    volume=100,
                    play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)

                self.grip_motor.on(speed=-speed, brake=False, block=False)

                self.touch_sensor.wait_for_pressed()

                self.grip_motor.off(brake=True)

            while self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                pass

    def main(self, speed: float = 100):
        self.grip_motor.on_for_seconds(speed=-50,
                                       seconds=1,
                                       brake=True,
                                       block=True)

        while True:
            self.drive_once_by_ir_beacon(speed=speed)

            self.grip_or_release_by_ir_beacon()
class Rov3r(IRBeaconRemoteControlledTank):
    def __init__(
            self,
            left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C,
            gear_motor_port: str = OUTPUT_A,
            touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3,
            ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1):
        super().__init__(
            left_motor_port=left_motor_port, right_motor_port=right_motor_port,
            ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel)

        self.gear_motor = MediumMotor(address=gear_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.speaker = Sound()
        self.dis = Display(desc='Display')


    def spin_gears(self, speed: float = 100):
        while True:
            if self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                self.gear_motor.on(
                    speed=speed,
                    block=False,
                    brake=False)

            else:
                self.gear_motor.off(brake=True)


    def change_screen_when_touched(self):
        while True:
            if self.touch_sensor.is_pressed:
                self.dis.image_filename(
                    filename='/home/robot/image/Angry.bmp',
                    clear_screen=True)
                self.dis.update()

            else:
                self.dis.image_filename(
                    filename='/home/robot/image/Fire.bmp',
                    clear_screen=True)
                self.dis.update()


    def make_noise_when_seeing_black(self):
        while True:
            if self.color_sensor.color == ColorSensor.COLOR_BLACK:
                self.speaker.play_file(
                    wav_file='/home/robot/sound/Ouch.wav',
                    volume=100,
                    play_type=Sound.PLAY_WAIT_FOR_COMPLETE)


    def main(self):
        self.speaker.play_file(
            wav_file='/home/robot/sound/Yes.wav',
            volume=100,
            play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

        Thread(target=self.make_noise_when_seeing_black,
               daemon=True).start()    

        Thread(target=self.spin_gears,
               daemon=True).start()    

        Thread(target=self.change_screen_when_touched,
               daemon=True).start()

        self.keep_driving_by_ir_beacon(speed=100)
class Catapult(IRBeaconRemoteControlledTank):
    def __init__(self,
                 left_motor_port: str = OUTPUT_B,
                 right_motor_port: str = OUTPUT_C,
                 catapult_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        super().__init__(left_motor_port=left_motor_port,
                         right_motor_port=right_motor_port,
                         ir_sensor_port=ir_sensor_port,
                         ir_beacon_channel=ir_beacon_channel)

        self.catapult_motor = MediumMotor(address=catapult_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.speaker = Sound()

    def scan_colors(self):
        while True:
            if self.color_sensor.color == ColorSensor.COLOR_YELLOW:
                pass

            elif self.color_sensor.color == ColorSensor.COLOR_WHITE:
                self.speaker.play_file(wav_file='/home/robot/sound/Good.wav',
                                       volume=100,
                                       play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

    def make_noise_when_touched(self):
        while True:
            if self.touch_sensor.is_pressed:
                self.speaker.play_file(wav_file='/home/robot/sound/Ouch.wav',
                                       volume=100,
                                       play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

    def throw_by_ir_beacon(self):
        while True:
            if self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                self.catapult_motor.on_for_degrees(speed=-100,
                                                   degrees=150,
                                                   brake=True,
                                                   block=True)

                self.catapult_motor.on_for_degrees(speed=100,
                                                   degrees=150,
                                                   brake=True,
                                                   block=True)

                while self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                    pass

    def main(self):
        self.speaker.play_file(wav_file='/home/robot/sound/Yes.wav',
                               volume=100,
                               play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

        Thread(target=self.make_noise_when_touched, daemon=True).start()

        Thread(target=self.throw_by_ir_beacon, daemon=True).start()

        Thread(target=self.scan_colors, daemon=True).start()

        self.keep_driving_by_ir_beacon(speed=100)
コード例 #21
0
class Ev3rstorm:
    def __init__(self,
                 left_foot_motor_port: str = OUTPUT_B,
                 right_foot_motor_port: str = OUTPUT_C,
                 bazooka_blast_motor_port: str = OUTPUT_A,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        self.tank_driver = MoveTank(left_motor_port=left_foot_motor_port,
                                    right_motor_port=right_foot_motor_port,
                                    motor_class=LargeMotor)

        self.bazooka_blast_motor = MediumMotor(
            address=bazooka_blast_motor_port)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.leds = Leds()
        self.speaker = Sound()

    def drive_once_by_ir_beacon(self, speed: float = 100):
        # forward
        if self.ir_sensor.top_left(
                channel=self.ir_beacon_channel) and self.ir_sensor.top_right(
                    channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=speed, right_speed=speed)

        # backward
        elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel
                                        ) and self.ir_sensor.bottom_right(
                                            channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=-speed, right_speed=-speed)

        # turn left on the spot
        elif self.ir_sensor.top_left(channel=self.ir_beacon_channel
                                     ) and self.ir_sensor.bottom_right(
                                         channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=-speed, right_speed=speed)

        # turn right on the spot
        elif self.ir_sensor.top_right(
                channel=self.ir_beacon_channel) and self.ir_sensor.bottom_left(
                    channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=speed, right_speed=-speed)

        # turn left forward
        elif self.ir_sensor.top_left(channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=0, right_speed=speed)

        # turn right forward
        elif self.ir_sensor.top_right(channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=speed, right_speed=0)

        # turn left backward
        elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=0, right_speed=-speed)

        # turn right backward
        elif self.ir_sensor.bottom_right(channel=self.ir_beacon_channel):
            self.tank_driver.on(left_speed=-speed, right_speed=0)

        # otherwise stop
        else:
            self.tank_driver.off(brake=False)

    def keep_driving_by_ir_beacon(self, speed: float = 100):
        while True:
            self.drive_once_by_ir_beacon(speed=speed)

    def dance_whenever_ir_beacon_pressed(self):
        while True:
            while self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                self.tank_driver.on_for_seconds(left_speed=randint(-100, 100),
                                                right_speed=randint(-100, 100),
                                                seconds=1,
                                                brake=False,
                                                block=True)

    def keep_detecting_objects_by_ir_sensor(self):
        while True:
            if self.ir_sensor.proximity < 25:
                self.leds.animate_police_lights(color1=Leds.ORANGE,
                                                color2=Leds.RED,
                                                group1=Leds.LEFT,
                                                group2=Leds.RIGHT,
                                                sleeptime=0.5,
                                                duration=5,
                                                block=False)

                self.speaker.play_file(wav_file='/home/robot/sound/Object.wav',
                                       volume=100,
                                       play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

                self.speaker.play_file(
                    wav_file='/home/robot/sound/Detected.wav',
                    volume=100,
                    play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

                self.speaker.play_file(
                    wav_file='/home/robot/sound/Error alarm.wav',
                    volume=100,
                    play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

            else:
                self.leds.all_off()

    def blast_bazooka_whenever_touched(self):
        while True:
            if self.touch_sensor.is_pressed:
                if self.color_sensor.ambient_light_intensity < 5:  # 15 not dark enough
                    self.speaker.play_file(
                        wav_file='/home/robot/sound/Up.wav',
                        volume=100,
                        play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

                    self.bazooka_blast_motor.on_for_rotations(speed=100,
                                                              rotations=-3,
                                                              brake=True,
                                                              block=True)

                    self.speaker.play_file(
                        wav_file='/home/robot/sound/Laughing 1.wav',
                        volume=100,
                        play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

                else:
                    self.speaker.play_file(
                        wav_file='/home/robot/sound/Down.wav',
                        volume=100,
                        play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

                    self.bazooka_blast_motor.on_for_rotations(speed=100,
                                                              rotations=3,
                                                              brake=True,
                                                              block=True)

                    self.speaker.play_file(
                        wav_file='/home/robot/sound/Laughing 2.wav',
                        volume=100,
                        play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

                self.touch_sensor.wait_for_released()

    def main(self, driving_speed: float = 100):
        # FIXME
        # Unhandled exception in thread started by <bound_method b6bbfd60 <Thread object at b6bbe450>.<function run at 0xb6bb4a20>>
        # Traceback (most recent call last):
        #   File "threading/threading.py", line 15, in run
        #   File "ev3dev2/sensor/lego.py", line 885, in top_left
        #   File "ev3dev2/sensor/lego.py", line 919, in buttons_pressed
        #   File "ev3dev2/sensor/__init__.py", line 203, in value
        #   File "ev3dev2/__init__.py", line 307, in get_attr_int
        # ValueError: invalid syntax for integer with base 10: ''
        Thread(target=self.dance_whenever_ir_beacon_pressed).start()

        # DON'T use IR Sensor in 2 different modes in the same program / loop
        # - https://github.com/pybricks/support/issues/62
        # - https://github.com/ev3dev/ev3dev/issues/1401
        # Thread(target=self.keep_detecting_objects_by_ir_sensor).start()

        Thread(target=self.blast_bazooka_whenever_touched).start()

        self.keep_driving_by_ir_beacon(speed=driving_speed)