Pepper: pass variable from Python to web JS

牧云@^-^@ 提交于 2019-12-24 06:05:39

问题


I'm programming an App for the Aldebaran's Pepper robot. I'm using Choregraphe and I made an html for displaying in robots tablet. I have made the boxes for displaying the web and I need to pass a variable from Python to the web Javascript.

Is there any way to do it?

The Python code is the same as the default of a Raise Event box, it receives a string "IMAGE" on his onStart input:

class MyClass(GeneratedClass):
    def __init__(self):
        GeneratedClass.__init__(self)
        pass

    def onLoad(self):
        self.memory = ALProxy("ALMemory")

    def onUnload(self):
        self.memory = None

    def onInput_onStart(self, p):
        self.memory.raiseEvent(self.getParameter("key"), p)
        self.onStopped(p)

    def onInput_onStop(self):
        self.onUnload() #~ it is recommended to call onUnload of this box in a onStop method, as the code written in onUnload is used to stop the box as well
        pass

And the Javascript code is this:

$('document').ready(function(){

    var session = new QiSession();

    session.service("ALMemory").done(function (ALMemory) {
        ALMemory.subscriber("PepperQiMessaging/totablet").done(function(subscriber) {
            $("#log").text("AAA");
            subscriber.signal.connect(toTabletHandler);
        });
    });

    function toTabletHandler(value) {
          $("#log").text("-> ");
    }
});

It enters the first #log but not the second of JS.


回答1:


yes, I think the webpage is loading too late to catch your event. One quick solution would be to send an event from Javascript when the page is ready, and wait for this event in your Python script. Once this event is received, then you know that the webpage is ready and you can send the "sendImage" event.




回答2:


I solved the problem. I put a delay box of 2 seconds between the show HTML box and the sendImage box like in the image below:

I think the problem was that the string that is send to tabled was sent before the web was prepared to receive it, and the delay of 2 seconds (with 1 it doesn't work) the page have time to prepare for receiving data.



来源:https://stackoverflow.com/questions/45876099/pepper-pass-variable-from-python-to-web-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!