async def read_gamepad_inputs(): global head_light_flag print("Ready to drive!!") turn_sound = SoundPlayer( "/home/pi/xbox-raspberrypi-rover/soundfiles/turn-signal.mp3", card) horn_sound = SoundPlayer( "/home/pi/xbox-raspberrypi-rover/soundfiles/Horn.mp3", card) while not is_connected(): time.sleep(2) # Wait 2 seconds for controller to come up and try again while is_connected() and remote_control.button_b == False: #print(" trigger_right = ", round(remote_control.trigger_right,2),end="\r") x = round(remote_control.joystick_left_x, 2) y = round(remote_control.joystick_left_y, 2) angle = get_angle_from_coords(x, y) if angle > 180: angle = 360 - angle #print("x:", x, " y:", y, " angle: ",angle,end="\r") turn_head(angle) direction = get_motor_direction(x, y) y = adjust_speed(y, angle) #print("x:", x, " y:", y, " direction: ",direction,end="\r") drive_motor(direction, y) if round(remote_control.trigger_right, 2) > 0.0: horn_sound.play(1.0) led.blue() elif round(remote_control.trigger_left, 2) > 0.0: led.cyan() elif remote_control.bump_left: turn_sound.play(1.0) led.turn_left(5) elif remote_control.bump_right: turn_sound.play(1.0) led.turn_right(5) elif remote_control.dpad_up: remote_control.dpad_up = False elif remote_control.dpad_left: remote_control.dpad_left = False elif remote_control.dpad_right: remote_control.dpad_right = False elif remote_control.button_a: remote_control.button_a = False elif head_light_flag == False: led.both_off() led_strip.colorWipe(strip, Color(0, 0, 0)) if turn_sound.isPlaying(): turn_sound.stop() await asyncio.sleep(100e-3) #100ms return
def scope(cv, x, step_x, id): global cvjob global durchgang global linedrop def measure_point(): while not GPIO.input(11): GPIO.output(9, 1) u1 = 0 u2 = 0 for i in range(10000): if GPIO.input(11): GPIO.output(9, 0) u1 = u1 + faktor #u1=u1+1 else: GPIO.output(9, 1) u2 = u2 + faktor #u2=u2+1 GPIO.output(9, 0) spannung = (nulloffset + u1 - u2) / 2000 #print("Spannung: ", spannung, " u0: ",(((u1 - u2) * 1000 / 100000) + 100)) return spannung #ende measure_point() def dbv_coordinate(V): Unull = 1 if V <= 0: V = 0.0001 dBV = 20 * math.log10(V / Unull) if dBV < -20: dBV = -19.9 elif dBV > 20: dBV = 19.9 #print("Spannung: ", V," dBV: ",dBV," Coord: ",(h/2-(dBV*20))) return (h / 2 - (dBV * 20)) #ende dbv_coordiante(dBV) def frequenz_koord(freq): #Dekade feststellen lfreq = 0 m = 0 if freq < 100: m = 0 lfreq = freq / 10 elif freq < 1000: m = 1 lfreq = freq / 100 elif freq < 10000: m = 2 lfreq = freq / 1000 elif freq <= 20000: m = 3 lfreq = freq / 10000 try: logwert = math.log10(lfreq) except: print("freq: ", freq, " lfreq: ", lfreq, " m: ", m, " logwert: ", logwert, " xwert: ", xwert) raise xwert = wh * logwert + (wh * m) #print ("freq: ",freq," lfreq: ",lfreq," m: ",m," logwert: ",logwert," xwert: ",xwert) return round(xwert) #ende frequenz_koord(freq) korr_faktor = 1.0 try: korr_faktor = float(korr_var.get()) except: print("Keine gültige Zahl: ", korr_var.get()) korr_faktor = 1.0 korr_var.set("1.0") if korr_faktor > 4 or korr_faktor <= 0: print("Keine gültige Zahl: ", korr_var.get()) korr_faktor = 1.0 korr_var.set("1.0") if x < len(freqliste): if id: last_y = cv.coords(id)[-1] else: if linedrop == 1: cv.delete("line_point" + str(durchgang)) #Linie wieder löschen linedrop = 0 last_y = h / 2 #x += step_x #Hier Ton erzeugen und dann Messen anschließend Frequenz erhoehen step_x entsprechend erhoehen # id = cv.create_line(x-step_x, last_y , x, measure_point()*2, fill = "black", tag="line_point", width=2) counter.set("Frequenz: " + str(freqliste[x])) old_x = 0 try: SoundPlayer.playTone(freqliste[x], 2, False, dev) #hier auch Zeit einstellen #SoundPlayer.playTone(300, 2, False, dev) mp = 0 messcounter = 0 #Mittelwert mess_liste = [] #Liste while SoundPlayer.isPlaying(): #print("Warte:..",SoundPlayer.isPlaying()) #mp += measure_point() #Mittelwert messcounter += 1 #Mittelwert mess_liste.append(measure_point()) #print("Sound ",freqliste[x]," Hz") #mp=mp/messcounter #Mittelwert bilden #mess_liste.remove(max(mess_liste)) #mess_liste.remove(min(mess_liste)) #messcounter -= 2 mp = median(mess_liste) #DEBUG Ausgaben #print("Listenlänge: ",len(mess_liste)," Max: ",max(mess_liste)," Min: ",min(mess_liste), "Mid: ",mp) old_x = x - step_x if old_x < 0: old_x = 0 messwert.set("Messwert: " + str(round(mp, 2)) + "V") messpunkte.set("Messpunke: " + str(messcounter)) id = cv.create_line(frequenz_koord(freqliste[old_x]), last_y, frequenz_koord(freqliste[x]), dbv_coordinate(mp * korr_faktor), fill=linienfarbe[durchgang], tag="line_point" + str(durchgang), width=2) x += step_x except: print("Exception x:", x, "old_x: ", old_x) raise else: # hier auch Frequenz zurücksetzen x = 0 id = None durchgang += 1 # cv.after(20, scope, cv, x, step_x, id) if startstop == 1 and durchgang < len(linienfarbe): cvjob = cv.after(20, scope, cv, x, step_x, id) else: print("Gestoppt!")
# Sound1b.py from soundplayer import SoundPlayer import time # Use device with ID 0 (mostly standard audio output) # Sound resource in current folder p = SoundPlayer("salza1.wav", 1) print "play for 10 s" p.play() # non blocking, volume = 1 n = 0 while True: if not p.isPlaying(): break print "playing:", n if n == 10: p.stop() print "stopped" n += 1 time.sleep(1) print "done"
# Sound2b.py from soundplayer import SoundPlayer import time # Sine of 1000 Hz during 5 s, non-blocking, device 1 SoundPlayer.playTone(1000, 5, False, 1) n = 0 while SoundPlayer.isPlaying(): print "playing #", n time.sleep(1) n += 1 print "done"
import time from soundplayer import SoundPlayer # Use device with ID 1 (mostly USB audio adapter) p = SoundPlayer("/home/pi/sound/salza1.wav", 1) print("play for 10 s with volume 0.5") p.play(0.5) # non-blocking, volume = 0.5 print("isPlaying:", p.isPlaying()) time.sleep(10) print("pause for 5 s") p.pause() print("isPlaying:", p.isPlaying()) time.sleep(5) print("resume for 10 s") p.resume() time.sleep(10) print("stop") p.stop() print("isPlaying:", p.isPlaying()) print = ("done")
while True: if GPIO.input(P_PAUSE) == GPIO.LOW and state == "PLAYING": state = "PAUSED" p.pause() print "playing->paused" elif GPIO.input(P_PLAY) == GPIO.LOW and state == "STOPPED": state = "PLAYING" p.play() print "stopped->playing, song ID", songID elif GPIO.input(P_PLAY) == GPIO.LOW and state == "PAUSED": state = "PLAYING" p.resume() print "paused->playing" elif GPIO.input(P_STOP) == GPIO.LOW and (state == "PAUSED" or state == "PLAYING"): state = "STOPPED" p.stop() print "paused/playing->stopped" elif GPIO.input(P_SELECT) == GPIO.LOW and state == "STOPPED": songID += 1 if songID == nbSongs: songID = 0 p = SoundPlayer("/home/pi/songs/song" + str(songID) + ".mp3", 1) print "stopped->playing, song ID", songID p.play() state = "PLAYING" if state == "PLAYING" and not p.isPlaying(): print "finished->stopped" state = "STOPPED" time.sleep(0.1)
# Sound1a.py import time from soundplayer import SoundPlayer # Use device with ID 1 (mostly USB audio adapter) p = SoundPlayer("/home/pi/Music/song0.mp3", 1) print "play for 10 s with volume 0.5" p.play(0.5) # non-blocking, volume = 0.5 print "isPlaying:", p.isPlaying() time.sleep(10) print "pause for 5 s" p.pause() print "isPlaying:", p.isPlaying() time.sleep(5) print "resume for 10 s" p.resume() time.sleep(10) print "stop" p.stop() print "isPlaying:", p.isPlaying() print "done"
elif GPIO.input(P_TON6) == GPIO.LOW: print "Ton6..." p.stop() DoSound.playTone(1000, 0.3, dev) p = SoundPlayer("/home/pi/mp3/Wie_kleine_Tiere.mp3", 1) p.play() elif GPIO.input(P_LOWBAT) == GPIO.LOW: time.sleep(0.1) # Entprellen zur Sicherheit if GPIO.input(P_LOWBAT) == GPIO.LOW: print "Batterie leer, Ausschaltmeldung und runterfahren" GPIO.output(P_POWER, False) # Netzteil abschalten p.stop() DoSound.playTone([440, 550, 660], 10, dev) p = SoundPlayer("/home/pi/mp3/BatterieLeer_n.mp3", 1) p.play() time.sleep(2) os.system("sudo poweroff") elif (GPIO.input(P_POWSWITCH) == GPIO.HIGH) or (schleife > AUSLIMIT): time.sleep(0.1) # Entprellen zur Sicherheit if (GPIO.input(P_POWSWITCH) == GPIO.HIGH) or (schleife > AUSLIMIT): print "Ausschalten gewuenscht, runterfahren und ausschalten" GPIO.output(P_POWER, False) p.stop() DoSound.playTone([440, 550, 660], 0.2, dev) os.system("sudo poweroff") if p.isPlaying() == True: schleife = 0 else: schleife = schleife + 1 time.sleep(0.1) # Do not waste processor time