Ask Your Question

only second message is sent to topic

asked 2017-08-21 06:42:31 -0500

FábioBarbosa gravatar image

Hi! i was writing a code where is publish a simple message to an existing topic. The problem is, that only on the second time i send a message is actually heard by the topic. Making the first message non existing.

Here's the code i used:

#include "ros/ros.h"
#include "trajectory_msgs/JointTrajectory.h"

 int main(int argc, char **argv)
   ros::init(argc, argv, "trajectory_test_node");
   ros::NodeHandle nh;

   ros::Rate loop_rate(0.2);
   ros::Publisher r_arm_comand_publisher = nh.advertise<trajectory_msgs::JointTrajectory>
   ("/signbot/r_arm_controller/command", 1000);

   // Create a message to send.
   trajectory_msgs::JointTrajectory msg;

  // Fill the names of the joints to be controlled.
  // Note that the vacuum_gripper_joint is not controllable.
   // Create one point in the trajectory.
   // Resize the vector to the same length as the joint names.
   // Values are initialized to 0.
   msg.points[0].positions.resize(msg.joint_names.size(), 0.0);
    msg.points[0].time_from_start = ros::Duration(1.0);


   ROS_INFO_STREAM ("\n1 - command SENT:\n" << msg);
   // *****************************************************************

   ROS_INFO_STREAM ("\n2 - command SENT:\n" << msg);
 ROS_INFO_STREAM("\n\n\t***** SHUTTING DOWN ********\n\n");
 return 0;

on the terminal where i execute the program, is printed this: image description

on the other terminal where i use rostopic echo, is printed this: image description

Could someone help me? Thanks

edit retag flag offensive close merge delete


Might be a duplicate of 11167. If so try to sleep a short moment before publishing.

Humpelstilzchen gravatar image Humpelstilzchen  ( 2017-08-21 06:52:07 -0500 )edit

It's good practice to copy and paste (formatted) terminal output rather than posting screen shots of it.

jayess gravatar image jayess  ( 2017-08-21 18:57:32 -0500 )edit

Humpelstilzchen: no, its not a duplicate, but thanks for the link, Atually i didnt find this question when i was searching on ros answers. i will try the solutions to see if i can resolve my problem. jayess: thanks for the advice, i correct that from now on, in my questions.

FábioBarbosa gravatar image FábioBarbosa  ( 2017-08-22 10:28:13 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2017-08-23 05:29:57 -0500

FábioBarbosa gravatar image

Found the problem. As say in link text "I guess the problem is that ROS needs some time to register at the core and to establish all subscriber connections". Which means, before publishing any message i should wait just a little time. So i use sleep(5); just before i call the publish() method. I could also write:

while(r_arm_comand_publisher.getNumSubscribers() != 1)
  ROS_INFO_STREAM("\n\tWaiting for subscibers\n number of subs:"<<r_arm_comand_publisher.getNumSubscribers()<<"\n");

but i am working with simulator gazebo, and i start the gazebo first. So there is always at least one subscriber listening.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-08-21 06:42:31 -0500

Seen: 90 times

Last updated: Aug 23 '17