Пример #1
0
class Gripp3r(RemoteControlledTank):
    WHEEL_DIAMETER = 26   # milimeters
    AXLE_TRACK = 115      # milimeters

    def __init__(
            self,
            left_track_motor_port: Port = Port.B,
            right_track_motor_port: Port = Port.C,
            gripping_motor_port: Port = Port.A,
            touch_sensor_port: Port = Port.S1,
            ir_sensor_port: Port = Port.S4,
            ir_beacon_channel: int = 1):
        super().__init__(
            wheel_diameter=self.WHEEL_DIAMETER,
            axle_track=self.AXLE_TRACK,
            left_motor_port=left_track_motor_port,
            right_motor_port=right_track_motor_port,
            ir_sensor_port=ir_sensor_port,
            ir_beacon_channel=ir_beacon_channel)

        self.ev3_brick = EV3Brick()

        self.gripping_motor = Motor(port=gripping_motor_port,
                                    positive_direction=Direction.CLOCKWISE)

        self.touch_sensor = TouchSensor(port=touch_sensor_port)

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

    def grip_or_release_by_ir_beacon(self, speed: float = 500):
        if Button.BEACON in \
                self.ir_sensor.buttons(channel=self.ir_beacon_channel):
            if self.touch_sensor.pressed():
                self.ev3_brick.speaker.play_file(file=SoundFile.AIR_RELEASE)

                self.gripping_motor.run_time(
                    speed=speed,
                    time=1000,
                    then=Stop.COAST,
                    wait=True)

            else:
                self.ev3_brick.speaker.play_file(file=SoundFile.AIRBRAKE)

                self.gripping_motor.run(speed=-speed)

                while not self.touch_sensor.pressed():
                    pass

                self.gripping_motor.stop()

            while Button.BEACON in \
                    self.ir_sensor.buttons(channel=self.ir_beacon_channel):
                pass
