Ask Your Question
0

publish fails to send message

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

FábioBarbosa gravatar image

Hi!

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.
   if(ros::ok())
   {
     // Create a message to send.
     trajectory_msgs::JointTrajectory msg;

     // Fill the names of the joints to be controlled.
     msg.joint_names.clear();
     msg.joint_names.push_back("r_shoulder_joint");
     msg.joint_names.push_back("r_top_arm_joint");
     msg.joint_names.push_back("r_elbow_joint");
     msg.joint_names.push_back("r_wrist_joint");
     // Create one point in the trajectory.
     msg.points.resize(1);
     // 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);
     r_arm_comand_publisher.publish(msg);
  }
 return 0;
}

Hope you can help :-)

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
0

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

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);
  ...
  ....
  r_arm_comand_publisher.publish(msg);

  ros::spinOnce();
  loop_rate.sleep();

in this way i get everything working well.

Thanks for all the help

edit flag offensive delete link more
1

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

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

Comments

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

FábioBarbosa gravatar imageFábioBarbosa ( 2017-07-14 07:10:14 -0600 )edit

Yes, that would be one way to do it.

gvdhoorn gravatar imagegvdhoorn ( 2017-07-14 07:14:51 -0600 )edit

try that, didnt work :S

FábioBarbosa gravatar imageFábioBarbosa ( 2017-07-14 08:17:41 -0600 )edit
0

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

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

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

Stats

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

Seen: 282 times

Last updated: Jul 19 '17