Ask Your Question
0

ros publisher not publishing

asked 2018-12-03 06:20:57 -0600

zhonghao gravatar image

updated 2018-12-03 06:33:09 -0600

hamid gravatar image

I have a class called PublishObject to manage my ros node, however, I only want to publish things when I use ctrl+c to to kill the spin() and return to the main function as following, however I find that when I return to the main function, everything works in the function node_instance.second_run_publish_matched_object(), but I think the publisher is notworking as the rviz doesn't receive anything?

How can I fix this? Or I am supposed only to using Publisher insided the callback function such that it will publish properly??

int main(int argc, char **argv)

  ros::init(argc, argv, "publish_object");
  PublishObject  node_instance;  
  ros::spin();
  node_instance.second_run_publish_matched_object(); // to publish things. 

return 0;
}
edit retag flag offensive close merge delete

Comments

it seems that when use ctrl+c to return to main function, my advertise rostopic also get killed

zhonghao gravatar imagezhonghao ( 2018-12-03 06:32:34 -0600 )edit

The purpose of ctrl+c is to shut your node down. You should not use it to switch to a different mode of operation as you're doing.

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2018-12-03 08:57:57 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-12-03 06:38:20 -0600

gvdhoorn gravatar image

Or I am supposed only to using Publisher insided the callback function such that it will publish properly??

No, you can publish anywhere else as well, except after ros::spin() has returned.

At that point ROS has shut down, so no events (ie: incoming messages or service requests) will be processed, and neither will outgoing messages be transmitted.

edit flag offensive delete link more

Comments

so in this case, how can I return to the main function but still before ros::spin()??Because I need to collect all the msgs from my subsciber first, then I can process it

zhonghao gravatar imagezhonghao ( 2018-12-03 06:41:49 -0600 )edit

you can use ros::spinOnce() in a loop and when you want to exit the loop just break the loop

hamid gravatar imagehamid ( 2018-12-03 07:57:11 -0600 )edit

but in this case when I break the loop, can I still publish things???,I think it shuts down ROS as well.

zhonghao gravatar imagezhonghao ( 2018-12-03 07:59:42 -0600 )edit
1

for breaking loop you don't need to suht down the node you can use something like this:

while(ros::ok)
{
ros::spinOnce();
if(user_input == 's')
 break;
loop_rate.sleep();
}
hamid gravatar imagehamid ( 2018-12-03 08:12:32 -0600 )edit

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: 2018-12-03 06:20:57 -0600

Seen: 106 times

Last updated: Dec 03 '18