def race_loss_routine(): '''Call this in case the worst happens''' Sound().speak('We did everything right!', play_type=Sound.PLAY_LOOP)
def play_beep(hz=300.0, length=1.0): Sound().play_tone(hz, length)
#/usr/bin/env python3 from ev3dev2.sound import Sound from time import sleep sound = Sound() #play a standard beep sound.beep() sleep(2) # pause for 2 seconds # Play a SINGLE 2000 Hz tone for 1.5 seconds sound.play_tone(2000, 1.5) sleep(2) # Play a SEQUENCE of tones sound.tone([(200, 2000, 400), (800, 1800, 2000)]) sleep(2) # Play a 500 Hz tone for 1 second and then wait 0.4 seconds # before playing the next tone # Play the tone three times # [(500, 1000, 400)] * 3 is the same as # [(500, 1000, 400), (500, 1000, 400), (500, 1000, 400)] sound.tone([(500, 1000, 400)] * 3) sleep(2) #text to speech sound.speak('Hello, my name is E V 3!')
def play_random_quote(): '''A word from our sponsor, Anatoly Dyatlov''' index = random.randint(0, len(soundList) - 1) # keep moving while speaking Sound().speak(soundList[index], play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)
#!/usr/bin/env python3 import os import sys import time from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_D, SpeedPercent, MoveTank, MoveSteering, MediumMotor, OUTPUT_B from ev3dev2.sensor import INPUT_1, INPUT_4, INPUT_2, INPUT_3 from ev3dev2.sensor.lego import TouchSensor, InfraredSensor, ColorSensor from ev3dev2.led import Leds from ev3dev2.button import Button from ev3dev2.sound import Sound ON = True OFF = False music = Sound() music.play_file("Confirm.wav") tank_drive = MoveTank(OUTPUT_A, OUTPUT_D) steering_drive = MoveSteering(OUTPUT_A, OUTPUT_D) ir = InfraredSensor() ir.mode = "IR-PROX" touch_sensor = TouchSensor() touch_sensor.mode = "TOUCH" color_arm = MediumMotor(OUTPUT_B) display_button = Button() color_sensor = ColorSensor() def deploy_color_sensor(): color_arm.on_for_rotations(SpeedPercent(5), 0.30) time.sleep(4.5) if color_sensor.color == 1: music.speak("I found something black on the surface of Mars")
from ev3dev2.sensor.lego import TouchSensor from ev3dev2.sensor.lego import UltrasonicSensor from ev3dev2.sound import Sound from time import sleep flip = 1 def drive(): global flip flip = flip * -1 tank_drive.on_for_seconds(SpeedPercent(30 * flip), SpeedPercent(30 * flip), 1, True, True) s = Sound() tank_drive = MoveTank(OUTPUT_A, OUTPUT_D) for x in range(4): drive() s.beep() sleep(1) for x in range(4): drive() s.beep(play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE) sleep(3) for x in range(4): drive() s.speak("testjes tests tests tests")
#!/usr/bin/env micropython from ev3dev2.sound import Sound player = Sound() player.play_song(( ('D4', 'e3'), # intro anacrouse ('D4', 'e3'), ('D4', 'e3'), ('G4', 'h'), # meas 1 ('D5', 'h'), ('C5', 'e3'), # meas 2 ('B4', 'e3'), ('A4', 'e3'), ('G5', 'h'), ('D5', 'q'), ('C5', 'e3'), # meas 3 ('B4', 'e3'), ('A4', 'e3'), ('G5', 'h'), ('D5', 'q'), ('C5', 'e3'), # meas 4 ('B4', 'e3'), ('C5', 'e3'), ('A4', 'h.'), ))
from ev3dev2.sensor import INPUT_1, INPUT_2, INPUT_3, INPUT_4 from ev3dev2.sensor.lego import ColorSensor from ev3dev2.sensor.lego import GyroSensor from ev3dev2.button import Button from ev3dev2.sound import Sound from ev3dev2.display import Display import time import sys btn = Button() color = ColorSensor(INPUT_4) tank_drive = MoveTank(OUTPUT_B, OUTPUT_C) #This is the template whenever we code motorA = MediumMotor(OUTPUT_A) motorD = MediumMotor(OUTPUT_D) Sound_ = Sound() Display_ = Display() Sound_.play_tone(frequency=400, duration=0.5, volume=50) #plays a note so we know when the code starts #yellow = forwards def swing_and_safety(): tank_drive = MoveTank(OUTPUT_B, OUTPUT_C) motorA = MediumMotor(OUTPUT_A) motorD = MediumMotor(OUTPUT_D) tank_drive.on_for_rotations(SpeedPercent(50), SpeedPercent(50), 6.67) #ROBOT MOVES FORWARD FROM BASE tank_drive.on_for_rotations(SpeedPercent(20), SpeedPercent(20), .8) # ROBOT MOVES INTO SWING tank_drive.on_for_rotations(SpeedPercent(-30), SpeedPercent(-30),
from ev3dev2.sensor.lego import ColorSensor from ev3dev2.sensor.lego import GyroSensor from ev3dev2.button import Button from ev3dev2.sound import Sound from ev3dev2.display import Display from robotClass import Robot import time btn = Button() # variable so we can get buttons pressed on EV3 color = ColorSensor(INPUT_4) # color sensor for checking attachment color tank_drive = MoveTank( OUTPUT_B, OUTPUT_C) # Creates a variable so we can control the drivetrain motorA = MediumMotor(OUTPUT_A) # left medium motor motorD = MediumMotor(OUTPUT_D) # right medium motor gyro = GyroSensor(INPUT_1) # gyro variable Sound_ = Sound() # beepity beep Display_ = Display() # for displaying text Sound_.play_tone( frequency=400, duration=0.5, volume=50 ) #there is a 15-20 second lag when we start a program so this tells us that master has alreafy started by beeping start = time.time( ) #this makes it so that when we call start it is equal to what ever far the robot is in the code (IE: 00:12 if it was 12 seconds in) btn.on_backspace = failsafe while True: #the code that is indented repetes forever untill we stop the program if (btn.down): #if the down button is pressed: break #leave the loop pass btn.wait_for_released( 'enter') #waits untill the enter(middle) button is pressed
#!/usr/bin/env micropython from ev3dev2.motor import LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_D from ev3dev2.sensor import INPUT_4 from ev3dev2.sensor.lego import InfraredSensor from ev3dev2.sound import Sound from multiprocessing import Process from time import sleep LARGE_MOTOR = LargeMotor(address=OUTPUT_D) MEDIUM_MOTOR = MediumMotor(address=OUTPUT_A) IR_SENSOR = InfraredSensor(address=INPUT_4) SPEAKER = Sound() def rattle(): while True: MEDIUM_MOTOR.on_for_seconds(speed=10, seconds=1, brake=False, block=True) SPEAKER.play_file(wav_file='/home/robot/sound/Snake rattle.wav', volume=50, play_type=Sound.PLAY_WAIT_FOR_COMPLETE) MEDIUM_MOTOR.on_for_seconds(speed=-10, seconds=1,
from ev3dev2.sound import Sound #dicionário que permite tocar músicas from ev3dev.ev3 import * #nao sei a diferença do de cima from time import sleep #from threading import Thread #dicionário que permite executar ações ao mesmo tempo from multiprocessing import Process #multi-process from ev3dev2.motor import LargeMotor, MediumMotor, MoveSteering, OUTPUT_D, OUTPUT_A, OUTPUT_B, SpeedRPS #dicionário dos motores disponíveis e usados no robot from PIL import Image #dicionário que permite apresentar imagens .bmp no lcd from ev3dev2.sensor.lego import GyroSensor from threading import Thread #dicionário que permite executar ações ao mesmo tempo import os #os.system('setfont Lat15-TerminusBold14') os.system('setfont Lat15-TerminusBold32x16') mySound = Sound() import colorTest import coord ############################################# # TO DO LIST # ############################################# ''' definir a melhor velocidade para o robot se mover definir a melhor velocidade para a garra abrir e fechar definir quantas rotações (ou segundos) são necessarias para andar 1 casa definir os angulos de rotação definir multiprocessing para andar e fazer a leitura ao mesmo tempo ''' ##########################################################################################
def sound(self): return Sound()
class Robot: #Components of the robot left_motor = LargeMotor(OUTPUT_B) right_motor = LargeMotor(OUTPUT_C) tank = MoveTank(OUTPUT_B, OUTPUT_C) touch_sensor = TouchSensor() ultrasonic_sensor = UltrasonicSensor() color_sensor = ColorSensor() sound = Sound() lcd = Display() btn = Button() black_square_sensor = BlackSquareSensor(color_sensor) #Useful preset (read: hardcoded) values DISTANCE_TO_ROTATION_AXIS = 0.25 def __init__(self, start_position=[0,0], start_direction=[0,-1]): """ Set the paramters for the robot :param start_position: The start position of the robot :param start_direction: The start direction vector of the robot """ self.position = start_position self.direction = start_direction self.sound.set_volume(100) self.display_text("Start") def move(self, speed=25): """ Move forward as a tank until it hits a black square and update the position :param speed: The speed to move :return: None """ self.position[0] += self.direction[0] self.position[1] += self.direction[1] #We start on a black square so we initalise to being on a white square self.black_square_sensor.start_reading(count=5, init_val=100, interval=0.1, wait_time=1) #Until we hit a black square, just keep moving forward self.tank.on(SpeedPercent(speed),SpeedPercent(speed)) #Block until we are over a black square while self.black_square_sensor.above_threshold(): continue self.tank.off() self.report_black_square() robot.correction() def check_next(self, speed=25): """ Perform a check of the space between current position and next black square This is very similiar to the move function, but has checks for touch :param speed: The speed which we travel over next space :return: True if touch sensor is activated during journey, False otherwise """ # Add half of position now, half later so we are sure to be in square self.position[0] += 0.5*self.direction[0] self.position[1] += 0.5*self.direction[1] #We are on a black square, so we initalise to white self.black_square_sensor.start_reading(count=5, init_val=100, interval=0.1, wait_time=1) distance_threshold = 10 # Until we hit a black square, just keep moving forward self.tank.on(SpeedPercent(speed), SpeedPercent(speed)) # Block until we are over a black square or until we touch something while self.black_square_sensor.above_threshold() and self.ultrasonic_sensor.distance_centimeters>distance_threshold: continue self.tank.off() if self.ultrasonic_sensor.distance_centimeters<distance_threshold: # We have found it! self.report_tower() return True # We are now over a black square, do a correction for any deviation self.position[0] += 0.5 * self.direction[0] self.position[1] += 0.5 * self.direction[1] self.report_black_square() robot.correction() return False def correction(self, dps=180): """ Do a correction for any deviation after travelling between squares The implementation below assumes that we didn't deviate too far between black squares This means we are still *roughly* orthogonal to the square We also assume that the squares are perfectly square, to within other errors The implementation is to find the angle left and right until we are off the square, take the midpoint and correct this amount. This should put us on a roughly equal but opposite deviation compared to the last trip, canceling the next problem :param dps: The speed to turn in degrees per second :return: None """ # Stop the old thread to start a new one with better parameters self.black_square_sensor.stop_reading() #Currently on a black square, move until on a white square self.black_square_sensor.start_reading(count=2, init_val=0, interval=0.1, wait_time=0.2) #While we are not back on the white keep turning start = time.time() self.tank.on(0, SpeedDPS(dps)) while not self.black_square_sensor.above_threshold(): continue end = time.time() left_angle = dps*(end-start) #We now know angular deviation to left, reset by moving back self.tank.on_for_degrees(0, SpeedDPS(-dps), left_angle) #Do the same for the right angle self.black_square_sensor.start_reading(count=2, init_val=0, interval=0.1, wait_time=0.2) start = time.time() self.tank.on(SpeedDPS(dps), 0) while not self.black_square_sensor.above_threshold(): continue end = time.time() right_angle = dps * (end - start) self.tank.on_for_degrees(SpeedDPS(-dps), 0, right_angle) # Now we have both left and right angles, lets average then move to corrected bearing angle_correction = (left_angle - right_angle) / 2 angle_correction = 0.65*(90/math.pi)*math.atan(math.radians(angle_correction)) self.tank.on_for_degrees(SpeedDPS(-dps), SpeedDPS(dps), angle_correction) def rotate(self, angle_count, speed=25): """ Rotate a multiple of 90 degrees on axis like a tank and update the direction vector We are defining positive rotation as turning clockwise :param angle_count: the angle to rotate through (as a multiple of 90 degrees) :param speed: The speed to rotate around at :return: """ # Update direction using rotation matrix direction_matrix = [[1, 0], [0, 1], [-1, 0], [0, -1]] current_index = direction_matrix.index(self.direction) self.direction = direction_matrix[(current_index - angle_count) % len(direction_matrix)] #First, prepare by moving a distance so we are over the black square self.tank.on_for_rotations(SpeedPercent(speed), SpeedPercent(speed), self.DISTANCE_TO_ROTATION_AXIS) #Now actually rotate on the axis self.tank.on_for_degrees(SpeedPercent(-speed), SpeedPercent(speed), 1.87*angle_count*90) #Finally, undo the prepartation by moving backwards and placing the light sensor over the black square self.tank.on_for_rotations(SpeedPercent(speed), SpeedPercent(speed), -self.DISTANCE_TO_ROTATION_AXIS) robot.correction() def report_black_square(self): """ Report the black square we are on, determined from position coordinates by formula below :return: None """ number = (self.position[0] + 1) + (self.position[1]) * 15 self.display_text(str(int(number))) self.sound.speak(str(int(number))) def report_tower(self): """ Report the blue number of where the tower is :return: None """ #TODO Ensure this reports correct blue number blue_number = 3 * (math.floor(self.position[1]) - 3) + (math.floor(self.position[0]) - 9) // 2 + 1 self.display_text(str(int(blue_number))) self.sound.speak(str(int(blue_number))) self.sound.beep() def display_text(self, string, font='courB24'): """ Display some text on the lcd Display :param string: The string to display :param font: the font to use :return: None """ self.lcd.clear() self.lcd.text_pixels(string, clear_screen=True, x=30, y=30, font=font) self.lcd.update() def finish(self): """ Finish the robots task and free resources :return: None """ self.black_square_sensor.stop_reading()
def sound(): my_sound = Sound() my_sound.tone([(392, 350, 100)])
# Connect two large motors on output ports B and C lmotor, rmotor = [LargeMotor(address) for address in (OUTPUT_B, OUTPUT_C)] # Connect touch sensor and remote control ts = TouchSensor() rc = InfraredSensor() # Initialize button handler button = Button() # Turn leds off leds = Leds() leds.all_off() spkr = Sound() def roll(motor, led_group, direction): """ 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): if state: # Roll when button is pressed