Tf Transforms

There are two methods for looking up tf2 transforms.

Component

The TfTransform component can be used to subscribe to transforms between two frames.

TfTransform {
  id: tfTransform
  active: true // This is the default, if false no updates will be received
  sourceFrame: "base_link"
  targetFrame: "odom"
}

Static

You can use the TfTransformListener singleton to look up transforms if you just need it once.

Button {
  text: "Look Up"
  onClicked: {
    var transformStamped = TfTransformListener.lookUpTransform(inputTargetFrame.text, inputSourceFrame.text)
    if (!transformStamped.valid)
    {
      transformResult.text = "Transform from '" + inputSourceFrame.text + "' to '" + inputTargetFrame.text + "' was not valid!\n" +
                              "Exception: " + transformStamped.exception + "\nMessage: " + transformStamped.message
      return
    }
    transformResult.text = "Position:\n" + printVector3(transformStamped.transform.translation) + "\nOrientation:\n" + printRotation(transformStamped.transform.rotation)
  }
}

Use the provided Time.now() static methods to look up at specific time points. For the latest, you can pass new Date(0). Be aware that in ros::Duration the double constructor argument represents seconds whereas here the duration is given in milliseconds.

Warning

Be aware that canLookUp can return a boolean value or a string error message. You should explicitly test for that since strings are truthy, too.

API

class TfTransformListener : public QObjectRos

Public Functions

QVariant canTransform(const QString &target_frame, const QString &source_frame, const ros::Time &time = ros::Time(0), double timeout = 0) const

Checks if a transform is possible. Returns true if possible, otherwise either false or if available a message why the transform failed.

Parameters
  • target_frame – The frame into which to transform.

  • source_frame – The frame from which to transform.

  • time – The time at which to transform in seconds.

  • timeout – How long to block before failing in milliseconds. Set to 0 for no timeout.

Returns

True if the transform is possible, otherwise an error message (string) if available, false if not.

QVariant canTransform(const QString &target_frame, const ros::Time &target_time, const QString &source_frame, const ros::Time &source_time, const QString &fixed_frame, double timeout = 0) const

Checks if a transform is possible. Returns true if possible, otherwise either false or if available a message why the transform failed.

Parameters
  • target_frame – The frame into which to transform.

  • target_time – The time into which to transform.

  • source_frame – The frame from which to transform.

  • source_time – The time from which to transform.

  • fixed_frame – The frame in which to treat the transform as constant in time.

  • timeout – How long to block before failing in milliseconds. Set to 0 for no timeout.

Returns

True if the transform is possible, otherwise an error message (string) if available, false if not.

QVariantMap lookUpTransform(const QString &target_frame, const QString &source_frame, const ros::Time &time = ros::Time(0), double timeout = 0)

Get the transform between two frames by frame id.

Parameters
  • target_frame – The frame to which the data should be transformed.

  • source_frame – The frame where the data originated.

  • time – The time at which the value of the transform is desired. Set to 0 for latest.

  • timeout – How long to block before failing in milliseconds. Set to 0 for no timeout.

Returns

A map containing a boolean valid field. If valid is true it also contains the transform. If valid is false, it might contain more information, e.g., an exception field with the name of the exception and a message field containing more information about the reason of failure.

QVariantMap lookUpTransform(const QString &target_frame, const ros::Time &target_time, const QString &source_frame, const ros::Time &source_time, const QString &fixed_frame, double timeout = 0)

Get the transform between two frames by frame id.

Parameters
  • target_frame – The frame to which the data should be transformed.

  • target_time – The time to which the data should be transformed. Set to 0 for latest.

  • source_frame – The frame where the data originated.

  • source_time – The time at which the source_frame should be evaluated. Set to 0 for latest.

  • fixed_frame – The frame in which to assume the transform is constant in time.

  • timeout – How long to block before failing in milliseconds. Set to 0 for no timeout.

Returns

A map containing a boolean valid field. If valid is true it also contains the transform. If valid is false, it might contain more information, e.g., an exception field with the name of the exception and a message field containing more information about the reason of failure.

void unregisterWrapper()

If the count of wrappers gets to zero, the resources of this singleton will be freed.

Signals

void transformChanged()

Emitted whenever a new transform arrived. Warning this signal is not throttled!

class TfTransform : public QObject

Represents a tf transform between source and target frame.

Public Functions

TfTransform()

The source frame of the tf transform, i.e., the frame where the data originated.

The target frame of the tf transform, i.e., the frame to which the data should be transformed. Whether this tf transform is enabled, i.e., receiving transform updates. Alias for enabled The last received transform as a geometry_msgs/TransformStamped with an added boolean valid field and optional error fields. See TfTransformListener::lookUpTransform An alias for transform. The translation part of the tf transform as a vector with x, y, z fields. Zero if no valid transform available (yet). The rotation part of the tf transform as a quaternion with w, x, y, z fields. Identity if no valid transform available (yet). The maximum rate in Hz at which tf updates are processed and emitted as changed signals. Default: 60 Note: The rate can not exceed 1000. To disable rate limiting set to 0. Whether the current transform, i.e., the fields message, translation and rotation are valid.