Creating a subscriber to get pose of the turtle [closed]

asked 2020-06-30 09:11:03 -0500

paradauz gravatar image

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;
}
edit retag flag offensive reopen merge delete

Closed for the following reason question is off-topic or not relevant. Please see http://wiki.ros.org/Support for more details. by gvdhoorn
close date 2020-06-30 09:46:17.231250

Comments

I'm closing this as off-topic.

It is a relevant question, but as you already note yourself:

It's more of a C++ than ROS question

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 the arrow operators.

After that, the syntax in your code snippet should become clear.

gvdhoorn gravatar image gvdhoorn  ( 2020-06-30 09:48:20 -0500 )edit