I'm currently doing the Python Classes part in the ROS Basics in 5 days. I don't get the working of "self.ctrl_c = False" and "rospy.on_shutdown(self.shutdownhook)" defined in the constructor. I also don't understand how it is connected with the method "def shutdownhook(self)". Could someone give some clarity on that? alt text

asked 10 May, 10:31

eashwar.erahan's gravatar image

eashwar.erahan
43
accept rate: 0%


Hi @eashwar.erahan,

Thanks for asking!

self.ctrl_c is an instance variable that will be present on any object of the class. Similarly, self.shutdownhook() is an instance method.

In the __init__ function (which is run when an object of the class is instantiated [created]), we are

  • setting ctrl_c as False by default and
  • specifying the function that will be called when ROS is shutting down (rospy.on_shutdown). In this case, we are telling ROS to call the def shutdownhook(self) function. self.shutdownhook is referring to the def shutdownhook(self) function.

We're are using self.ctrl_c to detect when the user has pressed Ctrl+C on the keyboard, in which case ROS would shut down. That's why were are setting self.ctrl_c = True in def shutdownhook(self), because ROS will call this function when it's shutting down.

So the program should run until the user presses Ctrl+C.

permanent link

answered 14 May, 16:39

bayodesegun%40bayodesegun.com's gravatar image

bayodesegun@...
602
accept rate: 41%

So the rospy.on_shutdown actually detects if the user is pressing "Ctrl+c" and the calls the method self.shutdownhook() which sets the self.ctrl_c to True?

(16 May, 12:57) eashwar.erahan

Not really, but close, @eashwar.erahan. It's not the rospy.on_shutdown() function that detects when Ctrl + C is pressed, it's the core ROS system. The ROS system calls rospy.on_shutdown() when the progra is shutting down for any reason, which can include

  • the program ended normally
  • the user pressed Ctrl + C

In the case of this program, we assume that it should not shut down unless someone stops it by pressing Ctrl + C...

(20 May, 11:14) bayodesegun@...

@eashwar.erahan, regarding your answer (actually a new question), please just post it as a separate question next time.

rospy.ROSInterruptException is one of the exceptions that could occur in ROS, and usually occurs when you press Ctrl + C. The code snippet in the image contains a Python implementation a 'try and catch' (exception handling) block for detecting and neatly handling any exceptions that could occur while running the code. In this case, it Ctrl + C is pressed, the program would stop but you won't get the ugly exception message that normally follows.

(20 May, 11:21) bayodesegun@...

Ok sure thanks for your answer. Its really helpful.

(20 May, 15:40) eashwar.erahan

The whole code:

try: movebb8_object.move_bb8() except rospy.ROSInterruptException: pass

What is doing is launching the move_bb8() function. In case that something goes wrong, ROS will generate an exception of type rospy.ROSInterruptException which is captured by the try - except code. By capturing the exception, you prevent that the code breaks when something goes wrong.

This has nothing to do with ROS but with typical Python programming. Please check our newly published Python course which teaches Python to understand that better.

permanent link

answered 20 May, 16:38

rickt%40insanitymind.com's gravatar image

rickt@insani...
101
accept rate: 0%

ok sure thanks :)

(21 May, 05:51) eashwar.erahan

Also I don't understand the following piece of code. What does rospy.ROSInterruptException do? alt text

permanent link

answered 16 May, 12:58

eashwar.erahan's gravatar image

eashwar.erahan
43
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×9
×3
×1

question asked: 10 May, 10:31

question was seen: 50 times

last updated: 21 May, 05:51

powered by OSQA