In this tutorial, we'll learn how ROS 1 transforms can help us calculate the position of an object not explicitly linked to a robot – in relation to both the sensor that detected it and the actuator that will interact with it.
Calculating positions in different frames
Let’s consider the robot model from our previous ROS transforms tutorials:
If our robot’s sensor were to detect an object, we'd have that object’s position in the sensor_link frame. To use our robot’s arm to grab it, however, we’d then need to calculate the object’s position in the arm_end_link frame.
With the ROS 1 tf2 library, we can easily transform our object’s position across frames – all without worrying about any of the complicated mathematical operations happening under the hood – and deduce how our arm needs to move to complete this task.
Create a sensor node to simulate object detection
First, let’s create a node that simulates our robot’s sensor.
In a sensor.cpp file, include the following libraries:
Define a Sensor class with the following private variables:
class Sensor: ros::NodeHandle{
private:
// Timer for the simulated detected object
ros::Timer object_timer_;
// Publisher for the simulated detected object
ros::Publisher pose_pub_;
// Pose to publish
geometry_msgs::PoseStamped pose_;
// Aux variable that determines the detected object’s position
int count_ = 0;
It should also include a timer callback, to periodically publish the detected object’s position:
void objectCallback(){
// All transforms must be correctly time-stamped
pose_.header.stamp = ros::Time::now();
pose_.header.frame_id = "sensor_link";
if (count_ % 2) {
pose_.pose.position.x = 1.0;
pose_.pose.position.y = 1.0;
} else {
pose_.pose.position.x = 2.0;
pose_.pose.position.y = 3.0;
}
// Change the detected object’s position, depending on whether count_ is even or odd
count_++;
pose_pub_.publish(pose_);
}
Next, write a class constructor that publishes the detected object’s pose on a timer (every second). Since poses are not transform messages, they will be published on a /detected_object topic (instead of /tf or /tf_static):
Define a TfListener class with the following private variables:
class TfListener : ros::NodeHandle{
private:
// Subscription to pose published by sensor node
ros::Subscriber pose_sub_;
// Buffer that stores several seconds of transforms for easy lookup by the listener
tf2_ros::Buffer tf_buffer_;
// Listener for the broadcast transform message
tf2_ros::TransformListener* tf_listener_;
// Pose in source frame (`sensor_link`)
geometry_msgs::PoseStamped pose_in_;
// Pose in target frame (`arm_end_link`)
geometry_msgs::PoseStamped pose_out_;
It should also include a poseCallback function, which looks up the transform between the source and target frames every time the sensor sends a new pose:
void poseCallback(const geometry_msgs::PoseStamped msg) {
try {
// Store the incoming pose in pose_in_
pose_in_ = msg;
// Transforms the pose between the source frame and target frame
tf_buffer_.transform<geometry_msgs::PoseStamped>(pose_in_, pose_out_,
"arm_end_link", ros::Duration(1));
// Log coordinates of pose in target frame
ROS_INFO("Object pose in 'arm_end_link' is:\n x,y,z = %.1f,%.1f,%.1f",
pose_out_.pose.position.x,
pose_out_.pose.position.y,
pose_out_.pose.position.z);
} catch (const tf2::TransformException & ex) {
ROS_WARN("Could not find object position in 'arm_end_link' frame.");
return;
}
}
Next, write a class constructor that loads and listens to your buffer of transforms, and then subscribe’s to the sensor’s /detected_object pose topic:
public:
TfListener():
NodeHandle("~") {
// Listen to the buffer of transforms
tf_listener_ = new tf2_ros::TransformListener(tf_buffer_);
// Subscribe to pose published by sensor node (check every second)
pose_sub_ = subscribe<geometry_msgs::PoseStamped>
("/detected_object", 10, &TfListener::poseCallback, this);
}
~TfListener(){}
};
Lastly, write a main function that declares and spins the node:
Let’s first publish the transforms between our base_link and both arm_base_link and sensor_link. Use the static_transform_publisher we’ve learned in the previous post:
Open a connection to your data in Foxglove using the Foxglove bridge. Open a 3D panel to visualize the frames and detected object positions, and a Log panel to see the printout of our object’s position.
The object is represented by a purple arrow in the 3D panel. Its position in the arm_end_link frame is printed by the tf_listener node in the Log panel.
Notice that the object arrow is different from the axes arrows that represent each frame. This is because the object is not a transform, but a Pose. If you use the Teleop panel to move the arm_end_link frame around, you can see firsthand how the position of your detected object changes.
Create a launch file
In a launch folder, let’s create a tf_launch.launch file to launch all nodes together:
By leveraging ROS 1 transforms and the tf2 library, you can easily integrate your robots' sensors and actuators to work better together.
For a reference to all the code covered in this post, check out our foxglove/tutorials GitHub repo. And as always, feel free to reach out to us directly in our Discord community to request a topic for the next tutorial!