class Gripp3r(IRBeaconRemoteControlledTank): def __init__(self, left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C, grip_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): super().__init__(left_motor_port=left_motor_port, right_motor_port=right_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.grip_motor = MediumMotor(address=grip_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.speaker = Sound() def grip_or_release_by_ir_beacon(self, speed: float = 50): while True: 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) Process(target=self.grip_or_release_by_ir_beacon, daemon=True).start() self.keep_driving_by_ir_beacon(speed=speed)
class CuriosityRov3r(IRBeaconRemoteControlledTank): def __init__( self, left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C, medium_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): super().__init__( left_motor_port=left_motor_port, right_motor_port=right_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.medium_motor = MediumMotor(address=medium_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.ir_beacon_channel = ir_beacon_channel self.beacon = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) self.dis = Screen() self.noise = Sound() def spin_fan(self, speed: float = 1000): while True: if self.color_sensor.reflected_light_intensity > 20: self.medium_motor.run_forever(speed_sp=speed) else: self.medium_motor.stop(stop_action=Motor.STOP_ACTION_HOLD) def say_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() self.noise.play(wav_file='/home/robot/sound/No.wav').wait() self.medium_motor.run_timed( speed_sp=-500, time_sp=3000, stop_action=Motor.STOP_ACTION_BRAKE) self.medium_motor.wait_while(Motor.STATE_RUNNING) def main(self, speed: float = 1000): Thread(target=self.say_when_touched, daemon=True).start() Thread(target=self.spin_fan, daemon=True).start() self.keep_driving_by_ir_beacon(speed=speed)
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): while True: 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) Thread(target=self.grip_or_release_claw_by_ir_beacon, daemon=True).start() self.keep_driving_by_ir_beacon(speed=speed)
InfraredSensor, INPUT_4, Sound ) MEDIUM_MOTOR = MediumMotor(address=OUTPUT_A) GO_MOTOR = LargeMotor(address=OUTPUT_B) STING_MOTOR = LargeMotor(address=OUTPUT_D) INFRARED_SENSOR = InfraredSensor(address=INPUT_4) SPEAKER = Sound() MEDIUM_MOTOR.run_timed( speed_sp=500, time_sp=1000, stop_action=Motor.STOP_ACTION_HOLD) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING) MEDIUM_MOTOR.run_timed( speed_sp=-500, time_sp=0.3 * 1000, stop_action=Motor.STOP_ACTION_HOLD) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING) STING_MOTOR.run_timed( speed_sp=400, time_sp=1000, stop_action=Motor.STOP_ACTION_HOLD) STING_MOTOR.wait_while(Motor.STATE_RUNNING)
# right backward LEFT_MOTOR.run_forever(speed_sp=-speed) RIGHT_MOTOR.run_forever(speed_sp=0) else: LEFT_MOTOR.stop(stop_action=Motor.STOP_ACTION_BRAKE) RIGHT_MOTOR.stop(stop_action=Motor.STOP_ACTION_BRAKE) while True: drive_once_by_ir_beacon(speed=1000) if BEACON_CONTROL.beacon: if TOUCH_SENSOR.is_pressed: MEDIUM_MOTOR.run_timed( speed_sp=500, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING) else: MEDIUM_MOTOR.run_forever(speed_sp=-500) while not TOUCH_SENSOR.is_pressed: pass MEDIUM_MOTOR.stop(stop_action=Motor.STOP_ACTION_BRAKE) while BEACON_CONTROL.beacon: pass
stop_action=Motor.STOP_ACTION_BRAKE) CHEST_MOTOR.wait_while(Motor.STATE_RUNNING) MEDIUM_MOTOR.run_forever(speed_sp=1000) TAIL_MOTOR.run_forever(speed_sp=-1000) CHEST_MOTOR.run_timed(speed_sp=-300, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) CHEST_MOTOR.wait_while(Motor.STATE_RUNNING) sleep(2) MEDIUM_MOTOR.run_timed(speed_sp=-1000, time_sp=1000, stop_action=Motor.STOP_ACTION_HOLD) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING) sleep(1) else: LIGHTS.set_color(group=Leds.LEFT, color=Leds.ORANGE, pct=1) LIGHTS.set_color(group=Leds.RIGHT, color=Leds.ORANGE, pct=1) TAIL_MOTOR.run_forever(speed_sp=1000) MEDIUM_MOTOR.run_timed(speed_sp=randint(-30, 30), time_sp=0.2 * 1000, stop_action=Motor.STOP_ACTION_COAST)
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 keep_driving_by_ir_beacon(self, speed: float = 1000): while True: 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): while True: 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) Thread(target=self.grip_or_release_by_ir_beacon, daemon=True).start() self.keep_driving_by_ir_beacon(speed=speed)
class Sweep3r(IRBeaconRemoteControlledTank): def __init__(self, right_motor_port: str = OUTPUT_C, left_motor_port: str = OUTPUT_B, medium_motor_port: str = OUTPUT_A, touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3, ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1): super().__init__(left_motor_port=left_motor_port, right_motor_port=right_motor_port, ir_sensor_port=ir_sensor_port, ir_beacon_channel=ir_beacon_channel) self.medium_motor = MediumMotor(address=medium_motor_port) self.touch_sensor = TouchSensor(address=touch_sensor_port) self.color_sensor = ColorSensor(address=color_sensor_port) self.screen = Screen() self.speaker = Sound() self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.remote_control = RemoteControl(sensor=self.ir_sensor, channel=ir_beacon_channel) def drill(self): while True: if self.remote_control.beacon: self.medium_motor.run_timed( speed_sp=1000, # deg/s time_sp=0.3 * 1000, # ms stop_action=Motor.STOP_ACTION_HOLD) self.medium_motor.wait_while(Motor.STATE_RUNNING) def move_when_touched(self): while True: if self.touch_sensor.is_pressed: self.right_motor.run_timed(time_sp=1000, speed_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.right_motor.wait_while(Motor.STATE_RUNNING) def move_when_see_smothing(self): while True: if self.color_sensor.reflected_light_intensity > 30: self.left_motor.run_timed(time_sp=1000, speed_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) self.left_motor.wait_while(Motor.STATE_RUNNING) def main( self, speed: float = 1000 # degrees per second ): self.screen.image.paste( im=Image.open('/home/robot/image/Pinch middle.bmp')) self.screen.update() Process(target=self.move_when_touched, daemon=True).start() Process(target=self.move_when_see_smothing, daemon=True).start() Process(target=self.drill, daemon=True).start() self.keep_driving_by_ir_beacon(speed=speed)
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 keep_driving_by_ir_beacon(self, speed: float = 1000): while True: 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): while True: 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): while True: 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): while True: 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): Process(target=self.bite_by_ir_beacon, daemon=True).start() Process(target=self.bite_if_touched, daemon=True).start() Process(target=self.run_away_if_chased, daemon=True).start() self.keep_driving_by_ir_beacon(speed=speed)
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): ...
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()
from PIL import Image from time import sleep MEDIUM_MOTOR = MediumMotor(address=OUTPUT_A) LEFT_MOTOR = LargeMotor(address=OUTPUT_B) RIGHT_MOTOR = LargeMotor(address=OUTPUT_C) IR_SENSOR = InfraredSensor(address=INPUT_4) SCREEN = Screen() SPEAKER = Sound() 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) while True: if IR_SENSOR.proximity < 25: SCREEN.image.paste(im=Image.open('/home/robot/image/Pinch right.bmp')) SCREEN.update() LEFT_MOTOR.run_to_rel_pos( speed_sp=750, # degrees/second position_sp=-1000, # degrees stop_action=Motor.STOP_ACTION_HOLD) RIGHT_MOTOR.run_to_rel_pos( speed_sp=750, # degrees/second
#!/usr/bin/env python3 from ev3dev.ev3 import( Motor, LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_B ) LARGE_MOTOR = LargeMotor(address=OUTPUT_B) MEDIUM_MOTOR = MediumMotor(address=OUTPUT_A) LARGE_MOTOR.run_forever(speed_sp=1000) for i in range(3): MEDIUM_MOTOR.run_timed( speed_sp=100, time_sp=1000, stop_action=Motor.STOP_ACTION_COAST) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING) MEDIUM_MOTOR.run_timed( speed_sp=-100, time_sp=1000, stop_action=Motor.STOP_ACTION_COAST) MEDIUM_MOTOR.wait_while(Motor.STATE_RUNNING)