ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Using roslibjs to read a ROS parameter

asked 2014-03-09 09:40:49 -0500

Pi Robot gravatar image

updated 2014-03-09 10:13:56 -0500

Hello,

I have some Javascript code that uses rosbridge_suite (Hydro debian install under Ubuntu 12.04) and roslibjs (latest version) to read in a parameter value from the rosbridge server and then set its value in a form field. It seems I don't understand the timing of when the parameter is read or the sequence in which Javascript functions are executed (or both) because I'm not getting the result I expected.

My code (included below) does the following:

  • sets a default value for the parameter
  • reads the parameter from the rosbridge server and, if not null, overrides the default value
  • sets the value of a form field to the parameter value

What I see on my form is that I always get the default value, not the value from the parameter server. If I put some log statements in my script, I see that the form value is getting updated before the parameter value is read via roslib even though the roslib code comes first. I am also verifying that the value is being read from the parameter server correctly.

Here now is the relevant snippet:

var maxLinearSpeed = 0.2;

// Create a Param object for the max linear speed
var maxLinearSpeedParam = new ROSLIB.Param({
     ros : ros,
     name :  '/maxLinearSpeed'
    });

// Get the value of the max linear speed paramater
maxLinearSpeedParam.get(function(value) {
   if (value != null) {
       maxLinearSpeed = value;
       console.log(maxLinearSpeed);
   }
});

var formElement = document.getElementById('maxLinearSpeedDisplay');
formElement.innerHTML = maxLinearSpeed;

So if I set the value of the maxLinearSpeed parameter to 0.5 on the rosbridge server, then run my script, I always see the default value of 0.2 in the form field labeled 'maxLinearSpeedDisplay' instead of 0.5 as I expected. Yet the statement console.log(value) above displays the correct value of 0.5.

Any idea what I am doing wrong?

Thanks,
patrick

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-03-09 11:50:52 -0500

rtoris288 gravatar image

All of the roslibjs calls are asynchronous; they are event driven. When you call 'get', you are passing in a callback function that will be called whenever rosbridge responds to you request. This prevents blocking and waiting which will lock up the entire browser. You could move your var formElement = document.getElementById('maxLinearSpeedDisplay'); formElement.innerHTML = maxLinearSpeed; inside your callback function so that the HTML is updated whenever a response comes back.

edit flag offensive delete link more

Comments

Ah ha! Now I understand. Many thanks--and yes, your suggestion does the trick. Thanks!

Pi Robot gravatar image Pi Robot  ( 2014-03-09 13:23:36 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-03-09 09:40:49 -0500

Seen: 1,787 times

Last updated: Mar 09 '14