Topics (Publisher-Subscriber)

Topic dapat digunakan oleh node untuk berkomunikasi satu sama lain. Setiap topik di berisi informasi yang bersesuaian dengan tipe message yang digunakan. Misalnya sebua message_a yang berisi satu variable integer dan satu variable string, di publish oleh sebuah topic_a, kemudian setiap node yang men-subscribe ke topic_a tersebut harus juga memiliki tipe message_a. Satu node bisa mem-publish lebih dari satu topic dan bisa men-subscribe lebih dari satu topic.

Salah satu kelebihan Topic dibandingkan Service adalah bawha Topic merupakan non-blocking communication type, yang berarti program bisa terus dieksekusi.

Contoh nodes yang berkomunikasi via topics:

Publisher-Subscriber Python

Node simple_publisher.py akan publish String message ke topic /rospy_pub_sub/event_out

 0 #!/usr/bin/env python
 1 """
 2 This script is a simple python node which publishes
 3 a std_msgs string.
 4 """
 5 #-*- encoding: utf-8 -*-
 6 __author__ = 'mohammad wasil'
 7 
 8 import rospy
 9 from std_msgs.msg import String
10 
11 if __name__ == '__main__':
12     '''
13     Initilize node and string type publish topic at the loop rate of 10Hz.
14     '''
15     
16     rospy.init_node("rospy_simple_publisher_node", anonymous=False)
17     rate = rospy.Rate(10)
18     pub = rospy.Publisher("rospy_pub_sub/event_out", String, queue_size=1)
19     
20     while not rospy.is_shutdown():
21         string_msg = "Kiriman data dari rospy_simple_publisher_node"
22         pub.publish(string_msg)
23         rate.sleep()
24 

Dan node simple_subscriber.py akan subscribe String message ke topic /rospy_pub_sub/event_out

 0 #!/usr/bin/env python
 1 """
 2 This script is a simple python node which subscribes to
 3 rospy_pub_sub/event_out topic
 4 """
 5 #-*- encoding: utf-8 -*-
 6 __author__ = 'mohammad wasil'
 7 
 8 import rospy
 9 from std_msgs.msg import String
10 
11 def pub_sub_callback(msg):
12     rospy.loginfo("Data yang diterima: %s", msg.data)
13 
14 if __name__ == '__main__':
15     '''
16     Initilize node and string type publish topic at the loop rate of 10Hz.
17     '''
18     
19     rospy.init_node("rospy_simple_subscriber_node", anonymous=False)
20     rate = rospy.Rate(10)
21 
22     while not rospy.is_shutdown():
23 
24         rospy.Subscriber("rospy_pub_sub/event_out", String, pub_sub_callback)
25         
26         rate.sleep()
27 

Dimana:

  • pub_sub_callback: adalah callback dari subscribernya.

Publisher-Subscriber C++

Sama halnya dengan Python code diatas, simple_publisher.cpp akan publish String message ke topic /rospy_pub_sub/event_out

 0 #include <ros/ros.h>
 1 #include <std_msgs/String.h>
 2 
 3 int main(int argc, char **argv)
 4 {
 5   ros::init(argc, argv, "roscpp_simple_publisher_node");
 6   ros::NodeHandle nh;
 7   ros::Publisher publisher = nh.advertise<std_msgs::String>("roscpp_pub_sub/event_out", 1);
 8   ros::Rate loop_rate(10);
 9   while(ros::ok())
10   {
11     std_msgs::String msg;
12     msg.data = "Kiriman data dari node roscpp_simple_publisher_node";
13     publisher.publish(msg);
14     ros::spinOnce();
15     loop_rate.sleep();
16     
17   }
18 
19   return 0;
20 
21 }

Sedangkan node simple_subscriber.cpp akan subscribe String message ke topic /rospy_pub_sub/event_out

 0 #include <ros/ros.h>
 1 #include <std_msgs/String.h>
 2 
 3 void pubSubCallback(const std_msgs::String::ConstPtr& msg)
 4 {
 5   ROS_INFO("Data diterima: %s", msg->data.c_str());
 6 }
 7 
 8 int main(int argc, char **argv)
 9 {
10   ros::init(argc, argv, "roscpp_simple_subscriber_node");
11   ros::NodeHandle nh;
12   
13   ros::Subscriber subscriber = nh.subscribe("roscpp_pub_sub/event_out", 1, pubSubCallback);
14   
15   ros::spin();
16 
17   return 0;
18 
19 }