(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Visualizing Interactive Markers

Description: This tutorial shows you how to visualize interactive markers using ros3djs.

Keywords: ros3djs, web interface, javascript, markers, Robot Web Tools

Tutorial Level: BEGINNER

Interactive Marker Example

In this tutorial, we show how to visualize interactive markers in the browser. To begin, create a new HTML document and copy the follow code sample into it.

The HTML Code

   1 <!DOCTYPE html>
   2 <html>
   3 <head>
   4 <meta charset="utf-8" />
   5 
   6 <script type="text/javascript" src="http://static.robotwebtools.org/threejs/current/three.min.js"></script>
   7 <script type="text/javascript" src="http://static.robotwebtools.org/EventEmitter2/current/eventemitter2.min.js"></script>
   8 <script type="text/javascript" src="http://static.robotwebtools.org/roslibjs/current/roslib.min.js"></script>
   9 <script type="text/javascript" src="http://static.robotwebtools.org/ros3djs/current/ros3d.min.js"></script>
  10 
  11 <script type="text/javascript" type="text/javascript">
  12   /**
  13    * Setup all visualization elements when the page is loaded.
  14    */
  15   function init() {
  16     // Connect to ROS.
  17     var ros = new ROSLIB.Ros({
  18       url : 'ws://localhost:9090'
  19     });
  20 
  21     // Create the main viewer.
  22     var viewer = new ROS3D.Viewer({
  23       divID : 'markers',
  24       width : 800,
  25       height : 600,
  26       antialias : true
  27     });
  28 
  29     // Setup a client to listen to TFs.
  30     var tfClient = new ROSLIB.TFClient({
  31       ros : ros,
  32       angularThres : 0.01,
  33       transThres : 0.01,
  34       rate : 10.0,
  35       fixedFrame : '/rotating_frame'
  36     });
  37 
  38     // Setup the marker client.
  39     var imClient = new ROS3D.InteractiveMarkerClient({
  40       ros : ros,
  41       tfClient : tfClient,
  42       topic : '/basic_controls',
  43       camera : viewer.camera,
  44       rootObject : viewer.selectableObjects
  45     });
  46   }
  47 </script>
  48 </head>
  49 
  50 <body onload="init()">
  51   <h1>Simple Marker Example</h1>
  52   <div id="markers"></div>
  53 </body>
  54 </html>

Code Explanation

Now that we have an example, let's look at each piece.

   6 <script type="text/javascript" src="http://static.robotwebtools.org/threejs/current/three.min.js"></script>
   7 <script type="text/javascript" src="http://static.robotwebtools.org/EventEmitter2/current/eventemitter2.min.js"></script>
   8 <script type="text/javascript" src="http://static.robotwebtools.org/roslibjs/current/roslib.min.js"></script>
   9 <script type="text/javascript" src="http://static.robotwebtools.org/ros3djs/current/ros3d.min.js"></script>

We first need to import all of the required JavaScript files. Here, we use the Robot Web Tools CDN.

  17     var ros = new ROSLIB.Ros({
  18       url : 'ws://localhost:9090'
  19     });

Next, we need to create a Ros node object to communicate with a rosbridge v2.0 server. In this example, the script will connect to localhost on the default port of 9090.

  22     var viewer = new ROS3D.Viewer({
  23       divID : 'markers',
  24       width : 800,
  25       height : 600,
  26       antialias : true
  27     });

We then need to create a 3D viewer. We provide the dimensions as well as the HTML div where the viewer will be placed.

  30     var tfClient = new ROSLIB.TFClient({
  31       ros : ros,
  32       angularThres : 0.01,
  33       transThres : 0.01,
  34       rate : 10.0,
  35       fixedFrame : '/rotating_frame'
  36     });

We then create a TF client. This client will subscribe to changes in the TF tree and update the scene appropriately. In this case, we will consider /rotating_frame to be the fixed frame. We also set change thresholds to throttle the rate at which changes are published.

  39     var imClient = new ROS3D.InteractiveMarkerClient({
  40       ros : ros,
  41       tfClient : tfClient,
  42       topic : '/basic_controls',
  43       camera : viewer.camera,
  44       rootObject : viewer.selectableObjects
  45     });

This section of code creates the actual InteractiveMarkerClient object. Here we provide the Ros node object and TF client created above, the viewer's scene to render to, a reference to the camera, and the name of the interactive marker topic to render.

  50 <body onload="init()">

It is important to remember to call the init() function when this page is loaded. This will call the JavaScript code discussed above.

  52   <div id="markers"></div>

Within the HTML <body> we create a div with a matching ID for the viewer. This is where the interactive markers will be shown.

Running the Example

At this point we are ready to run the example. To do so, you will need to have interactive_marker_tutorials, interactive_marker_proxy, rosbridge_server, and tf2_web_republisher installed. Refer to their respective Wiki pages for installation instructions, or simply install their latest builds from the Ubuntu repositories.

Simply launch the necessary nodes with the following:

  •    1 roscore
       2 rosrun interactive_marker_tutorials basic_controls
       3 rosrun interactive_marker_proxy proxy topic_ns:=/basic_controls target_frame:=/rotating_frame
       4 rosrun tf2_web_republisher tf2_web_republisher
       5 roslaunch rosbridge_server rosbridge_websocket.launch
    

Finally, you are now ready to bring up your HTML page in a web browser. The result should be a page that looks similar to the following:

Support

Please send bug reports to the GitHub Issue Tracker. Feel free to contact us at any point with questions and comments.

Wiki: ros3djs/Tutorials/VisualizingInteractiveMarkers (last edited 2019-08-08 18:39:23 by DavidMillard)