Pepper: pass variable from Python to web JS

2019-08-02 17:09发布

问题:

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.