def solid_green(): """blink the LED green""" try: from ev3dev.ev3 import Leds Leds.set_color(Leds.LEFT, Leds.GREEN) Leds.set_color(Leds.LEFT, Leds.GREEN) Leds.set(Leds.LEFT, trigger='default-on') Leds.set(Leds.RIGHT, trigger='default-on') time.sleep(0.5) Leds.all_off() except Exception as ex: logging.getLogger(__name__).error(str(ex))
def all_green(): process = robot.scan(200) g_count = 0 for i, j in process: if i == Color.GREEN: g_count += 1 if g_count == len(process): Leds.set_color(Leds.RIGHT, Leds.GREEN) Leds.set_color(Leds.LEFT, Leds.GREEN) Sound.speak("good to go, all green").wait() Leds.all_off() return True else: return False
def play_leds(): """Indicate that we've started""" from ev3dev.ev3 import Leds # save current state saved_state = [led.brightness_pct for led in Leds.LEFT + Leds.RIGHT] Leds.all_off() time.sleep(0.1) for _ in range(1): for color in (Leds.RED, Leds.YELLOW, Leds.GREEN): for group in (Leds.LEFT, Leds.RIGHT): Leds.set_color(group, color) time.sleep(0.1) Leds.all_off() time.sleep(0.5) for led, level in zip(Leds.RIGHT + Leds.LEFT, saved_state): led.brightness_pct = level
def play_leds(self): from ev3dev.ev3 import Leds # save current state saved_state = [led.brightness_pct for led in Leds.LEFT + Leds.RIGHT] Leds.all_off() time.sleep(0.1) # continuous mix of colors print('colors fade') for i in range(180): rd = math.radians(10 * i) Leds.red_left.brightness_pct = .5 * (1 + math.cos(rd)) Leds.green_left.brightness_pct = .5 * (1 + math.sin(rd)) Leds.red_right.brightness_pct = .5 * (1 + math.sin(rd)) Leds.green_right.brightness_pct = .5 * (1 + math.cos(rd)) time.sleep(0.05) Leds.all_off() time.sleep(0.5) for led, level in zip(Leds.RIGHT + Leds.LEFT, saved_state): led.brightness_pct = level
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 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()
""" This demo illustrates how to use the two red-green LEDs of the EV3 brick. """ import time import math from ev3dev.ev3 import Leds print(__doc__.lstrip()) print('saving current LEDs state') # save current state saved_state = [led.brightness_pct for led in Leds.LEFT + Leds.RIGHT] Leds.all_off() time.sleep(1) # cycle LEDs like a traffic light print('traffic light') for _ in range(3): for color in (Leds.GREEN, Leds.YELLOW, Leds.RED): for group in (Leds.LEFT, Leds.RIGHT): Leds.set_color(group, color) time.sleep(0.5) Leds.all_off() time.sleep(0.5) # blink LEDs from side to side now print('side to side')