-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrosbag_remote_record.py
executable file
·297 lines (258 loc) · 11.1 KB
/
rosbag_remote_record.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""
Copyright(c) <Florian Lier>
This file may be licensed under the terms of the
GNU Lesser General Public License Version 3 (the ``LGPL''),
or (at your option) any later version.
Software distributed under the License is distributed
on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
express or implied. See the LGPL for the specific language
governing rights and limitations.
You should have received a copy of the LGPL along with this
program. If not, go to http://www.gnu.org/licenses/lgpl.html
or write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
The development of this software was supported by the
Excellence Cluster EXC 277 Cognitive Interaction Technology.
The Excellence Cluster EXC 277 is a grant of the Deutsche
Forschungsgemeinschaft (DFG) in the context of the German
Excellence Initiative.
Authors: Florian Lier
<flier>@techfak.uni-bielefeld.de
"""
# STD IMPORTS
import sys
import os
import time
import signal
import psutil
import threading
import subprocess
from optparse import OptionParser
# RSB
RSB_SUPPORT = True
try:
import rsb
except ImportError:
RSB_SUPPORT = False
# ROS IMPORTS
ROS_SUPPORT = True
try:
import rospy
import roslib
from std_msgs.msg import Bool, String
except ImportError:
ROS_SUPPORT = False
class ROSRecordConnector(threading.Thread):
def __init__(self, _filename, _inscope, _triggerscope, _msglimit, _directory=None):
threading.Thread.__init__(self)
self.is_running = True
self.filename = _filename.strip()
if _directory is None:
self.directory = ""
else:
self.directory = _directory.strip()
if self.directory != "":
self.directory = self.directory + "/"
self.listen_topic = _triggerscope
self.inscope = _inscope
self.msglimit = _msglimit
self.recordprocess = None
def record_string_callback(self, ros_data):
if ros_data.data.lower().endswith(":start"):
# extract filename
idx = ros_data.data.lower().rfind(":start")
newfilename = ros_data.data[0:idx]
if len(newfilename) > 0 and newfilename.find(' ') == -1:
self.record_handling(True, self.directory + newfilename)
else:
print ">>> [ROS] Record filename malformed: '%s'. Should not be empty or contain spaces, using default name" % newfilename
self.record_handling(True, self.directory + self.filename)
else:
if ros_data.data.lower().endswith(":stop"):
self.record_handling(False)
else:
# invalid message
print ">>> [ROS] Record request invalid: %s. Should contain filename:start or :stop" % ros_data.data
return
def record_bool_callback(self, ros_data):
self.record_handling(ros_data.data, self.filename)
def record_handling(self, record, filename=None):
if (record and self.recordprocess is not None and self.recordprocess.is_recording):
return False # already recording
if (not record and self.recordprocess is None):
return False # already stopped
if self.recordprocess is not None:
print ">>> [ROS] Record status: %s" % str(self.recordprocess.is_recording)
else:
print ">>> [ROS] Record status: %s" % False
if record and filename is not None:
self.recordprocess = RecordBAG(filename, self.inscope, self.msglimit)
self.recordprocess.start()
return True
else:
if self.recordprocess is not None:
if self.recordprocess.stop():
self.recordprocess = None
return True
else:
return False
return True
def run(self):
print ">>> [ROS] Initializing ROSBAG REMOTE RECORD of: %s" % self.inscope.strip()
ros_subscriber = rospy.Subscriber(self.listen_topic, Bool, self.record_bool_callback, queue_size=1)
ros_subscriber2 = rospy.Subscriber(self.listen_topic + "/named", String, self.record_string_callback, queue_size=1)
print ">>> [ROS] Waiting for Bool (true for start) on topic : %s" % self.listen_topic
print ">>> [ROS] Waiting for filename:start on topic : %s" % self.listen_topic + "/named"
while self.is_running is True:
time.sleep(0.05)
if self.recordprocess is not None:
self.recordprocess.stop()
ros_subscriber.unregister()
ros_subscriber2.unregister()
print ">>> [ROS] Stopping ROSBAG REMOTE RECORD %s" % self.inscope.strip()
class RSBRecordConnector(threading.Thread):
def __init__(self, _filename, _inscope, _triggerscope, _msglimit):
threading.Thread.__init__(self)
self.is_running = True
self.filename = _filename.strip()
self.listen_scope = _triggerscope
self.inscope = _inscope.strip()
self.msglimit = _msglimit
self.recordprocess = None
def record_callback(self, event):
if (event.data and self.recordprocess is not None and self.recordprocess.is_recording):
return False # already recording
if (not event.data and self.recordprocess is None):
return False # already stopped
if self.recordprocess is not None:
print ">>> [RSB] Record status: %s" % str(self.recordprocess.is_recording)
else:
print ">>> [RSB] Record status: %s" % False
if event.data:
self.recordprocess = RecordBAG(self.filename, self.inscope, self.msglimit)
self.recordprocess.start()
return True
else:
if self.recordprocess is not None:
if self.recordprocess.stop():
self.recordprocess = None
return True
else:
return False
return True
def run(self):
print ">>> [RSB] Initializing ROSBAG REMOTE RECORD of: %s" % self.inscope.strip()
rsb_subscriber = rsb.createListener(self.listen_scope)
rsb_subscriber.addHandler(self.record_callback)
while self.is_running is True:
time.sleep(0.05)
if self.recordprocess is not None:
self.recordprocess.stop()
rsb_subscriber.deactivate()
print ">>> [RSB] Stopping ROSBAG REMOTE RECORD %s" % self.inscope.strip()
class RecordBAG(threading.Thread):
def __init__(self, _name, _scope, _msg_limit=None):
threading.Thread.__init__(self)
self.name = _name.strip()
self.scope = _scope.strip()
self.is_recording = False
self.process = None
self.msg_limit = _msg_limit
def stop(self):
print ">>> Received STOP"
if not self.is_recording:
print ">>> Already stopped"
return True
self.is_recording = False
try:
p = psutil.Process(self.process.pid)
try:
for sub in p.get_children(recursive=True):
sub.send_signal(signal.SIGINT)
self.process.send_signal(signal.SIGINT)
return True
except AttributeError:
# newer psutils
for sub in p.children(recursive=True):
sub.send_signal(signal.SIGINT)
self.process.send_signal(signal.SIGINT)
return True
except Exception as e:
print ">>> Maybe the process is already dead? %s" % str(e)
return False
def run(self):
print ">>> Recording: %s now" % self.scope
rosbag_filename = self.name + "-" + str(time.time()) + ".bag"
print ">>> Filename: %s" % rosbag_filename
if self.msg_limit is not None:
print ">>> stopping after %s messages" % str(self.msg_limit)
self.process = subprocess.Popen("rosbag record -l %s -O %s %s" % (str(self.msg_limit), rosbag_filename, self.scope), shell=True)
else:
self.process = subprocess.Popen("rosbag record -O %s %s" % (rosbag_filename, self.scope), shell=True)
self.is_recording = True
self.process.wait()
print ">>> Recording: %s stopped" % self.scope
self.is_recording = False
def signal_handler(sig, frame):
"""
Callback function for the signal handler, catches signals
and gracefully stops the application, i.e., exit subscribers
before killing the main thread.
:param sig the signal number
:param frame frame objects represent execution frames.
"""
print ">>> Exiting (signal %s)..." % str(sig)
r.is_running = False
print ">>> Bye!"
sys.exit(0)
if __name__ == '__main__':
parser = OptionParser(usage="Usage: %prog [options]")
parser.add_option("-m", "--middleware",
action="store",
dest="middleware",
help="Set the middleware [ros|rsb]")
parser.add_option("-i", "--inscope",
action="store",
dest="inscope",
help="Set the scopes/topics to record")
parser.add_option("-t", "--triggerscope",
action="store",
dest="triggerscope",
help="Set the root scope/topic for remote control (default:/meka/rosbagremote/record)\
another topic <root>/named provides a way to query a new bag file name while starting")
parser.add_option("-f", "--filename",
action="store",
dest="filename",
help="The name of the file that is saved")
parser.add_option("-d", "--directory",
action="store",
dest="directory",
help="(only for named trigger) the base directory in which the files should be saved")
parser.add_option("-l", "--limit",
action="store",
dest="msglimit",
help="The number of message to record before automatically stopping")
(options, args) = parser.parse_args()
if not options.filename or not options.inscope or not options.middleware:
print ">>> No inscope, filename given or middleware provided --> see help."
sys.exit(1)
if not options.triggerscope:
options.triggerscope = "/meka/rosbagremote/record"
if options.middleware.lower() == "ros":
if ROS_SUPPORT:
rospy.init_node('rosbag_remote_record', anonymous=True)
r = ROSRecordConnector(options.filename, options.inscope, options.triggerscope, options.msglimit, options.directory)
r.start()
else:
print ">>> ROS import failed, ros option cannot be used."
sys.exit(-1)
else:
if RSB_SUPPORT:
r = RSBRecordConnector(options.filename, options.inscope, options.triggerscope, options.msglimit)
r.start()
else:
print ">>> RSB import failed, rsb option cannot be used."
sys.exit(-1)
signal.signal(signal.SIGINT, signal_handler)
while True:
time.sleep(0.2)