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

Rviz not displaying marker on first call of publish(marker)

asked 2023-02-08 14:31:33 -0500

jtrubatch gravatar image

Using the attached code for some reason the marker does not appear with the first call of publish() without a small delay inserted. If I echo the visualization_marker topic without the delay it does not display the msg associated with the first publish(), only the subsequent msgs. With the delay it displays all the msgs. Is there something in my code causing this or am I missing something as to why there would need to be a delay between setting the msg parameters and publishing it?

This is being run on a ubuntu 16.04.7, kernel 4.15.0-142 using VMWare 17, with ROS Kinetic

Thanks

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

int main( int argc, char** argv )
{
  ros::init(argc, argv, "add_markers");
  ros::NodeHandle n;
  visualization_msgs::Marker marker;
  ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 1);

  uint32_t shape = visualization_msgs::Marker::CUBE;

  marker.header.frame_id = "map";
  marker.header.stamp = ros::Time::now();
  marker.ns = "add_markers";
  marker.id = 0;
  marker.type = shape;

  marker.action = visualization_msgs::Marker::ADD;

  marker.pose.position.x = 4; // Pick Up position 4, 4
  marker.pose.position.y = 4;
  marker.pose.position.z = 0;
  marker.pose.orientation.x = 0.0;
  marker.pose.orientation.y = 0.0;
  marker.pose.orientation.z = 0.0;
  marker.pose.orientation.w = 1.0;

  marker.scale.x = 0.25;
  marker.scale.y = 0.25;
  marker.scale.z = 0.25;

  marker.color.r = 0.0f;
  marker.color.g = 1.0f;
  marker.color.b = 0.0f;
  marker.color.a = 1.0;

  marker.lifetime = ros::Duration();

  ros::Duration(0.1).sleep(); // Why is this needed?
  marker_pub.publish(marker);
  ROS_INFO("Marker Placed");
  ros::Duration(3.0).sleep();

  marker.color.a = 0;
  marker_pub.publish(marker);
  ROS_INFO("Marker Hide");
  ros::Duration(3.0).sleep();

  marker.pose.position.x = 3.5; 
  marker.pose.position.y = -4;
  marker.color.a = 1.0;
  marker_pub.publish(marker);
  ROS_INFO("Marker Reappear");
  ros::Duration(2.0).sleep(); 

  return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-02-09 09:30:19 -0500

Mike Scheutzow gravatar image

updated 2023-02-09 09:31:18 -0500

There must be a delay. It takes time for the subscribers to be notified that the publisher has been created, and then more time for the subscriber to establish a network connection. The ros policy for a publisher that has no active subscribers is to simply discard the message. This policy makes sense because a subscriber may never connect.

edit flag offensive delete link more

Comments

Got it, thanks!

jtrubatch gravatar image jtrubatch  ( 2023-02-09 11:25:45 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2023-02-08 14:31:33 -0500

Seen: 137 times

Last updated: Feb 09 '23