Rviz not displaying marker on first call of publish(marker)
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;
}