Ask Your Question
0

How to publish before shutdown

asked 2018-12-19 02:19:58 -0600

KSWang gravatar image

Hi, everyone. I use rosserial_python to connect arduino in my project. I need to publish Twist information to arduino before I shut ros program down. I've already tried two ways to write shutdown function which is refer from http://wiki.ros.org/roscpp/Overview/I...

and here's one of my code:

rospy.loginfo(rospy.get_caller_id() + " shutdown------------------------")

twist.linear.x = forward_init

twist.angular.z = left_init

pub.publish(twist)

rospy.loginfo(rospy.get_caller_id() + " finish shutdown-----------------")

rospy.spin()

However in it shows both of loginfo, but it doesn't publish in this part. Appreciated for any suggestion. thanks a lot.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-12-19 02:29:50 -0600

mgruhler gravatar image

You link to the C++ documentation, but you show python code... How is this supposed to work?

Also, if you want to publish before shutdown, you need to do something entirely different.

In you code, you only publish once at the very beginning. Then you enter spin() which is a blocking call. You need to put the logic in another thread then, or use the while not rospy.is_shutdown(): pattern instead of spin().

To publish a message after shutdown has been triggered, but before it occurs (I understand your question this way), I don't see much of possibilities besides using a shutdown hook. But note that

Messages are not guaranteed to be published.

edit flag offensive delete link more

Comments

Hi, thanks for your reply. Sorry, I made a mistake of the website link. I refer this website and find another way for python. And I've used rospy.is_shutdown(), but it still not worked. I think my question is to publish when I shut down the program. Maybe it's fine before or after shutdown.

KSWang gravatar imageKSWang ( 2018-12-19 09:38:45 -0600 )edit

Well, that's the thing. When ROS is shut down, you cannot publish. Thus, your only chance would be to go via the shutdown hootk linked above. But then again,

the message is not guaranteed to be published.

...

mgruhler gravatar imagemgruhler ( 2018-12-20 01:50:52 -0600 )edit

But

This [the shutdown hook] will be invoked before actual shutdown occurs, so you can perform service and parameter server calls safely.

Would a service also be fine for you?

mgruhler gravatar imagemgruhler ( 2018-12-20 01:51:13 -0600 )edit

Thanks again, I slightly understand what you mean. but, I can't figure out how service will make it work.

KSWang gravatar imageKSWang ( 2018-12-20 02:08:36 -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

2 followers

Stats

Asked: 2018-12-19 02:19:58 -0600

Seen: 159 times

Last updated: Dec 19 '18