简体   繁体   中英

How do you get a module to function, and be recognized from within Choreographe on a Nao robot?

I'm using a Nao robot, and the python SDK, and I'm trying to create my own module for it. Right now it is just a dummy module with one function: getJoke() .

I have my file tellAJoke.py located under the path /home/nao/tellAJoke.py and I have updated the autoload.ini file to include the following:

[python]
/home/nao/tellAJoke.py

When I boot up the robot, it says what it normally does on startup, but also says what my getJoke() function returns, "This is a bad joke".

I'm not sure what I'm doing wrong here. When I ssh onto the robot, and run the code it runs just fine, but never when I want to import the module with ALProxy in Choreographe.

EDIT: I added the actual dummy code I have.

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

class JokerModule(ALModule):
    """Tells you a random joke"""
    def __init__(self, name):
        print "WE HAVE INITED"


        self.tts = ALProxy("ALTextToSpeech")
        ALModule.__init__(self, name)

        global memory
        memory = ALProxy("ALMemory")
        memory.subscribeToEvent("SayingJoke", "Joker", "getJoke")

    def getJoke(self, *_args):
        """Returns a joke"""
        self.tts.say("Joke time!")

def main():
    """main entry point"""
    pip = '192.168.1.104'
    pport = 9559

    myBroker = ALBroker("myBroker", '0.0.0.0', 0, pip, pport)

    global Joker
    Joker = JokerModule("Joker")
    speechProxy = ALProxy("ALTextToSpeech")
    Joker.getJoke()

if __name__ == '__main__':
    main()

Here is a guide on making services (aka "modules", but that term is confusing because it has another meaning in Python): http://doc.aldebaran.com/2-4/dev/libqi/guide/py-service.html (this doc is for NAOqi 2.4 but things should work mostly the same for 2.1, which is more often used on NAO)

But, you might want to try Robot Jumpstarter , that contains templates for various typical projects, including a python service (that works as explained in the document above).

clone it and run python jumpstart.py python-service tell-a-joke TellAJoke

... and it will generate a project that you can:

  • install on the robot witch Choregraphe
  • run in standalone with python tell-a-joke/app/scripts/tellajoke.py --qi-url your-naos-ip

... and in both cases you will be able to call it from from Choregraphe boxes etc.

(edit)

Now that you posted your code - in this specific case your problem is just that after Joker.getJoke(), your program reaches the end and terminates. The usual "modern" way of doing that would be with a qi.Application() that would .run() (all that is done in the jumpstarter template). You could do a while True: sleep(1) or something, which is not very pretty but would work (I recommend migrating to NAOqi 2, and instead of using ALProxy and ALBroker, use session.service and qi.Application ... the two are interoperable)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM