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

How can I publish an integer and string (Int16 & String) using roslibjs?

asked 2013-06-26 00:05:32 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

hi

I am recently learning robotic web tools.In git has some samples. I want to publish an indeger / string message .On refreshing the browser I want to get the string message in command prompt. But In my code it is not publish anything right now. Any helps are appreciated.

I run

 roscore
 rostopic echo /buttons
 roslaunch rosbridge_server rosbridge_websocket.launch

I am use the following code

 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="utf-8" />
 <script src="../include/EventEmitter2/eventemitter2.js"></script>
 <script src="../build/roslib.js"></script>

 <script>
   // Connecting to ROS
   // -----------------
   var ros = new ROSLIB.Ros();

   // If there is an error on the backend, an 'error' emit will be emitted.
   ros.on('error', function(error) {
     console.log(error);
   });

   // Find out exactly when we made a connection.
   ros.on('connection', function() {
     console.log('Connection made!');
   });

   // Create a connection to the rosbridge WebSocket server.
   ros.connect('ws://localhost:9090');

   // Publishing a Topic
   // ------------------

   // First, we create a Topic object with details of the topic's name and      message type.
   var testStr = new ROSLIB.Topic({
     ros : ros,
     name : '/buttons',
     messageType : 'std_msgs/String'
   });
   // Then we create the payload to be published. The object we pass in to ros.Message matches the 
   // fields defined in the geometry_msgs/Twist.msg definition.
   var str = new ROSLIB.Message(
         a:'hello'
    );

   // And finally, publish.
   testStr.publish(str);

 </script>
 </head>

 <body>
   <h1></h1>
   <p></p>
   <ol>
     <li><tt>roscore</tt></li>
     <li><tt>rostopic echo /buttons</tt></li>
     <li><tt>roslaunch rosbridge_server rosbridge_websocket.launch</tt></li>
   </ol>
 </body>
 </html>

My Doubts

1) Is it there in any problem of the syntax I used?

2) how can I publish Int16 messages?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
5

answered 2013-06-26 03:58:05 -0600

rtoris288 gravatar image

updated 2013-06-26 03:59:44 -0600

You have both a syntax error and a bug in your message definition. To catch syntax errors, it's useful to run the JavaScript error console your web browser provides.

var str = new ROSLIB.Message({
  a:'hello'
});

The syntax error is that you are missing brackets { } around the message data.

Secondly, you have defined your message type as std_msgs/String which does not have a field called a (see http://ros.org/doc/api/std_msgs/html/msg/String.html). Instead, the field is called data:

var str = new ROSLIB.Message({
  data : 'hello'
});

With the above change, everything should work.

To publish an std_msgs/Int16 message, simply change your data type:

var testInt = new ROSLIB.Topic({
  ros : ros,
  name : '/ints',
  messageType : 'std_msgs/Int16'
});

var int = new ROSLIB.Message({
  data : 5
});

testInt.publish(int);
edit flag offensive delete link more

Comments

1

hi ... thank you very much.. I have one more doubt... how can I publish a variable as an integer message...ie var int = new ROSLIB.Message({ data : int_variable }); instead of 5 just want to use a variable

unais gravatar image unais  ( 2013-06-27 00:10:35 -0600 )edit

It helps me to to publish a std_msgs/Int32, I was a little lost with this work. Thank you :D

Wilianson gravatar image Wilianson  ( 2016-11-07 11:46:59 -0600 )edit

Question Tools

Stats

Asked: 2013-06-26 00:05:32 -0600

Seen: 4,788 times

Last updated: Jun 26 '13