ROS學習--nao機器人開發

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/roboyun/article/details/50976674

公司有幾個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()



 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章