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 xfloat32 yfloat32 thetafloat32 linear_velocityfloat32 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:
- Enclude necessary headers.
- Initialize the ROS node.
- Set up a node handle.
- Create a subscriber object for the pose topic.
- Define a callback function to process incoming messages.
- 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:
- Start the ROS core with
roscore. - Launch the turtlesim node:
rosrun turtlesim turtlesim_node. - Run the teleoperation node for movement:
rosrun turtlesim turtle_teleop_key. - 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.