00001 // own class header first: https://stackoverflow.com/a/2762596/8224054 00002 #include "ros1_ros_cpptemplate/publisher.hpp" 00003 00004 #include <std_msgs/Int32.h> 00005 #include <sstream> 00006 #include <string> 00007 00008 namespace ros1_ros_cpptemplate 00009 { 00010 using ros1_cpptemplate::AtomicFibonacci; 00011 using ros1_cpptemplate::AtomicFibonacciPtr; 00012 00013 Publisher::Publisher(ros1_cpptemplate::AtomicFibonacciPtr atomic_fibonacci, 00014 ros::NodeHandle& node_handle, 00015 const std::string& output_topic_name) 00016 { 00017 int queue_size = 1; 00018 bool latched = false; 00019 publisher_ = node_handle.advertise<std_msgs::Int32>(output_topic_name, queue_size, latched); 00020 atomic_fibonacci_ = atomic_fibonacci; 00021 } 00022 00023 Publisher::~Publisher() 00024 { 00025 } 00026 00027 void Publisher::publish() 00028 { 00029 if (publisher_.getNumSubscribers() == 0) 00030 { 00031 ROS_INFO_STREAM("Nobody subscribed to " << publisher_.getTopic() << ", not publishing"); 00032 return; 00033 } 00034 00035 std::stringstream log_prefix_string_stream; 00036 log_prefix_string_stream << "Publishing on " << publisher_.getTopic() << ": "; 00037 std::string log_prefix = log_prefix_string_stream.str(); 00038 00039 int next_number = atomic_fibonacci_->nextAndLog(log_prefix); 00040 00041 // Ptr to enable intraporcess publishing: 00042 // http://wiki.ros.org/roscpp/Overview/Publishers%20and%20Subscribers#Intraprocess_Publishing 00043 std_msgs::Int32Ptr number_msg(new std_msgs::Int32()); 00044 number_msg->data = next_number; 00045 publisher_.publish(number_msg); 00046 } 00047 00048 } // namespace