Getting Started with Python SDK (3) - Reacting to Events

Getting Started with Python SDK (3) - Reacting to Events

In this section, I want NAO to say "hello, human" every time it detects a human face. To achieve this, we need to use the "FaceDetected" event under the ALFacedetection model.

​ So we need to write a NAOqi model in Python. To create a model, we first need a middleware (Broker).

​ Here is an example:

# -*- encoding: UTF-8 -*-

"""每当侦测到人脸时都要说一声“你好,人类”"""

import sys
import time

from naoqi import ALProxy
from naoqi import ALBroker
from naoqi import ALModule

from optparse import OptionParser

pip = "nao.local"

# 用全局变量来存储"HumanGreeter"模型中的实例
HumanGreeter = None
memory = None

class HumanGreeterModule(ALModule):

    def __init__(self, name):
        ALmodule.__init__(self, name)
        #在这里不需要IP和端口,
        #因为我们可以在之后用自己写的中间件去连NAOqi中的中间件

        #创建一个ALTextToSpeech稍后使用
        self.tts = ALProxy("ALTextToSpeech")

        #使用FaceDetected事件
        global memory
        memory = ALProxy("ALMemory")
        memory.subscribeToEvent("FaceDetected",
            "HumanGreeter", "onFaceDetected")

    def onFaceDetected(self,*_args):
        #这个方法将会在每次侦测到人脸的时候被调用

        #当走路的时候不使用该事件
        #以避免多次重复
        memory.unsubscribeToEvent("FaceDetected", "HumanGreeter")

        self.tts.setLanguage("Chinese")
        self.tts.say("你好,人类")

        #再次使用上面那个方法
        memory.subscribeToEvent("FaceDetected",
            "HumanGreeter", "onFaceDetected")

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


    #我们需要用这个中间件来构建NAOqi模型并使用其他模型
    #这个中间件必须在运行期间始终有效
    myBroker = ALBroker("myBroker",
       "0.0.0.0",   # 监听所有的
       0,           # 找到一个空端口并使用它
       pip,         # 父类中间件IP
       pport)       # 父类中间件端口

    #注意:HumanGreeter必须是全局变量
    #传入结构体的名字必须是这个变量的名字
    global HumanGreeter
    HumanGreeter = HumanGreeterModule("HumanGreeter")

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        print
        print "Interrupted by user, shutting down"
        myBroker.shutdown()
        sys.exit(0)



if __name__ == "__main__":
    main()  

Some notes:

  • To make sure we are using global variables for the instance
  • Make sure the name of the struct we pass into ALModule is the name of its own variable
  • If we write a docstring for this method, and it doesn't start with an underscore, our class's method is automatically converted to a bound method. (The method of your class are automatically transform into bound methods, providing that you wrote a doc string for this method, and it does not start with an underscore .)
  • Once the ALBroker object is created, we need to keep this object valid for the subscribing to work. We also need the middleware to be active to create the proxy without having to specify an IP or port.
  • Scripts must have --pip and --pport to work

Added content: Ways to get a Python script to run on NAO:

​ Pass the script to NAO, such as /home/nao/reacting_to_events.py, then edit the autoload.ini file under /home/nao/naoqi/preferences, and add:

[python]
/home/nao/reacting_to_events.py

Note that NAOqi automatically sets the -pip and -pport values ​​when running the script.

Finally, run the script, then put your face in front of NAO and you will hear a "Hello, Human".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325689557&siteId=291194637