top of page

Publishing and subscribing a topic in ROS

  • Shivangi
  • Jan 3, 2017
  • 2 min read

In previous tutorial we learnt how to create catkin workspaces and packages inside these workspaces. In this article we will be discussing about how to publish and subscribe a topic. So lets get started!!!

A brief intro:

Topics are the basic way of communicating between two nodes. A topic is uniquely identified by its name. Nodes can publish data to a topic or subscribe to data published onto it. So lets proceed towards publishing and subscribing a topic :

1.First open the terminal and type the command:

gedit

Now in this editor we will write our C++code for publisher node and subscriber node.

2.Copy the following publisher code with name talker.cpp inside the src folder inside the package created ,here say demo_pkg and paste it in text editor:

#include "ros/ros.h"

#include "std_msgs/Int32.h"

#include <iostream>

int main(int argc, char **argv)

{

ros::init(argc, argv,"talker");

ros::NodeHandle node_obj;

ros::Publisher number_publisher =

node_obj.advertise<std_msgs::Int32>("/numbers",10);

ros::Rate loop_rate(10);

int number_count = 0;

while (ros::ok())

{

std_msgs::Int32 msg;

msg.data = number_count;

ROS_INFO("%d",msg.data);

number_publisher.publish(msg);

ros::spinOnce();

loop_rate.sleep();

++number_count;

}

return 0;

}

3.Now similarly repeat the same procedure for subscriber node with name listener.cpp

inside the src folder inside the demo_pkg created:

#include "ros/ros.h"

#include "std_msgs/Int32.h"

#include <iostream>

void number_callback(const std_msgs::Int32::ConstPtr& msg)

{

ROS_INFO("Received [%d]",msg->data);

}

int main(int argc, char **argv)

{

ros::init(argc, argv,"listener");

ros::NodeHandle node_obj;

ros::Subscriber number_subscriber = node_obj.subscribe("/numbers",10,number_callback);

ros::spin();

return 0;

}

4.Now we will edit our CMakeList inside the package created(not the one which is inside the src folder of catkin workspace) to compile and build the source code.

Following is the structure of the list before editing it:

* cmake_minimum_required(VERSION 2.8.3)

* project(demo_pkg)

* ## Find catkin and any catkin packages

* find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg)

*

* ## Declare ROS messages and services

* add_message_files(DIRECTORY msg FILES Num.msg)

* add_service_files(DIRECTORY srv FILES AddTwoInts.srv)

*

* ## generate_messages(DEPENDENCIES std_msgs)

* Generate added messages and services

*

* ## Declare a catkin package

* catkin_package()

(Note:Remember order of CmakeList is very important while we are editing)

Now we will add the following lines:

#This will create executables of the nodes

add_executable(talker src/talker.cpp)

#This will generate message header file before building the target

add_dependencies(talker demo_pkg_generate_messages_cpp)

#This will link executables to the appropriate libraries

target_link_libraries(talker ${catkin_LIBRARIES})

#This will create executables of the nodes

add_executable(listener src/listener.cpp)

#This will generate message header file before building the target

add_dependencies(listener demo_pkg_generate_messages_cpp)

#This will link executables to the appropriate libraries

target_link_libraries(listener ${catkin_LIBRARIES})

After adding these lines CmakeList appears as:

*cmake_minimum_required(VERSION 2.8.3)

* project(demo_pkg) *

* ## Find catkin and any catkin packages

* find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg)

*

* ## Declare ROS messages and services

* add_message_files(FILES Num.msg)

* add_service_files(FILES AddTwoInts.srv)

*

* ## Generate added messages and services

* generate_messages(DEPENDENCIES std_msgs)

*

* ## Declare a catkin package

* catkin_package()

*

* ## Build talker and listener

* include_directories(include ${catkin_INCLUDE_DIRS})

*

* add_executable(talker src/talker.cpp)

* add_dependencies(talker beginner_tutorials_generate_messages_cpp)

* target_link_libraries(talker ${catkin_LIBRARIES})

*

* add_executable(listener src/listener.cpp)

* add_dependencies(listener beginner_tutorials_generate_messages_cpp)

* target_link_libraries(listener ${catkin_LIBRARIES})

5.In this step open new terminal and type the following commands:

*cd ~/catkin_ws/

*catkin_make

6.If your build is successful then type :

roscore

(This command is given to start masternode)

7.Open two new terminals and type following commands respectively in each:

*rosrun demo_pkg talker

*rosrun demo_pkg listener

Hence you will see the nodes one publishing the topic and other subscribing to it!!!

Here is the video for above tutorial:


Commenti


Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page