Go to the documentation of this file.00001
00002 #include "ros1_ros_cpptemplate/publisher.hpp"
00003 #include "ros1_ros_cpptemplate/subscriber.hpp"
00004 #include "ros1_ros_cpptemplate/service.hpp"
00005 #include <ros1_cpptemplate/atomic_fibonacci.hpp>
00006
00007 #include <dynamic_reconfigure/server.h>
00008 #include <ros1_template_msgs/FibonacciConfig.h>
00009
00010 #include <pluginlib/class_list_macros.h>
00011 #include <ros/console.h>
00012 #include <nodelet/nodelet.h>
00013
00014 #include <atomic>
00015 #include <thread>
00016 #include <string>
00017 #include <vector>
00018
00019 namespace ros1_ros_cpptemplate
00020 {
00021
00022 using ros1_cpptemplate::AtomicFibonacci;
00023 using ros1_cpptemplate::AtomicFibonacciPtr;
00024
00025 class Nodelet : public nodelet::Nodelet
00026 {
00027 public:
00028 Nodelet()
00029 {
00030
00031
00032 shutdown_requested_ = false;
00033 }
00034
00035 ~Nodelet()
00036 {
00037 NODELET_INFO_STREAM("Waiting for publishing threads to finish");
00038 shutdown_requested_ = true;
00039
00040 for (int i = 0; i < publish_threads_.size(); ++i)
00041 {
00042 publish_threads_[i].join();
00043 }
00044 }
00045
00046 virtual void onInit()
00047 {
00048 private_node_handle_ = this->getPrivateNodeHandle();
00049
00050
00051 double publish_rate = 10.0;
00052 private_node_handle_.getParam("publish_rate", publish_rate);
00053 int fibonacci_last_number = 0;
00054 private_node_handle_.getParam("start_last_number", fibonacci_last_number);
00055 int fibonacci_current_number = 1;
00056 private_node_handle_.getParam("start_current_number", fibonacci_current_number);
00057
00058
00059
00060
00061 int temp_fibonacci_max_number = 256;
00062
00063
00064
00065 NODELET_INFO_STREAM("Initializing with publish rate " << publish_rate << " Hz");
00066
00067 std::string fibonacci_log_name = "AtomicFibonacci";
00068 atomic_fibonacci_ = std::make_shared<AtomicFibonacci>(fibonacci_last_number, fibonacci_current_number,
00069 temp_fibonacci_max_number, fibonacci_log_name);
00070
00071 reconfigure_server_ = std::make_shared<dynamic_reconfigure::Server<ros1_template_msgs::FibonacciConfig>>
00072 (private_node_handle_);
00073 dynamic_reconfigure::Server<ros1_template_msgs::FibonacciConfig>::CallbackType reconfigure_cb =
00074 boost::bind(&Nodelet::reconfigureCB, this, _1, _2);
00075 reconfigure_server_->setCallback(reconfigure_cb);
00076
00077 std::string internal_publish_topic_name = "fibonacci_publisher_internal";
00078 publisher_used_interally_ = std::make_shared<Publisher>(atomic_fibonacci_, private_node_handle_,
00079 internal_publish_topic_name);
00080 std::string other_publish_topic_name = "fibonacci_publisher_other";
00081 publisher_other_ = std::make_shared<Publisher>(atomic_fibonacci_, private_node_handle_, other_publish_topic_name);
00082
00083 publish_threads_.emplace_back(&Nodelet::run, this, publish_rate, publisher_used_interally_);
00084 publish_threads_.emplace_back(&Nodelet::run, this, publish_rate, publisher_other_);
00085
00086
00087 subscriber_ = std::make_shared<Subscriber>(private_node_handle_, internal_publish_topic_name);
00088 service_ = std::make_shared<Service>(atomic_fibonacci_, private_node_handle_, "fibonacci_service");
00089
00090 NODELET_INFO_STREAM("Initialized");
00091 }
00092
00093 private:
00094 void run(double publish_rate, PublisherPtr publisher)
00095 {
00096 ros::Rate rate(publish_rate);
00097 while (!shutdown_requested_ && ros::ok())
00098 {
00099 publisher->publish();
00100 rate.sleep();
00101 }
00102 }
00103
00104 void reconfigureCB(ros1_template_msgs::FibonacciConfig &config, uint32_t level)
00105 {
00106 atomic_fibonacci_->setMax(config.fibonacci_max_number);
00107 NODELET_INFO_STREAM("Max. Fibonacci number set to " << config.fibonacci_max_number);
00108 }
00109
00110 std::atomic<bool> shutdown_requested_;
00111 ros::NodeHandle private_node_handle_;
00112
00113 std::shared_ptr<dynamic_reconfigure::Server<ros1_template_msgs::FibonacciConfig>> reconfigure_server_;
00114
00115 AtomicFibonacciPtr atomic_fibonacci_;
00116 PublisherPtr publisher_used_interally_;
00117 PublisherPtr publisher_other_;
00118 std::vector<std::thread> publish_threads_;
00119
00120 SubscriberPtr subscriber_;
00121 ServicePtr service_;
00122 };
00123
00124 }
00125
00126 PLUGINLIB_EXPORT_CLASS(ros1_ros_cpptemplate::Nodelet, nodelet::Nodelet);