Using roslibjs to read a ROS parameter
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