Publisher/Subscriber

From Introduction to Robotics (CS460)
Jump to: navigation, search
Creating Publisher and Subscriber for TurtleSim in ROS

This tutorial explains the creation of publisher and subscriber. It also explains how to get the message details and call backs for each subscriber. In second portion it creates a publisher for turtlesim.

In order to create the publisher and subscriber for turtlesim provided by ROS follow the following steps.

1. run roscore on the terminal, For this open the terminal and write roscore.

File:Roscore.jpg

2. Open new terminal using Ctrl+Shift+N or using mouse and run the turtlesim using following command rosrun turtlesim turtlesim_node

File:Tsim.jpg

3. See the list of topic , for this open new terminal and write rostopic list

File:Rstopiclist.jpg
4. There are three topics for the turtlesim naming

/turtle1/cmd_vel
/turtle1/color_sensor
/turtle1/pose

5. Now lets see which topic need subscriber and which one need publisher, for this run the following command

rostopic info /turtle1/color_sensor

File:Colorpose.jpg


and this shows it needs a subscriber as it is a published topic having type " turtlesim/Color "

now run the command " rostopic info /turtle1/pose "

File:Cmdvel.jpg

and this shows it needs a publisher as it is a subscriber on the topic " geometry_msgs/Twist "

6. Once we know about the subscribers and publisher , we can continue our programming. First we will create subscriber, so in order to create , create an empty file named subscriber_tsim.cpp . and copy the following code.



#include "ros/ros.h" 
#include "turtlesim/Color.h" 
#include "turtlesim/Pose.h"

// Topic messages callback
void color_callback(const turtlesim::Color::ConstPtr& col)
{
ROS_INFO("red: %d, green: %d, blue: %d", col->r, col->g, col->b);
}

//pose_callback
void pose_callback(const turtlesim::Pose& msgIn){

ROS_INFO_STREAM(std::setprecision(2) << std::fixed
<< "position=(" << msgIn.x << "," << msgIn.y << ")"
<< "direction=" << msgIn.theta); 
  
}

int main(int argc, char **argv)
{
    // Initiate a new ROS node named "subTsim"
 ros::init(argc, argv, "subTsim");
 //create a node handle: it is reference assigned to a new node
 ros::NodeHandle node;

    // Subscribe to a given topic, in this case "chatter".
 //color_callback: is the name of the callback function that will be executed each time a message is received.
    ros::Subscriber subscriber_color = node.subscribe("/turtle1/color_sensor", 1000, color_callback);

    
    ros::Subscriber subscriber_pose=node.subscribe("turtle1/pose", 1000, pose_callback);
    // Enter a loop, pumping callbacks
    ros::spin();

    return 0;
}

7. Now, we have to change the makings on CMakeLists.txt . add the following lines

add_executable(subtsim src/subscriber_tsim.cpp)
target_link_libraries (subtsim ${catkin_LIBRARIES})
add_dependencies(subtsim beginner_tutorials_generate_message_cpp)

Now, open the terminal and compile the code. for this type following

cd catkin_ws
catkin_make

if the compilation is successful then we can proceed.

9. now run the node we created , for this run the following code. rosrun beginner_tutorials subtsim

10. Now, we will create the publisher, for this create a file named publisher_tsim.cpp and copy the following code

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


int main(int argc, char **argv)
{
 // Initiate new ROS node named "talker"
 ros::init(argc, argv, "pubTsim");
 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", vel_msg.linear.x, vel_msg.angular.z);

    //publish the message
    velocity_publisher.publish(vel_msg);
       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
       count++;
   }
   return 0;
}

save the file.

11. Now go to CMakeLists.txt and add the following lines

add_executable(pubtsim src/publisher_tsim.cpp)
target_link_libraries (pubtsim ${catkin_LIBRARIES}) 
add_dependencies(pubtsim beginner_tutorials_generate_message_cpp)


12. Now, open the terminal and compile the code. for this write cd catkin_ws catkin_make

13. Now, launch the node, this will make the turtlesim run as random walk. you can also view the values from subscriber node.

File:Finalout.jpg

Video Tutorial

14. For more details you can watch the video tutorial at