ros::NodeHandle n;
ros::Publisher traj_pub = n.advertise<trajectory_msgs::JointTrajectory>("joint_path_command",100);
trajectory_msgs::JointTrajectory msg;
[Fill the trajectory message]
traj_pub.publish(msg)
Flavian,
The issue is likely not that the publisher is being destroyed too soon, but rather that you’re publishing a message too soon after advertising the publisher.
When a publisher is first created/advertised, ROS notifies the ROS master that a new topic publisher is available. If there are subscribers listening on that topic (e.g. rostopic echo, robot interface, etc.), then the ROS master passes them the publisher info, and they each create a connection with the publisher node. All that takes time. If you publish messages during that process, they will just get “lost”, which is probably what’s happening to your trajectory.
Try adding another sleep call between the n.advertise and traj_pub.publish calls. Alternatively, you can use a latching publisher or count the number of subscribers before publishing.
- Jeremy