Making an event module---reacting to an event

重看以前的代码,感觉有点简单了。这是一个简单的事件反应,每次检测到人脸的时候,机器人都会说“hello”。代码比较简单,注释里也都有解释。
#-*-encoding:UTF-8-*-

import sys
import time

from naoqi import ALProxy
from naoqi import ALBroker
from naoqi import ALModule
#做处理命令行参数使用
from optparse import OptionParser

NAO_IP="nao.local"

HumanGreeter=None
memory=None

class HumanGreeterModule(ALModule)://创建自己的模块,所有的模块都来自almodule
    "A simple module able to react to facedetection events"
    def __init__(self,name):
        ALModule.__init__(self,name)//创建自己模块的时候把集模块顺便一块初始化

        #create a proxy to ALTextSpeech
        self.tts=ALproxy("ALTextToSpeech")

        #subcribe to the facedetected event

        global memory
        memory=ALProxy("ALMemory")//memory 为自己创建模块的代理
        memory.subscribeToEvent("FaceDetected",//订阅事件的格式 依次是 事件名(eventname) 模块的名字 模块中的处理事件方法
                                "HumanGreeter",
                                "onFaceDetected")

    def onFaceDetected(self,*_args):
        #"this will be called each time a face is detected"
        #unsibcribe to the event when talking to aviod repetition
        
        memory.unsubscribeToEvent("FaceDetected",
                                  "HumanGreeter")
        self.tts.say("hello you")

        memory.subcribeToEvent("FaceDetected",
                               "HumanGreeter",
                               "onFaceDetected")


def main():

    parser=OptionParser()//创建OptionParser的对象,用于设置命令行参数
    parser.add_option("--pip",
                      help="Parent broker port, the ip address of your robot",
                      dest="pip")
    paser.add_option("--port",
                     help="Parent broker port, the port naoqi is listening to",
                     dest="port",
                     type="int")
    parser.set_defaults(
                     pip=NAO_IP,
                     port=9559)
    (opts,args_)=parser.parser_args()
    pip=opts.pip
    port=opts.port

    #we need this broker to beable to construct naoqi module
    #and subscribe to other modules
    #the broker must stay alive until the program exists
    myBroker=ALBroker("myBroker",
                      "0.0.0.0",
                      0,
                      pip,
                      port)
    global HumanGreeter
    HumanGreeter=HumanGreeterModule("HumanGreeeter")

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        print "interrupted by user,shutting down"
        myBroker.shutDown()
        sys.exit(0)
    
if __name__=="__main__":
    
    main()




    
    
                            

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值