Ask Your Question

How can I subscribe to a turtle's pose that is being published?

asked 2016-02-07 05:56:31 -0600

Archisman gravatar image

I am working on Indigo turtlesim and I am making a turtle move randomly.

This is how I am publishing the pose of a turtle which is randomly moving.

#include "ros/ros.h"
#include "geometry_msgs/Twist.h"
#include "geometry_msgs/Pose.h"
#include <sstream>

using namespace std;

int main(int argc, char **argv)

 // Initiate new ROS node named "pub1"
 ros::init(argc, argv, "pub1");
 ros::NodeHandle n;

 ros::Publisher velocity_publisher = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 1000);
 ros::Rate loop_rate(1); //1 message per second

   int count = 0;
   /* initialize random seed: */
   srand (time(NULL));
   while (ros::ok()) // Keep spinning loop until user presses Ctrl+C

   geometry_msgs::Twist vel_msg;
   //set a random linear velocity in the x-axis
   vel_msg.linear.x =(double)(rand() % 10 + 1)/4.0;
   vel_msg.linear.y =0;
   vel_msg.linear.z =0;
   //set a random angular velocity in the y-axis
   vel_msg.angular.x = 0;
   vel_msg.angular.y = 0;
   vel_msg.angular.z =(double)(rand() % 10 - 5)/2.0;

   //print the content of the message in the terminal
   ROS_INFO("[Random Walk] linear.x = %.2f, angular.z=%.2f\n, %d", vel_msg.linear.x, vel_msg.angular.z, count);

   //publish the message
   ros::spinOnce(); // Need to call this function often to allow ROS to process incoming messages

   loop_rate.sleep(); // Sleep for the rest of the cycle, to enforce the loop rate
   return 0;

And this is how I am trying to subscribe to the "velocity_msg" thing. But unfortunately it is not printing anything probably that is not getting subscribed to the pose.

#include "ros/ros.h"
#include "std_msgs/String.h"

 * This tutorial demonstrates simple receipt of messages over the ROS system.
void chatterCallback(const std_msgs::String::ConstPtr& vel_msg)
  ROS_INFO("%s", vel_msg->data.c_str());

int main(int argc, char **argv)
   * The ros::init() function needs to see argc and argv so that it can perform
   * any ROS arguments and name remapping that were provided at the command line.
   * For programmatic remappings you can use a different version of init() which takes
   * remappings directly, but for most command-line programs, passing argc and argv is
   * the easiest way to do it.  The third argument to init() is the name of the node.
   * You must call one of the versions of ros::init() before using any other
   * part of the ROS system.
  ros::init(argc, argv, "listener");

   * NodeHandle is the main access point to communications with the ROS system.
   * The first NodeHandle constructed will fully initialize this node, and the last
   * NodeHandle destructed will close down the node.
   ros::NodeHandle n;

   * The subscribe() call is how you tell ROS that you want to receive messages
   * on a given topic.  This invokes a call to the ROS
   * master node, which keeps a registry of who is publishing and who
   * is subscribing.  Messages are passed to a callback function, here
   * called chatterCallback.  subscribe() returns a Subscriber object that you
   * must hold on to until you want to unsubscribe.  When all copies of the Subscriber
   * object go out of scope, this callback will automatically be unsubscribed from
   * this ...
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-02-08 05:39:46 -0600

Handwerker gravatar image

updated 2016-02-08 05:40:33 -0600

please first understand the tutorial:ROS/Tutorials/WritingPublisherSubscriber(c++)

Note: The topic name of the publisher and subscriber that you creat must be identical! you can remap the topic name by name Remapping at the Roslaunch and Remapping Arguments at the command-line

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-02-07 05:55:42 -0600

Seen: 1,121 times

Last updated: Feb 08 '16