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

Unable to publish a message to a predefined topic

asked 2021-10-14 10:59:28 -0500

huytyskland gravatar image

updated 2021-10-14 13:26:40 -0500

gvdhoorn gravatar image

Hello,

My project is on Linux. I am having an issue with Publisher. I git clone a package in which a node called A1 has a topic named a1. I also installed another package in which a node called B2 has a topic named b2. Then I created myself a package in which my created node subsribed to the topic b2 and whenever it received a message, it will convert the message before publish the new message to the topic a1. With linux command: "rostopic pub a1......", I can read (using "rostopic echo a1") the data from topic a1. However, when I used the code in my .cpp file, I cannot echo the data of topic a1. Here is my abstract code:

....

void callBack(const received_message_type receivedMessage)
{

       new_message_type newMessage;
       [code for conversion]
       ros::NodeHandle nh_pub;
       ros::Publisher pub = nh_pub.advertise<new_message_type>("a1", 1000);
       pub.publish(newMessage);
}

int main (int argc, char **argv)
{

       ros::init(argc, argv, "simple_node");
       ros::NodeHandle nh_sub;
       ros::Subcriber sub = nh_sub.subscribe("b2", 1000, &callBack);

       ros::spin();
}

I also add "find_package" which includes node A1 and node B2 in CMakeLists.txt and <build_depend> as well as <exec_depend> of those 2 in package.xml. But cannot echo any data from a1.

The flow of my application is receiving arrow keys from node B2 which publich the message via topic b2. My created node will subscribe to b2 to convert the message from it and then publish to topic a1.

In my case, what could possibly be the cause?

Hope someone help me soon, thank you very much.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-10-14 13:27:25 -0500

gvdhoorn gravatar image

updated 2021-10-14 13:34:41 -0500

You have this:

void callBack(const received_message_type receivedMessage)
{
  new_message_type newMessage;
  [code for conversion]
  ros::NodeHandle nh_pub;
  ros::Publisher pub = nh_pub.advertise<new_message_type>("a1", 1000);
  ...
}

you are creating a Publisher inside a callback. That object is then also immediately destroyed at the end of the callback.

That is not a good idea and will not work reliably.

Setting up subscriptions takes time, and you don't give your subscribers any time to do that (as the publisher will be destroyed before the subscriber can receive any data).

Move your ros::Publisher object to a scope which lives longer than the callback and it should start working, provided there are no other problems with your code.

edit flag offensive delete link more

Comments

Thank you. That helps me to solve the issue.

huytyskland gravatar image huytyskland  ( 2021-10-15 01:21:45 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2021-10-14 10:59:28 -0500

Seen: 181 times

Last updated: Oct 14 '21