Tf Transforms

There are two methods for looking up tf2 transforms.

Component

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

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

It provides a valid property that indicates if a valid transform has been received. If it is valid, it contains a transform property with the stamped transform geometry_msgs/msg/TransformStamped and for convenience also a translation and a rotation property which refer to the translation and rotation in the transform.

Using the rate property, you can also change the maximum rate at which the transform is updated.

Static

You can also 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 Ros2.now() static methods to look up at specific time points. For the latest, you can pass new Date(0). Be aware that in JavaScript durations are 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 QObject

Public Functions

bool isInitialized() const

Returns true if the TfTransformListener was already initialized, false otherwise. This does not mean it will already receive tf2 data as Ros2 may not be initialized yet.

QVariant canTransform(const QString &target_frame, const QString &source_frame, const rclcpp::Time &time = rclcpp::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 rclcpp::Time &target_time, const QString &source_frame, const rclcpp::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 rclcpp::Time &time = rclcpp::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 rclcpp::Time &target_time, const QString &source_frame, const rclcpp::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.

class TfTransform : public QObject

Represents a tf transform between source and target frame.

Properties

QString sourceFrame

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

QString targetFrame

The target frame of the tf transform, i.e., the frame to which the data should be transformed.

bool enabled

Whether this tf transform is enabled, i.e., receiving transform updates.

QVariantMap transform

The last received transform as a geometry_msgs/msg/TransformStamped with an added boolean valid field and optional error fields. See TfTransformListener::lookUpTransform

QVariantMap message

An alias for transform.

QVariant translation

The translation part of the tf transform as a vector with x, y, z fields. Zero if no valid transform available (yet).

QVariant rotation

The rotation part of the tf transform as a quaternion with w, x, y, z fields. Identity if no valid transform available (yet).

qreal rate

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. Setting to 0 will disable updates.

bool valid

Whether the current transform, i.e., the fields message, translation and rotation are valid.