ROS学习--nao机器人开发

公司有几个nao机器人,主要是挂载语音识别合成等功能为了对外演示之用。

最近几天帮忙修改一个功能时看了nao的文档,地址如下:

http://doc.aldebaran.com/2-1/getting_started/helloworlds.html


nao机器人的系统也是基于linux的,其设计思路跟ROS有很多类似的地方,可以对照着学习加深理解。

ROS中的主要概念有节点(node),主题(topic),消息(message),服务(serveice)。除此之外,还有一个中心节点,就是启动时运行的roscore,负责维护已经启动的节点,维护各节点之间的消息传递等。

对应到nao机器人的naoqi系统上,首先它也有一个主节点即Broker,默认的端口就是本机IP,端口9559。而机器人的具体一个个功能节点,称之为module,如果我们要在自己的程序中调用机器人内置的module或第三方的module,只需要连接上主Broker,并且用Proxy实例化这个module就可以,这里说实例化未必准确,感觉类似PRC协议的远程调用。

那么我们想开发一个module并运行,该如何处理?

其实很方便,只要继承module就可以开发一个实现自己功能的module,然后创建一个Broker并将上面提到的主broker指定为,这个Broker的父Broker就可以,这样当你运行这个broker之后,你的broker会负责将本module注册到主broker当中。使用这个module的方式也和使用nao内置的module没有区别了。


下面是文档中的一个例子


# -*- encoding: UTF-8 -*-
""" Say 'hello, you' each time a human face is detected

"""

import sys
import time

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

from optparse import OptionParser

NAO_IP = "nao.local"


# Global variable to store the HumanGreeter module instance
HumanGreeter = None
memory = None


class HumanGreeterModule(ALModule):
    """ A simple module able to react
    to facedetection events

    """
    def __init__(self, name):
        ALModule.__init__(self, name)
        # No need for IP and port here because
        # we have our Python broker connected to NAOqi broker

        # Create a proxy to ALTextToSpeech for later use
        self.tts = ALProxy("ALTextToSpeech")

        # Subscribe to the FaceDetected event:
        global memory
        memory = ALProxy("ALMemory")
        memory.subscribeToEvent("FaceDetected",
            "HumanGreeter",
            "onFaceDetected")

    def onFaceDetected(self, *_args):
        """ This will be called each time a face is
        detected.

        """
        # Unsubscribe to the event when talking,
        # to avoid repetitions
        memory.unsubscribeToEvent("FaceDetected",
            "HumanGreeter")

        self.tts.say("Hello, you")

        # Subscribe again to the event
        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

    # 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 = 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: HumanGreeter must be a global variable
    # The name given to the constructor must be the name of the
    # variable
    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()



 


你可能感兴趣的:(机器人,Python)