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.tank_driver = MoveTank(left_motor_port=left_foot_motor_port, right_motor_port=right_foot_motor_port, motor_class=LargeMotor) 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.ir_beacon_channel = ir_beacon_channel self.leds = Leds() self.speaker = Sound() self.screen = Display() def drive_once_by_ir_beacon(self, speed: float = 100): # forward if self.ir_sensor.top_left( channel=self.ir_beacon_channel) and self.ir_sensor.top_right( channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=speed, right_speed=speed) # backward elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel ) and self.ir_sensor.bottom_right( channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=-speed, right_speed=-speed) # turn left on the spot elif self.ir_sensor.top_left(channel=self.ir_beacon_channel ) and self.ir_sensor.bottom_right( channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=-speed, right_speed=speed) # turn right on the spot elif self.ir_sensor.top_right( channel=self.ir_beacon_channel) and self.ir_sensor.bottom_left( channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=speed, right_speed=-speed) # turn left forward elif self.ir_sensor.top_left(channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=0, right_speed=speed) # turn right forward elif self.ir_sensor.top_right(channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=speed, right_speed=0) # turn left backward elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=0, right_speed=-speed) # turn right backward elif self.ir_sensor.bottom_right(channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=-speed, right_speed=0) # otherwise stop else: self.tank_driver.off(brake=False) def dance_if_ir_beacon_pressed(self): while self.ir_sensor.beacon(channel=self.ir_beacon_channel): self.tank_driver.on_for_seconds(left_speed=randint(-100, 100), right_speed=randint(-100, 100), seconds=1, brake=False, block=True) def detect_object_by_ir_sensor(self): if self.ir_sensor.proximity < 25: self.leds.animate_police_lights(color1=Leds.ORANGE, color2=Leds.RED, group1=Leds.LEFT, group2=Leds.RIGHT, sleeptime=0.5, duration=5, block=False) self.speaker.play_file(wav_file='/home/robot/sound/Object.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.speaker.play_file(wav_file='/home/robot/sound/Detected.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.speaker.play_file( wav_file='/home/robot/sound/Error alarm.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) 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_file(wav_file='/home/robot/sound/Up.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.bazooka_blast_motor.on_for_rotations(speed=100, rotations=-3, brake=True, block=True) self.speaker.play_file( wav_file='/home/robot/sound/Laughing 1.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) else: self.speaker.play_file(wav_file='/home/robot/sound/Down.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.bazooka_blast_motor.on_for_rotations(speed=100, rotations=3, brake=True, block=True) self.speaker.play_file( wav_file='/home/robot/sound/Laughing 2.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.touch_sensor.wait_for_released() def main(self, driving_speed: float = 100): self.screen.image_filename(filename='/home/robot/image/Target.bmp', clear_screen=True) 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()
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_port=left_foot_motor_port, right_motor_port=right_foot_motor_port, polarity=Motor.POLARITY_NORMAL, speed=1000, channel=ir_beacon_channel) self.tank_driver = MoveTank(left_motor_port=left_foot_motor_port, right_motor_port=right_foot_motor_port, motor_class=LargeMotor) 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.ir_beacon_channel = ir_beacon_channel self.leds = Leds() self.speaker = Sound() def dance_whenever_ir_beacon_pressed(self): while True: while self.ir_sensor.beacon(channel=self.ir_beacon_channel): self.tank_driver.on_for_seconds(left_speed=randint(-100, 100), right_speed=randint(-100, 100), seconds=1, brake=False, block=True) def keep_detecting_objects_by_ir_sensor(self): while True: if self.ir_sensor.proximity < 25: self.leds.animate_police_lights(color1=Leds.ORANGE, color2=Leds.RED, group1=Leds.LEFT, group2=Leds.RIGHT, sleeptime=0.5, duration=5, block=False) self.speaker.play_file(wav_file='/home/robot/sound/Object.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.speaker.play_file( wav_file='/home/robot/sound/Detected.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.speaker.play_file( wav_file='/home/robot/sound/Error alarm.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) 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_file( wav_file='/home/robot/sound/Up.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.bazooka_blast_motor.on_for_rotations(speed=100, rotations=-3, brake=True, block=True) else: self.speaker.play_file( wav_file='/home/robot/sound/Down.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.bazooka_blast_motor.on_for_rotations(speed=100, rotations=3, brake=True, block=True) self.touch_sensor.wait_for_released() def main(self): Thread(target=self.dance_whenever_ir_beacon_pressed, daemon=True).start() # DON'T use IR Sensor in 2 different modes in the same program / loop # - https://github.com/pybricks/support/issues/62 # - https://github.com/ev3dev/ev3dev/issues/1401 # Thread(target=self.keep_detecting_objects_by_ir_sensor).start() Thread(target=self.blast_bazooka_whenever_touched, daemon=True).start() super().main() # RemoteControlledTank.main()
class MindstormsGadget(AlexaGadget): def __init__(self): super().__init__(gadget_config_path='./auth.ini') # order queue self.queue = Queue() self.button = Button() self.leds = Leds() self.sound = Sound() self.console = Console() self.console.set_font("Lat15-TerminusBold16.psf.gz", True) self.dispense_motor = LargeMotor(OUTPUT_A) self.pump_motor = LargeMotor(OUTPUT_B) self.touch_sensor = TouchSensor(INPUT_1) # Start threads threading.Thread(target=self._handle_queue, daemon=True).start() threading.Thread(target=self._test, daemon=True).start() def on_connected(self, device_addr): self.leds.animate_rainbow(duration=3, block=False) self.sound.play_song((('C4', 'e3'), ('C5', 'e3'))) def on_disconnected(self, device_addr): self.leds.animate_police_lights('RED', 'ORANGE', duration=3, block=False) self.leds.set_color("LEFT", "BLACK") self.leds.set_color("RIGHT", "BLACK") self.sound.play_song((('C5', 'e3'), ('C4', 'e3'))) def _test(self): while 1: self.button.wait_for_pressed('up') order = { 'name': 'Test', 'tea': 'Jasmine', 'sugar': 100, 'ice': 100 } self.queue.put(order) sleep(1) def _handle_queue(self): while 1: if self.queue.empty(): continue order = self.queue.get() self._make(name=order['name'], tea=order['tea'], sugar=order['sugar'], ice=order['ice']) def _send_event(self, name, payload): self.send_custom_event('Custom.Mindstorms.Gadget', name, payload) def _affirm_receive(self): self.leds.animate_flash('GREEN', sleeptime=0.25, duration=0.5, block=False) self.sound.play_song((('C3', 'e3'), ('C3', 'e3'))) def on_custom_mindstorms_gadget_control(self, directive): try: payload = json.loads(directive.payload.decode("utf-8")) print("Control payload: {}".format(payload), file=sys.stderr) control_type = payload["type"] # regular voice commands if control_type == "automatic": self._affirm_receive() order = { "name": payload["name"] or "Anonymous", "tea": payload["tea"] or "Jasmine Milk Tea", "sugar": payload["sugar"] or 100, "ice": payload["ice"] or 100, } self.queue.put(order) # series of voice commands elif control_type == "manual": # Expected params: [command] control_command = payload["command"] if control_command == "dispense": self._affirm_receive() if payload['num']: self._dispense(payload['num']) else: self._dispense() elif control_command == "pour": self._affirm_receive() if payload['num']: self._pour(payload['num']) else: self._pour() except KeyError: print("Missing expected parameters: {}".format(directive), file=sys.stderr) def _make(self, name=None, tea="Jasmine Milk Tea", sugar=100, ice=100): if not self.touch_sensor.is_pressed: # cup is not in place self._send_event('CUP', None) self.touch_sensor.wait_for_pressed() sleep(3) # cup enter delay # mid_col = console.columns // 2 # mid_row = console.rows // 2 # mid_col = 1 # mid_row = 1 # alignment = "L" process = self.sound.play_file('mega.wav', 100, Sound.PLAY_NO_WAIT_FOR_COMPLETE) # dispense boba self._dispense() # dispense liquid self._pour(tea=tea) # self.console.text_at( # s, column=mid_col, row=mid_row, alignment=alignment, reset_console=True # ) # notify alexa that drink is finished payload = { "name": name, "tea": tea, "sugar": sugar, "ice": ice, } self._send_event("DONE", payload) process.kill() # kill song self.sound.play_song((('C4', 'q'), ('C4', 'q'), ('C4', 'q')), delay=0.1) self.touch_sensor.wait_for_released() # dispense liquid def _pour(self, time_in_s=10, tea="Jasmine Milk Tea"): # send event to alexa payload = {"time_in_s": time_in_s, "tea": tea} self._send_event("POUR", payload) self.pump_motor.run_forever(speed_sp=1000) sleep(time_in_s) self.pump_motor.stop() # dispense boba def _dispense(self, cycles=10): # send event to alexa payload = {"cycles": cycles} self._send_event("DISPENSE", payload) # ensure the dispenser resets to the correct position everytime if cycles % 2: cycles += 1 # agitate the boba to make it fall for i in range(cycles): deg = 45 if i % 2 else -45 self.dispense_motor.on_for_degrees(SpeedPercent(75), deg) sleep(0.5)
TOUCH_SENSOR = TouchSensor(address=INPUT_1) COLOR_SENSOR = ColorSensor(address=INPUT_3) IR_SENSOR = InfraredSensor(address=INPUT_4) LEDS = Leds() SPEAKER = Sound() while True: if IR_SENSOR.proximity < 25: TANK_DRIVER.off(brake=True) LEDS.animate_police_lights(color1=Leds.ORANGE, color2=Leds.RED, group1=Leds.LEFT, group2=Leds.RIGHT, sleeptime=0.5, duration=5, block=True) SPEAKER.play_file(wav_file='/home/robot/sound/Object.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) SPEAKER.play_file(wav_file='/home/robot/sound/Detected.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) SPEAKER.play_file(wav_file='/home/robot/sound/Error alarm.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE)
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, motor_class=LargeMotor, 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.speaker = Sound() def detect_object_by_ir_sensor(self): if self.ir_sensor.proximity < 25: self.leds.animate_police_lights(color1=Leds.ORANGE, color2=Leds.RED, group1=Leds.LEFT, group2=Leds.RIGHT, sleeptime=0.5, duration=5, block=False) self.speaker.play_file(wav_file='/home/robot/sound/Object.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.speaker.play_file(wav_file='/home/robot/sound/Detected.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.speaker.play_file( wav_file='/home/robot/sound/Error alarm.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) 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_file(wav_file='/home/robot/sound/Up.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.bazooka_blast_motor.on_for_rotations(speed=100, rotations=-3, brake=True, block=True) self.speaker.play_file( wav_file='/home/robot/sound/Laughing 1.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) else: self.speaker.play_file(wav_file='/home/robot/sound/Down.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.bazooka_blast_motor.on_for_rotations(speed=100, rotations=3, brake=True, block=True) self.speaker.play_file( wav_file='/home/robot/sound/Laughing 2.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.touch_sensor.wait_for_released() def main(self, driving_speed: float = 100): while True: self.drive_once_by_ir_beacon(speed=driving_speed) # 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()
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.tank_driver = MoveTank(left_motor_port=left_foot_motor_port, right_motor_port=right_foot_motor_port, motor_class=LargeMotor) 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.ir_beacon_channel = ir_beacon_channel self.leds = Leds() self.speaker = Sound() def drive_once_by_ir_beacon(self, speed: float = 100): # forward if self.ir_sensor.top_left( channel=self.ir_beacon_channel) and self.ir_sensor.top_right( channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=speed, right_speed=speed) # backward elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel ) and self.ir_sensor.bottom_right( channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=-speed, right_speed=-speed) # turn left on the spot elif self.ir_sensor.top_left(channel=self.ir_beacon_channel ) and self.ir_sensor.bottom_right( channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=-speed, right_speed=speed) # turn right on the spot elif self.ir_sensor.top_right( channel=self.ir_beacon_channel) and self.ir_sensor.bottom_left( channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=speed, right_speed=-speed) # turn left forward elif self.ir_sensor.top_left(channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=0, right_speed=speed) # turn right forward elif self.ir_sensor.top_right(channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=speed, right_speed=0) # turn left backward elif self.ir_sensor.bottom_left(channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=0, right_speed=-speed) # turn right backward elif self.ir_sensor.bottom_right(channel=self.ir_beacon_channel): self.tank_driver.on(left_speed=-speed, right_speed=0) # otherwise stop else: self.tank_driver.off(brake=False) def keep_driving_by_ir_beacon(self, speed: float = 100): while True: self.drive_once_by_ir_beacon(speed=speed) def dance_whenever_ir_beacon_pressed(self): while True: while self.ir_sensor.beacon(channel=self.ir_beacon_channel): self.tank_driver.on_for_seconds(left_speed=randint(-100, 100), right_speed=randint(-100, 100), seconds=1, brake=False, block=True) def keep_detecting_objects_by_ir_sensor(self): while True: if self.ir_sensor.proximity < 25: self.leds.animate_police_lights(color1=Leds.ORANGE, color2=Leds.RED, group1=Leds.LEFT, group2=Leds.RIGHT, sleeptime=0.5, duration=5, block=False) self.speaker.play_file(wav_file='/home/robot/sound/Object.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.speaker.play_file( wav_file='/home/robot/sound/Detected.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.speaker.play_file( wav_file='/home/robot/sound/Error alarm.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) 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_file( wav_file='/home/robot/sound/Up.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.bazooka_blast_motor.on_for_rotations(speed=100, rotations=-3, brake=True, block=True) self.speaker.play_file( wav_file='/home/robot/sound/Laughing 1.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) else: self.speaker.play_file( wav_file='/home/robot/sound/Down.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.bazooka_blast_motor.on_for_rotations(speed=100, rotations=3, brake=True, block=True) self.speaker.play_file( wav_file='/home/robot/sound/Laughing 2.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.touch_sensor.wait_for_released() def main(self, driving_speed: float = 100): # FIXME # Unhandled exception in thread started by <bound_method b6bbfd60 <Thread object at b6bbe450>.<function run at 0xb6bb4a20>> # Traceback (most recent call last): # File "threading/threading.py", line 15, in run # File "ev3dev2/sensor/lego.py", line 885, in top_left # File "ev3dev2/sensor/lego.py", line 919, in buttons_pressed # File "ev3dev2/sensor/__init__.py", line 203, in value # File "ev3dev2/__init__.py", line 307, in get_attr_int # ValueError: invalid syntax for integer with base 10: '' Thread(target=self.dance_whenever_ir_beacon_pressed).start() # DON'T use IR Sensor in 2 different modes in the same program / loop # - https://github.com/pybricks/support/issues/62 # - https://github.com/ev3dev/ev3dev/issues/1401 # Thread(target=self.keep_detecting_objects_by_ir_sensor).start() Thread(target=self.blast_bazooka_whenever_touched).start() self.keep_driving_by_ir_beacon(speed=driving_speed)
#!/usr/bin/env python3 from ev3dev2.button import Button from ev3dev2.led import Leds from ev3dev2.sound import Sound sound = Sound() sound.beep() button = Button() leds = Leds() while True: if(button.right): leds.animate_cycle(('RED', 'GREEN', 'AMBER', 'ORANGE', 'YELLOW')) elif(button.left): leds.animate_flash('RED', sleeptime=0.5, duration=5) elif(button.down): leds.animate_police_lights('RED', 'GREEN', sleeptime=0.5, duration=5) elif(button.up): leds.animate_rainbow(increment_by=0.1, sleeptime=0.1, duration=10) else: leds.all_off() # Write your program here
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, motor_class=LargeMotor, 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.speaker = Sound() self.screen = Display() def dance_whenever_ir_beacon_pressed(self): while True: while self.ir_sensor.beacon(channel=self.tank_drive_ir_beacon_channel): self.tank_driver.on_for_seconds( left_speed=randint(-100, 100), right_speed=randint(-100, 100), seconds=1, brake=False, block=True) def keep_detecting_objects_by_ir_sensor(self): while True: if self.ir_sensor.proximity < 25: self.leds.animate_police_lights( color1=Leds.ORANGE, color2=Leds.RED, group1=Leds.LEFT, group2=Leds.RIGHT, sleeptime=0.5, duration=5, block=False) self.speaker.play_file( wav_file='/home/robot/sound/Object.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.speaker.play_file( wav_file='/home/robot/sound/Detected.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.speaker.play_file( wav_file='/home/robot/sound/Error alarm.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) 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_file( wav_file='/home/robot/sound/Up.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.bazooka_blast_motor.on_for_rotations( speed=100, rotations=-3, brake=True, block=True) self.speaker.play_file( wav_file='/home/robot/sound/Laughing 1.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) else: self.speaker.play_file( wav_file='/home/robot/sound/Down.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.bazooka_blast_motor.on_for_rotations( speed=100, rotations=3, brake=True, block=True) self.speaker.play_file( wav_file='/home/robot/sound/Laughing 2.wav', volume=100, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) self.touch_sensor.wait_for_released() def main(self, driving_speed: float = 100): self.screen.image_filename( filename='/home/robot/image/Target.bmp', clear_screen=True) self.screen.update() # FIXME # Traceback (most recent call last): # File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner # self.run() # File "/usr/lib/python3.5/threading.py", line 862, in run # self._target(*self._args, **self._kwargs) # File "/home/robot/Ev3rstorm/Ev3rstorm-Super.EV3Dev2.Python3.Threading.py", line 170, in dance_whenever_ir_beacon_pressed # while self.ir_sensor.beacon(channel=self.ir_beacon_channel): # File "/usr/lib/python3/dist-packages/ev3dev2/sensor/lego.py", line 909, in beacon # return 'beacon' in self.buttons_pressed(channel) # File "/usr/lib/python3/dist-packages/ev3dev2/sensor/lego.py", line 919, in buttons_pressed # return self._BUTTON_VALUES.get(self.value(channel), []) # File "/usr/lib/python3/dist-packages/ev3dev2/sensor/__init__.py", line 203, in value # self._value[n], value = self.get_attr_int(self._value[n], 'value' + str(n)) # File "/usr/lib/python3/dist-packages/ev3dev2/__init__.py", line 307, in get_attr_int # return attribute, int(value) # ValueError: invalid literal for int() with base 10: '' Thread(target=self.dance_whenever_ir_beacon_pressed, daemon=True).start() # DON'T use IR Sensor in 2 different modes in the same program / loop # - https://github.com/pybricks/support/issues/62 # - https://github.com/ev3dev/ev3dev/issues/1401 # Thread(target=self.keep_detecting_objects_by_ir_sensor, # daemon=True).start() Thread(target=self.blast_bazooka_whenever_touched, daemon=True).start() self.keep_driving_by_ir_beacon(speed=driving_speed)