Ask Your Question
0

only second message is sent to topic

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

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.
   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);
    msg.points[0].time_from_start = ros::Duration(1.0);

   if(ros::ok())
  {
    r_arm_comand_publisher.publish(msg);

   ros::spinOnce();
   loop_rate.sleep();
   ROS_INFO_STREAM ("\n1 - command SENT:\n" << msg);
   // *****************************************************************
   r_arm_comand_publisher.publish(msg);

   ros::spinOnce();
   loop_rate.sleep();
   ROS_INFO_STREAM ("\n2 - command SENT:\n" << msg);
 }
 loop_rate.sleep();
 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

Comments

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

Humpelstilzchen gravatar imageHumpelstilzchen ( 2017-08-21 06:52:07 -0600 )edit

It's good practice to copy and paste (formatted) terminal output rather than posting screen shots of it. http://wiki.ros.org/Support#Do

jayess gravatar imagejayess ( 2017-08-21 18:57:32 -0600 )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 imageFábioBarbosa ( 2017-08-22 10:28:13 -0600 )edit

1 Answer

Sort by » oldest newest most voted
0

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

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");
  sleep(5);
}

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

Stats

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

Seen: 55 times

Last updated: Aug 23 '17