class Rov3r(IRBeaconRemoteControlledTank, EV3Brick):
    WHEEL_DIAMETER = 23
    AXLE_TRACK = 65

    def __init__(self,
                 left_motor_port: Port = Port.B,
                 right_motor_port: Port = Port.C,
                 gear_motor_port: Port = Port.A,
                 touch_sensor_port: Port = Port.S1,
                 color_sensor_port: Port = Port.S3,
                 ir_sensor_port: Port = Port.S4,
                 ir_beacon_channel: int = 1):
        super().__init__(wheel_diameter=self.WHEEL_DIAMETER,
                         axle_track=self.AXLE_TRACK,
                         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 = Motor(port=gear_motor_port,
                                positive_direction=Direction.CLOCKWISE)

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

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

    def spin_gears(self, speed: float = 1000):
        while True:
            if Button.BEACON in self.ir_sensor.buttons(
                    channel=self.ir_beacon_channel):
                self.gear_motor.run(speed=1000)

            else:
                self.gear_motor.stop()

    def change_screen_when_touched(self):
        while True:
            if self.touch_sensor.pressed():
                self.screen.load_image(ImageFile.ANGRY)

            else:
                self.screen.load_image(ImageFile.FORWARD)

    def make_noise_when_seeing_black(self):
        while True:
            if self.color_sensor.color == Color.BLACK:
                self.speaker.play_file(file=SoundFile.OUCH)

    def main(self):
        self.speaker.play_file(file=SoundFile.YES)

        Process(target=self.make_noise_when_seeing_black).start()

        Process(target=self.spin_gears).start()

        Process(target=self.change_screen_when_touched).start()

        self.keep_driving_by_ir_beacon(speed=1000)
class Sweep3r(IRBeaconRemoteControlledTank, EV3Brick):
    WHEEL_DIAMETER = 40
    AXLE_TRACK = 110


    def __init__(
            self,
            left_foot_motor_port: Port = Port.B, right_foot_motor_port: Port = Port.C,
            medium_motor_port: Port = Port.A,
            touch_sensor_port: Port = Port.S1, color_sensor_port: Port = Port.S3,
            ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1):            
        super().__init__(
            wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK,
            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 = Motor(port=medium_motor_port)

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

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


    def drill(self):
        while True:
            if Button.BEACON in self.ir_sensor.buttons(channel=self.ir_beacon_channel):
                self.medium_motor.run_angle(
                    speed=1000,
                    rotation_angle=2 * 360,
                    then=Stop.HOLD,
                    wait=True)


    def move_when_touched(self):
        while True:    
            if self.touch_sensor.pressed():
                self.drive_base.turn(angle=100)


    def move_when_see_smothing(self):
        while True:
            if self.color_sensor.reflection() > 30:
                self.drive_base.turn(angle=-100)

        
    def main(self, speed: float = 1000):
        self.screen.load_image(ImageFile.PINCHED_MIDDLE)

        Process(target=self.move_when_touched).start()

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

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

        self.keep_driving_by_ir_beacon(speed=speed)
Пример #4
0
class Remote():
    def __init__(self) -> None:
        super().__init__()

        self.inf_sensor = InfraredSensor(Port.S3)

    def get_button(self):
        pressButton = self.inf_sensor.buttons(1)
        brick.display.text(str(pressButton))
        return pressButton

    def get_distance(self):
        dist = self.inf_sensor.distance()
        brick.display.text(str(dist))
        return dist
class Gripp3r(IRBeaconRemoteControlledTank, EV3Brick):
    WHEEL_DIAMETER = 26
    AXLE_TRACK = 115

    def __init__(self,
                 left_motor_port: Port = Port.B,
                 right_motor_port: Port = Port.C,
                 grip_motor_port: Port = Port.A,
                 touch_sensor_port: Port = Port.S1,
                 ir_sensor_port: Port = Port.S4,
                 ir_beacon_channel: int = 1):
        super().__init__(wheel_diameter=self.WHEEL_DIAMETER,
                         axle_track=self.AXLE_TRACK,
                         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.drive_base.settings(
            straight_speed=750,  # milimeters per second
            straight_acceleration=750,
            turn_rate=90,  # degrees per second
            turn_acceleration=90)

        self.grip_motor = Motor(port=grip_motor_port,
                                positive_direction=Direction.CLOCKWISE)

        self.touch_sensor = TouchSensor(port=touch_sensor_port)

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

    def grip_or_release_by_ir_beacon(self, speed: float = 500):
        while True:
            if Button.BEACON in self.ir_sensor.buttons(
                    channel=self.ir_beacon_channel):
                if self.touch_sensor.pressed():
                    self.speaker.play_file(file=SoundFile.AIR_RELEASE)

                    self.grip_motor.run_time(speed=speed,
                                             time=1000,
                                             then=Stop.BRAKE,
                                             wait=True)

                else:
                    self.speaker.play_file(file=SoundFile.AIRBRAKE)

                    self.grip_motor.run(speed=-speed)

                    while not self.touch_sensor.pressed():
                        pass

                    self.grip_motor.stop()

                while Button.BEACON in self.ir_sensor.buttons(
                        channel=self.ir_beacon_channel):
                    pass

    def main(self, speed: float = 1000):
        self.grip_motor.run_time(speed=-500,
                                 time=1000,
                                 then=Stop.BRAKE,
                                 wait=True)

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

        self.keep_driving_by_ir_beacon(speed=speed)
Пример #6
0
class Spik3r:
    def __init__(
            self,
            crushing_claw_motor_port: Port = Port.A,
            moving_motor_port: Port = Port.B,
            lightning_tail_motor_port: Port = Port.D,
            touch_sensor_port: Port = Port.S1,
            color_sensor_port: Port = Port.S3,
            ir_sensor_port: Port = Port.S4,
            ir_beacon_channel: int = 1):
        self.ev3_brick = EV3Brick()

        self.crushing_claw_motor = \
            Motor(port=crushing_claw_motor_port,
                  positive_direction=Direction.CLOCKWISE)
        self.moving_motor = \
            Motor(port=moving_motor_port,
                  positive_direction=Direction.CLOCKWISE)
        self.lightning_tail_motor = \
            Motor(port=lightning_tail_motor_port,
                  positive_direction=Direction.CLOCKWISE)

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

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

    def sting_by_ir_beacon(self, speed: float = 1000):
        """
        Spik3r stings with its Lightning Tail when the Beacon button is pressed
        (inspiration from LEGO Mindstorms EV3 Home Ed.: Spik3r: Tutorial #1)
        """
        if Button.BEACON in \
                self.ir_sensor.buttons(channel=self.ir_beacon_channel):
            self.lightning_tail_motor.run_angle(
                speed=-750,
                rotation_angle=220,
                then=Stop.HOLD,
                wait=False)

            self.ev3_brick.speaker.play_file(file=SoundFile.ERROR_ALARM)

            self.lightning_tail_motor.run_time(
                speed=-speed,
                time=1000,
                then=Stop.COAST,
                wait=True)

            self.lightning_tail_motor.run_time(
                speed=speed,
                time=1000,
                then=Stop.COAST,
                wait=True)

            while Button.BEACON in \
                    self.ir_sensor.buttons(channel=self.ir_beacon_channel):
                pass

    def move_by_ir_beacon(self, speed: float = 1000):
        """
        Spik3r moves forward when the IR Beacon's two Up buttons are pressed,
        and turns right when only the Right Up button is pressed
        (inspiration from LEGO Mindstorms EV3 Home Ed.: Spik3r: Tutorial #2)
        """
        ir_buttons_pressed = \
            set(self.ir_sensor.buttons(channel=self.ir_beacon_channel))

        if ir_buttons_pressed == {Button.RIGHT_UP, Button.LEFT_UP}:
            self.moving_motor.run(speed=speed)

        elif ir_buttons_pressed == {Button.RIGHT_UP}:
            self.moving_motor.run(speed=-speed)

        else:
            self.moving_motor.stop()

    def pinch_if_touched(self, speed: float = 1000):
        """
        Spik3r crushes objects with its Claw when the Touch Sensor is pressed
        (inspiration from LEGO Mindstorms EV3 Home Ed.: Spik3r: Tutorial #3)
        """
        if self.touch_sensor.pressed():
            self.crushing_claw_motor.run_time(
                speed=speed,
                time=1000,
                then=Stop.COAST,
                wait=True)

            self.crushing_claw_motor.run_time(
                speed=-speed,
                time=1000,
                then=Stop.COAST,
                wait=True)

    def main(self, speed: float = 1000):
        """
        Spik3r's main program performing various capabilities
        """
        self.ev3_brick.screen.load_image(ImageFile.WARNING)

        while True:
            self.move_by_ir_beacon(speed=speed)
            self.sting_by_ir_beacon(speed=speed)
            self.pinch_if_touched(speed=speed)
            wait(1)
Пример #7
0
class Kraz33Hors3:
    def __init__(self,
                 back_foot_motor_port: Port = Port.C,
                 front_foot_motor_port: Port = Port.B,
                 gear_motor_port: Port = Port.A,
                 touch_sensor_port: Port = Port.S1,
                 color_sensor_port: Port = Port.S3,
                 ir_sensor_port: Port = Port.S4,
                 ir_beacon_channel: int = 1):
        self.front_foot_motor = Motor(port=front_foot_motor_port,
                                      positive_direction=Direction.CLOCKWISE)
        self.back_foot_motor = Motor(
            port=back_foot_motor_port,
            positive_direction=Direction.COUNTERCLOCKWISE)

        self.gear_motor = Motor(port=gear_motor_port)

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

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

    def drive_once_by_ir_beacon(
            self,
            speed: float = 1000  # deg/s
    ):
        ir_beacons_pressed = set(
            self.ir_sensor.buttons(channel=self.ir_beacon_channel))

        # forward
        if ir_beacons_pressed == {Button.LEFT_UP, Button.RIGHT_UP}:
            self.front_foot_motor.run_time(
                speed=speed,
                time=1000,  # ms
                then=Stop.COAST,
                wait=False)

            self.back_foot_motor.run_time(
                speed=speed,
                time=1000,  # ms
                then=Stop.COAST,
                wait=True)

        # backward
        elif ir_beacons_pressed == {Button.LEFT_DOWN, Button.RIGHT_DOWN}:
            self.front_foot_motor.run_time(
                speed=-speed,
                time=1000,  # ms
                then=Stop.COAST,
                wait=False)

            self.back_foot_motor.run_time(
                speed=-speed,
                time=1000,  # ms
                then=Stop.COAST,
                wait=True)

        # move crazily
        elif ir_beacons_pressed == {Button.BEACON}:
            self.gear_motor.run(speed=speed)

            self.front_foot_motor.run_time(
                speed=speed / 3,
                time=1000,  # ms
                then=Stop.COAST,
                wait=False)

            self.back_foot_motor.run_time(
                speed=-speed / 3,
                time=1000,  # ms
                then=Stop.COAST,
                wait=True)

        else:
            self.gear_motor.stop()

    def keep_driving_by_ir_beacon(
            self,
            speed: float = 1000  # deg/s
    ):
        while True:
            self.drive_once_by_ir_beacon(speed=speed)

    def back_whenever_touched(
            self,
            speed: float = 1000  # deg/s
    ):
        while True:
            if self.touch_sensor.pressed():
                self.front_foot_motor.run_time(
                    speed=-speed,
                    time=1000,  # ms
                    then=Stop.COAST,
                    wait=False)

                self.back_foot_motor.run_time(
                    speed=-speed,
                    time=1000,  # ms
                    then=Stop.COAST,
                    wait=True)

    def main(
            self,
            speed: float = 1000  # deg/s
    ):
        Process(target=self.back_whenever_touched).start()
        # FIXME: as soon as Touch Sensor pressed
        # OSError: [Errno 5] EIO:
        # Unexpected hardware input/output error with a motor or sensor:
        # --> Try unplugging the sensor or motor and plug it back in again.
        # --> To see which sensor or motor is causing the problem,
        #     check the line in your script that matches
        #     the line number given in the 'Traceback' above.
        # --> Try rebooting the hub/brick if the problem persists.

        self.keep_driving_by_ir_beacon(speed=speed)
Пример #8
0
class Spik3r(EV3Brick):
    def __init__(self,
                 sting_motor_port: Port = Port.D,
                 go_motor_port: Port = Port.B,
                 claw_motor_port: Port = Port.A,
                 touch_sensor_port: Port = Port.S1,
                 color_sensor_port: Port = Port.S3,
                 ir_sensor_port: Port = Port.S4,
                 ir_beacon_channel: int = 1):
        self.sting_motor = Motor(port=sting_motor_port,
                                 positive_direction=Direction.CLOCKWISE)
        self.go_motor = Motor(port=go_motor_port,
                              positive_direction=Direction.CLOCKWISE)
        self.claw_motor = Motor(port=claw_motor_port,
                                positive_direction=Direction.CLOCKWISE)

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

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

    def sting_by_ir_beacon(self):
        while True:
            ir_buttons_pressed = set(
                self.ir_sensor.buttons(channel=self.ir_beacon_channel))

            if ir_buttons_pressed == {Button.BEACON}:
                self.sting_motor.run_angle(speed=-750,
                                           rotation_angle=220,
                                           then=Stop.HOLD,
                                           wait=False)

                self.speaker.play_file(file=SoundFile.EV3)

                self.sting_motor.run_time(speed=-1000,
                                          time=1000,
                                          then=Stop.HOLD,
                                          wait=True)

                self.sting_motor.run_time(speed=1000,
                                          time=1000,
                                          then=Stop.HOLD,
                                          wait=True)

                while Button.BEACON in self.ir_sensor.buttons(
                        channel=self.ir_beacon_channel):
                    pass

    def be_noisy_to_people(self):
        while True:
            if self.color_sensor.reflection() > 30:
                for i in range(4):
                    self.speaker.play_file(file=SoundFile.ERROR_ALARM)

    def pinch_if_touched(self):
        while True:
            if self.touch_sensor.pressed():
                self.claw_motor.run_time(speed=500,
                                         time=1000,
                                         then=Stop.HOLD,
                                         wait=True)

                self.claw_motor.run_time(speed=-500,
                                         time=0.3 * 1000,
                                         then=Stop.HOLD,
                                         wait=True)

    def keep_driving_by_ir_beacon(self):
        while True:
            ir_buttons_pressed = set(
                self.ir_sensor.buttons(channel=self.ir_beacon_channel))

            if ir_buttons_pressed == {Button.RIGHT_UP, Button.LEFT_UP}:
                self.go_motor.run(speed=910)

            elif ir_buttons_pressed == {Button.RIGHT_UP}:
                self.go_motor.run(speed=-1000)

            else:
                self.go_motor.stop()

    def main(self):
        self.screen.load_image(ImageFile.EVIL)

        # FIXME: OSError: [Errno 5] EIO:
        # Unexpected hardware input/output error with a motor or sensor:
        # --> Try unplugging the sensor or motor and plug it back in again.
        # --> To see which sensor or motor is causing the problem,
        #     check the line in your script that matches
        #     the line number given in the 'Traceback' above.
        # --> Try rebooting the hub/brick if the problem persists.
        Process(target=self.be_noisy_to_people).start()
        Process(target=self.sting_by_ir_beacon).start()
        Process(target=self.pinch_if_touched).start()
        self.keep_driving_by_ir_beacon()
class Gripp3r(EV3Brick):
    WHEEL_DIAMETER = 26
    AXLE_TRACK = 115

    def __init__(self,
                 left_motor_port: Port = Port.B,
                 right_motor_port: Port = Port.C,
                 grip_motor_port: Port = Port.A,
                 touch_sensor_port: Port = Port.S1,
                 ir_sensor_port: Port = Port.S4,
                 ir_beacon_channel: int = 1):
        self.drive_base = DriveBase(
            left_motor=Motor(port=left_motor_port,
                             positive_direction=Direction.CLOCKWISE),
            right_motor=Motor(port=right_motor_port,
                              positive_direction=Direction.CLOCKWISE),
            wheel_diameter=self.WHEEL_DIAMETER,
            axle_track=self.AXLE_TRACK)

        self.drive_base.settings(
            straight_speed=750,  # milimeters per second
            straight_acceleration=750,
            turn_rate=90,  # degrees per second
            turn_acceleration=90)

        self.grip_motor = Motor(port=grip_motor_port,
                                positive_direction=Direction.CLOCKWISE)

        self.touch_sensor = TouchSensor(port=touch_sensor_port)

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

    def keep_driving_by_ir_beacon(
            self,
            channel: int = 1,
            speed: float = 1000  # milimeters per second
    ):
        while True:
            ir_beacon_buttons_pressed = set(
                self.ir_sensor.buttons(channel=channel))

            # forward
            if ir_beacon_buttons_pressed == {Button.LEFT_UP, Button.RIGHT_UP}:
                self.drive_base.drive(
                    speed=speed,
                    turn_rate=0  # degrees per second
                )

            # backward
            elif ir_beacon_buttons_pressed == {
                    Button.LEFT_DOWN, Button.RIGHT_DOWN
            }:
                self.drive_base.drive(
                    speed=-speed,
                    turn_rate=0  # degrees per second
                )

            # turn left on the spot
            elif ir_beacon_buttons_pressed == {
                    Button.LEFT_UP, Button.RIGHT_DOWN
            }:
                self.drive_base.drive(
                    speed=0,
                    turn_rate=-90  # degrees per second
                )

            # turn right on the spot
            elif ir_beacon_buttons_pressed == {
                    Button.LEFT_DOWN, Button.RIGHT_UP
            }:
                self.drive_base.drive(
                    speed=0,
                    turn_rate=90  # degrees per second
                )

            # turn left forward
            elif ir_beacon_buttons_pressed == {Button.LEFT_UP}:
                self.drive_base.drive(
                    speed=speed,
                    turn_rate=-90  # degrees per second
                )

            # turn right forward
            elif ir_beacon_buttons_pressed == {Button.RIGHT_UP}:
                self.drive_base.drive(
                    speed=speed,
                    turn_rate=90  # degrees per second
                )

            # turn left backward
            elif ir_beacon_buttons_pressed == {Button.LEFT_DOWN}:
                self.drive_base.drive(
                    speed=-speed,
                    turn_rate=90  # degrees per second
                )

            # turn right backward
            elif ir_beacon_buttons_pressed == {Button.RIGHT_DOWN}:
                self.drive_base.drive(
                    speed=-speed,
                    turn_rate=-90  # degrees per second
                )

            # otherwise stop
            else:
                self.drive_base.stop()

    def grip_or_release_by_ir_beacon(self, speed: float = 500):
        while True:
            if Button.BEACON in self.ir_sensor.buttons(
                    channel=self.ir_beacon_channel):
                if self.touch_sensor.pressed():
                    self.speaker.play_file(file=SoundFile.AIR_RELEASE)

                    self.grip_motor.run_time(speed=speed,
                                             time=1000,
                                             then=Stop.BRAKE,
                                             wait=True)

                else:
                    self.speaker.play_file(file=SoundFile.AIRBRAKE)

                    self.grip_motor.run(speed=-speed)

                    while not self.touch_sensor.pressed():
                        pass

                    self.grip_motor.stop()

                while Button.BEACON in self.ir_sensor.buttons(
                        channel=self.ir_beacon_channel):
                    pass

    def main(self, speed: float = 1000):
        self.grip_motor.run_time(speed=-500,
                                 time=1000,
                                 then=Stop.BRAKE,
                                 wait=True)

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

        self.keep_driving_by_ir_beacon(speed=speed)
Пример #10
0
class Ev3rstorm(EV3Brick):
    WHEEL_DIAMETER = 26
    AXLE_TRACK = 102

    def __init__(self,
                 left_foot_motor_port: Port = Port.B,
                 right_foot_motor_port: Port = Port.C,
                 bazooka_blast_motor_port: Port = Port.A,
                 touch_sensor_port: Port = Port.S1,
                 color_sensor_port: Port = Port.S3,
                 ir_sensor_port: Port = Port.S4,
                 ir_beacon_channel: int = 1):
        self.drive_base = DriveBase(
            left_motor=Motor(port=left_foot_motor_port,
                             positive_direction=Direction.CLOCKWISE),
            right_motor=Motor(port=right_foot_motor_port,
                              positive_direction=Direction.CLOCKWISE),
            wheel_diameter=self.WHEEL_DIAMETER,
            axle_track=self.AXLE_TRACK)
        self.drive_base.settings(
            straight_speed=300,  # milimeters per second
            straight_acceleration=300,
            turn_rate=90,  # degrees per second
            turn_acceleration=90)

        self.bazooka_blast_motor = Motor(
            port=bazooka_blast_motor_port,
            positive_direction=Direction.CLOCKWISE)

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

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

    def drive_once_by_ir_beacon(
            self,
            speed: float = 1000  # milimeters per second
    ):
        ir_beacon_buttons_pressed = set(
            self.ir_sensor.buttons(channel=self.ir_beacon_channel))

        # forward
        if ir_beacon_buttons_pressed == {Button.LEFT_UP, Button.RIGHT_UP}:
            self.drive_base.drive(
                speed=speed,
                turn_rate=0  # degrees per second
            )

        # backward
        elif ir_beacon_buttons_pressed == {
                Button.LEFT_DOWN, Button.RIGHT_DOWN
        }:
            self.drive_base.drive(
                speed=-speed,
                turn_rate=0  # degrees per second
            )

        # turn left on the spot
        elif ir_beacon_buttons_pressed == {Button.LEFT_UP, Button.RIGHT_DOWN}:
            self.drive_base.drive(
                speed=0,
                turn_rate=-90  # degrees per second
            )

        # turn right on the spot
        elif ir_beacon_buttons_pressed == {Button.LEFT_DOWN, Button.RIGHT_UP}:
            self.drive_base.drive(
                speed=0,
                turn_rate=90  # degrees per second
            )

        # turn left forward
        elif ir_beacon_buttons_pressed == {Button.LEFT_UP}:
            self.drive_base.drive(
                speed=speed,
                turn_rate=-90  # degrees per second
            )

        # turn right forward
        elif ir_beacon_buttons_pressed == {Button.RIGHT_UP}:
            self.drive_base.drive(
                speed=speed,
                turn_rate=90  # degrees per second
            )

        # turn left backward
        elif ir_beacon_buttons_pressed == {Button.LEFT_DOWN}:
            self.drive_base.drive(
                speed=-speed,
                turn_rate=90  # degrees per second
            )

        # turn right backward
        elif ir_beacon_buttons_pressed == {Button.RIGHT_DOWN}:
            self.drive_base.drive(
                speed=-speed,
                turn_rate=-90  # degrees per second
            )

        # otherwise stop
        else:
            self.drive_base.stop()

    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.pressed():
                if self.color_sensor.ambient() < 5:  # 15 not dark enough
                    self.speaker.play_file(file=SoundFile.UP)

                    self.bazooka_blast_motor.run_angle(
                        speed=1000,  # degrees per second
                        rotation_angle=-3 * 360,  # degrees
                        then=Stop.HOLD,
                        wait=True)

                else:
                    self.speaker.play_file(file=SoundFile.DOWN)

                    self.bazooka_blast_motor.run_angle(
                        speed=1000,  # degrees per second
                        rotation_angle=3 * 360,  # degrees
                        then=Stop.HOLD,
                        wait=True)

    def main(
            self,
            driving_speed: float = 1000  # mm/s
    ):
        self.screen.load_image(ImageFile.TARGET)

        Process(target=self.blast_bazooka_whenever_touched).start()
        # OSError: [Errno 5] EIO:
        # Unexpected hardware input/output error with a motor or sensor:
        # --> Try unplugging the sensor or motor and plug it back in again.
        # --> To see which sensor or motor is causing the problem,
        #     check the line in your script that matches
        #     the line number given in the 'Traceback' above.
        # --> Try rebooting the hub/brick if the problem persists.

        self.keep_driving_by_ir_beacon(speed=driving_speed)
Пример #11
0
    if Button.RIGHT_DOWN in infraredSensor.buttons(2):
        manualSound = False

# system setup
ev3.speaker.set_volume(100, which='_all_')
motor.reset_angle(20)
soundMotor.reset_angle(0)
motor.run_target(500, 75, wait=True)
ev3.light.on(Color.GREEN)
watch.reset()

# main project loop
while shutdown == False:

    ''' finishing the progamm '''
    if Button.LEFT_UP in infraredSensor.buttons(4):
        shutdown = True

    ''' checking touch sensor '''
    if touchSensor.pressed() == True:
        if manualLight == False and manualSound == False:
            manualLight = True
            manualSound = True
        else:
            manualLight = False
            manualSound = False
        wait(200)

    if manualLight == True and manualSound == True:

        ev3.light.on(Color.RED)
Пример #12
0
# Start
#
brick.sound.beep(2000)
brick.sound.beep(1000)
brick.sound.beep(5000)

useful_buttons = [
    Button.LEFT_UP,
    Button.LEFT_DOWN,
    Button.RIGHT_UP,
    Button.RIGHT_DOWN,
]

while not touch_sensor.pressed():
    (dist, angle) = ir.beacon(1)
    buttons = ir.buttons(1)
    print(dist, angle, buttons)
    # if angle is not None and dist > 15:
    #     tracks.drive(2 * dist, 2 * angle)
    if useful_buttons in buttons:
        if Button.LEFT_UP in buttons and Button.RIGHT_UP in buttons:
            print("forward")
            tracks.drive(200, 0)
        elif Button.LEFT_UP in buttons:
            print("left")
            tracks.drive(200, -30)
        elif Button.RIGHT_UP in buttons:
            print("right")
            tracks.drive(200, 30)
        elif Button.LEFT_DOWN in buttons and Button.RIGHT_DOWN in buttons:
            print("shoot")
Пример #13
0
def main():
    ev3 = EV3Brick()
    ev3.speaker.beep()
    ev3.screen.clear()
    ev3.light.on(Color.YELLOW)

    colors = ColorSensor(Port.S3)
    ir = InfraredSensor(Port.S4)

    left_motor = Motor(Port.B, Direction.CLOCKWISE)
    left_speed = 0
    left_pressed = False

    right_motor = Motor(Port.C, Direction.CLOCKWISE)
    right_speed = 0
    right_pressed = False

    speed_mult = 128

    ev3.light.off()

    while not ev3.buttons.pressed():

        b = []
        ch = 0

        # look for button(s) pressed in channel
        for channel in range(1, 5):
            cb = ir.buttons(channel)

            if (len(cb)):
                b = cb
                ch = channel
                break

        # left motor control
        if (Button.LEFT_UP in b):
            if (not left_pressed):
                left_pressed = True
                if (left_speed < 0):
                    left_speed = 0
                else:
                    left_speed = speed_mult * ch

        elif (Button.LEFT_DOWN in b):
            if (not left_pressed):
                left_pressed = True
                if (0 < left_speed):
                    left_speed = 0
                else:
                    left_speed = -1 * speed_mult * ch

        else:
            left_pressed = False

        # right motor control
        if (Button.RIGHT_UP in b):
            if (not right_pressed):
                right_pressed = True
                if (right_speed < 0):
                    right_speed = 0
                else:
                    right_speed = speed_mult * ch

        elif (Button.RIGHT_DOWN in b):
            if (not right_pressed):
                right_pressed = True
                if (0 < right_speed):
                    right_speed = 0
                else:
                    right_speed = -1 * speed_mult * channel

        else:
            right_pressed = False

        left_motor.run(left_speed)
        right_motor.run(right_speed)

        wait(50)
Пример #14
0
class Ev3rstorm(EV3Brick):
    WHEEL_DIAMETER = 26  # milimeters
    AXLE_TRACK = 102  # milimeters

    def __init__(self,
                 left_foot_motor_port: Port = Port.B,
                 right_foot_motor_port: Port = Port.C,
                 bazooka_blast_motor_port: Port = Port.A,
                 touch_sensor_port: Port = Port.S1,
                 color_sensor_port: Port = Port.S3,
                 ir_sensor_port: Port = Port.S4,
                 ir_beacon_channel: int = 1):
        self.drive_base = DriveBase(
            left_motor=Motor(port=left_foot_motor_port,
                             positive_direction=Direction.CLOCKWISE),
            right_motor=Motor(port=right_foot_motor_port,
                              positive_direction=Direction.CLOCKWISE),
            wheel_diameter=self.WHEEL_DIAMETER,
            axle_track=self.AXLE_TRACK)

        self.bazooka_blast_motor = Motor(
            port=bazooka_blast_motor_port,
            positive_direction=Direction.CLOCKWISE)

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

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

    def drive_once_by_ir_beacon(
            self,
            speed: float = 1000,  # mm/s
            turn_rate: float = 90  # rotational speed deg/s
    ):
        ir_beacon_button_pressed = set(
            self.ir_sensor.buttons(channel=self.ir_beacon_channel))

        # forward
        if ir_beacon_button_pressed == {Button.LEFT_UP, Button.RIGHT_UP}:
            self.drive_base.drive(speed=speed, turn_rate=0)

        # backward
        elif ir_beacon_button_pressed == {Button.LEFT_DOWN, Button.RIGHT_DOWN}:
            self.drive_base.drive(speed=-speed, turn_rate=0)

        # turn left on the spot
        elif ir_beacon_button_pressed == {Button.LEFT_UP, Button.RIGHT_DOWN}:
            self.drive_base.drive(speed=0, turn_rate=-turn_rate)

        # turn right on the spot
        elif ir_beacon_button_pressed == {Button.RIGHT_UP, Button.LEFT_DOWN}:
            self.drive_base.drive(speed=0, turn_rate=turn_rate)

        # turn left forward
        elif ir_beacon_button_pressed == {Button.LEFT_UP}:
            self.drive_base.drive(speed=speed, turn_rate=-turn_rate)

        # turn right forward
        elif ir_beacon_button_pressed == {Button.RIGHT_UP}:
            self.drive_base.drive(speed=speed, turn_rate=turn_rate)

        # turn left backward
        elif ir_beacon_button_pressed == {Button.LEFT_DOWN}:
            self.drive_base.drive(speed=-speed, turn_rate=turn_rate)

        # turn right backward
        elif ir_beacon_button_pressed == {Button.RIGHT_DOWN}:
            self.drive_base.drive(speed=-speed, turn_rate=-turn_rate)

        # otherwise stop
        else:
            self.drive_base.stop()

    def keep_driving_by_ir_beacon(
            self,
            speed: float = 1000,  # mm/s
            turn_rate: float = 90  # rotational speed deg/s
    ):
        while True:
            self.drive_once_by_ir_beacon(speed=speed, turn_rate=turn_rate)

    def dance_whenever_ir_beacon_pressed(self):
        while True:
            while Button.BEACON in self.ir_sensor.buttons(
                    channel=self.ir_beacon_channel):
                self.drive_base.turn(angle=randint(-360, 360))

    def keep_detecting_objects_by_ir_sensor(self):
        while True:
            if self.ir_sensor.distance() < 25:
                self.light.on(color=Color.RED)
                self.speaker.play_file(file=SoundFile.OBJECT)
                self.speaker.play_file(file=SoundFile.DETECTED)
                self.speaker.play_file(file=SoundFile.ERROR_ALARM)

            else:
                self.light.off()

    def blast_bazooka_whenever_touched(self):
        MEDIUM_MOTOR_N_ROTATIONS_PER_BLAST = 3
        MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST = MEDIUM_MOTOR_N_ROTATIONS_PER_BLAST * 360

        while True:
            if self.touch_sensor.pressed():
                if self.color_sensor.ambient() < 5:  # 15 not dark enough
                    self.speaker.play_file(file=SoundFile.UP)

                    self.bazooka_blast_motor.run_angle(
                        speed=2 *
                        MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST,  # shoot quickly in half a second
                        rotation_angle=
                        -MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST,
                        then=Stop.HOLD,
                        wait=True)

                    self.speaker.play_file(file=SoundFile.LAUGHING_1)

                else:
                    self.speaker.play_file(file=SoundFile.DOWN)

                    self.bazooka_blast_motor.run_angle(
                        speed=2 *
                        MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST,  # shoot quickly in half a second
                        rotation_angle=
                        MEDIUM_MOTOR_ROTATIONAL_DEGREES_PER_BLAST,
                        then=Stop.HOLD,
                        wait=True)

                    self.speaker.play_file(file=SoundFile.LAUGHING_2)

    def main(
            self,
            driving_speed: float = 1000  # mm/s
    ):
        self.screen.load_image(ImageFile.TARGET)

        # FIXME: following thread seems to fail to run
        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)
Пример #15
0
class Ev3rstorm(EV3Brick):
    WHEEL_DIAMETER = 26
    AXLE_TRACK = 102

    def __init__(self,
                 left_foot_motor_port: Port = Port.B,
                 right_foot_motor_port: Port = Port.C,
                 bazooka_blast_motor_port: Port = Port.A,
                 touch_sensor_port: Port = Port.S1,
                 color_sensor_port: Port = Port.S3,
                 ir_sensor_port: Port = Port.S4,
                 ir_beacon_channel: int = 1):
        self.drive_base = DriveBase(
            left_motor=Motor(port=left_foot_motor_port,
                             positive_direction=Direction.CLOCKWISE),
            right_motor=Motor(port=right_foot_motor_port,
                              positive_direction=Direction.CLOCKWISE),
            wheel_diameter=self.WHEEL_DIAMETER,
            axle_track=self.AXLE_TRACK)
        self.drive_base.settings(
            straight_speed=300,  # milimeters per second
            straight_acceleration=300,
            turn_rate=90,  # degrees per second
            turn_acceleration=90)

        self.bazooka_blast_motor = Motor(
            port=bazooka_blast_motor_port,
            positive_direction=Direction.CLOCKWISE)

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

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

    def drive_once_by_ir_beacon(
            self,
            speed: float = 1000  # milimeters per second
    ):
        ir_beacon_buttons_pressed = set(
            self.ir_sensor.buttons(channel=self.ir_beacon_channel))

        # forward
        if ir_beacon_buttons_pressed == {Button.LEFT_UP, Button.RIGHT_UP}:
            self.drive_base.drive(
                speed=speed,
                turn_rate=0  # degrees per second
            )

        # backward
        elif ir_beacon_buttons_pressed == {
                Button.LEFT_DOWN, Button.RIGHT_DOWN
        }:
            self.drive_base.drive(
                speed=-speed,
                turn_rate=0  # degrees per second
            )

        # turn left on the spot
        elif ir_beacon_buttons_pressed == {Button.LEFT_UP, Button.RIGHT_DOWN}:
            self.drive_base.drive(
                speed=0,
                turn_rate=-90  # degrees per second
            )

        # turn right on the spot
        elif ir_beacon_buttons_pressed == {Button.LEFT_DOWN, Button.RIGHT_UP}:
            self.drive_base.drive(
                speed=0,
                turn_rate=90  # degrees per second
            )

        # turn left forward
        elif ir_beacon_buttons_pressed == {Button.LEFT_UP}:
            self.drive_base.drive(
                speed=speed,
                turn_rate=-90  # degrees per second
            )

        # turn right forward
        elif ir_beacon_buttons_pressed == {Button.RIGHT_UP}:
            self.drive_base.drive(
                speed=speed,
                turn_rate=90  # degrees per second
            )

        # turn left backward
        elif ir_beacon_buttons_pressed == {Button.LEFT_DOWN}:
            self.drive_base.drive(
                speed=-speed,
                turn_rate=90  # degrees per second
            )

        # turn right backward
        elif ir_beacon_buttons_pressed == {Button.RIGHT_DOWN}:
            self.drive_base.drive(
                speed=-speed,
                turn_rate=-90  # degrees per second
            )

        # otherwise stop
        else:
            self.drive_base.stop()

    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.pressed():
                if self.color_sensor.ambient() < 5:  # 15 not dark enough
                    self.speaker.play_file(file=SoundFile.UP)

                    self.bazooka_blast_motor.run_angle(
                        speed=1000,  # degrees per second
                        rotation_angle=-3 * 360,  # degrees
                        then=Stop.HOLD,
                        wait=True)

                else:
                    self.speaker.play_file(file=SoundFile.DOWN)

                    self.bazooka_blast_motor.run_angle(
                        speed=1000,  # degrees per second
                        rotation_angle=3 * 360,  # degrees
                        then=Stop.HOLD,
                        wait=True)

    def main(
            self,
            driving_speed: float = 1000  # mm/s
    ):
        self.screen.load_image(ImageFile.TARGET)

        run_parallel(self.keep_driving_by_ir_beacon,
                     self.blast_bazooka_whenever_touched)
Пример #16
0
def main():
    ev3 = EV3Brick()
    ev3.speaker.beep()
    ev3.screen.clear()
    ev3.light.on(Color.RED)

    right_motor = Motor(Port.B, Direction.CLOCKWISE)
    left_motor = Motor(Port.D, Direction.CLOCKWISE)
    wheel_diameter = 42

    right_motor.reset_angle(0.0)
    left_motor.reset_angle(0.0)

    imu = dIMU(Port.S2)
    ir = InfraredSensor(Port.S3)

    # calibrate IMU
    # hold balance bot upright while calibration is in process
    performCalibration(ev3, imu)

    ev3.light.on(Color.YELLOW)

    ## ---- ##

    ir_channel = 1

    emer_off_tilt_angle = 0.3

    emer_off_ticks_max = 4
    emer_off_ticks = 0

    gyro_ang = 0.0  # gyro angle in degrees

    motor_pos = 0.0  # Rotation angle of motor in degrees
    motor_sum = 0.0

    d = [0.0, 0.0, 0.0, 0.0]
    stamp = time.time()

    loops = 10  # Initialization

    speed = 0  # Forward motion speed of robot [-10,10]
    direction = 0  # Direction of robot [-50(left),50(right)]

    while True:

        # Get time in seconds since last step
        now = time.time()
        dt = now - stamp

        stamp = now

        # read gyro
        (gx, gy, gz) = imu.gyro_axes()

        gyro_spd = gz  # degrees / sec
        gyro_ang += (gyro_spd * dt)
        # integrate angle speed to get angle

        # Get motor rotation angle and rotational angle speed
        prev_motor_sum = motor_sum
        motor_sum = right_motor.angle() + left_motor.angle()

        d.append(motor_sum - prev_motor_sum)
        d.pop(0)

        motor_pos += d[-1]
        motor_spd = (sum(d) / len(d)) / dt
        # motor rotational speed

        # check initialized
        if (0 < loops):
            loops -= 1

            if (not loops):
                ev3.light.on(Color.GREEN)

        # compute new motor power in [-100,100]
        else:
            motor_pos -= speed
            pwr = 0.08 * motor_spd + 0.12 * motor_pos + 1.0 * gyro_spd + 25 * gyro_ang

            if (pwr > 100):
                pwr = 100
            elif (pwr < -100):
                pwr = -100

            right_motor.dc(int(pwr - direction))
            left_motor.dc(int(pwr + direction))

        # check too much tilt
        (ax, ay, az) = imu.acc_axes(True)

        if (abs(ay) <= emer_off_tilt_angle):
            emer_off_ticks = 0

        else:
            emer_off_ticks += 1

            if (emer_off_ticks_max <= emer_off_ticks):
                ev3.light.on(Color.RED)
                break

        # check buttons
        b = ir.buttons(ir_channel)

        if (Button.BEACON in b):
            break

        elif ((Button.LEFT_UP in b) and (Button.RIGHT_UP in b)):
            speed = 20
            direction = 0
        elif ((Button.LEFT_DOWN in b) and (Button.RIGHT_DOWN in b)):
            speed = -20
            direction = 0

        elif ((Button.LEFT_UP in b) and (Button.RIGHT_DOWN in b)):
            speed = 0
            direction = 35
        elif ((Button.RIGHT_UP in b) and (Button.LEFT_DOWN in b)):
            speed = 0
            direction = -35

        elif ((Button.LEFT_UP in b) or (Button.RIGHT_DOWN in b)):
            speed = 0
            direction = 25
        elif ((Button.RIGHT_UP in b) or (Button.LEFT_DOWN in b)):
            speed = 0
            direction = -25

        else:
            speed = 0
            direction = 0

    right_motor.stop()
    left_motor.stop()
Пример #17
0
class RemoteControlledTank:
    """
    This reusable mixin provides the capability of driving a robot
    with a Driving Base by the IR beacon
    """
    def __init__(
            self,
            wheel_diameter: float, axle_track: float,   # both in milimeters
            left_motor_port: Port = Port.B, right_motor_port: Port = Port.C,
            ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1):
        self.driver = \
            DriveBase(
                left_motor=Motor(port=left_motor_port,
                                 positive_direction=Direction.CLOCKWISE),
                right_motor=Motor(port=right_motor_port,
                                  positive_direction=Direction.CLOCKWISE),
                wheel_diameter=wheel_diameter,
                axle_track=axle_track)

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

    def drive_by_ir_beacon(
            self,
            speed: float = 1000,    # mm/s
            turn_rate: float = 90   # rotational speed deg/s
            ):
        ir_beacon_button_pressed = \
            set(self.ir_sensor.buttons(channel=self.ir_beacon_channel))

        # forward
        if ir_beacon_button_pressed == {Button.LEFT_UP, Button.RIGHT_UP}:
            self.driver.drive(
                speed=speed,
                turn_rate=0)

        # backward
        elif ir_beacon_button_pressed == {Button.LEFT_DOWN, Button.RIGHT_DOWN}:
            self.driver.drive(
                speed=-speed,
                turn_rate=0)

        # turn left on the spot
        elif ir_beacon_button_pressed == {Button.LEFT_UP, Button.RIGHT_DOWN}:
            self.driver.drive(
                speed=0,
                turn_rate=-turn_rate)

        # turn right on the spot
        elif ir_beacon_button_pressed == {Button.RIGHT_UP, Button.LEFT_DOWN}:
            self.driver.drive(
                speed=0,
                turn_rate=turn_rate)

        # turn left forward
        elif ir_beacon_button_pressed == {Button.LEFT_UP}:
            self.driver.drive(
                speed=speed,
                turn_rate=-turn_rate)

        # turn right forward
        elif ir_beacon_button_pressed == {Button.RIGHT_UP}:
            self.driver.drive(
                speed=speed,
                turn_rate=turn_rate)

        # turn left backward
        elif ir_beacon_button_pressed == {Button.LEFT_DOWN}:
            self.driver.drive(
                speed=-speed,
                turn_rate=turn_rate)

        # turn right backward
        elif ir_beacon_button_pressed == {Button.RIGHT_DOWN}:
            self.driver.drive(
                speed=-speed,
                turn_rate=-turn_rate)

        # otherwise stop
        else:
            self.driver.stop()
Пример #18
0
class Bobb3e:
    WHEEL_DIAMETER = 24  # milimeters
    AXLE_TRACK = 100  # milimeters

    def __init__(self,
                 left_motor_port: str = Port.B,
                 right_motor_port: str = Port.C,
                 lift_motor_port: str = Port.A,
                 ir_sensor_port: str = Port.S4,
                 ir_beacon_channel: int = 1):
        self.ev3_brick = EV3Brick()

        left_motor = Motor(port=left_motor_port,
                           positive_direction=Direction.COUNTERCLOCKWISE)
        right_motor = Motor(port=right_motor_port,
                            positive_direction=Direction.COUNTERCLOCKWISE)
        self.drive_base = DriveBase(left_motor=left_motor,
                                    right_motor=right_motor,
                                    wheel_diameter=self.WHEEL_DIAMETER,
                                    axle_track=self.AXLE_TRACK)

        self.lift_motor = Motor(port=lift_motor_port,
                                positive_direction=Direction.CLOCKWISE)

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

        self.reversing = False

    def drive_or_operate_forks_by_ir_beacon(
            self,
            driving_speed: float = 1000,  # mm/s
            turn_rate: float = 90  # rotational speed deg/s
    ):
        """
        Read the commands from the remote control and convert them into actions
        such as go forward, lift and turn.
        """
        while True:
            ir_beacon_button_pressed = \
                set(self.ir_sensor.buttons(channel=self.ir_beacon_channel))

            # lower the forks
            if ir_beacon_button_pressed == {Button.LEFT_UP, Button.LEFT_DOWN}:
                self.reversing = False

                self.drive_base.stop()

                self.lift_motor.run(speed=100)

            # raise the forks
            elif ir_beacon_button_pressed == \
                    {Button.RIGHT_UP, Button.RIGHT_DOWN}:
                self.reversing = False

                self.drive_base.stop()

                self.lift_motor.run(speed=-100)

            # forward
            elif ir_beacon_button_pressed == {Button.LEFT_UP, Button.RIGHT_UP}:
                self.reversing = False

                self.drive_base.drive(speed=driving_speed, turn_rate=0)

                self.lift_motor.hold()

            # backward
            elif ir_beacon_button_pressed == \
                    {Button.LEFT_DOWN, Button.RIGHT_DOWN}:
                self.reversing = True

                self.drive_base.drive(speed=-driving_speed, turn_rate=0)

                self.lift_motor.hold()

            # turn left on the spot
            elif ir_beacon_button_pressed == \
                    {Button.LEFT_UP, Button.RIGHT_DOWN}:
                self.reversing = False

                self.drive_base.drive(speed=0, turn_rate=-turn_rate)

                self.lift_motor.hold()

            # turn right on the spot
            elif ir_beacon_button_pressed == \
                    {Button.RIGHT_UP, Button.LEFT_DOWN}:
                self.reversing = False

                self.drive_base.drive(speed=0, turn_rate=turn_rate)

                self.lift_motor.hold()

            # turn left forward
            elif ir_beacon_button_pressed == {Button.LEFT_UP}:
                self.reversing = False

                self.drive_base.drive(speed=driving_speed,
                                      turn_rate=-turn_rate)

                self.lift_motor.hold()

            # turn right forward
            elif ir_beacon_button_pressed == {Button.RIGHT_UP}:
                self.reversing = False

                self.drive_base.drive(speed=driving_speed, turn_rate=turn_rate)

                self.lift_motor.hold()

            # turn left backward
            elif ir_beacon_button_pressed == {Button.LEFT_DOWN}:
                self.reversing = True

                self.drive_base.drive(speed=-driving_speed,
                                      turn_rate=turn_rate)

                self.lift_motor.hold()

            # turn right backward
            elif ir_beacon_button_pressed == {Button.RIGHT_DOWN}:
                self.reversing = True

                self.drive_base.drive(speed=-driving_speed,
                                      turn_rate=-turn_rate)

                self.lift_motor.hold()

            # otherwise stop
            else:
                self.reversing = False

                self.drive_base.stop()

                self.lift_motor.hold()

            wait(10)

    def sound_alarm_whenever_reversing(self):
        while True:
            if self.reversing:
                self.ev3_brick.speaker.play_file(file=SoundFile.BACKING_ALERT)

            wait(10)
Пример #19
0
class R3ptar:
    """
    R3ptar can be driven around by the IR Remote Control,
    strikes when the Beacon button is pressed,
    and hisses when the Touch Sensor is pressed
    (inspiration from LEGO Mindstorms EV3 Home Edition: R3ptar: Tutorial #4)
    """
    def __init__(self,
                 steering_motor_port: Port = Port.A,
                 driving_motor_port: Port = Port.B,
                 striking_motor_port: Port = Port.D,
                 touch_sensor_port: Port = Port.S1,
                 ir_sensor_port: Port = Port.S4,
                 ir_beacon_channel: int = 1):
        self.ev3_brick = EV3Brick()

        self.steering_motor = Motor(port=steering_motor_port,
                                    positive_direction=Direction.CLOCKWISE)
        self.driving_motor = Motor(port=driving_motor_port,
                                   positive_direction=Direction.CLOCKWISE)
        self.striking_motor = Motor(port=striking_motor_port,
                                    positive_direction=Direction.CLOCKWISE)

        self.touch_sensor = TouchSensor(port=touch_sensor_port)

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

    def drive_by_ir_beacon(
            self,
            speed: float = 1000,  # mm/s
    ):
        ir_beacons_pressed = \
            set(self.ir_sensor.buttons(channel=self.ir_beacon_channel))

        if ir_beacons_pressed == {Button.LEFT_UP, Button.RIGHT_UP}:
            self.driving_motor.run(speed=speed)

        elif ir_beacons_pressed == {Button.LEFT_DOWN, Button.RIGHT_DOWN}:
            self.driving_motor.run(speed=-speed)

        elif ir_beacons_pressed == {Button.LEFT_UP}:
            self.steering_motor.run(speed=-500)
            self.driving_motor.run(speed=speed)

        elif ir_beacons_pressed == {Button.RIGHT_UP}:
            self.steering_motor.run(speed=500)
            self.driving_motor.run(speed=speed)

        elif ir_beacons_pressed == {Button.LEFT_DOWN}:
            self.steering_motor.run(speed=-500)
            self.driving_motor.run(speed=-speed)

        elif ir_beacons_pressed == {Button.RIGHT_DOWN}:
            self.steering_motor.run(speed=500)
            self.driving_motor.run(speed=-speed)

        else:
            self.steering_motor.hold()
            self.driving_motor.stop()

    def strike_by_ir_beacon(self, speed: float = 1000):
        if Button.BEACON in \
                self.ir_sensor.buttons(channel=self.ir_beacon_channel):
            self.striking_motor.run_time(speed=speed,
                                         time=1000,
                                         then=Stop.COAST,
                                         wait=True)

            self.striking_motor.run_time(speed=-speed,
                                         time=1000,
                                         then=Stop.COAST,
                                         wait=True)

            while Button.BEACON in \
                    self.ir_sensor.buttons(channel=self.ir_beacon_channel):
                pass

    def hiss_if_touched(self):
        if self.touch_sensor.pressed():
            self.ev3_brick.speaker.play_file(file=SoundFile.SNAKE_HISS)

    def main(self, speed: float = 1000):
        """
        R3ptar's main program performing various capabilities
        """
        while True:
            self.drive_by_ir_beacon(speed=speed)
            self.strike_by_ir_beacon(speed=speed)
            self.hiss_if_touched()
            wait(1)
                       wheel_diameter=WHEEL_DIAMETER,
                       axle_track=AXLE_TRACK)
DRIVE_BASE.settings(
    straight_speed=300,  # milimeters per second
    straight_acceleration=300,
    turn_rate=90,  # degrees per second
    turn_acceleration=90)

MEDIUM_MOTOR = Motor(port=Port.A, positive_direction=Direction.CLOCKWISE)

IR_SENSOR = InfraredSensor(port=Port.S4)

while True:
    BRICK.light.on(color=Color.ORANGE)

    if Button.BEACON in IR_SENSOR.buttons(channel=1):
        # FIXME: make it work

        distance, heading = IR_SENSOR.beacon(channel=1)
        heading_difference = heading - (-3)
        proximity_difference = distance - 70

        if (heading_difference == 0) and (proximity_difference == 0):
            DRIVE_BASE.stop()

            BRICK.light.on(color=Color.RED)

            MEDIUM_MOTOR.run_angle(
                speed=1000,  # degrees per second
                rotation_angle=6 * 360,  # degrees
                then=Stop.HOLD,
Пример #21
0
class IRBeaconRemoteControlledTank:
    def __init__(
            self,
            wheel_diameter: float,
            axle_track: float,  # both in milimeters
            left_motor_port: Port = Port.B,
            right_motor_port: Port = Port.C,
            polarity: str = 'normal',
            ir_sensor_port: Port = Port.S4,
            ir_beacon_channel: int = 1):
        self.left_motor = Motor(
            port=left_motor_port,
            positive_direction=Direction.CLOCKWISE
            if polarity == 'normal' else Direction.COUNTERCLOCKWISE)

        self.right_motor = Motor(
            port=left_motor_port,
            positive_direction=Direction.CLOCKWISE
            if polarity == 'normal' else Direction.COUNTERCLOCKWISE)

        self.drive_base = DriveBase(left_motor=self.left_motor,
                                    right_motor=self.right_motor,
                                    wheel_diameter=wheel_diameter,
                                    axle_track=axle_track)

        self.ir_sensor = InfraredSensor(port=ir_sensor_port)
        self.tank_drive_ir_beacon_channel = ir_beacon_channel

    def drive_once_by_ir_beacon(
            self,
            speed: float = 1000,  # mm/s
            turn_rate: float = 90  # rotational speed deg/s
    ):
        ir_beacon_button_pressed = \
            set(self.ir_sensor.buttons(
                    channel=self.tank_drive_ir_beacon_channel))

        # forward
        if ir_beacon_button_pressed == {Button.LEFT_UP, Button.RIGHT_UP}:
            self.drive_base.drive(speed=speed, turn_rate=0)

        # backward
        elif ir_beacon_button_pressed == {Button.LEFT_DOWN, Button.RIGHT_DOWN}:
            self.drive_base.drive(speed=-speed, turn_rate=0)

        # turn left on the spot
        elif ir_beacon_button_pressed == {Button.LEFT_UP, Button.RIGHT_DOWN}:
            self.drive_base.drive(speed=0, turn_rate=-turn_rate)

        # turn right on the spot
        elif ir_beacon_button_pressed == {Button.RIGHT_UP, Button.LEFT_DOWN}:
            self.drive_base.drive(speed=0, turn_rate=turn_rate)

        # turn left forward
        elif ir_beacon_button_pressed == {Button.LEFT_UP}:
            self.drive_base.drive(speed=speed, turn_rate=-turn_rate)

        # turn right forward
        elif ir_beacon_button_pressed == {Button.RIGHT_UP}:
            self.drive_base.drive(speed=speed, turn_rate=turn_rate)

        # turn left backward
        elif ir_beacon_button_pressed == {Button.LEFT_DOWN}:
            self.drive_base.drive(speed=-speed, turn_rate=turn_rate)

        # turn right backward
        elif ir_beacon_button_pressed == {Button.RIGHT_DOWN}:
            self.drive_base.drive(speed=-speed, turn_rate=-turn_rate)

        # otherwise stop
        else:
            self.drive_base.stop()

    # 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 = 1000,  # mm/s
            turn_rate: float = 90  # rotational speed deg/s
    ):
        while True:
            self.drive_once_by_ir_beacon(speed=speed, turn_rate=turn_rate)
Пример #22
0
class Catapult(IRBeaconRemoteControlledTank, EV3Brick):
    WHEEL_DIAMETER = 23
    AXLE_TRACK = 65

    def __init__(self,
                 left_motor_port: Port = Port.B,
                 right_motor_port: Port = Port.C,
                 catapult_motor_port: Port = Port.A,
                 touch_sensor_port: Port = Port.S1,
                 color_sensor_port: Port = Port.S3,
                 ir_sensor_port: Port = Port.S4,
                 ir_beacon_channel: int = 1):
        super().__init__(wheel_diameter=self.WHEEL_DIAMETER,
                         axle_track=self.AXLE_TRACK,
                         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 = Motor(port=catapult_motor_port,
                                    positive_direction=Direction.CLOCKWISE)

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

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

    def scan_colors(self):
        while True:
            if self.color_sensor.color() == Color.YELLOW:
                pass

            elif self.color_sensor.color() == Color.WHITE:
                self.speaker.play_file(file=SoundFile.GOOD)

    def make_noise_when_touched(self):
        while True:
            if self.touch_sensor.pressed():
                self.speaker.play_file(file=SoundFile.OUCH)

    def throw_by_ir_beacon(self):
        while True:
            if Button.BEACON in self.ir_sensor.buttons(
                    channel=self.ir_beacon_channel):
                self.catapult_motor.run_angle(speed=3500,
                                              rotation_angle=-150,
                                              then=Stop.HOLD,
                                              wait=True)

                self.catapult_motor.run_angle(speed=3500,
                                              rotation_angle=150,
                                              then=Stop.HOLD,
                                              wait=True)

                while Button.BEACON in self.ir_sensor.buttons(
                        channel=self.ir_beacon_channel):
                    pass

    def main(self):
        self.speaker.play_file(file=SoundFile.YES)

        Process(target=self.make_noise_when_touched).start()

        Process(target=self.throw_by_ir_beacon).start()

        Process(target=self.scan_colors).start()

        self.keep_driving_by_ir_beacon(speed=1000)
class R3ptar(EV3Brick):
    def __init__(self,
                 turn_motor_port: Port = Port.A,
                 move_motor_port: Port = Port.B,
                 scare_motor_port: Port = Port.D,
                 touch_sensor_port: Port = Port.S1,
                 color_sensor_port: Port = Port.S3,
                 ir_sensor_port: Port = Port.S4,
                 ir_beacon_channel: int = 1):
        self.turn_motor = Motor(port=turn_motor_port,
                                positive_direction=Direction.CLOCKWISE)
        self.move_motor = Motor(port=move_motor_port,
                                positive_direction=Direction.CLOCKWISE)
        self.scare_motor = Motor(port=scare_motor_port,
                                 positive_direction=Direction.CLOCKWISE)

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

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

    def drive_once_by_ir_beacon(self, speed: float = 1000):
        ir_beacons_pressed = set(
            self.ir_sensor.buttons(channel=self.ir_beacon_channel))

        if ir_beacons_pressed == {Button.LEFT_UP, Button.RIGHT_UP}:
            self.move_motor.run(speed=speed)

        elif ir_beacons_pressed == {Button.LEFT_DOWN, Button.RIGHT_DOWN}:
            self.move_motor.run(speed=-speed)

        elif ir_beacons_pressed == {Button.LEFT_UP}:
            self.turn_motor.run(speed=-500)

            self.move_motor.run(speed=speed)

        elif ir_beacons_pressed == {Button.RIGHT_UP}:
            self.turn_motor.run(speed=500)

            self.move_motor.run(speed=speed)

        elif ir_beacons_pressed == {Button.LEFT_DOWN}:
            self.turn_motor.run(speed=-500)

            self.move_motor.run(speed=-speed)

        elif ir_beacons_pressed == {Button.RIGHT_DOWN}:
            self.turn_motor.run(speed=500)

            self.move_motor.run(speed=-speed)

        else:
            self.turn_motor.hold()

            self.move_motor.stop()

    def bite_by_ir_beacon(self, speed: float = 1000):
        if Button.BEACON in self.ir_sensor.buttons(
                channel=self.ir_beacon_channel):
            self.speaker.play_file(file=SoundFile.SNAKE_HISS)

            self.scare_motor.run_time(speed=speed,
                                      time=1000,
                                      then=Stop.HOLD,
                                      wait=True)

            self.scare_motor.run_time(speed=-300,
                                      time=1000,
                                      then=Stop.COAST,
                                      wait=True)

            while Button.BEACON in self.ir_sensor.buttons(
                    channel=self.ir_beacon_channel):
                pass

    def run_away_if_chased(self):
        if self.color_sensor.reflection() > 30:
            self.move_motor.run_time(speed=500,
                                     time=4000,
                                     then=Stop.HOLD,
                                     wait=True)

            for i in range(2):
                self.turn_motor.run_time(speed=500,
                                         time=1000,
                                         then=Stop.HOLD,
                                         wait=True)

                self.turn_motor.run_time(speed=-500,
                                         time=1000,
                                         then=Stop.HOLD,
                                         wait=True)

    def bite_if_touched(self):
        if self.touch_sensor.pressed():
            self.speaker.play_file(file=SoundFile.SNAKE_HISS)

            self.scare_motor.run_time(speed=1000,
                                      time=1000,
                                      then=Stop.HOLD,
                                      wait=True)

            self.scare_motor.run_time(speed=-300,
                                      time=1000,
                                      then=Stop.COAST,
                                      wait=True)

    def main(self, speed: float = 1000):
        while True:
            self.drive_once_by_ir_beacon(speed=speed)

            self.bite_by_ir_beacon(speed=speed)

            self.bite_if_touched()

            self.run_away_if_chased()
Пример #24
0
class Rac3Truck:
    WHEEL_DIAMETER = 30   # milimeters
    AXLE_TRACK = 120      # milimeters

    def __init__(
            self,
            left_motor_port: str = Port.B, right_motor_port: str = Port.C,
            polarity: str = 'inversed',
            steer_motor_port: str = Port.A,
            ir_sensor_port: str = Port.S4, ir_beacon_channel: int = 1):
        if polarity == 'normal':
            self.left_motor = Motor(port=left_motor_port,
                                    positive_direction=Direction.CLOCKWISE)
            self.right_motor = Motor(port=right_motor_port,
                                     positive_direction=Direction.CLOCKWISE)
        else:
            self.left_motor = \
                Motor(port=left_motor_port,
                      positive_direction=Direction.COUNTERCLOCKWISE)
            self.right_motor = \
                Motor(port=right_motor_port,
                      positive_direction=Direction.COUNTERCLOCKWISE)
        self.driver = DriveBase(left_motor=self.left_motor,
                                right_motor=self.right_motor,
                                wheel_diameter=self.WHEEL_DIAMETER,
                                axle_track=self.AXLE_TRACK)

        self.steer_motor = Motor(port=steer_motor_port,
                                 positive_direction=Direction.CLOCKWISE)

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

    def reset(self):
        self.steer_motor.run_time(
            speed=300,
            time=1500,
            then=Stop.COAST,
            wait=True)

        self.steer_motor.run_angle(
            speed=-500,
            rotation_angle=120,
            then=Stop.HOLD,
            wait=True)

        self.steer_motor.reset_angle(angle=0)

    def steer_left(self):
        if self.steer_motor.angle() > -65:
            self.steer_motor.run_target(
                speed=-200,
                target_angle=-65,
                then=Stop.HOLD,
                wait=True)

        else:
            self.steer_motor.hold()

    def steer_right(self):
        if self.steer_motor.angle() < 65:
            self.steer_motor.run_target(
                speed=200,
                target_angle=65,
                then=Stop.HOLD,
                wait=True)

        else:
            self.steer_motor.hold()

    def steer_center(self):
        if self.steer_motor.angle() < -7:
            self.steer_motor.run_target(
                speed=200,
                target_angle=4,
                then=Stop.HOLD,
                wait=True)

        elif self.steer_motor.angle() > 7:
            self.steer_motor.run_target(
                speed=-200,
                target_angle=-4,
                then=Stop.HOLD,
                wait=True)

        self.steer_motor.hold()

        wait(100)

    def drive_by_ir_beacon(self):
        ir_beacon_button_pressed = \
            set(self.ir_sensor.buttons(channel=self.ir_beacon_channel))

        # forward
        if ir_beacon_button_pressed == {Button.LEFT_UP, Button.RIGHT_UP}:
            self.driver.drive(
                speed=800,
                turn_rate=0)

            self.steer_center()

        # backward
        elif ir_beacon_button_pressed == {Button.LEFT_DOWN, Button.RIGHT_DOWN}:
            self.driver.drive(
                speed=-800,
                turn_rate=0)

            self.steer_center()

        # turn left forward
        elif ir_beacon_button_pressed == {Button.LEFT_UP}:
            self.left_motor.run(speed=600)
            self.right_motor.run(speed=1000)

            self.steer_left()

        # turn right forward
        elif ir_beacon_button_pressed == {Button.RIGHT_UP}:
            self.left_motor.run(speed=1000)
            self.right_motor.run(speed=600)

            self.steer_right()

        # turn left backward
        elif ir_beacon_button_pressed == {Button.LEFT_DOWN}:
            self.left_motor.run(speed=-600)
            self.right_motor.run(speed=-1000)

            self.steer_left()

        # turn right backward
        elif ir_beacon_button_pressed == {Button.RIGHT_DOWN}:
            self.left_motor.run(speed=-1000)
            self.right_motor.run(speed=-600)

            self.steer_right()

        # otherwise stop
        else:
            self.driver.stop()

            self.steer_center()
Пример #25
0
class Kraz33Hors3:
    def __init__(self,
                 back_foot_motor_port: Port = Port.C,
                 front_foot_motor_port: Port = Port.B,
                 gear_motor_port: Port = Port.A,
                 touch_sensor_port: Port = Port.S1,
                 color_sensor_port: Port = Port.S3,
                 ir_sensor_port: Port = Port.S4,
                 ir_beacon_channel: int = 1):
        self.front_foot_motor = Motor(port=front_foot_motor_port,
                                      positive_direction=Direction.CLOCKWISE)
        self.back_foot_motor = Motor(
            port=back_foot_motor_port,
            positive_direction=Direction.COUNTERCLOCKWISE)

        self.gear_motor = Motor(port=gear_motor_port)

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

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

    def drive_once_by_ir_beacon(
            self,
            speed: float = 1000  # deg/s
    ):
        ir_beacons_pressed = set(
            self.ir_sensor.buttons(channel=self.ir_beacon_channel))

        # forward
        if ir_beacons_pressed == {Button.LEFT_UP, Button.RIGHT_UP}:
            self.front_foot_motor.run_time(
                speed=speed,
                time=1000,  # ms
                then=Stop.COAST,
                wait=False)

            self.back_foot_motor.run_time(
                speed=speed,
                time=1000,  # ms
                then=Stop.COAST,
                wait=True)

        # backward
        elif ir_beacons_pressed == {Button.LEFT_DOWN, Button.RIGHT_DOWN}:
            self.front_foot_motor.run_time(
                speed=-speed,
                time=1000,  # ms
                then=Stop.COAST,
                wait=False)

            self.back_foot_motor.run_time(
                speed=-speed,
                time=1000,  # ms
                then=Stop.COAST,
                wait=True)

        # move crazily
        elif ir_beacons_pressed == {Button.BEACON}:
            self.gear_motor.run(speed=speed)

            self.front_foot_motor.run_time(
                speed=speed / 3,
                time=1000,  # ms
                then=Stop.COAST,
                wait=False)

            self.back_foot_motor.run_time(
                speed=-speed / 3,
                time=1000,  # ms
                then=Stop.COAST,
                wait=True)

        else:
            self.gear_motor.stop()

    def keep_driving_by_ir_beacon(
            self,
            speed: float = 1000  # deg/s
    ):
        while True:
            self.drive_once_by_ir_beacon(speed=speed)

    def back_whenever_touched(
            self,
            speed: float = 1000  # deg/s
    ):
        while True:
            if self.touch_sensor.pressed():
                self.front_foot_motor.run_time(
                    speed=-speed,
                    time=1000,  # ms
                    then=Stop.COAST,
                    wait=False)

                self.back_foot_motor.run_time(
                    speed=-speed,
                    time=1000,  # ms
                    then=Stop.COAST,
                    wait=True)

    def main(
            self,
            speed: float = 1000  # deg/s
    ):
        Thread(target=self.back_whenever_touched).start()

        self.keep_driving_by_ir_beacon(speed=speed)
Пример #26
0
class Spik3r(EV3Brick):
    def __init__(
            self,
            sting_motor_port: Port = Port.D, go_motor_port: Port = Port.B,
            claw_motor_port: Port = Port.A,
            touch_sensor_port: Port = Port.S1, color_sensor_port: Port = Port.S3,
            ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1):
        self.sting_motor = Motor(port=sting_motor_port,
                                 positive_direction=Direction.CLOCKWISE)
        self.go_motor = Motor(port=go_motor_port,
                              positive_direction=Direction.CLOCKWISE)
        self.claw_motor = Motor(port=claw_motor_port,
                                positive_direction=Direction.CLOCKWISE)

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

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


    def sting_by_ir_beacon(self):
        while True:
            ir_buttons_pressed = set(self.ir_sensor.buttons(channel=self.ir_beacon_channel))

            if ir_buttons_pressed == {Button.BEACON}:
                self.sting_motor.run_angle(
                    speed=-750,
                    rotation_angle=220,
                    then=Stop.HOLD,
                    wait=False)

                self.speaker.play_file(file=SoundFile.EV3)

                self.sting_motor.run_time(
                    speed=-1000,
                    time=1000,
                    then=Stop.HOLD,
                    wait=True)

                self.sting_motor.run_time(
                    speed=1000,
                    time=1000,
                    then=Stop.HOLD,
                    wait=True)

                while Button.BEACON in self.ir_sensor.buttons(channel=self.ir_beacon_channel):
                    pass


    def be_noisy_to_people(self):
        while True:
            if self.color_sensor.reflection() > 30:
                for i in range(4):
                    self.speaker.play_file(file=SoundFile.ERROR_ALARM)
                        

    def pinch_if_touched(self):
        while True:
            if self.touch_sensor.pressed():
                self.claw_motor.run_time(
                    speed=500,
                    time=1000,
                    then=Stop.HOLD,
                    wait=True)

                self.claw_motor.run_time(
                    speed=-500,
                    time=0.3 * 1000,
                    then=Stop.HOLD,
                    wait=True)


    def keep_driving_by_ir_beacon(self):
        while True: 
            ir_buttons_pressed = set(self.ir_sensor.buttons(channel=self.ir_beacon_channel))

            if ir_buttons_pressed == {Button.RIGHT_UP, Button.LEFT_UP}:
                self.go_motor.run(speed=910)

            elif ir_buttons_pressed == {Button.RIGHT_UP}:
                self.go_motor.run(speed=-1000)

            else:
                self.go_motor.stop()


    def main(self):
        self.screen.load_image(ImageFile.EVIL)
        
        run_parallel(
            self.be_noisy_to_people,
            self.sting_by_ir_beacon,
            self.pinch_if_touched,
            self.keep_driving_by_ir_beacon)
Пример #27
0
class EV3Game:
    N_LEVELS = 9
    N_OFFSET_DEGREES_FOR_HOLD_CUP = 60
    N_SHUFFLE_SECONDS = 15

    def __init__(self,
                 b_motor_port: Port = Port.B,
                 c_motor_port: Port = Port.C,
                 grip_motor_port: Port = Port.A,
                 touch_sensor_port: Port = Port.S1,
                 ir_sensor_port: Port = Port.S4,
                 ir_beacon_channel: int = 1):
        self.ev3_brick = EV3Brick()

        self.b_motor = Motor(port=b_motor_port,
                             positive_direction=Direction.CLOCKWISE)
        self.c_motor = Motor(port=c_motor_port,
                             positive_direction=Direction.CLOCKWISE)

        self.grip_motor = Motor(port=grip_motor_port,
                                positive_direction=Direction.CLOCKWISE)

        self.touch_sensor = TouchSensor(port=touch_sensor_port)

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

    def calibrate_grip(self):
        self.grip_motor.run_until_stalled(speed=-100,
                                          then=Stop.HOLD,
                                          duty_limit=None)

        self.grip_motor.run_angle(speed=100,
                                  rotation_angle=30,
                                  then=Stop.HOLD,
                                  wait=True)

    def display_level(self):
        self.ev3_brick.screen.clear()

        self.ev3_brick.screen.print('Level {}'.format(self.level))

        wait(300)

    def start_up(self):
        self.ev3_brick.light.on(color=Color.RED)

        self.calibrate_grip()

        self.level = 1

        self.display_level()

        self.choice = 2

        self.current_b = self.current_c = 1

    def select_level(self):
        while not self.touch_sensor.pressed():
            ir_buttons_pressed = \
                set(self.ir_sensor.buttons(channel=self.ir_beacon_channel))

            if ir_buttons_pressed.intersection(
                    {Button.LEFT_UP, Button.RIGHT_UP}) and \
                    (self.level < self.N_LEVELS):
                self.level += 1

                self.display_level()

            elif ir_buttons_pressed.intersection(
                    {Button.LEFT_DOWN, Button.RIGHT_DOWN}) and \
                    (self.level > 1):
                self.level -= 1

                self.display_level()

        self.ev3_brick.speaker.play_file(file=SoundFile.GO)

    def move_1_rotate_b(self):
        if self.current_b == 1:
            self.rotate_b = self.N_OFFSET_DEGREES_FOR_HOLD_CUP + 180

        elif self.current_b == 2:
            self.rotate_b = 2 * self.N_OFFSET_DEGREES_FOR_HOLD_CUP + 180

        elif self.current_b == 3:
            self.rotate_b = 180

    def move_1_rotate_c(self):
        if self.current_c == 1:
            self.rotate_c = 0

        elif self.current_c == 2:
            self.rotate_c = -self.N_OFFSET_DEGREES_FOR_HOLD_CUP

        elif self.current_c == 3:
            self.rotate_c = self.N_OFFSET_DEGREES_FOR_HOLD_CUP

    def move_1(self):
        self.move_1_rotate_b()
        self.move_1_rotate_c()

        self.current_b = 3
        self.current_c = 1

    def move_2_rotate_b(self):
        if self.current_b == 1:
            self.rotate_b = -self.N_OFFSET_DEGREES_FOR_HOLD_CUP - 180

        elif self.current_b == 2:
            self.rotate_b = -180

        elif self.current_b == 3:
            self.rotate_b = -2 * self.N_OFFSET_DEGREES_FOR_HOLD_CUP - 180

    move_2_rotate_c = move_1_rotate_c

    def move_2(self):
        self.move_2_rotate_b()
        self.move_2_rotate_c()

        self.current_b = 2
        self.current_c = 1

    def move_3_rotate_b(self):
        if self.current_b == 1:
            self.rotate_b = 0

        elif self.current_b == 2:
            self.rotate_b = self.N_OFFSET_DEGREES_FOR_HOLD_CUP

        elif self.current_b == 3:
            self.rotate_b = -self.N_OFFSET_DEGREES_FOR_HOLD_CUP

    def move_3_rotate_c(self):
        if self.current_c == 1:
            self.rotate_c = self.N_OFFSET_DEGREES_FOR_HOLD_CUP + 180

        elif self.current_c == 2:
            self.rotate_c = 180

        elif self.current_c == 3:
            self.rotate_c = 2 * self.N_OFFSET_DEGREES_FOR_HOLD_CUP + 180

    def move_3(self):
        self.move_3_rotate_b()
        self.move_3_rotate_c()

        self.current_b = 1
        self.current_c = 2

    move_4_rotate_b = move_3_rotate_b

    def move_4_rotate_c(self):
        if self.current_c == 1:
            self.rotate_c = -self.N_OFFSET_DEGREES_FOR_HOLD_CUP - 180

        elif self.current_c == 2:
            self.rotate_c = -2 * self.N_OFFSET_DEGREES_FOR_HOLD_CUP - 180

        elif self.current_c == 3:
            self.rotate_c = -180

    def move_4(self):
        self.move_4_rotate_b()
        self.move_4_rotate_c()

        self.current_b = 1
        self.current_c = 3

    def execute_move(self):
        speed = 100 * self.level

        if self.current_b == 1:
            self.b_motor.run_angle(speed=speed,
                                   rotation_angle=self.rotate_b,
                                   then=Stop.HOLD,
                                   wait=True)

            self.c_motor.run_angle(speed=speed,
                                   rotation_angle=self.rotate_c,
                                   then=Stop.HOLD,
                                   wait=True)

        else:
            assert self.current_c == 1

            self.c_motor.run_angle(speed=speed,
                                   rotation_angle=self.rotate_c,
                                   then=Stop.HOLD,
                                   wait=True)

            self.b_motor.run_angle(speed=speed,
                                   rotation_angle=self.rotate_b,
                                   then=Stop.HOLD,
                                   wait=True)

    def update_ball_cup(self):
        if self.move in {1, 2}:
            if self.cup_with_ball == 1:
                self.cup_with_ball = 2

            elif self.cup_with_ball == 2:
                self.cup_with_ball = 1

        else:
            if self.cup_with_ball == 2:
                self.cup_with_ball = 3

            elif self.cup_with_ball == 3:
                self.cup_with_ball = 2

    def shuffle(self):
        shuffle_start_time = time()

        while time() - shuffle_start_time < self.N_SHUFFLE_SECONDS:
            self.move = randint(1, 4)

            if self.move == 1:
                self.move_1()

            elif self.move == 2:
                self.move_2()

            elif self.move == 3:
                self.move_3()

            elif self.move == 4:
                self.move_4()

            self.execute_move()
            self.update_ball_cup()

    def reset_motor_positions(self):
        """
        Resetting motors' positions like it is done when the moves finish
        """
        # Resetting Motor B to Position 1,
        # which, for Motor B corresponds to Move 3
        self.move_3_rotate_b()

        # Reseting Motor C to Position 1,
        # which, for Motor C corresponds to Move 1
        self.move_1_rotate_c()

        self.current_b = self.current_c = 1

        # Executing the reset for both motors
        self.execute_move()

    def select_choice(self):
        self.choice = None

        while not self.choice:
            ir_buttons_pressed = \
                set(self.ir_sensor.buttons(channel=self.ir_beacon_channel))

            if ir_buttons_pressed == {Button.LEFT_UP}:
                self.choice = 1

            elif ir_buttons_pressed == {Button.BEACON}:
                self.choice = 2

                # wait for BEACON button to turn off
                while set(self.ir_sensor.buttons(
                            channel=self.ir_beacon_channel)) \
                        == {Button.BEACON}:
                    wait(10)

            elif ir_buttons_pressed == {Button.RIGHT_UP}:
                self.choice = 3

    def cup_to_center(self):
        # Saving a copy of the current Level
        self.level_copy = self.level

        # Using Level 1 to rotate the chosen cup to the center
        self.level = 1

        if self.choice == 1:
            self.move = 1
            self.move_1()

            self.execute_move()
            self.update_ball_cup()

        elif self.choice == 3:
            self.move = 3
            self.move_3()

            self.execute_move()
            self.update_ball_cup()

        self.reset_motor_positions()

        # Restoring previous value of Level
        self.level = self.level_copy

    def lift_cup(self):
        self.grip_motor.run_angle(speed=100,
                                  rotation_angle=220,
                                  then=Stop.HOLD,
                                  wait=True)
Пример #28
0
class MarsRov3r(IRBeaconRemoteControlledTank, EV3Brick):
    WHEEL_DIAMETER = 40
    AXLE_TRACK = 105

    is_gripping = False

    def __init__(
            self,
            left_motor_port: Port = Port.B, right_motor_port: Port = Port.C,
            grip_motor_port: Port = Port.A,
            touch_sensor_port: Port = Port.S1,
            ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1):
        super().__init__(
            wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK,
            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.drive_base.settings(
            straight_speed=750,   # milimeters per second
            straight_acceleration=750,
            turn_rate=90,   # degrees per second
            turn_acceleration=90)

        self.grip_motor = Motor(port=grip_motor_port,
                                positive_direction=Direction.CLOCKWISE)

        self.touch_sensor = TouchSensor(port=touch_sensor_port)

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

    def grip_or_release_claw_by_ir_beacon(self):
        if Button.BEACON in \
                self.ir_sensor.buttons(channel=self.ir_beacon_channel):
            if self.is_gripping:
                self.grip_motor.run_time(
                    speed=1000,
                    time=2000,
                    then=Stop.HOLD,
                    wait=True)

                self.speaker.play_file(file=SoundFile.AIR_RELEASE)

                self.is_gripping = False

            else:
                self.grip_motor.run_time(
                    speed=-1000,
                    time=2000,
                    then=Stop.HOLD,
                    wait=True)

                self.speaker.play_file(file=SoundFile.AIRBRAKE)

                self.is_gripping = True

            while Button.BEACON in self.ir_sensor.buttons(channel=1):
                pass

    def main(self):
        self.grip_motor.run_time(
            speed=500,
            time=1000,
            then=Stop.BRAKE,
            wait=True)

        while True:
            self.grip_or_release_claw_by_ir_beacon()
            self.drive_once_by_ir_beacon()
Пример #29
0
                hasRecentlyFallenBackIR = True
        else:
            hasRecentlyFallenBackIR = False

        # Move sensor platform towards beacon platform
        beaconAngle = beaconData[1]
        if (beaconAngle is not None and moveDirection == 0
                and enableSensorAutoMove):
            if (beaconAngle >= posDeadZone):
                sensorDirection = 2
            elif (beaconAngle <= negDeadZone):
                sensorDirection = 1

    # Directional buttons on wireless controller is used for general movement
    # Left stick = move robot, right stick = move sensor platform
    for btn in ir.buttons(infraredChannel):
        if btn == Button.LEFT_UP:
            moveDirection = 1
        elif btn == Button.LEFT_DOWN:
            moveDirection = 2
        elif btn == Button.RIGHT_UP:
            sensorDirection = 1
        elif btn == Button.RIGHT_DOWN:
            sensorDirection = 2

    # Reset sensors position when not pressing them
    if (sensorDirection == 0):
        currentAngle = sensorMotorLeft.angle()
        diff = currentAngle - initialSensorAngleLeft

        if diff >= sensorAutoDeadzone: