Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Static Coordinate Transforms in ROS using TF2

Tech Nov 30 13

In ROS, coordinate transformations between fixed reference frames are handled through static transforms. These represent permanent spatial relationships between coordinate systems, such as a lidar sensor mounted on a robot base.

C++ Implementation

For a lidar sensor positioned at (0.5, 0.0, 0.3) relative to the robot base with no rotation, we create two nodes:

Transform Publisher

#include <ros/ros.h>
#include <tf2_ros/static_transform_broadcaster.h>
#include <geometry_msgs/TransformStamped.h>
#include <tf2/LinearMath/Quaternion.h>

int main(int argc, char **argv) {
    ros::init(argc, argv, "static_tf_publisher");
    tf2_ros::StaticTransformBroadcaster static_broadcaster;
    
    geometry_msgs::TransformStamped static_transform;
    static_transform.header.stamp = ros::Time::now();
    static_transform.header.frame_id = "robot_base";
    static_transform.child_frame_id = "lidar_sensor";
    
    static_transform.transform.translation.x = 0.5;
    static_transform.transform.translation.y = 0.0;
    static_transform.transform.translation.z = 0.3;
    
    tf2::Quaternion quat;
    quat.setRPY(0, 0, 0);
    static_transform.transform.rotation.x = quat.x();
    static_transform.transform.rotation.y = quat.y();
    static_transform.transform.rotation.z = quat.z();
    static_transform.transform.rotation.w = quat.w();
    
    static_broadcaster.sendTransform(static_transform);
    ros::spin();
    return 0;
}

Transform Listener

#include <ros/ros.h>
#include <tf2_ros/transform_listener.h>
#include <tf2_geometry_msgs/tf2_geometry_msgs.h>

int main(int argc, char **argv) {
    ros::init(argc, argv, "static_tf_listener");
    ros::NodeHandle nh;
    
    tf2_ros::Buffer tf_buffer;
    tf2_ros::TransformListener tf_listener(tf_buffer);
    
    ros::Rate rate(1);
    while (ros::ok()) {
        geometry_msgs::PointStamped sensor_point;
        sensor_point.header.frame_id = "lidar_sensor";
        sensor_point.header.stamp = ros::Time::now();
        sensor_point.point.x = 2.0;
        sensor_point.point.y = 2.5;
        sensor_point.point.z = 0.3;
        
        try {
            geometry_msgs::PointStamped base_point = 
                tf_buffer.transform(sensor_point, "robot_base");
            ROS_INFO("Base coordinates: (%.2f, %.2f, %.2f)",
                    base_point.point.x,
                    base_point.point.y,
                    base_point.point.z);
        } catch (tf2::TransformException &ex) {
            ROS_WARN("%s", ex.what());
        }
        
        rate.sleep();
    }
    return 0;
}

Python Implementatoin

Transform Publisher

#!/usr/bin/env python
import rospy
import tf2_ros
from geometry_msgs.msg import TransformStamped

if __name__ == "__main__":
    rospy.init_node("static_tf_publisher_py")
    broadcaster = tf2_ros.StaticTransformBroadcaster()
    
    static_transform = TransformStamped()
    static_transform.header.stamp = rospy.Time.now()
    static_transform.header.frame_id = "robot_base"
    static_transform.child_frame_id = "lidar_sensor"
    
    static_transform.transform.translation.x = 0.5
    static_transform.transform.translation.y = 0.0
    static_transform.transform.translation.z = 0.3
    
    static_transform.transform.rotation.w = 1.0
    
    broadcaster.sendTransform(static_transform)
    rospy.spin()

Transform Listener

#!/usr/bin/env python
import rospy
import tf2_ros
from tf2_geometry_msgs import PointStamped

if __name__ == "__main__":
    rospy.init_node("static_tf_listener_py")
    tf_buffer = tf2_ros.Buffer()
    tf_listener = tf2_ros.TransformListener(tf_buffer)
    
    rate = rospy.Rate(1)
    while not rospy.is_shutdown():
        sensor_point = PointStamped()
        sensor_point.header.frame_id = "lidar_sensor"
        sensor_point.header.stamp = rospy.Time.now()
        sensor_point.point.x = 2.0
        sensor_point.point.y = 2.5
        sensor_point.point.z = 0.3
        
        try:
            base_point = tf_buffer.transform(sensor_point, "robot_base")
            rospy.loginfo("Base coordinates: (%.2f, %.2f, %.2f)",
                         base_point.point.x,
                         base_point.point.y,
                         base_point.point.z)
        except tf2_ros.TransformException as ex:
            rospy.logwarn("%s", ex)
        
        rate.sleep()
Tags: ROSTF2

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.