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

publish fails to send message

asked 2017-07-14 03:53:08 -0500

FábioBarbosa gravatar image


i'm writing a simple c++ code where i publish an message of type trajectory_msgs::JointTrajector. When i execute the code, everything works well except the moment of publishing the message. So what i understood is that my publisher.publish(msg); is having problem, is this my problem or is this type of message impossible to send?

heres my code:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "trajectory_msgs/JointTrajectory.h"
#include "trajectory_msgs/JointTrajectoryPoint.h"

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

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

   /// Create a JointTrajectory with all positions set to zero, and command the arm.
     // Create a message to send.
     trajectory_msgs::JointTrajectory msg;

     // Fill the names of the joints to be controlled.
     // 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);
     // How long to take getting to the point (floating point seconds).
     msg.points[0].time_from_start = ros::Duration(0.0001);

     ROS_INFO_STREAM ("Sending command:\n" << msg);
 return 0;

Hope you can help :-)

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted

answered 2017-07-19 12:55:18 -0500

FábioBarbosa gravatar image

i found the solution. I dont known why but ros::Duration(3).sleep(); doesnt help, So, i use loop_rate.sleep();. Like:

  ros::Rate loop_rate(0.2);


in this way i get everything working well.

Thanks for all the help

edit flag offensive delete link more

answered 2017-07-14 05:50:48 -0500

gvdhoorn gravatar image

is this my problem or is this type of message impossible to send?

no, not at all, but you don't seem to give ROS any opportunity to properly send it out to your subscribers, as your program immediately exits after your call to publish(..).

That won't work, as all the infrastructure will be teared down before the middleware has had time to process everything.

Add a sleep or delay before your return 0 and things will probably work (provided there are no other problems with your code).

Know that pub/sub is an asynchronous communication system. If you require confirmation or guaranteed delivery, use services.

edit flag offensive delete link more


what you are saying is to wirte something like this: ros::Duration(10).sleep(); before return 0;?

FábioBarbosa gravatar image FábioBarbosa  ( 2017-07-14 07:10:14 -0500 )edit

Yes, that would be one way to do it.

gvdhoorn gravatar image gvdhoorn  ( 2017-07-14 07:14:51 -0500 )edit

try that, didnt work :S

FábioBarbosa gravatar image FábioBarbosa  ( 2017-07-14 08:17:41 -0500 )edit

answered 2017-07-17 09:29:18 -0500

Nico__ gravatar image

As @gvdhoorn said, pub/sub is an asynchronous communication system.

To be sure your publisher send one message before closing, you can add a line to wait for a subscriber to subscribe to your topic.

You can do that using r_arm_comand_publisher.getNumSubscribers() which send the number of subscribers currently connected to your topic.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-07-14 03:53:08 -0500

Seen: 1,043 times

Last updated: Jul 19 '17