Implementing a Subscriber Node for Turtle Pose Data in ROS

To monitor and display the reeal-time pose of a turtle in the turtlesim simulation, create a ROS subscriber node that prints pose data as the turtle moves.

First, identify the topic and message type used for pose data. The turtle's pose is published on the topic /turtle1/pose with the message type turtlesim/Pose. Use ROS command-line tools to verify this:

rostopic list
rostopic type /turtle1/pose
rosmsg info turtlesim/Pose

The message structure includes fields for position, orientation, and velocities:

  • float32 x
  • float32 y
  • float32 theta
  • float32 linear_velocity
  • float32 angular_velocity

You can test the topic dircetly in a terminal with:

rostopic echo /turtle1/pose

To build a custom C++ subscriber, create a ROS package with dependencies on roscpp, std_msgs, and turtlesim. Implement a node that subscribes to the pose topic and logs the data.

Key steps for the subscriber:

  1. Enclude necessary headers.
  2. Initialize the ROS node.
  3. Set up a node handle.
  4. Create a subscriber object for the pose topic.
  5. Define a callback function to process incoming messages.
  6. Use ros::spin() to handle callbacks.

Example implementation:

#include <ros/ros.h>
#include <turtlesim/Pose.h>

void poseCallback(const turtlesim::Pose::ConstPtr& msg) {
    ROS_INFO("Turtle pose - x: %.2f, y: %.2f, theta: %.2f, linear velocity: %.2f, angular velocity: %.2f",
             msg->x, msg->y, msg->theta, msg->linear_velocity, msg->angular_velocity);
}

int main(int argc, char** argv) {
    ros::init(argc, argv, "pose_subscriber");
    ros::NodeHandle node;
    ros::Subscriber sub = node.subscribe("/turtle1/pose", 10, poseCallback);
    ros::spin();
    return 0;
}

Configure the package by updating package.xml to include dependencies and modify CMakeLists.txt to compile the node. Build the workspace with catkin_make.

To run the system:

  1. Start the ROS core with roscore.
  2. Launch the turtlesim node: rosrun turtlesim turtlesim_node.
  3. Run the teleoperation node for movement: rosrun turtlesim turtle_teleop_key.
  4. Execute the subscriber node from the workspace:
cd ~/catkin_ws
source devel/setup.bash
rosrun your_package_name pose_subscriber

The subscriber will output pose data to the terminal as the turtle moves, similar to the rostopic echo command.

Tags: ROS subscriber turtlesim C++ pose data

Posted on Mon, 03 Aug 2026 16:58:11 +0000 by homerjsimpson