nodelet.cpp
Go to the documentation of this file.
1 
6 
7 #include <dynamic_reconfigure/server.h>
8 #include <ros1_template_msgs/FibonacciConfig.h>
9 
11 #include <ros/console.h>
12 #include <nodelet/nodelet.h>
13 
14 #include <atomic>
15 #include <thread>
16 #include <string>
17 #include <vector>
18 
19 namespace ros1_ros_cpptemplate
20 {
21 
24 
25 class Nodelet : public nodelet::Nodelet
26 {
27 public:
29  {
30  // initialize simple member variables here instead of the
31  // initializer list for cleaner structure
32  shutdown_requested_ = false;
33  }
34 
36  {
37  NODELET_INFO_STREAM("Waiting for publishing threads to finish");
38  shutdown_requested_ = true;
39 
40  for (int i = 0; i < publish_threads_.size(); ++i)
41  {
42  publish_threads_[i].join();
43  }
44  }
45 
46  virtual void onInit()
47  {
49 
50  // get fixed parameters
51  double publish_rate = 10.0;
52  private_node_handle_.getParam("publish_rate", publish_rate);
53  int fibonacci_last_number = 0;
54  private_node_handle_.getParam("start_last_number", fibonacci_last_number);
55  int fibonacci_current_number = 1;
56  private_node_handle_.getParam("start_current_number", fibonacci_current_number);
57 
58  // The max number will be set by the reconfigure callback. When the reconfigure
59  // server is constructed it loads the values from the param server (from the files)
60  // and triggers a callback.
61  int temp_fibonacci_max_number = 256;
62 
63  // NODELET logging will put the name of the nodelet instead of
64  // the nodelet manager into the log
65  NODELET_INFO_STREAM("Initializing with publish rate " << publish_rate << " Hz");
66 
67  std::string fibonacci_log_name = "AtomicFibonacci";
68  atomic_fibonacci_ = std::make_shared<AtomicFibonacci>(fibonacci_last_number, fibonacci_current_number,
69  temp_fibonacci_max_number, fibonacci_log_name);
70 
71  reconfigure_server_ = std::make_shared<dynamic_reconfigure::Server<ros1_template_msgs::FibonacciConfig>>
73  dynamic_reconfigure::Server<ros1_template_msgs::FibonacciConfig>::CallbackType reconfigure_cb =
74  boost::bind(&Nodelet::reconfigureCB, this, _1, _2);
75  reconfigure_server_->setCallback(reconfigure_cb);
76 
77  std::string internal_publish_topic_name = "fibonacci_publisher_internal";
79  internal_publish_topic_name);
80  std::string other_publish_topic_name = "fibonacci_publisher_other";
81  publisher_other_ = std::make_shared<Publisher>(atomic_fibonacci_, private_node_handle_, other_publish_topic_name);
82 
83  publish_threads_.emplace_back(&Nodelet::run, this, publish_rate, publisher_used_interally_);
84  publish_threads_.emplace_back(&Nodelet::run, this, publish_rate, publisher_other_);
85 
86  // subsciber and service stuff last, or at least after everything else is ready
87  subscriber_ = std::make_shared<Subscriber>(private_node_handle_, internal_publish_topic_name);
88  service_ = std::make_shared<Service>(atomic_fibonacci_, private_node_handle_, "fibonacci_service");
89 
90  NODELET_INFO_STREAM("Initialized");
91  }
92 
93 private:
94  void run(double publish_rate, PublisherPtr publisher)
95  {
96  ros::Rate rate(publish_rate);
97  while (!shutdown_requested_ && ros::ok())
98  {
99  publisher->publish();
100  rate.sleep();
101  }
102  }
103 
104  void reconfigureCB(ros1_template_msgs::FibonacciConfig &config, uint32_t level)
105  {
106  atomic_fibonacci_->setMax(config.fibonacci_max_number);
107  NODELET_INFO_STREAM("Max. Fibonacci number set to " << config.fibonacci_max_number);
108  }
109 
110  std::atomic<bool> shutdown_requested_;
112 
113  std::shared_ptr<dynamic_reconfigure::Server<ros1_template_msgs::FibonacciConfig>> reconfigure_server_;
114 
118  std::vector<std::thread> publish_threads_;
119 
122 };
123 
124 } // namespace
125 
#define NODELET_INFO_STREAM(...)
std::shared_ptr< AtomicFibonacci > AtomicFibonacciPtr
std::shared_ptr< dynamic_reconfigure::Server< ros1_template_msgs::FibonacciConfig > > reconfigure_server_
Definition: nodelet.cpp:113
std::vector< std::thread > publish_threads_
Definition: nodelet.cpp:118
ros::NodeHandle & getPrivateNodeHandle() const
PLUGINLIB_EXPORT_CLASS(ros1_ros_cpptemplate::Nodelet, nodelet::Nodelet)
AtomicFibonacciPtr atomic_fibonacci_
Definition: nodelet.cpp:115
std::shared_ptr< Publisher > PublisherPtr
Definition: publisher.hpp:56
void reconfigureCB(ros1_template_msgs::FibonacciConfig &config, uint32_t level)
Definition: nodelet.cpp:104
std::atomic< bool > shutdown_requested_
Definition: nodelet.cpp:110
ROSCPP_DECL bool ok()
ros::NodeHandle private_node_handle_
Definition: nodelet.cpp:111
PublisherPtr publisher_used_interally_
Definition: nodelet.cpp:116
bool sleep()
void run(double publish_rate, PublisherPtr publisher)
Definition: nodelet.cpp:94
bool getParam(const std::string &key, std::string &s) const
std::shared_ptr< Service > ServicePtr
Definition: service.hpp:58
std::shared_ptr< Subscriber > SubscriberPtr
Definition: subscriber.hpp:51


ros1_ros_cpptemplate
Author(s): Alexander Reimann
autogenerated on Sat Sep 2 2017 02:38:06