def events_and_callbacks_qi_framework(): """Example of getting callbacks for events""" # ALMemory acts as the hub for the distribution of event notifications. # Source: https://developer.softbankrobotics.com/nao6/naoqi-developer-guide/naoqi-apis/naoqi-core/almemory # Example: https://developer.softbankrobotics.com/nao6/naoqi-developer-guide/other-tutorials/python-sdk-tutorials/python-sdk-examples/vision/face # Create a broker # TODO(TK): why? naoqi.ALBroker("pythonBroker", IP_ME, PORT_ME, IP_ROBOT, PORT_ROBOT) proxy_memory = naoqi.ALProxy("ALMemory", IP_ROBOT, PORT_ROBOT) # Register callback: def mycallback(key, value): print("qi callback: key={}, value={}".format(key, value)) sess = proxy_memory.session() mem = sess.service("ALMemory") sub = mem.subscriber("FaceDetected") sub.signal.connect(functools.partial(mycallback, "FaceDetected")) # Raise an event: proxy_memory.raiseEvent("FaceDetected", str(datetime.datetime.now())) proxy_memory.raiseEvent("AnotherEvent", str(datetime.datetime.now())) time.sleep(0.1) # give it some time to process
def main(): """ Main entry point """ parser = OptionParser() parser.add_option("--pip", help="Parent broker port. The IP address or your robot", dest="pip") parser.add_option( "--pport", help="Parent broker port. The port NAOqi is listening to", dest="pport", type="int") parser.set_defaults(pip=NAO_IP, pport=NAO_PORT) (opts, args_) = parser.parse_args() pip = opts.pip pport = opts.pport # We need this broker to be able to construct # NAOqi modules and subscribe to other modules # The broker must stay alive until the program exists myBroker = naoqi.ALBroker( "myBroker", "0.0.0.0", # listen to anyone 0, # find a free port and use it pip, # parent broker IP pport) # parent broker port try: p = ALProxy("SpeechRecognition") p.exit() # kill previous instance, useful for developing ;) except: pass # Reinstantiate module # Warning: SpeechRecognition must be a global variable # The name given to the constructor must be the name of the # variable global SpeechRecognition SpeechRecognition = SpeechRecognitionModule("SpeechRecognition", pip) # uncomment for debug purposes # usually a subscribing client will call start() from ALProxy #SpeechRecognition.start() #SpeechRecognition.startRecording() #SpeechRecognition.calibrate() #SpeechRecognition.enableAutoDetection() #SpeechRecognition.startRecording() try: while True: time.sleep(1) except KeyboardInterrupt: print print "Interrupted by user, shutting down" myBroker.shutdown() sys.exit(0)
def main(): """ Main entry point """ parser = OptionParser() parser.add_option("--pip", help="Parent broker port. The IP address or your robot", dest="pip") parser.add_option("--pport", help="Parent broker port. The port NAOqi is listening to", dest="pport", type="int") parser.set_defaults( pip=NAO_IP, pport=9559) (opts, args_) = parser.parse_args() pip = opts.pip pport = opts.pport # We need this broker to be able to construct # NAOqi modules and subscribe to other modules # The broker must stay alive until the program exists myBroker = naoqi.ALBroker("myBroker", "0.0.0.0", # listen to anyone 0, # find a free port and use it pip, # parent broker IP pport) # parent broker port # Warning: SoundReceiver must be a global variable # The name given to the constructor must be the name of the # variable global SoundReceiver SoundReceiver = SoundReceiverModule("SoundReceiver", pip) SoundReceiver.start() try: while True: time.sleep(1) except KeyboardInterrupt: print print "Interrupted by user, shutting down" myBroker.shutdown() sys.exit(0)
def events_and_callbacks_naoqi(): """Example of getting callbacks for events""" # ALMemory acts as the hub for the distribution of event notifications. # Source: https://developer.softbankrobotics.com/nao6/naoqi-developer-guide/naoqi-apis/naoqi-core/almemory # Example: https://developer.softbankrobotics.com/nao6/naoqi-developer-guide/other-tutorials/python-sdk-tutorials/python-sdk-examples/vision/face class MyModule(naoqi.ALModule): """Mandatory docstring""" def myCallback(self, key, value, message): """Mandatory docstring""" print("Callback: key={}, value={}, message={}".format( key, value, message)) # Create a broker # TODO(TK): why? naoqi.ALBroker("pythonBroker", IP_ME, PORT_ME, IP_ROBOT, PORT_ROBOT) # Create an instance of our callback handling module, and add it to global scope: global myModule # needs to be in global scope myModule = MyModule("myModule") # [naoqi] Subscribe to events: proxy_memory = naoqi.ALProxy("ALMemory", IP_ROBOT, PORT_ROBOT) print "FaceDetected events before={}".format( proxy_memory.getEventHistory("FaceDetected")) proxy_memory.subscribeToEvent("FaceDetected", "myModule", "myCallback") # qi framework def mycallback(key, value): print("qi callback: key={}, value={}".format(key, value)) sess = proxy_memory.session() mem = sess.service("ALMemory") sub = mem.subscriber("FaceDetected") sub.signal.connect(functools.partial(mycallback, "FaceDetected")) # Raise an event: proxy_memory.raiseEvent("FaceDetected", str(datetime.datetime.now())) proxy_memory.raiseEvent("AnotherEvent", str(datetime.datetime.now())) print "FaceDetected events after={}".format( proxy_memory.getEventHistory("FaceDetected")) time.sleep(0.1) # give it some time to process
def main(): """ Main entry point """ parser = OptionParser() parser.add_option("--pip", help="Parent broker port. The IP address or your robot", dest="pip") parser.add_option( "--pport", help="Parent broker port. The port NAOqi is listening to", dest="pport", type="int") parser.set_defaults(pip=NAO_IP, pport=9559) (opts, args_) = parser.parse_args() pip = opts.pip pport = opts.pport # We need this broker to be able to construct # NAOqi modules and subscribe to other modules # The broker must stay alive until the program exists myBroker = naoqi.ALBroker( "myBroker", "0.0.0.0", # listen to anyone 0, # find a free port and use it pip, # parent broker IP pport) # parent broker port try: ## global SoundReceiver global SpeechRecogModule global SoundLocModule global VisionRecogModule try: TTSProxy = naoqi.ALProxy("ALTextToSpeech", pip, pport) # motionProxy = naoqi.ALProxy("ALMotion", pip, pport) MemoryProxy = naoqi.ALProxy("ALMemory", pip, pport) #naoqi.ALProxy('ALBasicAwareness',pip, pport).stopAwareness() SoundReceiver = SoundReceiverModule('SoundReceiver', pip, pport) SpeechRecogModule = SpeechRecoModule('SpeechRecogModule', pip, pport) VisionRecogModule = VisionRecoModule('VisionRecogModule', pip, pport) # SoundLocModule = SoundLocalizationModule('SoundLocModule', pip, pport) except Exception, e: print "Could not create proxy" print "Error was: ", e """ #Detecta 'Ayuda' y guarda en qué dirección la detectó """ SpeechRecogModule.start() # SoundLocModule.start() TTSProxy.say('Pidame ayuda') MemoryProxy.insertData("speechrecog", "None") # MemoryProxy.insertData("azimuth", 0.0) #MuevaLaCabeza(0.0) while True: readdata = '' try: readdata = MemoryProxy.getData("speechrecog") # azimuth = float(MemoryProxy.getData("azimuth")) except Exception as e: print "readdata error", e # azimuth = 0.0 if readdata == "ayuda": #MuevaLaCabeza(11) # print azimuth MemoryProxy.insertData("speechrecog", "None") break TTSProxy.say('O key') SpeechRecogModule.stop() # SoundLocModule.stop() # angle = azimuth # h = 0.2 # x = h/sin(angle) # y = h/cos(angle) # motionProxy.moveTo(x, y, angle) """ #Se mueve hacia la dirección del sonido detectado """ """ #Detecta ukulele """ VisionRecogModule.start() TTSProxy.say('Muéstreme el ukelele') MemoryProxy.insertData("visionrecog", "None") while True: readdata = '' try: readdata = MemoryProxy.getData("visionrecog") except Exception as e: print "readdata error", e if readdata == "ukulele": MemoryProxy.insertData("visionrecog", "None") break VisionRecogModule.stop() TTSProxy.say('He visto el ukelele, ¡Afinemos!') """ #Empieza a escuchar cada cuerda, calcula la frecuencia más cercana y dar retroalimentación para afinarla """ SoundReceiver.start() time.sleep(SLEEP_TIME) i = 0 afinando = True while (afinando): TTSProxy.say("Toque la cuerda número " + str(i + 1) + ", que debe ser la nota " + NOTES[i]) time.sleep(SLEEP_TIME) try: lastfreq = [ MemoryProxy.getData("freqs" + str(j)) for j in range(0, 50) ] print "ultimasFreq :", str(lastfreq) except RuntimeError, e: # catch exception print "error read data", e lastfreq = 0 if lastfreq != 0: roundedfreqs = np.rint(lastfreq) print roundedfreqs medianfreq = mode(roundedfreqs) print(medianfreq.mode[0]) #medianfreq = np.median(lastfreq) TTSProxy.say("La frecuencia que escuché es de" + str(medianfreq.mode[0]) + "hercios") medianfreq = int(medianfreq.mode[0]) try: logdiffarray = np.zeros(3) print logdiffarray logdiffarray[0] = log_diff(medianfreq, FREQ[i]) logdiffarray[1] = log_diff(medianfreq, FREQ[i] / 2) logdiffarray[2] = log_diff(medianfreq, FREQ[i] * 2) print logdiffarray logdiff = 1 for diff in logdiffarray: if np.abs(diff) < np.abs(logdiff): logdiff = diff print logdiff except Exception, e: print e logdiff = 1 if np.abs(logdiff) <= PITCH_THRESHHOLD: TTSProxy.say("La cuerda está afinada Muy Bien!") if (i == len(NOTES) - 1): afinando = False else: i += 1 else: if logdiff > 0: TTSProxy.say("La cuerda está muy alta, bájele") else: TTSProxy.say("La cuerda está muy baja, súbale")
def __init__(self, name='NaoBroker', nao_ip='localhost', nao_port=9559): self.name = name #Make sure the name you pass to the constructor of ALModule matches the name of your variable. if self.NaoBroker == None: self.NaoBroker = naoqi.ALBroker(self.name, "0.0.0.0", 0, nao_ip, nao_port) print "Broker ", self.NaoBroker, " initialized at ", nao_ip, " ", nao_port if self.naoqi == None: self.naoqi = naoqi.ALModule.__init__(self, name) try: self.motionProxy = naoqi.ALProxy("ALMotion") self.robotConfig = self.motionProxy.getRobotConfig() for i in range(len(self.robotConfig[0])): log( str(self.robotConfig[0][i]) + ": " + str(self.robotConfig[1][i]), 0) except RuntimeError as e: log("Error when creating proxies:", 3) log(str(e), 3) try: self.memoryProxy = naoqi.ALProxy("ALMemory") except RuntimeError as e: log("Error when creating proxies:", 3) log(str(e), 3) try: self.tts = naoqi.ALProxy("ALTextToSpeech") except RuntimeError as e: log("Error when creating proxies:", 3) log(str(e), 3) try: self.faceProxy = naoqi.ALProxy("ALFaceDetection") except RuntimeError as e: log("Error when creating proxies:", 3) log(str(e), 3) self.faceProxy = None try: self.ledProxy = naoqi.ALProxy("ALLeds") except RuntimeError as e: log("Error when creating proxies:", 3) log(str(e), 3) self.ledProxy = None try: self.trackfaceProxy = naoqi.ALProxy("ALFaceTracker") except RuntimeError as e: log("Error when creating proxies:", 3) log(str(e), 3) self.trackfaceProxy = None try: self.speechProxy = naoqi.ALProxy("ALSpeechRecognition") except RuntimeError as e: log("Error when creating proxies:", 3) log(str(e), 3) self.speechProxy = None try: self.audioProxy = naoqi.ALProxy("ALAudioDevice") except RuntimeError as e: log("Error when creating proxies:", 3) log(str(e), 3) self.audioProxy = None try: self.cameraProxy = naoqi.ALProxy("ALVideoDevice") except RuntimeError as e: log("Error when creating proxies:", 3) log(str(e), 3) self.cameraProxy = None try: self.playProxy = naoqi.ALProxy("ALAudioPlayer") except RuntimeError as e: log("Error when creating proxies:", 3) log(str(e), 3) self.playProxy = None try: self.videoProxy = naoqi.ALProxy("ALVisionToolbox") except RuntimeError as e: log("Error when creating proxies:", 3) log(str(e), 3) self.videoProxy = None try: self.sonarProxy = naoqi.ALProxy("ALSonar") except RuntimeError as e: log("Error when creating proxies:", 3) log(str(e), 3) self.sonarProxy = None try: self.sensorsProxy = naoqi.ALProxy("ALSensors") except RuntimeError as e: log("Error when creating proxies:", 3) log(str(e), 3) self.sensorsProxy = None try: self.postureProxy = naoqi.ALProxy("ALRobotPosture") self.postureList = self.postureProxy.getPostureList() log(str(self.postureList), 0) except RuntimeError as e: log("Error when creating proxies:", 3) log(str(e), 3) self.postureProxy = None
def main(): """ Main entry point """ parser = OptionParser() parser.add_option("--pip", help="Parent broker port. The IP address or your robot", dest="pip") parser.add_option("--pport", help="Parent broker port. The port NAOqi is listening to", dest="pport", type="int") parser.set_defaults( pip=NAO_IP, pport=NAO_PORT) (opts, args_) = parser.parse_args() pip = opts.pip pport = opts.pport # We need this broker to be able to construct # NAOqi modules and subscribe to other modules # The broker must stay alive until the program exists myBroker = naoqi.ALBroker("myBroker", "0.0.0.0", # listen to anyone 0, # find a free port and use it pip, # parent broker IP pport) # parent broker port try: p = ALProxy("BaseSpeechReceiverModule") p.exit() # kill previous instance except: pass # Reinstantiate module # Warning: ReceiverModule must be a global variable # The name given to the constructor must be the name of the # variable global BaseSpeechReceiverModule BaseSpeechReceiverModule = BaseSpeechReceiverModule("BaseSpeechReceiverModule", pip) BaseSpeechReceiverModule.start() if(False): #one-shot recording for at least 5 seconds SpeechRecognition = ALProxy("SpeechRecognition") SpeechRecognition.start() SpeechRecognition.setHoldTime(5) SpeechRecognition.setIdleReleaseTime(1.7) SpeechRecognition.setMaxRecordingDuration(10) SpeechRecognition.startRecording() else: # auto-detection SpeechRecognition = ALProxy("SpeechRecognition") SpeechRecognition.start() SpeechRecognition.setHoldTime(2.5) SpeechRecognition.setIdleReleaseTime(1.0) SpeechRecognition.setMaxRecordingDuration(10) SpeechRecognition.setLookaheadDuration(0.5) #SpeechRecognition.setLanguage("de-de") #SpeechRecognition.calibrate() SpeechRecognition.setAutoDetectionThreshold(5) SpeechRecognition.enableAutoDetection() #SpeechRecognition.startRecording() try: while True: time.sleep(1) except KeyboardInterrupt: print print "Interrupted by user, shutting down" myBroker.shutdown() sys.exit(0)
def main(): """ Main entry point """ parser = OptionParser() parser.add_option("--pip", help="Parent broker port. The IP address or your robot", dest="pip") parser.add_option( "--pport", help="Parent broker port. The port NAOqi is listening to", dest="pport", type="int") parser.set_defaults(pip=NAO_IP, pport=NAO_PORT) (opts, args_) = parser.parse_args() pip = opts.pip pport = opts.pport # We need this broker to be able to construct # NAOqi modules and subscribe to other modules # The broker must stay alive until the program exists myBroker = naoqi.ALBroker( "myBroker", "0.0.0.0", # listen to anyone 0, # find a free port and use it pip, # parent broker IP pport) # parent broker port try: p = ALProxy("ReceiverTestModule") p.exit() # kill previous instance except: pass # Reinstantiate module # Warning: ReceiverModule must be a global variable # The name given to the constructor must be the name of the # variable global ReceiverTestModule ReceiverTestModule = ReceiverTestModule("ReceiverTestModule", pip) ReceiverTestModule.start() SpeechRecognition = ALProxy("SpeechRecognition") SpeechRecognition.start() #SpeechRecognition.setLanguage("de-de") SpeechRecognition.calibrate() SpeechRecognition.enableAutoDetection() # ROS node rospy.init_node('pepper_listen', anonymous=True) try: while not rospy.is_shutdown(): time.sleep(1) except KeyboardInterrupt: print "Interrupted by user, shutting down" except rospy.ROSInterruptException: print "Interrupted by ROS, shutting down" pass myBroker.shutdown() sys.exit(0)