nodelet.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2016, Ryohei Ueda.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/o2r other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
36 #include "opencv_apps/nodelet.h"
37 #if BOOST_VERSION < 106000 // since 1.60.0, boost uses placeholders namesapce for _1,_2...
38 #ifndef BOOST_PLAEHOLDERS
39 #define BOOST_PLAEHOLDERS
40 namespace boost
41 {
42 namespace placeholders
43 {
44 boost::arg<1> _1;
45 boost::arg<2> _2;
46 boost::arg<3> _3;
47 boost::arg<4> _4;
48 boost::arg<5> _5;
49 boost::arg<6> _6;
50 boost::arg<7> _7;
51 boost::arg<8> _8;
52 boost::arg<9> _9;
53 } // namespace placeholders
54 } // namespace boost
55 #endif // BOOST_PLAEHOLDERS
56 #endif // BOOST_VERSION < 106000
57 
58 namespace opencv_apps
59 {
60 void Nodelet::onInit()
61 {
62  connection_status_ = NOT_SUBSCRIBED;
63  nh_.reset(new ros::NodeHandle(getMTNodeHandle()));
64  pnh_.reset(new ros::NodeHandle(getMTPrivateNodeHandle()));
65  pnh_->param("always_subscribe", always_subscribe_, false);
66  pnh_->param("verbose_connection", verbose_connection_, false);
67  if (!verbose_connection_)
68  {
69  nh_->param("verbose_connection", verbose_connection_, false);
70  }
71  // timer to warn when no connection in a few seconds
72  ever_subscribed_ = false;
73  timer_ = nh_->createWallTimer(ros::WallDuration(5), &Nodelet::warnNeverSubscribedCallback, this,
74  /*oneshot=*/true);
75 }
76 
77 void Nodelet::onInitPostProcess()
78 {
79  if (always_subscribe_)
80  {
81  subscribe();
82  }
83 }
84 
85 void Nodelet::warnNeverSubscribedCallback(const ros::WallTimerEvent& event)
86 {
87  if (!ever_subscribed_)
88  {
89  NODELET_WARN("'%s' subscribes topics only with child subscribers.", nodelet::Nodelet::getName().c_str());
90  }
91 }
92 
93 void Nodelet::connectionCallback(const ros::SingleSubscriberPublisher& pub)
94 {
95  if (verbose_connection_)
96  {
97  NODELET_INFO("New connection or disconnection is detected");
98  }
99  if (!always_subscribe_)
100  {
101  boost::mutex::scoped_lock lock(connection_mutex_);
102  for (const ros::Publisher& pub : publishers_)
103  {
104  if (pub.getNumSubscribers() > 0)
105  {
106  if (!ever_subscribed_)
107  {
108  ever_subscribed_ = true;
109  }
110  if (connection_status_ != SUBSCRIBED)
111  {
112  if (verbose_connection_)
113  {
114  NODELET_INFO("Subscribe input topics");
115  }
116  subscribe();
117  connection_status_ = SUBSCRIBED;
118  }
119  return;
120  }
121  }
122  if (connection_status_ == SUBSCRIBED)
123  {
124  if (verbose_connection_)
125  {
126  NODELET_INFO("Unsubscribe input topics");
127  }
128  unsubscribe();
129  connection_status_ = NOT_SUBSCRIBED;
130  }
131  }
132 }
133 
134 void Nodelet::imageConnectionCallback(const image_transport::SingleSubscriberPublisher& pub)
135 {
136  if (verbose_connection_)
137  {
138  NODELET_INFO("New image connection or disconnection is detected");
139  }
140  if (!always_subscribe_)
141  {
142  boost::mutex::scoped_lock lock(connection_mutex_);
143  for (const image_transport::Publisher& pub : image_publishers_)
144  {
145  if (pub.getNumSubscribers() > 0)
146  {
147  if (!ever_subscribed_)
148  {
149  ever_subscribed_ = true;
150  }
151  if (connection_status_ != SUBSCRIBED)
152  {
153  if (verbose_connection_)
154  {
155  NODELET_INFO("Subscribe input topics");
156  }
157  subscribe();
158  connection_status_ = SUBSCRIBED;
159  }
160  return;
161  }
162  }
163  if (connection_status_ == SUBSCRIBED)
164  {
165  if (verbose_connection_)
166  {
167  NODELET_INFO("Unsubscribe input topics");
168  }
169  unsubscribe();
170  connection_status_ = NOT_SUBSCRIBED;
171  }
172  }
173 }
174 
175 void Nodelet::cameraConnectionCallback(const image_transport::SingleSubscriberPublisher& pub)
176 {
177  cameraConnectionBaseCallback();
178 }
179 
180 void Nodelet::cameraInfoConnectionCallback(const ros::SingleSubscriberPublisher& pub)
181 {
182  cameraConnectionBaseCallback();
183 }
184 
185 void Nodelet::cameraConnectionBaseCallback()
186 {
187  if (verbose_connection_)
188  {
189  NODELET_INFO("New image connection or disconnection is detected");
190  }
191  if (!always_subscribe_)
192  {
193  boost::mutex::scoped_lock lock(connection_mutex_);
194  for (const image_transport::CameraPublisher& pub : camera_publishers_)
195  {
196  if (pub.getNumSubscribers() > 0)
197  {
198  if (!ever_subscribed_)
199  {
200  ever_subscribed_ = true;
201  }
202  if (connection_status_ != SUBSCRIBED)
203  {
204  if (verbose_connection_)
205  {
206  NODELET_INFO("Subscribe input topics");
207  }
208  subscribe();
209  connection_status_ = SUBSCRIBED;
210  }
211  return;
212  }
213  }
214  if (connection_status_ == SUBSCRIBED)
215  {
216  if (verbose_connection_)
217  {
218  NODELET_INFO("Unsubscribe input topics");
219  }
220  unsubscribe();
221  connection_status_ = NOT_SUBSCRIBED;
222  }
223  }
224 }
225 } // namespace opencv_apps
#define NODELET_WARN(...)
boost::arg< 4 > _4
Definition: nodelet.cpp:47
Demo code to calculate moments.
Definition: nodelet.h:68
boost::arg< 2 > _2
Definition: nodelet.cpp:45
boost::arg< 9 > _9
Definition: nodelet.cpp:52
const std::string & getName() const
void subscribe()
boost::arg< 3 > _3
Definition: nodelet.cpp:46
boost::arg< 7 > _7
Definition: nodelet.cpp:50
boost::arg< 6 > _6
Definition: nodelet.cpp:49
boost::arg< 1 > _1
Definition: nodelet.cpp:44
#define NODELET_INFO(...)
boost::arg< 8 > _8
Definition: nodelet.cpp:51
void unsubscribe()
boost::arg< 5 > _5
Definition: nodelet.cpp:48


opencv_apps
Author(s): Kei Okada
autogenerated on Thu Feb 2 2023 03:40:24