random_record.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2008, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 import unittest
35 import rospy
36 import rostest
37 import sys
38 try:
39  from cStringIO import StringIO
40 except ImportError:
41  from io import StringIO
42 import time
43 from random_messages import RandomMsgGen
44 import subprocess
45 import signal
46 import os
47 import atexit
48 
49 class RandomRecord(unittest.TestCase):
50 
51  def test_random_record(self):
52  rospy.init_node('random_pub')
53 
54  if (len(sys.argv) < 2):
55  raise Exception("Expected seed as first argument")
56 
57  seed = int(sys.argv[1])
58 
59  seed = int(sys.argv[1])
60  topics = int(sys.argv[2])
61  length = float(sys.argv[3])
62 
63  rmg = RandomMsgGen(seed, topics, length)
64 
65  publishers = {}
66 
67  for (topic, msg_class) in rmg.topics():
68  publishers[topic] = rospy.Publisher(topic, msg_class)
69 
70  bagpath = os.path.join('/tmp', 'test_rosbag_random_record_%d'%seed)
71  cmd = ['rosbag', 'record', '-a', '-O', bagpath]
72  f1 = subprocess.Popen(cmd)
73 
74  def finalkill():
75  try:
76  os.kill(f1.pid, signal.SIGKILL)
77  except:
78  pass
79 
80  atexit.register(finalkill)
81 
82  # Sleep an extra 5 seconds for good measure
83  rospy.sleep(rospy.Duration.from_sec(5.0))
84 
85  start = rospy.Time.now()
86  for (topic, msg, time) in rmg.messages():
87  d = start + rospy.Duration.from_sec(time) - rospy.Time.now()
88  rospy.sleep(d)
89  publishers[topic].publish(msg)
90 
91  # Sleep an extra 5 seconds for good measure
92  rospy.sleep(rospy.Duration.from_sec(5.0))
93 
94  # Initial terminate using SIGINT so bag clean up nicely
95  os.kill(-os.getpgrp(), signal.SIGINT)
96 
97  # Sleep an extra 5 seconds for good measure
98  rospy.sleep(rospy.Duration.from_sec(5.0))
99 
100  # Keep trying to kill until it's dead instead of blocking on communicate
101  while (f1.poll() is None):
102  try:
103  os.kill(f1.pid, signal.SIGKILL)
104  except:
105  pass
106  rospy.sleep(rospy.Duration.from_sec(1.0))
107 
108  self.assertEqual(f1.returncode, 0)
109 
110 
111 if __name__ == '__main__':
112  rostest.rosrun('test_rosbag', 'random_record_play', RandomRecord, sys.argv)


test_rosbag
Author(s): Tim Field, Jeremy Leibs, James Bowman, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:53:09