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

Why do I need to enter a loop to display a marker?

asked 2014-12-11 03:10:40 -0500

VictorLamoine gravatar image

I followed the tutorial on publishing markers in Rviz, I am wondering why I need to enter a loop to publish a marker that has an infinite lifetime (ros::Duration)

I would expect the following code to display the marker but it does not:

#include <ros/ros.h>
#include <visualization_msgs/Marker.h>

int main(int argc, char **argv)
{
  ros::init(argc, argv, "ros_test");
  ros::NodeHandle node;
  ros::Rate r(1);

  // Test marker
  ros::Publisher marker_pub = node.advertise<visualization_msgs::Marker>("my_marker", 1);
  visualization_msgs::Marker marker;

  marker.header.frame_id = "/base_link";
  marker.header.stamp = ros::Time::now();
  marker.type = visualization_msgs::Marker::CUBE;
  marker.action = visualization_msgs::Marker::ADD;
  marker.lifetime = ros::Duration();
  // Pose
  marker.pose.position.x = 0.5;
  marker.pose.position.y = 0;
  marker.pose.position.z = 0;
  marker.pose.orientation.x = 0;
  marker.pose.orientation.y = 0;
  marker.pose.orientation.z = 0;
  marker.pose.orientation.w = 1;
  // Scale
  marker.scale.x = marker.scale.y = marker.scale.z = 0.1;
  // Color
  marker.color.r = marker.color.b = 0.2;
  marker.color.g = 0.6f;
  marker.color.a = 1.0;
  marker_pub.publish(marker);
  return 0;
}

Adding a loop enables to visualize the marker:

  while (ros::ok())
  {
    marker_pub.publish(marker);
  }

I don't want to loop; how to publish the marker once in Rviz?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2014-12-11 03:12:51 -0500

VictorLamoine gravatar image

updated 2015-01-29 08:16:39 -0500

The problem is not that the marker is not published; the problem is that Rviz didn't have time to be started at the first marker publishing.

The solution is to give time to Rviz to start before publishing anything:

  // Color
  marker.color.r = marker.color.b = 0.2;
  marker.color.g = 0.6f;
  marker.color.a = 1.0;
  sleep(5); // TODO: Give time to Rviz to be fully started
  marker_pub.publish(marker);
  return 0;

As @dornhege mentioned this is not ideal. Another option is to use a latched publisher.

edit flag offensive delete link more

Comments

6

It's actually not startup time, but the time to create a connection between both nodes. Without the sleep you immediately quit before that's happened. The correct way is to wait for marker_pub.getNumSubscribers() > 0.

dornhege gravatar image dornhege  ( 2014-12-11 05:29:49 -0500 )edit

That's right, I created a pull request to add the changes into the tutorial.

VictorLamoine gravatar image VictorLamoine  ( 2014-12-11 07:00:23 -0500 )edit

I don't think that a latched publisher will help at all. The problem is that you binary is gone before rviz is connected. Between you creating the publisher and the program quitting are only a couple of C++ calls. The latching happens in the node, not on the server, so the publish will be lost.

dornhege gravatar image dornhege  ( 2015-01-29 09:54:17 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-12-11 03:10:40 -0500

Seen: 1,357 times

Last updated: Jan 29 '15