Programming- Nao Robot by choregraphe
#! /usr/bin/python import naoqi import time ipaddress = "192.168.1.106" #Nao's IP address testtype = 6 # a broker is needed for any event handling broker = naoqi.ALBroker("pythonBroker", "0.0.0.0", 9999, ipaddress, 9559) # code example to say anything talkproxy = naoqi.ALProxy("ALTextToSpeech", ipaddress, 9559) talkproxy.say("Hello") # code example to retrieve sensor values if testtype == 2: sonarproxy = naoqi.ALProxy("ALSonar", ipaddress, 9559) memoryproxy = naoqi.ALProxy("ALMemory", ipaddress, 9559) sonarproxy.subscribe("MyApplication") leftdist = memoryproxy.getData("Device/SubDeviceList/US/Left/Sensor/Value") rightdist = memoryproxy.getData("Device/SubDeviceList/US/Right/Sensor/Value") print "Left distance = ", leftdist, "Right distance = ", rightdist sonarproxy.unsubscribe("MyApplication") # code example to move to one of the standard poses if testtype == 3: postureproxy = naoqi.ALProxy("ALRobotPosture", ipaddress, 9559) postureproxy.goToPosture("StandInit", 1.0) #code example to walk if testtype == 4: motionproxy = naoqi.ALProxy("ALMotion", ipaddress, 9559) t = True motionproxy.setWalkArmsEnabled(t,t) motionproxy.moveTo(-0.2, 0.0, 0.0) # walk to a particular destination time.sleep(2) motionproxy.setWalkTargetVelocity(1.0,0.0,0.0,1.0) # walk in direction til stop time.sleep(4) motionproxy.stopMove() # code example to detect sound direction if testtype == 5: class myModule(naoqi.ALModule): """callback module""" def soundChanged(self, strVarName, value, strMessage): """callback function""" if value[1][0] < 0: print "Turn right", -value[1][0], "radians" else: print "Turn left", value[1][0], "radians" pythonModule = myModule("pythonModule") soundproxy = naoqi.ALProxy("ALAudioSourceLocalization", ipaddress, 9559) memoryproxy = naoqi.ALProxy("ALMemory", ipaddress, 9559) memoryproxy.subscribeToEvent("ALAudioSourceLocalization/SoundLocated", "pythonModule", "soundChanged") soundproxy.subscribe("MyApplication") time.sleep(10) soundproxy.unsubscribe("MyApplication") memoryproxy.unsubscribeToEvent("ALAudioSourceLocalization/SoundLocated", "pythonModule", "soundChanged") # code example to recognize words if testtype == 6: class myModule(naoqi.ALModule): """callback module""" def wordChanged(self, strVarName, value, strMessage): """callback function""" if value[1] > 0.6: print "Word recognized is", value[0] pythonModule = myModule("pythonModule") speechproxy = naoqi.ALProxy("ALSpeechRecognition", ipaddress, 9559) memoryproxy = naoqi.ALProxy("ALMemory", ipaddress, 9559) #speechproxy.setWordListAsVocabulary(["yes", "no"]) t = True speechproxy.setVocabulary(["yes", "no"], t) memoryproxy.subscribeToEvent("WordRecognized", "pythonModule", "wordChanged") speechproxy.subscribe("MyApplication") time.sleep(15) speechproxy.unsubscribe("MyApplication") memoryproxy.unsubscribeToEvent("WordRecognized", "pythonModule", "wordChanged") talkproxy.say("Goodbye")