$search
00001 #!/usr/bin/env python 00002 # Software License Agreement (BSD License) 00003 # 00004 # Copyright (c) 2008, Willow Garage, Inc. 00005 # All rights reserved. 00006 # 00007 # Redistribution and use in source and binary forms, with or without 00008 # modification, are permitted provided that the following conditions 00009 # are met: 00010 # 00011 # * Redistributions of source code must retain the above copyright 00012 # notice, this list of conditions and the following disclaimer. 00013 # * Redistributions in binary form must reproduce the above 00014 # copyright notice, this list of conditions and the following 00015 # disclaimer in the documentation and/or other materials provided 00016 # with the distribution. 00017 # * Neither the name of Willow Garage, Inc. nor the names of its 00018 # contributors may be used to endorse or promote products derived 00019 # from this software without specific prior written permission. 00020 # 00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 # POSSIBILITY OF SUCH DAMAGE. 00033 # 00034 00035 import roslib 00036 roslib.load_manifest('pr2_msgs') 00037 00038 import sys 00039 import struct 00040 00041 import unittest 00042 00043 import rostest 00044 import rosbag 00045 from rosbag.migration import MessageMigrator, checkbag, fixbag 00046 00047 import re 00048 from cStringIO import StringIO 00049 import os 00050 00051 import rospy 00052 00053 import math 00054 00055 migrator = MessageMigrator() 00056 00057 def repack(x): 00058 return struct.unpack('<f',struct.pack('<f',x))[0] 00059 00060 class TestPR2MsgsMigration(unittest.TestCase): 00061 00062 00063 # (*) LaserScannerSignal.saved 00064 00065 ########### LaserScannerSignal ############### 00066 00067 def get_old_laser_scanner_signal(self): 00068 laser_scanner_signal_classes = self.load_saved_classes('pr2_mechanism_controllers_LaserScannerSignal.saved') 00069 00070 laser_scanner_signal = laser_scanner_signal_classes['pr2_mechanism_controllers/LaserScannerSignal'] 00071 00072 return laser_scanner_signal(None, 123) 00073 00074 def get_new_laser_scanner_signal(self): 00075 from pr2_msgs.msg import LaserScannerSignal 00076 00077 return LaserScannerSignal(None, 123) 00078 00079 def test_laser_scanner_signal(self): 00080 self.do_test('laser_scanner_signal', self.get_old_laser_scanner_signal, self.get_new_laser_scanner_signal) 00081 00082 00083 ########### BatteryState ############### 00084 00085 # The BatteryState message can no longer be migrated. 00086 00087 # def get_old_battery_state(self): 00088 # battery_state_classes = self.load_saved_classes('BatteryState.saved') 00089 # 00090 # battery_state = battery_state_classes['robot_msgs/BatteryState'] 00091 # 00092 # return battery_state(None, 1.23, 4.56, 7.89) 00093 # 00094 # def get_new_battery_state(self): 00095 # from pr2_msgs.msg import BatteryState 00096 # 00097 # return BatteryState(None, 1.23, 4.56, 7.89) 00098 # 00099 # def test_battery_state(self): 00100 # self.do_test('battery_state', self.get_old_battery_state, self.get_new_battery_state) 00101 00102 00103 ########### Helper functions ########### 00104 00105 def setUp(self): 00106 self.pkg_dir = roslib.packages.get_pkg_dir("pr2_msgs") 00107 00108 00109 def load_saved_classes(self,saved_msg): 00110 f = open("%s/test/saved/%s"%(self.pkg_dir,saved_msg), 'r') 00111 00112 type_line = f.readline() 00113 pat = re.compile(r"\[(.*)]:") 00114 type_match = pat.match(type_line) 00115 00116 self.assertTrue(type_match is not None, "Full definition file malformed. First line should be: '[my_package/my_msg]:'") 00117 00118 saved_type = type_match.groups()[0] 00119 saved_full_text = f.read() 00120 00121 saved_classes = roslib.genpy.generate_dynamic(saved_type,saved_full_text) 00122 00123 self.assertTrue(saved_classes is not None, "Could not generate class from full definition file.") 00124 self.assertTrue(saved_classes.has_key(saved_type), "Could not generate class from full definition file.") 00125 00126 return saved_classes 00127 00128 def do_test(self, name, old_msg, new_msg): 00129 # Name the bags 00130 oldbag = "%s/test/%s_old.bag"%(self.pkg_dir,name) 00131 newbag = "%s/test/%s_new.bag"%(self.pkg_dir,name) 00132 00133 # Create an old message 00134 with rosbag.Bag(oldbag, 'w') as bag: 00135 bag.write("topic", old_msg(), roslib.rostime.Time()) 00136 00137 # Check and migrate 00138 res = checkbag(migrator, oldbag) 00139 self.assertTrue(not False in [m[1] == [] for m in res], 'Bag not ready to be migrated') 00140 res = fixbag(migrator, oldbag, newbag) 00141 self.assertTrue(res, 'Bag not converted successfully') 00142 00143 # Pull the first message out of the bag 00144 msgs = [msg for msg in rosbag.Bag(newbag).read_messages()] 00145 00146 # Reserialize the new message so that floats get screwed up, etc. 00147 m = new_msg() 00148 buff = StringIO() 00149 m.serialize(buff) 00150 m.deserialize(buff.getvalue()) 00151 00152 #Compare 00153 # print "old" 00154 # print roslib.message.strify_message(msgs[0][1]) 00155 # print "new" 00156 # print roslib.message.strify_message(m) 00157 00158 # Strifying them helps make the comparison easier until I figure out why the equality operator is failing 00159 self.assertTrue(roslib.message.strify_message(msgs[0][1]) == roslib.message.strify_message(m)) 00160 # self.assertTrue(msgs[0][1] == m) 00161 00162 #Cleanup 00163 os.remove(oldbag) 00164 os.remove(newbag) 00165 00166 00167 if __name__ == '__main__': 00168 rostest.unitrun('pr2_msgs', 'test_pr2_msgs_migration', TestPR2MsgsMigration, sys.argv)