Пример #1
0
    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)
Пример #2
0
    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()

        self.drive_base = \
            DriveBase(
                left_motor=
                    Motor(port=left_motor_port,
                          positive_direction=Direction.COUNTERCLOCKWISE),
                right_motor=
                    Motor(port=right_motor_port,
                          positive_direction=Direction.COUNTERCLOCKWISE),
                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
Пример #3
0
    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
Пример #4
0
    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
Пример #5
0
    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
Пример #6
0
    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 __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
Пример #8
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
Пример #9
0
    def __init__(self,
                 lever_motor_port: Port = Port.D,
                 touch_sensor_port: Port = Port.S1,
                 ir_sensor_port: Port = Port.S4):
        self.ev3_brick = EV3Brick()

        self.lever_motor = Motor(port=lever_motor_port,
                                 positive_direction=Direction.CLOCKWISE)

        self.touch_sensor = TouchSensor(port=touch_sensor_port)

        self.ir_sensor = InfraredSensor(port=ir_sensor_port)
Пример #10
0
def fahre_bis_ir():
    """
    drive until ir distance drops below 50%
    """
    ev3.screen.load_image(ImageFile.PINCHED_LEFT)

    infrarot = InfraredSensor(Port.S4)
    db.drive(speed=20, turn_rate=0)
    while (infrarot.distance() > 50):
        time.sleep(0.1)
    db.stop()
    db.turn(180)
    db.straight(150)
Пример #11
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 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)
Пример #13
0
    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
Пример #14
0
    def __init__(
            self,
            left_motor_port: str = Port.B, right_motor_port: str = Port.C,
            middle_motor_port: str = Port.A,
            touch_sensor_port: str = Port.S1, ir_sensor_port: str = Port.S4):
        self.ev3_brick = EV3Brick()
        
        self.left_motor = Motor(port=left_motor_port,
                                positive_direction=Direction.CLOCKWISE)
        self.right_motor = Motor(port=right_motor_port,
                                 positive_direction=Direction.CLOCKWISE)
        self.middle_motor = Motor(port=middle_motor_port,
                                  positive_direction=Direction.CLOCKWISE)

        self.touch_sensor = TouchSensor(port=touch_sensor_port)

        self.ir_sensor = InfraredSensor(port=ir_sensor_port)
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)
Пример #16
0
    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)
Пример #17
0
    def __init__(
            self,
            left_motor_port: Port = Port.B, right_motor_port: Port = Port.C,
            jaw_motor_port: Port = Port.A,
            touch_sensor_port: Port = Port.S1,
            ir_sensor_port: Port = Port.S4, ir_beacon_channel: int = 1):
        self.left_motor = Motor(port=left_motor_port,
                                positive_direction=Direction.CLOCKWISE)
        self.right_motor = Motor(port=right_motor_port,
                                 positive_direction=Direction.CLOCKWISE)

        self.jaw_motor = Motor(port=jaw_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 __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 __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
Пример #20
0
    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
Пример #21
0
class captorDistance:
    def __init__(self):
        self.infraredSensor = InfraredSensor(Port.S2)

    def getDistance(self):
        dMesurer = self.infraredSensor.distance()
        #distance = dMesurer**2*0.0177 + dMesurer*0.0167 + 7.7471
        distance = dMesurer * 1.0356 - 4.3746
        #print(distance)

        return distance
Пример #22
0
    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
Пример #23
0
    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
Пример #24
0
    def __init__(self):
        """The type of sensor is determined during the initialization."""

        self.type = "none"
        try:
            self.sensor = InfraredSensor(Port.S2)
            self.type = "infra"
        except:
            try:
                self.sensor = UltrasonicSensor(Port.S2)
                self.type = "ultra"
            except:
                print("No distance sensor")
Пример #25
0
    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
Пример #26
0
class distanceSensor:
    """ Defines a sensor that reads distance from a specific sensor. """

    def __init__(self, sensor_input):
        """
        Initializes the ultrasonic sensor component.
        :param sensor_input: the input pin of the sensor
        """
        self._sensor = InfraredSensor(sensor_input)

    def read_distance(self):
        """
        Reads the measured distance.
        :return: the distance in centimeters
        """
        return self._sensor.distance()*1.0356 - 4.3746
Пример #27
0
class El3ctricGuitar:
    NOTES = [1318, 1174, 987, 880, 783, 659, 587, 493, 440, 392, 329, 293]
    N_NOTES = len(NOTES)

    def __init__(self,
                 lever_motor_port: Port = Port.D,
                 touch_sensor_port: Port = Port.S1,
                 ir_sensor_port: Port = Port.S4):
        self.ev3_brick = EV3Brick()

        self.lever_motor = Motor(port=lever_motor_port,
                                 positive_direction=Direction.CLOCKWISE)

        self.touch_sensor = TouchSensor(port=touch_sensor_port)

        self.ir_sensor = InfraredSensor(port=ir_sensor_port)

    def start_up(self):
        self.ev3_brick.screen.load_image(ImageFile.EV3)

        self.ev3_brick.light.on(color=Color.ORANGE)

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

        self.lever_motor.run_angle(speed=50,
                                   rotation_angle=-30,
                                   then=Stop.BRAKE,
                                   wait=True)

        wait(100)

        self.lever_motor.reset_angle(angle=0)

    def play_note(self):
        if not self.touch_sensor.pressed():
            raw = sum(self.ir_sensor.distance() for _ in range(4)) / 4

            self.ev3_brick.speaker.beep(
                frequency=self.NOTES[min(int(raw / 5), self.N_NOTES - 1)] -
                11 * self.lever_motor.angle(),
                duration=100)

        wait(1)
Пример #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()
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)
Пример #30
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)