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

[Solved] publisher problem in hydro

asked 2013-12-12 21:47:57 -0500

ffontana gravatar image

updated 2013-12-12 22:33:01 -0500

Hi everyone

I have a really strange behaviour I can not explain. I have a really simple program where I publish a message and then quit. When im not sleeping after creating the publisher, but i start immediately to publish the messages gets lost. Any ideas what the case may be? I am using hydro, and the roscore runs locally.

thanks flavio

#include "ros/ros.h"
#include "std_msgs/Float32.h"

int main(int argc, char **argv)
{
  ros::init(argc, argv, "publish_test");

  ros::NodeHandle nh;
  ros::Publisher set_control_active_pub = nh.advertise< std_msgs::Float32>("/pixhawk_bridge/control_active2",10 );


  ros::Duration(2).sleep();


  std_msgs::Float32 activate_msg;
  activate_msg.data = 0;
  set_control_active_pub.publish( activate_msg );
  ros::spinOnce();
  ros::Duration(2).sleep();




  return 0;
}
edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2013-12-12 21:54:54 -0500

demmeln gravatar image

In essence it is likely that the message gets published before any subscribers have the chance to establish a connection to the publisher. It takes some time. There are some ways to check if and how many subscribers are connected to your publisher. For a detailed answer see this question & answer:

http://answers.ros.org/question/11167/how-do-i-publish-exactly-one-message/

edit flag offensive delete link more
1

answered 2013-12-12 21:53:30 -0500

Tirjen gravatar image

I guess it could be because you publish the message only once. try to put set_control_active_pub.publish( activate_msg ); into a while, something like:

  std_msgs::Float32 activate_msg;
  while(ros::ok()){
    activate_msg.data = 0;
    set_control_active_pub.publish( activate_msg );
    ros::spinOnce();
    ros::Duration(2).sleep();
  }
edit flag offensive delete link more
0

answered 2013-12-12 22:32:25 -0500

ffontana gravatar image

Hi Tirjen Thanks a lot for your reply, publishing in a loop is not an option.

Hi demmeln Thanks a lot for your reply, this is explained my problem.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-12-12 21:47:57 -0500

Seen: 259 times

Last updated: Dec 12 '13