test_tools
robocup_recorder.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 """Data collector
4 
5 """
6 import os
7 import subprocess
8 
9 import rospy
10 from std_srvs.srv import Trigger, TriggerResponse
11 
12 
13 class Recorder():
14  def __init__(self):
15  self._recording_process = None
16  self._s = rospy.Service('robocup_recorder_toggle', Trigger, self._toggle_recording)
17  self._recorder_name = rospy.get_param('recorder_name', 'robocup_recorder')
18 
19  if self._recorder_name == rospy.get_name():
20  rospy.logerr("Parameter recorder_name should be different from the robocup recorder server node name.")
21  exit(1)
22 
23  self._filename_prefix = rospy.get_param('filename_prefix', '/tmp/robocup_record')
24  self._topic_regex = rospy.get_param('topic_regex', "'.*record/.*'")
25 
26  def _toggle_recording(self, req):
27  if not self._recording_process:
28  FNULL = open(os.devnull, 'w')
29  self._recording_process = subprocess.Popen(['rosbag', 'record',
30  '--regex', self._topic_regex,
31  '-o', self._filename_prefix,
32  '__name:=%s' % self._recorder_name],
33  stdout=FNULL,
34  stderr=subprocess.STDOUT)
35  rospy.loginfo("Started recording")
36  return TriggerResponse(True, 'started recording')
37  else:
38  self.clean_up()
39  return TriggerResponse(False, 'stopped recording')
40 
41  def clean_up(self):
42  FNULL = open(os.devnull, 'w')
43  subprocess.Popen(['rosnode', 'kill', self._recorder_name],
44  stdout=FNULL,
45  stderr=subprocess.STDOUT)
46  self._recording_process.wait()
47  self._recording_process = None
48  rospy.loginfo("Killed the recording process.")
49 
50 if __name__ == "__main__":
51  rospy.init_node('robocup_recorder_server')
52  recorder = Recorder()
53  rospy.spin()
54  recorder.clean_up()
robocup_recorder.Recorder.clean_up
def clean_up(self)
Definition: robocup_recorder.py:41
robocup_recorder.Recorder._toggle_recording
def _toggle_recording(self, req)
Definition: robocup_recorder.py:26
robocup_recorder.Recorder._recorder_name
_recorder_name
Definition: robocup_recorder.py:17
robocup_recorder.Recorder._s
_s
Definition: robocup_recorder.py:16
robocup_recorder.Recorder._topic_regex
_topic_regex
Definition: robocup_recorder.py:24
robocup_recorder.Recorder.__init__
def __init__(self)
Definition: robocup_recorder.py:14
robocup_recorder.Recorder
Definition: robocup_recorder.py:13
robocup_recorder.Recorder._recording_process
_recording_process
Definition: robocup_recorder.py:15
robocup_recorder.Recorder._filename_prefix
_filename_prefix
Definition: robocup_recorder.py:23