|
| 1 | +#!/usr/bin/python |
| 2 | +""" |
| 3 | +Copyright (c) 2015, |
| 4 | +Clearpath Robotics, Inc. |
| 5 | +Enrique Fernandez Perdomo |
| 6 | +All rights reserved. |
| 7 | +
|
| 8 | +Redistribution and use in source and binary forms, with or without |
| 9 | +modification, are permitted provided that the following conditions are met: |
| 10 | + * Redistributions of source code must retain the above copyright |
| 11 | + notice, this list of conditions and the following disclaimer. |
| 12 | + * Redistributions in binary form must reproduce the above copyright |
| 13 | + notice, this list of conditions and the following disclaimer in the |
| 14 | + documentation and/or other materials provided with the distribution. |
| 15 | + * Neither the name of Systems, Robotics and Vision Group, University of |
| 16 | + the Balearican Islands nor the names of its contributors may be used to |
| 17 | + endorse or promote products derived from this software without specific |
| 18 | + prior written permission. |
| 19 | +
|
| 20 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 21 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 22 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | +DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
| 24 | +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 25 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 27 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | +""" |
| 31 | + |
| 32 | + |
| 33 | +import rospy |
| 34 | +import rosbag |
| 35 | +import tf.transformations as tft |
| 36 | +from geometry_msgs.msg import Transform, Vector3, Quaternion |
| 37 | + |
| 38 | +import numpy |
| 39 | +import argparse |
| 40 | + |
| 41 | + |
| 42 | +def transform_vector3_msg_to_tf(msg): |
| 43 | + return tft.translation_matrix([msg.x, msg.y, msg.z]) |
| 44 | + |
| 45 | + |
| 46 | +def transform_tf_to_vector3_msg(tf): |
| 47 | + return Vector3(*tf) |
| 48 | + |
| 49 | + |
| 50 | +def transform_quaternion_msg_to_tf(msg): |
| 51 | + return tft.quaternion_matrix([msg.x, msg.y, msg.z, msg.w]) |
| 52 | + |
| 53 | + |
| 54 | +def transform_tf_to_quaternion_msg(tf): |
| 55 | + q = tft.quaternion_from_euler(*rotation) |
| 56 | + return Quaternion(*q) |
| 57 | + |
| 58 | + |
| 59 | +def transform_msg_to_tf(msg): |
| 60 | + translation = transform_vector3_msg_to_tf(msg.translation) |
| 61 | + rotation = transform_quaternion_msg_to_tf(msg.rotation) |
| 62 | + |
| 63 | + return numpy.dot(rotation, translation) |
| 64 | + |
| 65 | + |
| 66 | +def transform_tf_to_msg(tf): |
| 67 | + _, _, rotation, translation, _ = tft.decompose_matrix(tf) |
| 68 | + |
| 69 | + translation = transform_tf_to_vector3_msg(translation) |
| 70 | + rotation = transform_tf_to_quaternion_msg(rotation) |
| 71 | + |
| 72 | + return Transform(translation, rotation) |
| 73 | + |
| 74 | + |
| 75 | +def transform_tf(inbag, outbag, transform, frame_id, child_frame_id): |
| 76 | + rospy.loginfo(' Processing input bagfile: %s', inbag) |
| 77 | + rospy.loginfo(' Writing to output bagfile: %s', outbag) |
| 78 | + rospy.loginfo(' Transforming TF: %s -> %s' % (frame_id, child_frame_id)) |
| 79 | + |
| 80 | + outbag = rosbag.Bag(outbag,'w') |
| 81 | + for topic, msg, t in rosbag.Bag(inbag, 'r').read_messages(): |
| 82 | + if topic == "/tf": |
| 83 | + new_transforms = [] |
| 84 | + for tf in msg.transforms: |
| 85 | + if tf.header.frame_id == frame_id and tf.child_frame_id == child_frame_id: |
| 86 | + tf_transform = transform_msg_to_tf(tf.transform) |
| 87 | + tf_transform = numpy.dot(tf_transform, transform) |
| 88 | + |
| 89 | + tf.transform = transform_tf_to_msg(tf_transform) |
| 90 | + new_transforms.append(tf) |
| 91 | + msg.transforms = new_transforms |
| 92 | + outbag.write(topic, msg, t) |
| 93 | + |
| 94 | + rospy.loginfo('Closing output bagfile and exit...') |
| 95 | + outbag.close(); |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + parser = argparse.ArgumentParser( |
| 100 | + description='transform all transforms from the /tf topic that contain one of the given frame_ids in the header as parent.') |
| 101 | + |
| 102 | + parser.add_argument('-i', metavar='INPUT_BAGFILE', required=True, help='input bagfile') |
| 103 | + parser.add_argument('-o', metavar='OUTPUT_BAGFILE', required=True, help='output bagfile') |
| 104 | + parser.add_argument('-x', metavar='TRANSFORM_X', required=False, type=float, default=0.0, help='transform translation on x-axis [m]') |
| 105 | + parser.add_argument('-y', metavar='TRANSFORM_Y', required=False, type=float, default=0.0, help='transform translation on y-axis [m]') |
| 106 | + parser.add_argument('-z', metavar='TRANSFORM_Z', required=False, type=float, default=0.0, help='transform translation on z-axis [m]') |
| 107 | + parser.add_argument('-R', metavar='TRANSFORM_ROLL', required=False, type=float, default=0.0, help='transform rotation roll [deg]') |
| 108 | + parser.add_argument('-P', metavar='TRANSFORM_PITCH', required=False, type=float, default=0.0, help='transform rotation pitch [deg]') |
| 109 | + parser.add_argument('-Y', metavar='TRANSFORM_YAW', required=False, type=float, default=0.0, help='transform rotation yaw [deg]') |
| 110 | + parser.add_argument('-f', metavar='FRAME_ID', required=True, help='frame_id of the transform to change/transform from the /tf topic') |
| 111 | + parser.add_argument('-c', metavar='CHILD_FRAME_ID', required=True, help='child_frame_id of the transform to change/transform from the /tf topic') |
| 112 | + |
| 113 | + args = parser.parse_args() |
| 114 | + |
| 115 | + # Create transform: |
| 116 | + translation = numpy.array([args.x, args.y, args.z]) |
| 117 | + rotation = numpy.deg2rad([args.R, args.P, args.Y]) |
| 118 | + |
| 119 | + transform = tft.compose_matrix(angles=rotation, translate=translation) |
| 120 | + |
| 121 | + try: |
| 122 | + transform_tf(args.i, args.o, transform, args.f, args.c) |
| 123 | + except Exception, e: |
| 124 | + import traceback |
| 125 | + traceback.print_exc() |
0 commit comments