getTransform

Retrieve transformation between two coordinate frames

The behavior of getTransform changed in R2018a. When using the tftree input argument, the function no longer returns an empty transform when the transform is unavailable and no sourcetime is specified. If getTransform waits for the specified timeout period and the transform is still not available, the function returns an error. The timeout period is 0 by default.

Syntax

tf = getTransform(tftree,targetframe,sourceframe)
tf = getTransform(tftree,targetframe,sourceframe,sourcetime)
tf = getTransform(___,"Timeout",timeout)
tf = getTransform(bagSel,targetframe,sourceframe)
tf = getTransform(bagSel,targetframe,sourceframe,sourcetime)

Description

tf = getTransform(tftree,targetframe,sourceframe) returns the latest known transformation between two coordinate frames in tftree. Create the tftree object using rostf, which requires a connection to a ROS network.

Transformations are structured as a 3-D translation (three-element vector) and a 3-D rotation (quaternion).

tf = getTransform(tftree,targetframe,sourceframe,sourcetime) returns the transformation from the tftree at the given source time. If the transformation is not available at that time, an error is displayed.

example

tf = getTransform(___,"Timeout",timeout) also specifies a timeout period, in seconds, to wait for the transformation to be available. Otherwise, if the transformation doesn't become available in the timeout period, the function returns an error. This option can be combined with the previous syntaxes.

example

tf = getTransform(bagSel,targetframe,sourceframe) returns the latest transformation between two frames in the rosbag in bagSel. To get the bagSel input, load a rosbag using rosbag.

tf = getTransform(bagSel,targetframe,sourceframe,sourcetime) returns the transformation at the specified sourcetime in the rosbag in bagSel.

Examples

collapse all

This example shows how to set up a ROS transformation tree and transform frames based on this information. It uses time-buffered transformations to access transformations at different times.

Create a ROS transformation tree. Use rosinit to connect to a ROS network. Replace ipaddress with your ROS network address.

ipaddress = '192.168.203.129';
rosinit(ipaddress)
tftree = rostf;
pause(1)
Initializing global node /matlab_global_node_60416 with NodeURI http://192.168.203.1:53781/

Look at the available frames on the transformation tree.

tftree.AvailableFrames
ans =

  36×1 cell array

    {'base_footprint'            }
    {'base_link'                 }
    {'camera_depth_frame'        }
    {'camera_depth_optical_frame'}
    {'camera_link'               }
    {'camera_rgb_frame'          }
    {'camera_rgb_optical_frame'  }
    {'caster_back_link'          }
    {'caster_front_link'         }
    {'cliff_sensor_front_link'   }
    {'cliff_sensor_left_link'    }
    {'cliff_sensor_right_link'   }
    {'gyro_link'                 }
    {'mount_asus_xtion_pro_link' }
    {'odom'                      }
    {'plate_bottom_link'         }
    {'plate_middle_link'         }
    {'plate_top_link'            }
    {'pole_bottom_0_link'        }
    {'pole_bottom_1_link'        }
    {'pole_bottom_2_link'        }
    {'pole_bottom_3_link'        }
    {'pole_bottom_4_link'        }
    {'pole_bottom_5_link'        }
    {'pole_kinect_0_link'        }
    {'pole_kinect_1_link'        }
    {'pole_middle_0_link'        }
    {'pole_middle_1_link'        }
    {'pole_middle_2_link'        }
    {'pole_middle_3_link'        }
    {'pole_top_0_link'           }
    {'pole_top_1_link'           }
    {'pole_top_2_link'           }
    {'pole_top_3_link'           }
    {'wheel_left_link'           }
    {'wheel_right_link'          }

Check if the desired transformation is available now. For this example, check for the transformation from 'camera_link' to 'base_link'.

canTransform(tftree,'base_link','camera_link')
ans =

  logical

   1

Get the transformation for 3 seconds from now. getTransform will wait until the transformation becomes available with the specified timeout.

