def handleMessage(message): angle_coef = 20 global readyToRide # print('delivered : '+ str(message[0])) if message[0] == 254: return if message[0] == 255: mid_motor.stop() if message[0] % 4 == 1: Leds.set_color(Leds.RIGHT, Leds.RED) Leds.set_color(Leds.LEFT, Leds.GREEN) left_motor.run_forever(speed_sp=-1000) if (min((message[0] - 128) // 4 * angle_coef, 1000) < -800): right_motor.run_forever(speed_sp=min((message[0] - 128) // 4 * angle_coef, 1000)) else: right_motor.run_forever(speed_sp=-1000) print(-1000, min(-1000 + message[0] // 4 * angle_coef, 1000)) # Sound.speak('RUN') # print("a") Sound.speak('Stop') print("b") print("ya loh") Sound.speak('HAHA PAPA CARLO YA OBMANUL TEBIA') print("ya loh1") sleep(5) print("ya loh2") if message[0] % 4 == 0: Leds.set_color(Leds.RIGHT, Leds.YELLOW) right_motor.stop() left_motor.stop() Sound.speak('Stop') print("b") print("ya loh") Sound.speak('HAHA PAPA CARLO YA OBMANUL TEBIA') print("ya loh1") sleep(5) print("ya loh2") if message[0] % 4 == 2: Leds.set_color(Leds.LEFT, Leds.RED) Leds.set_color(Leds.RIGHT, Leds.GREEN) if (min((message[0] - 128) // 4 * angle_coef, 1000) < -800): left_motor.run_forever(speed_sp=min((message[0] - 128) // 4 * angle_coef, 1000)) else: left_motor.run_forever(speed_sp=-1000) right_motor.run_forever(speed_sp=-1000) print(min(-1000 + message[0] // 4 * angle_coef, 1000), -1000) Sound.speak('Stop') print("b") print("ya loh") Sound.speak('HAHA PAPA CARLO YA OBMANUL TEBIA') print("ya loh1") sleep(5) print("ya loh2") if message[0] % 4 == 3: readyToRide = True left_motor.run_forever(speed_sp=-1000) right_motor.run_forever(speed_sp=-1000)
def blink_green(): """blink the LED green""" try: from ev3dev.ev3 import Leds Leds.set_color(Leds.LEFT, Leds.GREEN) Leds.set_color(Leds.RIGHT, Leds.GREEN) Leds.set(Leds.LEFT, brightness_pct=0.5, trigger='timer') Leds.set(Leds.RIGHT, brightness_pct=0.5, trigger='timer') except Exception as ex: logging.getLogger(__name__).error(str(ex))
def wait_for_load(self): Leds.set_color(Leds.LEFT, Leds.RED) while (True): if (self.button.any()): if (self.button.up): self.load_map() break elif (self.button.down): break sleep(0.01) sleep(2)
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
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()
TAIL_MOTOR = LargeMotor(address=OUTPUT_B) CHEST_MOTOR = LargeMotor(address=OUTPUT_D) IR_SENSOR = InfraredSensor(address=INPUT_4) LIGHTS = Leds() SPEAKER = Sound() CHEST_MOTOR.run_timed(speed_sp=-300, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) CHEST_MOTOR.wait_while(Motor.STATE_RUNNING) while True: if IR_SENSOR.proximity < 30: LIGHTS.set_color(group=Leds.LEFT, color=Leds.RED, pct=1) LIGHTS.set_color(group=Leds.RIGHT, color=Leds.RED, pct=1) MEDIUM_MOTOR.stop(stop_action=Motor.STOP_ACTION_HOLD) TAIL_MOTOR.stop(stop_action=Motor.STOP_ACTION_BRAKE) SPEAKER.play(wav_file='/home/robot/sound/Snake hiss.wav') CHEST_MOTOR.run_timed(speed_sp=1000, time_sp=1000, stop_action=Motor.STOP_ACTION_BRAKE) CHEST_MOTOR.wait_while(Motor.STATE_RUNNING) MEDIUM_MOTOR.run_forever(speed_sp=1000)
if btn.is_pressed: lcd.draw.text((20, 20), 'Sleeping', fill='black') lcd.update() print('sleeping...') measure_thread = Thread(target=dist_measure) measure_thread.start() sleep(5) lcd.clear() while not btn.is_pressed: if dist_measure() == True: while dist_measure() == True: if color.value() != 1: print("True drive") leds.set_color(Leds.LEFT, Leds.GREEN) leds.set_color(Leds.LEFT, Leds.GREEN) motor_left.run_forever(speed_sp=900) motor_right.run_forever(speed_sp=900) else: print("True back") motor_left.stop(stop_action="brake") motor_right.stop(stop_action='brake') motor_left.run_timed(time_sp=1, speed_sp=-750) motor_right.run_timed(time_sp=1, speed_sp=-750) motor_left.stop(stop_action="coast") motor_right.stop(stop_action='coast') elif dist_measure() == False: while dist_measure() == False:
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()
measue = ir.value() if measue <= 60: return True else: return False while True: try: if color_left.value() == 1 and color_right.value() == 1: if dist_measure() == True: while True: print("Found!") leds.set_color(Leds.LEFT, Leds.GREEN) leds.set_color(Leds.LEFT, Leds.GREEN) motor_left.run_forever(speed_sp=-900) motor_right.run_forever(speed_sp=-900) if not color_left.value() != 1 or color_right.value() != 1: print("FOund line") break else: motor_left.run_forever(speed_sp=-900) motor_right.run_forever(speed_sp=900) print("Seeking") else: motor_left.stop(stop_action="brake") motor_right.stop(stop_action='brake')
bt_left = False bt_right = False btn.on_left = left btn.on_right = right bt_sair = False lcd = Screen() mystring = 'Pressione para iniciar:' if __name__ == '__main__': while True: lcd.clear() size = lcd.draw.textsize(mystring) lcd.draw.text((89-size[0]/2, 59), mystring) lcd.update() Leds.set_color(ev3.Leds.LEFT, ev3.Leds.RED) Leds.set_color(ev3.Leds.RIGHT, ev3.Leds.RED) print('aguardando bt para iniciar') while not btn.any(): time.sleep(0.01) if 'right' in btn.buttons_pressed: bt_sair = True if bt_sair: break Leds.set_color(ev3.Leds.LEFT, ev3.Leds.ORANGE) # ready go Leds.set_color(ev3.Leds.RIGHT, ev3.Leds.ORANGE) # go go go print('Iniciando') time.sleep(1) buffer_cor = 0 n_buffer_cor = 4 cor_esquerda = []
LEFT_FOOT_MOTOR = LargeMotor(address=OUTPUT_B) RIGHT_FOOT_MOTOR = LargeMotor(address=OUTPUT_C) 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
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') for _ in range(3): for led in (Leds.red_left, Leds.red_right, Leds.green_left, Leds.green_right): led.brightness_pct = 100 time.sleep(0.5) led.brightness_pct = 0 Leds.all_off()