Creating a subscriber to get pose of the turtle [closed]
Hi, I just started learning ROS and C++ and I am trying to write a subscriber to get data from /turtle1/pose and print the values. I modified the "listener.cpp" file from the tutorial, but I am confused with how the messages are received through the topic. I had a problem with printing the values form the message in poseCallback
function. I got it working, eventually, but only through playing with the code. I don't understand in what form msg
is received as well as how the msg->x, msg->y ...
syntax works. It's more of a C++ than ROS questions, but I think I have a better chance of finding the answer here. I would also really appreciate any feedback or suggestion on how to make it better/more efficient.
This is full code for the subscriber
#include "ros/ros.h"
#include "turtlesim/Pose.h"
void poseCallback(const turtlesim::Pose::ConstPtr& msg) {
ROS_INFO("Position of turtle: x:[%f] y:[%f] theta:[%f] linVel:[%f] angVel:[%f]", msg->x, msg->y, msg->theta, msg->linear_velocity, msg->angular_velocity);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "getting_pose");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/turtle1/pose", 100, poseCallback);
ros::spin();
return 0;
}
I'm closing this as
off-topic
.It is a relevant question, but as you already note yourself:
and it indeed is.
I would advise you to find some resources on C++ programming. Specifically on pointers, references and dereferencing those two and using the
dot
or thearrow
operators.After that, the syntax in your code snippet should become clear.