desiredTime = rostime('now') + 3;
tform = getTransform(tftree,'base_link','camera_link',...
                     desiredTime,'Timeout',5);

Create a ROS message to transform. Messages could also be retrieved off the ROS network.

pt = rosmessage('geometry_msgs/PointStamped');
pt.Header.FrameId = 'camera_link';
pt.Point.X = 3;
pt.Point.Y = 1.5;
pt.Point.Z = 0.2;

Transform the ROS message to the 'base_link' frame using the desired time saved from before.

tfpt = transform(tftree,'base_link',pt,desiredTime);

Optional: You can also use apply with the stored tform to apply this transformation to the pt message.

tfpt2 = apply(tform,pt);

Shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_60416 with NodeURI http://192.168.203.1:53781/

This example shows how to access time-buffered transformations on the ROS network. Access transformations for specific times and modify the BufferTime property based on your desired times.

Create a ROS transformation tree. Use rosinit to connect to a ROS network. Replace ipaddress with your ROS network address.

ipaddress = '192.168.203.129';
rosinit(ipaddress)
tftree = rostf;
pause(2);
Initializing global node /matlab_global_node_29163 with NodeURI http://192.168.203.1:53691/

Get the transformation from 1 second ago.

desiredTime = rostime('now') - 1;
tform = getTransform(tftree,'base_link','camera_link',desiredTime);

The transformation buffer time is 10 seconds by default. Modify the BufferTime property of the transformation tree to increase the buffer time and wait for that buffer to fill.

tftree.BufferTime = 15;
pause(15);

Get the transformation from 12 seconds ago.

desiredTime = rostime('now') - 12;
tform = getTransform(tftree,'base_link','camera_link',desiredTime);

You can also get transformations at a time in the future. getTransform will wait until the transformation is available. You can also specify a timeout to error out if no transformation is found. This example waits 5 seconds for the transformation at 3 seconds from now to be available.

desiredTime = rostime('now') + 3;
tform = getTransform(tftree,'base_link','camera_link',desiredTime,'Timeout',5);

Shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_29163 with NodeURI http://192.168.203.1:53691/

Get transformations from rosbag (.bag) files by loading the rosbag and checking the available frames. From these frames, use getTransform to query the transformation between two coordinate frames.

Load the rosbag.

bag = rosbag('ros_turtlesim.bag');

Get a list of available frames.

frames = bag.AvailableFrames;

Get the latest transformation between two coordinate frames.

tf = getTransform(bag,'world',frames{1});

Check for a transformation available at a specific time and retrieve the transformation. Use canTransform to check if the transformation is available. Specify the time using rostime.

tfTime = rostime(bag.StartTime + 1);
if (canTransform(bag,'world',frames{1},tfTime))
    tf2 = getTransform(bag,'world',frames{1},tfTime);
end

Input Arguments

collapse all

ROS transformation tree, specified as a TransformationTree object handle. You can create a transformation tree by calling the rostf function.

Selection of rosbag messages, specified as a BagSelection object handle. To create a selection of rosbag messages, use rosbag.

Target coordinate frame, specified as a string scalar or character vector. You can view the available frames for transformation by calling tftree.AvailableFrames.

Initial coordinate frame, specified as a string scalar or character vector. You can view the available frames for transformation by calling tftree.AvailableFrames.

ROS or system time, specified as a Time object handle. By default, sourcetime is the ROS simulation time published on the clock topic. If the use_sim_time ROS parameter is set to true, sourcetime returns the system time. You can create a Time object using rostime.

Timeout for receiving transform, specified as a scalar in seconds. The function returns an error if the timeout is reached and no transform becomes available.

Output Arguments

collapse all

Transformation between coordinate frames, returned as a TransformStamped object handle. Transformations are structured as a 3-D translation (three-element vector) and a 3-D rotation (quaternion).

Introduced in R2015a