1

Hello I want to know how to load a Dialog Topic file using python. I made sure that the file path is right, but it keeps saying that it isn't. I have also used the tutorials in NAO 2.1's documentation ALDialog and ALModule Please send me a code that works or tell me the error. I tried using the following code:

NAO_IP = "nao.local"

dialog_p = None
ModuleInstance = None

class NaoFalanteModule(ALModule):

    def __init__(self, name):
        ALModule.__init__(self, name)
        self.tts = ALProxy("ALTextToSpeech")
        self.tts.setLanguage("Brazilian")

        global dialog_p
        try:
            dialog_p = ALProxy("ALDialog")
        except Exception, e:
            print "Error dialog"
            print str(e)
            exit(1)
        dialog_p.setLanguage("Brazilian")
        self.naoAlc()

    def naoAlc(self):

        topf_path = "/simpleTestes/diaSimples/testeSimples_ptb.top"
        topf_path = topf_path.decode("utf-8")
        topic = dialog_p.loadTopic(topf_path.encode("utf-8"))
        # Start dialog
        dialog_p.subscribe("NaoFalanteModule")
        dialog_p.activateTopic(topic)
        raw_input(u"Press  'Enter' to exit.")
        dialog_p.unload(topic)
        dialog_p.unsubscribe

def main():
    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

    myBroker = ALBroker("myBroker",
                        "0.0.0.0",  
                        0, 
                        pip,  
                        pport)  
    global ModuleInstance
    ModuleInstance = NaoFalanteModule("ModuleInstance")

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

if __name__ == "__main__":
    main()
robsmayer
  • 15
  • 4
  • Welcome to StackOverflow. What is the actual file path of your .top file? And why are you decoding the path and then encoding it? – MandyShaw Aug 09 '18 at 20:30
  • @MandyShaw Hello, thank you. Well it is, but according to the other answer it is not. So I'll try with it. I decoded and encoded because I was desperate to find the error so I tried many things. Anyway thank you for the welcome :) – robsmayer Aug 10 '18 at 02:03

1 Answers1

1

The path to the topic needs to be the absolute path to that file, whereas you're passing a relative path compared to your current execution directory. The reason is that ALDialog is a separate service running in it's own process and knows nothing about the execution context of whoever is calling it.

And the program .top file must be uploaded to the robot using Choregraphe.

So, your absolute path in this case might be something like

topf_path = "/home/nao/simpleTestes/diaSimples/testeSimples_ptb.top"

... or if you want to be a bit cleaner, if you know your script is being executed at the root of your application package, use os.path:

topf_path = os.path.abspath("diaSimples/testeSimples_ptb.top")
robsmayer
  • 15
  • 4
Emile
  • 2,946
  • 2
  • 19
  • 22