Coding publisher and subscriber were easy but they are getting confusing for me because of rospy.spin(), rospy.on_shutdown() and other related functions that are used many times in different ways in tutorials and lectures. I was about to provide some samples from lectures and tutorials but it is also messy so that I will ask rather simple questions to get rid of my doubts.
I understand that
rospy.spin()is used when a subscriber is involved. But where in the code? Where should it be placed? Can i place it in a subscriber at a random place? Also, why did not you use spin function in
odom_topic_subscriberin this tutorial? That collapses what I learnt in ROS courses.
rospy.on_shutdown()is used but how and why? For instance as below,
shutdownhookfunction is passed to
2.1. Is that just to modify variable of
Trueso that the while loop is interrupted? If so, I have read on internet that
rospy.on_shutdown()function does not take any argument. Is passing shutdownhook function to on_shutdown for preventing getting odometry data and nothing related with shutting down the node?
rospy.on_shutdown()is inside the main function and is not called from another function. As soon as the program runs, isn’t it executed and shutdown the rosnode? Or, is
rospy.on_shutdown()called ONLY WHEN ctrl+c is pressed by the user? In short,
how and whenis it called?
ctrl_c = False def shutdownhook(): # works better than the rospy.is_shut_down() global ctrl_c print "shutdown time!" ctrl_c = True rospy.on_shutdown(shutdownhook) while not ctrl_c: data = odom_reader_object.get_odomdata() rospy.loginfo(data) rate.sleep()
For the code below, you already subscribe to
/odomtopic and call
topic_callbackfunction. That is to read Odometry data. So why did you also create
get_odomdatafunction and call it from while loop in the main function? That seems to me like doing the same thing with more function. I mean, I think we could achieve the same result without while loop in the main function since we subscribe to /odom topic. Also, i did not understand this assignment:
self._odomdata = msg. Isn’t the
Odometry(). Can you please elaborate the code below line-by-line if possible? I really need an answer. I am so confused.
class OdomTopicReader(object): def __init__(self, topic_name = '/odom'): self._topic_name = topic_name self._sub = rospy.Subscriber(self._topic_name, Odometry, self.topic_callback) self._odomdata = Odometry() def topic_callback(self, msg): self._odomdata = msg rospy.logdebug(self._odomdata) def get_odomdata(self): return self._odomdata
Thanks in advance.