class Gripp3r: def __init__(self, left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C, grip_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): self.left_motor = LargeMotor(address=left_motor_port) self.right_motor = LargeMotor(address=right_motor_port) self.grip_motor = MediumMotor(address=grip_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.speaker = Sound() def drive_once_by_ir_beacon(self, speed: float = 1000): if self.beacon.red_up and self.beacon.blue_up: # go forward self.left_motor.run_forever(speed_sp=speed) self.right_motor.run_forever(speed_sp=speed) elif self.beacon.red_down and self.beacon.blue_down: # go backward self.left_motor.run_forever(speed_sp=-speed) self.right_motor.run_forever(speed_sp=-speed) elif self.beacon.red_up and self.beacon.blue_down: # turn around left self.left_motor.run_forever(speed_sp=-speed) self.right_motor.run_forever(speed_sp=speed) elif self.beacon.red_down and self.beacon.blue_up: # turn around right self.left_motor.run_forever(speed_sp=speed) self.right_motor.run_forever(speed_sp=-speed) elif self.beacon.red_up: # turn left self.left_motor.run_forever(speed_sp=0) self.right_motor.run_forever(speed_sp=speed) elif self.beacon.blue_up: # turn right self.left_motor.run_forever(speed_sp=speed) self.right_motor.run_forever(speed_sp=0) elif self.beacon.red_down: # left backward self.left_motor.run_forever(speed_sp=0) self.right_motor.run_forever(speed_sp=-speed) elif self.beacon.blue_down: # right backward self.left_motor.run_forever(speed_sp=-speed) self.right_motor.run_forever(speed_sp=0) else: self.left_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) def grip_or_release_by_ir_beacon(self, speed: float = 50): if self.beacon.beacon: if self.touch_sensor.is_pressed: self.speaker.play(wav_file='/home/robot/sound/Air release.wav') self.grip_motor.run_timed(speed_sp=500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.grip_motor.wait_while(Motor.STATE_RUNNING) else: self.speaker.play(wav_file='/home/robot/sound/Airbrake.wav') self.grip_motor.run_forever(speed_sp=-500) while not self.touch_sensor.is_pressed: pass self.grip_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) while self.beacon.beacon: pass def main(self, speed: float = 1000): self.grip_motor.run_timed(speed_sp=-500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.grip_motor.wait_while(Motor.STATE_RUNNING) while True: self.drive_once_by_ir_beacon(speed=speed) self.grip_or_release_by_ir_beacon()
for i in range(2): for p in range(3): MEDIUM_MOTOR.run_timed(speed_sp=750, time_sp=0.2 * 1000, stop_action=Motor.STOP_ACTION_HOLD) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING) MEDIUM_MOTOR.run_timed(speed_sp=-750, time_sp=0.2 * 1000, stop_action=Motor.STOP_ACTION_HOLD) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING) GO_MOTOR.run_to_rel_pos(speed_sp=1000, position_sp=3 * 360, stop_action=Motor.STOP_ACTION_HOLD) GO_MOTOR.wait_while(Motor.STATE_RUNNING) SPEAKER.play(wav_file='/home/robot/sound/Blip 2.wav').wait() GO_MOTOR.run_to_rel_pos(speed_sp=1000, position_sp=-2 * 360, stop_action=Motor.STOP_ACTION_HOLD) GO_MOTOR.wait_while(Motor.STATE_RUNNING) SPEAKER.play(wav_file='/home/robot/sound/Blip 4.wav').wait() GO_MOTOR.run_forever(speed_sp=250) SPEAKER.play(wav_file='/home/robot/sound/Blip 1.wav').wait()
class R3ptar: def __init__(self, turn_motor_port: str = OUTPUT_A, move_motor_port: str = OUTPUT_B, scare_motor_port: str = OUTPUT_D, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): self.turn_motor = MediumMotor(address=turn_motor_port) self.move_motor = LargeMotor(address=move_motor_port) self.scare_motor = LargeMotor(address=scare_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.noise = Sound() def drive_once_by_ir_beacon(self, speed: float = 1000): if self.beacon.red_up and self.beacon.blue_up: self.move_motor.run_forever(speed_sp=speed) elif self.beacon.red_down and self.beacon.blue_down: self.move_motor.run_forever(speed_sp=-speed) elif self.beacon.red_up: self.turn_motor.run_forever(speed_sp=-500) self.move_motor.run_forever(speed_sp=speed) elif self.beacon.blue_up: self.turn_motor.run_forever(speed_sp=500) self.move_motor.run_forever(speed_sp=speed) elif self.beacon.red_down: self.turn_motor.run_forever(speed_sp=-500) self.move_motor.run_forever(speed_sp=-speed) elif self.beacon.blue_down: self.turn_motor.run_forever(speed_sp=500) self.move_motor.run_forever(speed_sp=-speed) else: self.turn_motor.stop(stop_action=Motor.STOP_ACTION_HOLD) self.move_motor.stop(stop_action=Motor.STOP_ACTION_COAST) def bite_by_ir_beacon(self, speed: float = 1000): if self.beacon.beacon: self.noise.play(wav_file='/home/robot/sound/Snake hiss.wav') self.scare_motor.run_timed(speed_sp=speed, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.scare_motor.wait_while(Motor.STATE_RUNNING) self.scare_motor.run_timed(speed_sp=-300, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.scare_motor.wait_while(Motor.STATE_RUNNING) while self.beacon.beacon: pass def run_away_if_chased(self): if self.color_sensor.reflected_light_intensity > 30: self.move_motor.run_timed(speed_sp=500, time_sp=4000, stop_action=Motor.STOP_ACTION_BRAKE) self.move_motor.wait_while(Motor.STATE_RUNNING) for i in range(2): self.turn_motor.run_timed(speed_sp=500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.turn_motor.wait_while(Motor.STATE_RUNNING) self.turn_motor.run_timed(speed_sp=-500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.turn_motor.wait_while(Motor.STATE_RUNNING) def bite_if_touched(self): if self.touch_sensor.is_pressed: self.noise.play(wav_file='/home/robot/sound/Snake hiss.wav') self.scare_motor.run_timed(speed_sp=1000, time_sp=1000, stop_action=Motor.STOP_ACTION_COAST) self.scare_motor.wait_while(Motor.STATE_RUNNING) self.scare_motor.run_timed(speed_sp=-300, time_sp=1000, stop_action=Motor.STOP_ACTION_COAST) self.scare_motor.wait_while(Motor.STATE_RUNNING) 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()
class Dinor3x(IRBeaconRemoteControlledTank): """ Challenges: - Can you make DINOR3X remote controlled with the IR-Beacon? - Can you attach a colorsensor to DINOR3X, and make it behave differently depending on which color is in front of the sensor (red = walk fast, white = walk slow, etc.)? """ # https://sites.google.com/site/ev3python/learn_ev3_python/using-motors MEDIUM_MOTOR_POWER_FACTOR = 1.4 def __init__(self, left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C, jaw_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): super().__init__(left_motor_port=left_motor_port, right_motor_port=right_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.jaw_motor = MediumMotor(address=jaw_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.button = Button() self.speaker = Sound() def calibrate_legs(self): self.left_motor.run_forever(speed_sp=100) self.right_motor.run_forever(speed_sp=200) while self.touch_sensor.is_pressed: pass self.left_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.left_motor.run_forever(speed_sp=400) while not self.touch_sensor.is_pressed: pass self.left_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.left_motor.run_to_rel_pos(position_sp=-0.2 * 360, speed_sp=500, stop_action=Motor.STOP_ACTION_BRAKE) self.left_motor.wait_while(Motor.STATE_RUNNING) self.right_motor.run_forever(speed_sp=400) while not self.touch_sensor.is_pressed: pass self.right_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.run_to_rel_pos(position_sp=-0.2 * 360, speed_sp=500, stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.wait_while(Motor.STATE_RUNNING) self.left_motor.reset() self.right_motor.reset() def close_mouth(self): self.jaw_motor.run_forever(speed_sp=self.MEDIUM_MOTOR_POWER_FACTOR * 200) sleep(1) self.jaw_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) def roar(self): self.speaker.play(wav_file='/home/robot/sound/T-rex roar.wav') self.jaw_motor.run_to_rel_pos(position_sp=-60, speed_sp=self.MEDIUM_MOTOR_POWER_FACTOR * 400, stop_action=Motor.STOP_ACTION_BRAKE) self.jaw_motor.wait_while(Motor.STATE_RUNNING) # FIXME: jaw keeps opening wider and wider and doesn't close for i in range(12): self.jaw_motor.run_timed(speed_sp=-self.MEDIUM_MOTOR_POWER_FACTOR * 400, time_sp=0.05 * 1000, stop_action=Motor.STOP_ACTION_BRAKE) self.jaw_motor.wait_while(Motor.STATE_RUNNING) self.jaw_motor.run_timed(speed_sp=self.MEDIUM_MOTOR_POWER_FACTOR * 400, time_sp=0.05 * 1000, stop_action=Motor.STOP_ACTION_BRAKE) self.jaw_motor.wait_while(Motor.STATE_RUNNING) self.jaw_motor.run_forever(speed_sp=self.MEDIUM_MOTOR_POWER_FACTOR * 200) sleep(0.5) def walk_until_blocked(self): self.left_motor.run_forever(speed_sp=-400) self.right_motor.run_forever(speed_sp=-400) while self.ir_sensor.proximity >= 25: pass self.left_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) def run_away(self): self.left_motor.run_to_rel_pos(speed_sp=750, position_sp=3 * 360, stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.run_to_rel_pos(speed_sp=750, position_sp=3 * 360, stop_action=Motor.STOP_ACTION_BRAKE) self.left_motor.wait_while(Motor.STATE_RUNNING) self.right_motor.wait_while(Motor.STATE_RUNNING) def jump(self): """ Dinor3x Mission 02 Challenge: make it jump """ ... # TRANSLATED FROM EV3-G MY BLOCKS # ------------------------------- def leg_adjust(self, cyclic_degrees: float, speed: float = 1000, leg_offset_percent: float = 0, mirrored_adjust: bool = False, brake: bool = True): ... def leg_to_pos(self, speed: float = 1000, left_position: float = 0, right_position: float = 0): self.left_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.stop(stop_action=Motor.STOP_ACTION_BRAKE) self.left_motor.run_to_rel_pos( speed_sp=speed, position_sp=left_position - cyclic_position_offset( rotation_sensor=self.left_motor.position, cyclic_degrees=360), stop_action=Motor.STOP_ACTION_BRAKE) self.left_motor.wait_while(Motor.STATE_RUNNING) self.right_motor.run_to_rel_pos( speed_sp=speed, position_sp=right_position - cyclic_position_offset( rotation_sensor=self.right_motor.position, cyclic_degrees=360), stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.wait_while(Motor.STATE_RUNNING) def turn(self, speed: float = 1000, n_steps: int = 1): ... def walk(self, speed: float = 1000): ... def walk_steps(self, speed: float = 1000, n_steps: int = 1): ...
MEDIUM_MOTOR = MediumMotor(address=OUTPUT_A) IR_SENSOR = InfraredSensor(address=INPUT_4) LEDS = Leds() SPEAKER = Sound() while True: if IR_SENSOR.proximity < 25: LEFT_FOOT_MOTOR.stop(stop_action=Motor.STOP_ACTION_BRAKE) RIGHT_FOOT_MOTOR.stop(stop_action=Motor.STOP_ACTION_BRAKE) LEDS.set_color(group=Leds.LEFT, color=Leds.RED, pct=1) LEDS.set_color(group=Leds.RIGHT, color=Leds.RED, pct=1) SPEAKER.play(wav_file='/home/robot/sound/Object.wav').wait() SPEAKER.play(wav_file='/home/robot/sound/Detected.wav').wait() SPEAKER.play(wav_file='/home/robot/sound/Error alarm.wav').wait() MEDIUM_MOTOR.run_forever(speed_sp=1000 # degrees per second ) LEFT_FOOT_MOTOR.run_to_rel_pos( position_sp=360, # degrees speed_sp=1000, # degrees per second stop_action=Motor.STOP_ACTION_BRAKE) RIGHT_FOOT_MOTOR.run_to_rel_pos( position_sp=360, # degrees speed_sp=800, # degrees per second stop_action=Motor.STOP_ACTION_BRAKE) LEFT_FOOT_MOTOR.wait_while(Motor.STATE_RUNNING)
class Rov3r(IRBeaconRemoteControlledTank): def __init__(self, left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C, gear_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): super().__init__(left_motor_port=left_motor_port, right_motor_port=right_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.gear_motor = MediumMotor(address=gear_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.speaker = Sound() self.dis = Screen() def spin_gears(self, speed: float = 1000): while True: if self.beacon.beacon: self.gear_motor.run_forever(speed_sp=speed) else: self.gear_motor.stop(stop_action=Motor.STOP_ACTION_HOLD) def change_screen_when_touched(self): while True: if self.touch_sensor.is_pressed: self.dis.image.paste( im=Image.open('/home/robot/image/Angry.bmp')) self.dis.update() else: self.dis.image.paste( im=Image.open('/home/robot/image/Fire.bmp')) self.dis.update() def make_noise_when_seeing_black(self): while True: if self.color_sensor.color == ColorSensor.COLOR_BLACK: self.speaker.play(wav_file='/home/robot/sound/Ouch.wav').wait() def main(self): self.speaker.play(wav_file='/home/robot/sound/Yes.wav').wait() Thread(target=self.make_noise_when_seeing_black, daemon=True).start() Thread(target=self.spin_gears, daemon=True).start() Thread(target=self.change_screen_when_touched, daemon=True).start() self.keep_driving_by_ir_beacon()
class Ev3rstorm: def __init__(self, left_foot_motor_port: str = OUTPUT_B, right_foot_motor_port: str = OUTPUT_C, bazooka_blast_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): self.left_foot_motor = LargeMotor(address=left_foot_motor_port) self.right_foot_motor = LargeMotor(address=right_foot_motor_port) self.bazooka_blast_motor = MediumMotor( address=bazooka_blast_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.remote_control = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.leds = Leds() self.screen = Screen() self.speaker = Sound() def drive_once_by_ir_beacon( self, speed: float = 1000 # degrees per second ): # forward if self.remote_control.red_up and self.remote_control.blue_up: self.left_foot_motor.run_forever(speed_sp=speed) self.right_foot_motor.run_forever(speed_sp=speed) # backward elif self.remote_control.red_down and self.remote_control.blue_down: self.left_foot_motor.run_forever(speed_sp=-speed) self.right_foot_motor.run_forever(speed_sp=-speed) # turn left on the spot elif self.remote_control.red_up and self.remote_control.blue_down: self.left_foot_motor.run_forever(speed_sp=-speed) self.right_foot_motor.run_forever(speed_sp=speed) # turn right on the spot elif self.remote_control.red_down and self.remote_control.blue_up: self.left_foot_motor.run_forever(speed_sp=speed) self.right_foot_motor.run_forever(speed_sp=-speed) # turn left forward elif self.remote_control.red_up: self.right_foot_motor.run_forever(speed_sp=speed) # turn right forward elif self.remote_control.blue_up: self.left_foot_motor.run_forever(speed_sp=speed) # turn left backward elif self.remote_control.red_down: self.right_foot_motor.run_forever(speed_sp=-speed) # turn right backward elif self.remote_control.blue_down: self.left_foot_motor.run_forever(speed_sp=-speed) # otherwise stop else: self.left_foot_motor.stop(stop_action=Motor.STOP_ACTION_COAST) self.right_foot_motor.stop(stop_action=Motor.STOP_ACTION_COAST) def dance_if_ir_beacon_pressed(self): while self.remote_control.beacon: self.left_foot_motor.run_timed(speed_sp=randint(-1000, 1000), time_sp=1000, stop_action=Motor.STOP_ACTION_COAST) self.right_foot_motor.run_timed( speed_sp=randint(-1000, 1000), time_sp=1000, stop_action=Motor.STOP_ACTION_COAST) self.left_foot_motor.wait_while(Motor.STATE_RUNNING) self.right_foot_motor.wait_while(Motor.STATE_RUNNING) def detect_object_by_ir_sensor(self): if self.ir_sensor.proximity < 25: self.leds.set_color(group=Leds.LEFT, color=Leds.RED, pct=1) self.leds.set_color(group=Leds.RIGHT, color=Leds.RED, pct=1) self.speaker.play(wav_file='/home/robot/sound/Object.wav').wait() self.speaker.play(wav_file='/home/robot/sound/Detected.wav').wait() self.speaker.play( wav_file='/home/robot/sound/Error alarm.wav').wait() else: self.leds.all_off() def blast_bazooka_if_touched(self): if self.touch_sensor.is_pressed: if self.color_sensor.ambient_light_intensity < 5: # 15 not dark enough self.speaker.play(wav_file='/home/robot/sound/Up.wav').wait() self.bazooka_blast_motor.run_to_rel_pos( speed_sp=1000, # degrees per second position_sp=-3 * 360, # degrees stop_action=Motor.STOP_ACTION_HOLD) else: self.speaker.play(wav_file='/home/robot/sound/Down.wav').wait() self.bazooka_blast_motor.run_to_rel_pos( speed_sp=1000, # degrees per second position_sp=3 * 360, # degrees stop_action=Motor.STOP_ACTION_HOLD) while self.touch_sensor.is_pressed: pass def main( self, driving_speed: float = 1000 # degrees per second ): self.screen.image.paste(im=Image.open('/home/robot/image/Target.bmp')) self.screen.update() while True: self.drive_once_by_ir_beacon(speed=driving_speed) self.dance_if_ir_beacon_pressed() # DON'T use IR Sensor in 2 different modes in the same program / loop # - https://github.com/pybricks/support/issues/62 # - https://github.com/ev3dev/ev3dev/issues/1401 # self.detect_object_by_ir_sensor() self.blast_bazooka_if_touched()
for i in range(2): LEFT_MOTOR.run_to_rel_pos( position_sp=2 * 360, # degrees speed_sp=750, # degrees per second stop_action=Motor.STOP_ACTION_BRAKE) RIGHT_MOTOR.run_to_rel_pos( position_sp=2 * 360, # degrees speed_sp=750, # degrees per second stop_action=Motor.STOP_ACTION_BRAKE) LEFT_MOTOR.wait_while(Motor.STATE_RUNNING) RIGHT_MOTOR.wait_while(Motor.STATE_RUNNING) MEDIUM_MOTOR.run_forever(speed_sp=1000) SPEAKER.play(wav_file='/home/robot/sound/Airbrake.wav').wait() sleep(0.5) LEFT_MOTOR.run_to_rel_pos( position_sp=3 * 360, # degrees speed_sp=750, # degrees per second stop_action=Motor.STOP_ACTION_BRAKE) LEFT_MOTOR.wait_while(Motor.STATE_RUNNING) MEDIUM_MOTOR.run_forever(speed_sp=-1000) SPEAKER.play(wav_file='/home/robot/sound/Air release.wav').wait() sleep(0.5)
speed_sp=750, # degrees/second position_sp=1000, # degrees stop_action=Motor.STOP_ACTION_HOLD) LEFT_MOTOR.wait_while(Motor.STATE_RUNNING) RIGHT_MOTOR.wait_while(Motor.STATE_RUNNING) SCREEN.image.paste(im=Image.open('/home/robot/image/Angry.bmp')) SCREEN.update() MEDIUM_MOTOR.run_timed( speed_sp=1000, # deg/s time_sp=0.3 * 1000, # ms stop_action=Motor.STOP_ACTION_HOLD) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING) SPEAKER.play(wav_file='/home/robot/sound/Laughing 2.wav').wait() MEDIUM_MOTOR.run_timed( speed_sp=-200, # deg/s time_sp=1000, # ms stop_action=Motor.STOP_ACTION_HOLD) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING) else: SCREEN.image.paste(im=Image.open('/home/robot/image/Crazy 1.bmp')) SCREEN.update() LEFT_MOTOR.run_forever(speed_sp=750) RIGHT_MOTOR.run_forever(speed_sp=750) MEDIUM_MOTOR.run_timed(
class Spik3r: def __init__(self, sting_motor_port: str = OUTPUT_D, go_motor_port: str = OUTPUT_B, claw_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): self.sting_motor = LargeMotor(address=sting_motor_port) self.go_motor = LargeMotor(address=go_motor_port) self.claw_motor = MediumMotor(address=claw_motor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel self.beacon = RemoteControl(sensor=self.ir_sensor, channel=self.ir_beacon_channel) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.dis = Screen() self.speaker = Sound() def sting_by_ir_beacon(self): while True: if self.beacon.beacon: self.sting_motor.run_to_rel_pos( speed_sp=750, position=-220, stop_action=Motor.STOP_ACTION_HOLD) self.sting_motor.wait_while(Motor.STATE_RUNNING) self.speaker.play( wav_file='/home/robot/sound/Blip 3.wav').wait() self.sting_motor.run_timed(speed_sp=-1000, time_sp=1000, stop_action=Motor.STOP_ACTION_HOLD) self.sting_motor.wait_while(Motor.STATE_RUNNING) self.sting_motor.run_timed(speed_sp=1000, time_sp=1000, stop_action=Motor.STOP_ACTION_HOLD) self.sting_motor.wait_while(Motor.STATE_RUNNING) while self.beacon.beacon: pass def be_noisy_to_people(self): while True: if self.color_sensor.reflected_light_intensity > 30: for i in range(4): self.speaker.play_song( (('D4', 'e3'), ('D4', 'e3'), ('D4', 'e3'), ('G4', 'h'), ('D5', 'h'), ('C5', 'e3'), ('B4', 'e3'), ('A4', 'e3'), ('G5', 'h'), ('D5', 'q'), ('C5', 'e3'), ('B4', 'e3'), ('A4', 'e3'), ('G5', 'h'), ('D5', 'q'), ('C5', 'e3'), ('B4', 'e3'), ('C5', 'e3'), ('A4', 'h.'))) def pinch_if_touched(self): while True: if self.touch_sensor.is_pressed: self.claw_motor.run_timed(speed_sp=500, time_sp=1000, stop_action=Motor.STOP_ACTION_HOLD) self.claw_motor.wait_while(Motor.STATE_RUNNING) self.claw_motor.run_timed(speed_sp=-500, time_sp=0.3 * 1000, stop_action=Motor.STOP_ACTION_HOLD) self.claw_motor.wait_while(Motor.STATE_RUNNING) def keep_driving_by_ir_beacon(self): while True: if self.beacon.red_up and self.beacon.blue_up: self.go_motor.run_forever(speed_sp=910) elif self.beacon.blue_up: self.go_motor.run_forever(speed_sp=-1000) else: self.go_motor.stop(stop_action=Motor.STOP_ACTION_HOLD) def main(self): self.dis.image.paste(im=Image.open('/home/robot/image/Evil.bmp')) self.dis.update() # FIXME: ValueError: invalid literal for int() with base 10: '' or '9\n9' # when multiple threads access the same Sensor Thread(target=self.pinch_if_touched, daemon=True).start() Thread(target=self.be_noisy_to_people, daemon=True).start() Thread(target=self.sting_by_ir_beacon, daemon=True).start() self.keep_driving_by_ir_beacon()
def playFile(filename, wait = False): process = _Sound.play(filename) if wait: process.wait()
#!/usr/bin/env python3 from ev3dev.ev3 import (Motor, MediumMotor, OUTPUT_A, Sound) from time import sleep MEDIUM_MOTOR = MediumMotor(address=OUTPUT_A) SPEAKER = Sound() MEDIUM_MOTOR.run_timed(speed_sp=-500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING) SPEAKER.play(wav_file='/home/robot/sound/Airbrake.wav') MEDIUM_MOTOR.run_timed(speed_sp=500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING) sleep(1) SPEAKER.play(wav_file='/home/robot/sound/Air release.wav') MEDIUM_MOTOR.run_timed(speed_sp=-500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING)
def play(self, song): """Play the song""" file_name = song.get('file_name') from ev3dev.ev3 import Sound Sound.play(file_name).wait()
class Ev3rstorm: def __init__(self, left_foot_motor_port: str = OUTPUT_B, right_foot_motor_port: str = OUTPUT_C, bazooka_blast_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): self.left_foot_motor = LargeMotor(address=left_foot_motor_port) self.right_foot_motor = LargeMotor(address=right_foot_motor_port) self.bazooka_blast_motor = MediumMotor( address=bazooka_blast_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.remote_control = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.screen = Screen() self.speaker = Sound() def drive_once_by_ir_beacon( self, speed: float = 1000 # degrees per second ): # forward if self.remote_control.red_up and self.remote_control.blue_up: self.left_foot_motor.run_forever(speed_sp=speed) self.right_foot_motor.run_forever(speed_sp=speed) # backward elif self.remote_control.red_down and self.remote_control.blue_down: self.left_foot_motor.run_forever(speed_sp=-speed) self.right_foot_motor.run_forever(speed_sp=-speed) # turn left on the spot elif self.remote_control.red_up and self.remote_control.blue_down: self.left_foot_motor.run_forever(speed_sp=-speed) self.right_foot_motor.run_forever(speed_sp=speed) # turn right on the spot elif self.remote_control.red_down and self.remote_control.blue_up: self.left_foot_motor.run_forever(speed_sp=speed) self.right_foot_motor.run_forever(speed_sp=-speed) # turn left forward elif self.remote_control.red_up: self.right_foot_motor.run_forever(speed_sp=speed) # turn right forward elif self.remote_control.blue_up: self.left_foot_motor.run_forever(speed_sp=speed) # turn left backward elif self.remote_control.red_down: self.right_foot_motor.run_forever(speed_sp=-speed) # turn right backward elif self.remote_control.blue_down: self.left_foot_motor.run_forever(speed_sp=-speed) # otherwise stop else: self.left_foot_motor.stop(stop_action=Motor.STOP_ACTION_COAST) self.right_foot_motor.stop(stop_action=Motor.STOP_ACTION_COAST) def blast_bazooka_if_touched(self): if self.touch_sensor.is_pressed: if self.color_sensor.ambient_light_intensity < 5: # 15 not dark enough self.speaker.play(wav_file='/home/robot/sound/Up.wav').wait() self.bazooka_blast_motor.run_to_rel_pos( speed_sp=1000, # degrees per second position_sp=-3 * 360, # degrees stop_action=Motor.STOP_ACTION_HOLD) else: self.speaker.play(wav_file='/home/robot/sound/Down.wav').wait() self.bazooka_blast_motor.run_to_rel_pos( speed_sp=1000, # degrees per second position_sp=3 * 360, # degrees stop_action=Motor.STOP_ACTION_HOLD) while self.touch_sensor.is_pressed: pass def main( self, driving_speed: float = 1000 # degrees per second ): self.screen.image.paste(im=Image.open('/home/robot/image/Target.bmp')) self.screen.update() while True: self.drive_once_by_ir_beacon(speed=driving_speed) self.blast_bazooka_if_touched()
class Catapult(IRBeaconRemoteControlledTank): def __init__(self, left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C, catapult_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): super().__init__(left_motor_port=left_motor_port, right_motor_port=right_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.catapult_motor = MediumMotor(address=catapult_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.speaker = Sound() def scan_colors(self): if self.color_sensor.color == ColorSensor.COLOR_YELLOW: pass elif self.color_sensor.color == ColorSensor.COLOR_WHITE: self.speaker.play(wav_file='/home/robot/sound/Good.wav').wait() def make_noise_when_touched(self): if self.touch_sensor.is_pressed: self.speaker.play(wav_file='/home/robot/sound/Ouch.wav').wait() def throw_by_ir_beacon(self): if self.beacon.beacon: self.catapult_motor.run_to_rel_pos( speed_sp=1000, position_sp=-150, stop_action=Motor.STOP_ACTION_HOLD) self.catapult_motor.wait_while(Motor.STATE_RUNNING) self.catapult_motor.run_to_rel_pos( speed_sp=1000, position_sp=150, stop_action=Motor.STOP_ACTION_HOLD) self.catapult_motor.wait_while(Motor.STATE_RUNNING) while self.beacon.beacon: pass def main(self): self.speaker.play(wav_file='/home/robot/sound/Yes.wav').wait() while True: self.drive_once_by_ir_beacon(speed=1000) self.make_noise_when_touched() self.throw_by_ir_beacon() self.scan_colors()
LEFT_MOTOR.run_to_rel_pos( position_sp=2 * 360, # degrees speed_sp=750, # degrees per second stop_action=Motor.STOP_ACTION_BRAKE) RIGHT_MOTOR.run_to_rel_pos( position_sp=2 * 360, # degrees speed_sp=750, # degrees per second stop_action=Motor.STOP_ACTION_BRAKE) LEFT_MOTOR.wait_while(Motor.STATE_RUNNING) RIGHT_MOTOR.wait_while(Motor.STATE_RUNNING) MEDIUM_MOTOR.run_to_rel_pos( position_sp=3 * 360, # degrees speed_sp=750, # degrees per second stop_action=Motor.STOP_ACTION_BRAKE) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING) LEFT_MOTOR.run_to_rel_pos( position_sp=-2 * 360, # degrees speed_sp=750, # degrees per second stop_action=Motor.STOP_ACTION_BRAKE) RIGHT_MOTOR.run_to_rel_pos( position_sp=-2 * 360, # degrees speed_sp=750, # degrees per second stop_action=Motor.STOP_ACTION_BRAKE) LEFT_MOTOR.wait_while(Motor.STATE_RUNNING) RIGHT_MOTOR.wait_while(Motor.STATE_RUNNING) SPEAKER.play(wav_file='/home/robot/sound/Fanfare.wav').wait()
class Ev3rstorm(RemoteControlledTank): def __init__( self, left_foot_motor_port: str = OUTPUT_B, right_foot_motor_port: str = OUTPUT_C, bazooka_blast_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): super().__init__( left_motor=left_foot_motor_port, right_motor=right_foot_motor_port, polarity=Motor.POLARITY_NORMAL) self.bazooka_blast_motor = MediumMotor(address=bazooka_blast_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.leds = Leds() self.screen = Screen() self.speaker = Sound() def keep_detecting_objects_by_ir_sensor(self): while True: if self.ir_sensor.proximity < 25: self.leds.set_color( group=Leds.LEFT, color=Leds.RED, pct=1) self.leds.set_color( group=Leds.RIGHT, color=Leds.RED, pct=1) self.speaker.play(wav_file='/home/robot/sound/Object.wav').wait() self.speaker.play(wav_file='/home/robot/sound/Detected.wav').wait() self.speaker.play(wav_file='/home/robot/sound/Error alarm.wav').wait() else: self.leds.all_off() def blast_bazooka_whenever_touched(self): while True: if self.touch_sensor.is_pressed: if self.color_sensor.ambient_light_intensity < 5: # 15 not dark enough self.speaker.play(wav_file='/home/robot/sound/Up.wav').wait() self.bazooka_blast_motor.run_to_rel_pos( speed_sp=1000, # degrees per second position_sp=-3 * 360, # degrees stop_action=Motor.STOP_ACTION_HOLD) else: self.speaker.play(wav_file='/home/robot/sound/Down.wav').wait() self.bazooka_blast_motor.run_to_rel_pos( speed_sp=1000, # degrees per second position_sp=3 * 360, # degrees stop_action=Motor.STOP_ACTION_HOLD) while self.touch_sensor.is_pressed: pass def main(self): self.screen.image.paste(im=Image.open('/home/robot/image/Target.bmp')) self.screen.update() # 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, # daemon=True).start() Thread(target=self.blast_bazooka_whenever_touched, daemon=True).start() super().main() # RemoteControlledTank.main()
class MarsRov3r(IRBeaconRemoteControlledTank): def __init__(self, left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C, grip_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): super().__init__(left_motor_port=left_motor_port, right_motor_port=right_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.grip_motor = MediumMotor(address=grip_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.speaker = Sound() is_gripping = False def grip_or_release_claw_by_ir_beacon(self): if self.beacon.beacon: if self.is_gripping: self.grip_motor.run_timed(speed_sp=1000, time_sp=2000, stop_action=Motor.STOP_ACTION_BRAKE) self.grip_motor.wait_while(Motor.STATE_RUNNING) self.speaker.play( wav_file='/home/robot/sound/Air release.wav').wait() self.is_gripping = False else: self.grip_motor.run_timed(speed_sp=-1000, time_sp=2000, stop_action=Motor.STOP_ACTION_BRAKE) self.grip_motor.wait_while(Motor.STATE_RUNNING) self.speaker.play( wav_file='/home/robot/sound/Airbrake.wav').wait() self.is_gripping = True while self.beacon.beacon: pass def main(self, speed: float = 1000): self.grip_motor.run_timed(speed_sp=500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.grip_motor.wait_while(Motor.STATE_RUNNING) while True: self.grip_or_release_claw_by_ir_beacon() self.drive_once_by_ir_beacon(speed=speed)
TOUCH_SENSOR = TouchSensor(address=INPUT_1) LEDS = Leds() SCREEN = Screen() SPEAKER = Sound() while True: SCREEN.image.paste(im=Image.open('/home/robot/image/ZZZ.bmp'), box=(0, 0)) SCREEN.update() LEDS.set_color(group=Leds.LEFT, color=Leds.ORANGE, pct=1) LEDS.set_color(group=Leds.RIGHT, color=Leds.ORANGE, pct=1) # TODO: parallel process/thread while not TOUCH_SENSOR.is_pressed: SPEAKER.play(wav_file='/home/robot/sound/Snoring.wav').wait() SCREEN.image.paste(im=Image.open('/home/robot/image/Winking.bmp'), box=(0, 0)) SCREEN.update() SPEAKER.play(wav_file='/home/robot/sound/Activate.wav').wait() SPEAKER.play(wav_file='/home/robot/sound/EV3.wav').wait() SCREEN.image.paste(im=Image.open('/home/robot/image/Neutral.bmp'), box=(0, 0)) SCREEN.update() LEDS.set_color(group=Leds.LEFT, color=Leds.GREEN, pct=1) LEDS.set_color(group=Leds.RIGHT, color=Leds.GREEN, pct=1)