Skip to content

Instantly share code, notes, and snippets.

@eborghi10
Created April 9, 2021 12:56
Show Gist options
  • Save eborghi10/489ec36c36af6eb65d657deab17a87ba to your computer and use it in GitHub Desktop.
Save eborghi10/489ec36c36af6eb65d657deab17a87ba to your computer and use it in GitHub Desktop.
ROSInterruptException example
#!/usr/bin/env python
import rospy
name = "caught_exception"
rospy.init_node(name)
r = rospy.Rate(1.)
start = rospy.Time.now()
while not rospy.is_shutdown():
rospy.logwarn(name)
try:
r.sleep()
except rospy.ROSInterruptException:
pass
#!/usr/bin/env python
import rospy
name = "dying_node"
rospy.init_node(name)
r = rospy.Rate(1.)
start = rospy.Time.now()
while not rospy.is_shutdown():
rospy.logwarn(name)
r.sleep()
#!/usr/bin/env python
import rospy
name = "killer_node"
rospy.init_node(name)
r = rospy.Rate(1.)
start = rospy.Time.now()
while not rospy.is_shutdown() and (rospy.Time.now() - start) < rospy.Duration(5.):
rospy.logwarn(name)
r.sleep()
<launch>
<node pkg="gazebo_ros" name="gzserver" type="gzserver" required="true"/>
<node pkg="my_pkg" name="killer" type="killer_node.py" output="screen" required="true"/>
<node pkg="my_pkg" name="testing" type="dying_node.py" output="screen" required="false"/>
<!-- These two nodes solve the issue from above -->
<!-- <node pkg="my_pkg" name="testing" type="caught_exception.py" output="screen" required="false"/> -->
<!-- <node pkg="my_pkg" name="testing" type="timer_node.py" output="screen" required="false"/> -->
</launch>
#!/usr/bin/env python
import rospy
def timer(event):
print 'Timer called at ' + str(event.current_real)
name = "timer_node"
rospy.init_node(name)
rospy.Timer(rospy.Duration(1.), timer)
rospy.spin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment