Skip to content

Instantly share code, notes, and snippets.

@pet1330
Last active November 15, 2018 12:24
Show Gist options
  • Select an option

  • Save pet1330/8147e6ab651106896da36a7ef1847432 to your computer and use it in GitHub Desktop.

Select an option

Save pet1330/8147e6ab651106896da36a7ef1847432 to your computer and use it in GitHub Desktop.
RosBag Decorator
#!/usr/bin/python
def rosbagit(method):
import subprocess
import roslib.packages
def cachable(method):
def cache_call(*args, **kw):
try:
return method.__globals__[method]
except:
method.__globals__[method] = method(*args, **kw)
return method.__globals__[method]
return cache_call
@cachable
def rosbag_ex_path():
return roslib.packages.find_node('rosbag', 'record')[0]
def start_recording(record_path):
return subprocess.Popen([record_path, '-a'], shell=False)
def stop_recording(process):
process.send_signal(subprocess.signal.SIGINT)
def record_rosbag(*args, **kw):
try:
process = start_recording(rosbag_ex_path())
result = method(*args, **kw)
finally:
stop_recording(process)
return result
return record_rosbag
#######################################################################
########################### Example Usage ###########################
#######################################################################
import rospy
from std_msgs.msg import String
class MainObject(object):
def __init__(self):
super(MainObject, self).__init__()
self.pub = rospy.Publisher('/test/topic', String, queue_size="10")
@rosbagit
def some_long_running_function(self):
print "working..."
r = rospy.Rate(30)
for i in range(60):
self.pub.publish(String("%i" % i))
r.sleep()
def main():
rospy.init_node('recorder')
mo = MainObject()
for i in range(3):
mo.some_long_running_function()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment