示例#1
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()
示例#2
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)
示例#3
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()
示例#4
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 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.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: int = 100):
        while True:
            self.drive_once_by_ir_beacon(speed=speed)

    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, driving_speed: float = 100):
        Thread(target=self.blast_bazooka_whenever_touched).start()

        self.keep_driving_by_ir_beacon(speed=driving_speed)
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()
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 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()
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()
示例#10
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)
示例#11
0
class IRBeaconRemoteControlledTank:
    def __init__(
        self,
        left_motor_port: str = OUTPUT_B,
        right_motor_port: str = OUTPUT_C,
        motor_class=LargeMotor,
        polarity: str = Motor.POLARITY_NORMAL,
        ir_sensor_port: str = INPUT_4,
        # sites.google.com/site/ev3devpython/learn_ev3_python/using-sensors
        ir_beacon_channel: int = 1):
        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=motor_class)

        self.steer_driver = \
            MoveSteering(
                left_motor_port=left_motor_port,
                right_motor_port=right_motor_port,
                motor_class=motor_class)

        self.left_motor.polarity = self.right_motor.polarity = \
            self.tank_driver.left_motor.polarity = \
            self.tank_driver.right_motor.polarity = \
            self.steer_driver.left_motor.polarity = \
            self.steer_driver.right_motor.polarity = polarity

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.tank_drive_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.tank_drive_ir_beacon_channel) and \
                self.ir_sensor.top_right(
                    channel=self.tank_drive_ir_beacon_channel):
            self.tank_driver.on(left_speed=speed, right_speed=speed)

        # backward
        elif self.ir_sensor.bottom_left(
                    channel=self.tank_drive_ir_beacon_channel) and \
                self.ir_sensor.bottom_right(
                    channel=self.tank_drive_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.tank_drive_ir_beacon_channel) and \
                self.ir_sensor.bottom_right(
                    channel=self.tank_drive_ir_beacon_channel):
            self.steer_driver.on(steering=-100, speed=speed)

        # turn right on the spot
        elif self.ir_sensor.top_right(
                    channel=self.tank_drive_ir_beacon_channel) and \
                self.ir_sensor.bottom_left(
                    channel=self.tank_drive_ir_beacon_channel):
            self.steer_driver.on(steering=100, speed=speed)

        # turn left forward
        elif self.ir_sensor.top_left(
                channel=self.tank_drive_ir_beacon_channel):
            self.steer_driver.on(steering=-50, speed=speed)

        # turn right forward
        elif self.ir_sensor.top_right(
                channel=self.tank_drive_ir_beacon_channel):
            self.steer_driver.on(steering=50, speed=speed)

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

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

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

    # this method must be used in a parallel process/thread
    # in order not to block other operations
    def keep_driving_by_ir_beacon(self, speed: float = 100):
        while True:
            self.drive_once_by_ir_beacon(speed=speed)
示例#12
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)