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

ros publisher not publishing

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

zhonghao gravatar image

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

Hamid Didari 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 image zhonghao  ( 2018-12-03 06:32:34 -0500 )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 image PeteBlackerThe3rd  ( 2018-12-03 08:57:57 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

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

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 image zhonghao  ( 2018-12-03 06:41:49 -0500 )edit

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

Hamid Didari gravatar image Hamid Didari  ( 2018-12-03 07:57:11 -0500 )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 image zhonghao  ( 2018-12-03 07:59:42 -0500 )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 Didari gravatar image Hamid Didari  ( 2018-12-03 08:12:32 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-12-03 06:20:57 -0500

Seen: 537 times

Last updated: Dec 03 '18