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()
class Rov3r(RemoteControlledTank): 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=left_motor_port, right_motor=right_motor_port, polarity=Motor.POLARITY_NORMAL) 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')) 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() super().main() # RemoteControlledTank.main()
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 Ev3rstorm(IRBeaconRemoteControlledTank): 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_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.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.screen = Screen() self.speaker = Sound() 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, driving_speed: float = 1000 # degrees per second ): self.screen.image.paste(im=Image.open('/home/robot/image/Target.bmp')) self.screen.update() Thread(target=self.blast_bazooka_whenever_touched, daemon=True).start() self.keep_driving_by_ir_beacon(speed=driving_speed)
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): 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.screen = Screen() self.speaker = Sound() 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() Process(target=self.blast_bazooka_whenever_touched, daemon=True).start() super().main() # RemoteControlledTank.main()
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): 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.screen = Screen() self.speaker = Sound()
def __init__(self, left_foot_track_motor_port: str = OUTPUT_B, right_foot_track_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, driving_ir_beacon_channel: int = 1, shooting_ir_beacon_channel: int = 2): self.left_foot_track_motor = LargeMotor( address=left_foot_track_motor_port) self.right_foot_track_motor = LargeMotor( address=right_foot_track_motor_port) self.bazooka_blast_motor = MediumMotor( address=bazooka_blast_motor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.driving_remote_control = RemoteControl( sensor=self.ir_sensor, channel=driving_ir_beacon_channel) self.shooting_remote_control = RemoteControl( sensor=self.ir_sensor, channel=shooting_ir_beacon_channel) self.touch_sensor = TouchSensor(address=INPUT_1) self.color_sensor = ColorSensor(address=INPUT_3) self.leds = Leds() self.screen = Screen() assert self.left_foot_track_motor.connected assert self.right_foot_track_motor.connected assert self.bazooka_blast_motor.connected assert self.ir_sensor.connected assert self.touch_sensor.connected assert self.color_sensor.connected # reset the motors for motor in (self.left_foot_track_motor, self.right_foot_track_motor, self.bazooka_blast_motor): motor.reset() motor.position = 0 motor.stop_action = Motor.STOP_ACTION_BRAKE self.draw_face()
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_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.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.leds = Leds() self.screen = Screen() self.speaker = Sound()
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 __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=left_motor_port, right_motor=right_motor_port, polarity=Motor.POLARITY_NORMAL) 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 __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 __init__(self, left_motor_port: str = OUTPUT_B, right_motor_port: str = OUTPUT_C, medium_motor_port: str = OUTPUT_A, 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.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()
class Ev3rstorm: def __init__(self, left_foot_track_motor_port: str = OUTPUT_B, right_foot_track_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, driving_ir_beacon_channel: int = 1, shooting_ir_beacon_channel: int = 2): self.left_foot_track_motor = LargeMotor( address=left_foot_track_motor_port) self.right_foot_track_motor = LargeMotor( address=right_foot_track_motor_port) self.bazooka_blast_motor = MediumMotor( address=bazooka_blast_motor_port) self.ir_sensor = InfraredSensor(address=ir_sensor_port) self.driving_remote_control = RemoteControl( sensor=self.ir_sensor, channel=driving_ir_beacon_channel) self.shooting_remote_control = RemoteControl( sensor=self.ir_sensor, channel=shooting_ir_beacon_channel) self.touch_sensor = TouchSensor(address=INPUT_1) self.color_sensor = ColorSensor(address=INPUT_3) self.leds = Leds() self.screen = Screen() assert self.left_foot_track_motor.connected assert self.right_foot_track_motor.connected assert self.bazooka_blast_motor.connected assert self.ir_sensor.connected assert self.touch_sensor.connected assert self.color_sensor.connected # reset the motors for motor in (self.left_foot_track_motor, self.right_foot_track_motor, self.bazooka_blast_motor): motor.reset() motor.position = 0 motor.stop_action = Motor.STOP_ACTION_BRAKE self.draw_face() def draw_face(self): w, h = self.screen.shape y = h // 2 eye_xrad = 20 eye_yrad = 30 pup_xrad = 10 pup_yrad = 10 def draw_eye(x): self.screen.draw.ellipse( (x - eye_xrad, y - eye_yrad, x + eye_xrad, y + eye_yrad)) self.screen.draw.ellipse( (x - pup_xrad, y - pup_yrad, x + pup_xrad, y + pup_yrad), fill='black') draw_eye(w // 3) draw_eye(2 * w // 3) self.screen.update() def shoot(self, direction='up'): """ Shot a ball in the specified direction (valid choices are 'up' and 'down') """ self.bazooka_blast_motor.run_to_rel_pos( speed_sp=900, # degrees per second position_sp=-3 * 360 if direction == 'up' else 3 * 360, # degrees stop_action=Motor.STOP_ACTION_BRAKE) self.bazooka_blast_motor.wait_while(Motor.STATE_RUNNING) def rc_loop( self, driving_speed: float = 1000 # degrees per second ): """ Enter the remote control loop. RC buttons on channel 1 control the robot movement, channel 2 is for shooting things. The loop ends when the touch sensor is pressed. """ def roll(motor: Motor, led_group: str, speed: float): """ Generate remote control event handler. It rolls given motor into given direction (1 for forward, -1 for backward). When motor rolls forward, the given led group flashes green, when backward -- red. When motor stops, the leds are turned off. The on_press function has signature required by RemoteControl class. It takes boolean state parameter; True when button is pressed, False otherwise. """ def on_press(state: int): if state: # roll when button is pressed motor.run_forever(speed_sp=speed) self.leds.set_color( group=led_group, color=Leds.GREEN if speed > 0 else Leds.RED, pct=1) else: # stop otherwise motor.stop(stop_action=Motor.STOP_ACTION_COAST) self.leds.set_color(group=led_group, color=Leds.BLACK, pct=1) return on_press self.driving_remote_control.on_red_up = \ roll(motor=self.right_foot_track_motor, led_group=Leds.RIGHT, speed=driving_speed) self.driving_remote_control.on_red_down = \ roll(motor=self.right_foot_track_motor, led_group=Leds.RIGHT, speed=-driving_speed) self.driving_remote_control.on_blue_up = \ roll(motor=self.left_foot_track_motor, led_group=Leds.LEFT, speed=driving_speed) self.driving_remote_control.on_blue_down = \ roll(motor=self.left_foot_track_motor, led_group=Leds.LEFT, speed=-driving_speed) def shoot(direction: str): def on_press(state: int): if state: self.shoot(direction) return on_press self.shooting_remote_control.on_red_up = shoot('up') self.shooting_remote_control.on_blue_up = shoot('up') self.shooting_remote_control.on_red_down = shoot('down') self.shooting_remote_control.on_blue_down = shoot('down') # now that the event handlers are assigned, # let's enter the processing loop: while not self.touch_sensor.is_pressed: self.driving_remote_control.process() self.shooting_remote_control.process()
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)
#!/usr/bin/env python3 from ev3dev.ev3 import (Motor, LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C, Screen, Sound) from PIL import Image MEDIUM_MOTOR = MediumMotor(address=OUTPUT_A) LEFT_MOTOR = LargeMotor(address=OUTPUT_B) RIGHT_MOTOR = LargeMotor(address=OUTPUT_C) SCREEN = Screen() SPEAKER = Sound() SCREEN.image.paste(im=Image.open('/home/robot/image/Pinch left.bmp')) SCREEN.update() 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
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 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()
#!/usr/bin/env python from ev3dev.ev3 import LargeMotor, \ OUTPUT_A, Sound, Screen from time import time, sleep N = 3000 file_name = "data_gyro.txt" pwr = 100 motor = LargeMotor(OUTPUT_A) lcd = Screen() Sound().beep() fout = open(file_name, "w") sleep(0.05) rotation = motor.position motor.run_direct(duty_cycle_sp=pwr) start_time = time() for i in range(0, N): lcd.clear() t = time() - start_time if rotation < 354: pwr = 100 if rotation >= 354 and rotation <= 366: pwr = 0 if rotation > 366: pwr = -100 motor.duty_cycle_sp = pwr rotation = motor.position line = "%f\t%d\n" % (t, rotation) fout.write(line) lcd.draw.text((0, 10), str(rotation)) lcd.update() fout.close()
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()
from ev3dev.ev3 import (Motor, LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C, TouchSensor, ColorSensor, INPUT_1, INPUT_3, Screen, Sound) from multiprocessing import Process from PIL import Image from time import time LEFT_FOOT_MOTOR = LargeMotor(address=OUTPUT_B) RIGHT_FOOT_MOTOR = LargeMotor(address=OUTPUT_C) MEDIUM_MOTOR = MediumMotor(address=OUTPUT_A) COLOR_SENSOR = ColorSensor(address=INPUT_3) TOUCH_SENSOR = TouchSensor(address=INPUT_1) SCREEN = Screen() SPEAKER = Sound() def run_away_whenever_dark(): while True: if COLOR_SENSOR.ambient_light_intensity < 5: # 15 not dark enough SCREEN.image.paste( im=Image.open('/home/robot/image/Middle left.bmp'), box=(0, 0)) SCREEN.update() LEFT_FOOT_MOTOR.run_timed( speed_sp=-800, # degrees per second time_sp=1500, # miliseconds stop_action=Motor.STOP_ACTION_BRAKE) RIGHT_FOOT_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()