ros publisher not publishing
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 nodeinstance.secondrunpublishmatched_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;
}
Asked by zhonghao on 2018-12-03 07:20:57 UTC
Answers
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.
Asked by gvdhoorn on 2018-12-03 07:38:20 UTC
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
Asked by zhonghao on 2018-12-03 07:41:49 UTC
you can use ros::spinOnce() in a loop and when you want to exit the loop just break the loop
Asked by Hamid Didari on 2018-12-03 08:57:11 UTC
but in this case when I break the loop, can I still publish things???,I think it shuts down ROS as well.
Asked by zhonghao on 2018-12-03 08:59:42 UTC
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();
}
Asked by Hamid Didari on 2018-12-03 09:12:32 UTC
Comments
it seems that when use ctrl+c to return to main function, my advertise rostopic also get killed
Asked by zhonghao on 2018-12-03 07:32:34 UTC
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.
Asked by PeteBlackerThe3rd on 2018-12-03 09:57:57 UTC