Skip to content

useTF

Subscribes to the transform of frameId (relative to fixedFrame) via tf2_web_republisher, re-rendering with each update.

tsx
import { useTF } from "roshooks";

const transform = useTF({ frameId: "base_link", fixedFrame: "odom" });

Options

OptionTypeDefaultDescription
frameIdstringThe TF frame to subscribe to, e.g. "base_link".
fixedFramestring"base_link"The fixed frame to express the transform in.
angularThresnumber2.0Angular threshold used by the TF republisher.
transThresnumber0.01Translation threshold used by the TF republisher.
ratenumber10.0Update rate (Hz) for the TF republisher.
updateDelaynumber50Time (ms) to wait after a new subscription before updating the republisher's list of TFs.
topicTimeoutnumber2.0Timeout parameter for the TF republisher.
serverNamestring"/tf2_web_republisher"The name of the tf2_web_republisher server/action.
ros2booleanfalseUse the ROS 2 action-based client (ROS2TFClient) instead of the ROS 1 service-based one (TFClient).

Returns

Transform | null — the latest transform ({ translation, rotation }), or null until the first one arrives.

Example

tsx
function BaseLinkPosition() {
  const transform = useTF({ frameId: "base_link", fixedFrame: "map" });

  if (!transform) return <p>waiting for tf…</p>;

  const { x, y, z } = transform.translation;
  return (
    <p>
      x: {x.toFixed(2)}, y: {y.toFixed(2)}, z: {z.toFixed(2)}
    </p>
  );
}

ROS 2

tsx
const transform = useTF({ frameId: "base_link", fixedFrame: "odom", ros2: true });

TIP

Each call to useTF creates its own TFClient/ROS2TFClient and tears it down on unmount. If several components in your app watch the same frame, consider the shared-atom pattern from Global State with Jotai to keep it to a single republisher subscription.

Released under the BSD-3 License.