rgbd_camera_subscriber.cpp
Go to the documentation of this file.
3 
8 
9 #include <sensor_msgs/PointCloud2.h>
11 
12 inline void increment(int* value)
13 {
14  ++(*value);
15 }
16 
17 namespace camera_throttle {
18 
20 {
21  explicit Impl(size_t queue_size)
22  : sync(queue_size), syncPcl(queue_size),
23  unsubscribed(false), hasPcl(false),
25  {}
26 
28  {
29  shutdown();
30  }
31 
32  bool isValid() const
33  {
34  return !this->unsubscribed;
35  }
36 
37  void shutdown()
38  {
39  if (!this->unsubscribed) {
40  this->unsubscribed = true;
41  this->rgbSub.unsubscribe();
42  this->rgbInfoSub.unsubscribe();
43  this->depthSub.unsubscribe();
44  this->depthInfoSub.unsubscribe();
45  if (this->hasPcl)
46  this->pclSub.unsubscribe();
47  }
48  }
49 
51  {
52  int threshold = (this->hasPcl ? 6 : 5) * allReceived;
53  if (rgbReceived > threshold || rgbInfoReceived > threshold || depthReceived > threshold || depthInfoReceived > threshold || pclReceived > threshold) {
54  ROS_WARN_NAMED("sync", // Can suppress ros.image_transport.sync independent of anything else
55  "[rgbd_image_transport] Topics '%s', '%s', '%s', '%s'%s do not appear to be synchronized. "
56  "In the last 10s:\n"
57  "\tRGB messages received: %d\n"
58  "\tRGB CameraInfo messages received: %d\n"
59  "\tDepth messages received: %d\n"
60  "\tDepth CameraInfo messages received: %d\n"
61  "%s"
62  "\tSynchronized pairs: %d",
63  rgbSub.getTopic().c_str(), rgbInfoSub.getTopic().c_str(),
64  depthSub.getTopic().c_str(), depthInfoSub.getTopic().c_str(),
65  (this->hasPcl ? " and '" + pclSub.getTopic() + "'" : std::string()).c_str(),
67  (this->hasPcl ? (std::string("\tPointcloud messages received: ") + cras::to_string(pclReceived) + "\n") : std::string()).c_str(),
68  allReceived);
69  }
71  }
72 
81  bool hasPcl;
82  // For detecting when the topics aren't synchronized
85 };
86 
88  const std::string& rgb_base_topic, const std::string& depth_base_topic, size_t queue_size,
89  const Callback& callback, const ros::VoidPtr& tracked_object,
90  const image_transport::TransportHints& transport_hints_rgb,
91  const image_transport::TransportHints& transport_hints_depth)
92  : impl(new Impl(queue_size))
93 {
94  this->impl->hasPcl = false;
95 
96  // Must explicitly remap the image topic since we then do some string manipulation on it
97  // to figure out the sibling camera_info topic.
98  const auto rgb_topic = rgb_nh.resolveName(rgb_base_topic);
99  const auto rgb_info_topic = image_transport::getCameraInfoTopic(rgb_topic);
100  const auto depth_topic = depth_nh.resolveName(depth_base_topic);
101  const auto depth_info_topic = image_transport::getCameraInfoTopic(depth_topic);
102 
103  impl->rgbSub.subscribe(image_it, rgb_topic, queue_size, transport_hints_rgb);
104  impl->rgbInfoSub.subscribe(rgb_nh, rgb_info_topic, queue_size, transport_hints_rgb.getRosHints());
105  impl->depthSub.subscribe(image_it, depth_topic, queue_size, transport_hints_depth);
106  impl->depthInfoSub.subscribe(depth_nh, depth_info_topic, queue_size, transport_hints_depth.getRosHints());
107  impl->sync.connectInput(impl->rgbSub, impl->rgbInfoSub, impl->depthSub, impl->depthInfoSub);
108  // need for Boost.Bind here is kind of broken
109  impl->sync.registerCallback(boost::bind(callback, _1, _2, _3, _4));
110 
111  // Complain every 10s if it appears that the image and info topics are not synchronized
112  impl->rgbSub.registerCallback(boost::bind(increment, &impl->rgbReceived));
113  impl->rgbInfoSub.registerCallback(boost::bind(increment, &impl->rgbInfoReceived));
114  impl->depthSub.registerCallback(boost::bind(increment, &impl->depthReceived));
115  impl->depthInfoSub.registerCallback(boost::bind(increment, &impl->depthInfoReceived));
116  impl->sync.registerCallback(boost::bind(increment, &impl->allReceived));
117  impl->checkSyncedTimer = rgb_nh.createWallTimer(ros::WallDuration(10.0),
118  boost::bind(&Impl::checkImagesSynchronized, impl.get()));
119 }
120 
122  const std::string& rgb_base_topic, const std::string& depth_base_topic,
123  const std::string& pcl_topic, size_t queue_size,
124  const PclCallback& callback, const ros::VoidPtr& tracked_object,
125  const image_transport::TransportHints& transport_hints_rgb,
126  const image_transport::TransportHints& transport_hints_depth,
127  const ros::TransportHints& transport_hints_pcl)
128  : impl(new Impl(queue_size))
129 {
130  this->impl->hasPcl = true;
131 
132  // Must explicitly remap the image topic since we then do some string manipulation on it
133  // to figure out the sibling camera_info topic.
134  const auto rgb_topic = rgb_nh.resolveName(rgb_base_topic);
135  const auto rgb_info_topic = image_transport::getCameraInfoTopic(rgb_topic);
136  const auto depth_topic = depth_nh.resolveName(depth_base_topic);
137  const auto depth_info_topic = image_transport::getCameraInfoTopic(depth_topic);
138 
139  impl->rgbSub.subscribe(image_it, rgb_topic, queue_size, transport_hints_rgb);
140  impl->rgbInfoSub.subscribe(rgb_nh, rgb_info_topic, queue_size, transport_hints_rgb.getRosHints());
141  impl->depthSub.subscribe(image_it, depth_topic, queue_size, transport_hints_depth);
142  impl->depthInfoSub.subscribe(depth_nh, depth_info_topic, queue_size, transport_hints_depth.getRosHints());
143  impl->pclSub.subscribe(pcl_nh, pcl_topic, queue_size, transport_hints_pcl);
144  impl->syncPcl.connectInput(impl->rgbSub, impl->rgbInfoSub, impl->depthSub, impl->depthInfoSub, impl->pclSub);
145  // need for Boost.Bind here is kind of broken
146  impl->syncPcl.registerCallback(boost::bind(callback, _1, _2, _3, _4, _5));
147 
148  // Complain every 10s if it appears that the image and info topics are not synchronized
149  impl->rgbSub.registerCallback(boost::bind(increment, &impl->rgbReceived));
150  impl->rgbInfoSub.registerCallback(boost::bind(increment, &impl->rgbInfoReceived));
151  impl->depthSub.registerCallback(boost::bind(increment, &impl->depthReceived));
152  impl->depthInfoSub.registerCallback(boost::bind(increment, &impl->depthInfoReceived));
153  impl->pclSub.registerCallback(boost::bind(increment, &impl->pclReceived));
154  impl->syncPcl.registerCallback(boost::bind(increment, &impl->allReceived));
155  impl->checkSyncedTimer = rgb_nh.createWallTimer(ros::WallDuration(10.0),
156  boost::bind(&Impl::checkImagesSynchronized, impl.get()));
157 }
158 
160 {
161  if (impl) return impl->rgbSub.getTopic();
162  return {};
163 }
164 
166 {
167  if (impl) return impl->rgbInfoSub.getTopic();
168  return {};
169 }
170 
172 {
173  if (impl) return impl->depthSub.getTopic();
174  return {};
175 }
176 
178 {
179  if (impl) return impl->depthInfoSub.getTopic();
180  return {};
181 }
182 
184 {
185  if (impl && impl->hasPcl) return impl->pclSub.getTopic();
186  return {};
187 }
188 
190 {
191  if (impl && impl->isValid())
192  return std::max(std::max(
193  std::max(impl->rgbSub.getNumPublishers(), impl->rgbInfoSub.getSubscriber().getNumPublishers()),
194  std::max(impl->depthSub.getNumPublishers(), impl->depthInfoSub.getSubscriber().getNumPublishers())
195  ), (this->impl->hasPcl ? impl->pclSub.getSubscriber().getNumPublishers() : 0));
196 
197  return 0;
198 }
199 
201 {
202  if (impl) return impl->rgbSub.getTransport();
203  return {};
204 }
205 
207 {
208  if (impl) return impl->depthSub.getTransport();
209  return {};
210 }
211 
213 {
214  if (impl) impl->shutdown();
215 }
216 
217 RgbdCameraSubscriber::operator void*() const
218 {
219  return (impl && impl->isValid()) ? (void*)1 : (void*)0;
220 }
221 
222 }
camera_throttle::RgbdCameraSubscriber::Impl::syncPcl
message_filters::TimeSynchronizer< sensor_msgs::Image, sensor_msgs::CameraInfo, sensor_msgs::Image, sensor_msgs::CameraInfo, sensor_msgs::PointCloud2 > syncPcl
Definition: rgbd_camera_subscriber.cpp:79
image_transport::SubscriberFilter
image_transport::getCameraInfoTopic
IMAGE_TRANSPORT_DECL std::string getCameraInfoTopic(const std::string &base_topic)
camera_throttle::RgbdCameraSubscriber::getDepthTopic
std::string getDepthTopic() const
Get the depth base topic (on which the raw image is published).
Definition: rgbd_camera_subscriber.cpp:171
camera_throttle::RgbdCameraSubscriber::getRGBTransport
std::string getRGBTransport() const
Returns the name of the transport being used for RGB.
Definition: rgbd_camera_subscriber.cpp:200
camera_throttle::RgbdCameraSubscriber::Impl::checkImagesSynchronized
void checkImagesSynchronized()
Definition: rgbd_camera_subscriber.cpp:50
boost::shared_ptr< void >
camera_throttle::RgbdCameraSubscriber::PclCallback
boost::function< void(const sensor_msgs::ImageConstPtr &, const sensor_msgs::CameraInfoConstPtr &, const sensor_msgs::ImageConstPtr &, const sensor_msgs::CameraInfoConstPtr &, const sensor_msgs::PointCloud2ConstPtr &)> PclCallback
Definition: rgbd_camera_subscriber.h:42
camera_throttle::RgbdCameraSubscriber::Impl::rgbReceived
int rgbReceived
Definition: rgbd_camera_subscriber.cpp:84
camera_throttle::RgbdCameraSubscriber::getRGBTopic
std::string getRGBTopic() const
Get the RGB base topic (on which the raw image is published).
Definition: rgbd_camera_subscriber.cpp:159
camera_throttle::RgbdCameraSubscriber::Impl::pclReceived
int pclReceived
Definition: rgbd_camera_subscriber.cpp:84
camera_throttle::RgbdCameraSubscriber::Impl::depthReceived
int depthReceived
Definition: rgbd_camera_subscriber.cpp:84
camera_throttle::RgbdCameraSubscriber::Impl::rgbInfoSub
message_filters::Subscriber< sensor_msgs::CameraInfo > rgbInfoSub
Definition: rgbd_camera_subscriber.cpp:75
camera_throttle::RgbdCameraSubscriber::Callback
boost::function< void(const sensor_msgs::ImageConstPtr &, const sensor_msgs::CameraInfoConstPtr &, const sensor_msgs::ImageConstPtr &, const sensor_msgs::CameraInfoConstPtr &)> Callback
Definition: rgbd_camera_subscriber.h:37
ros::WallTimer
ros::TransportHints
camera_throttle::RgbdCameraSubscriber::Impl::Impl
Impl(size_t queue_size)
Definition: rgbd_camera_subscriber.cpp:21
camera_throttle::RgbdCameraSubscriber::RgbdCameraSubscriber
RgbdCameraSubscriber()
Definition: rgbd_camera_subscriber.h:44
time_synchronizer.h
camera_throttle::RgbdCameraSubscriber::Impl::pclSub
message_filters::Subscriber< sensor_msgs::PointCloud2 > pclSub
Definition: rgbd_camera_subscriber.cpp:77
message_filters::Subscriber::unsubscribe
void unsubscribe()
image_transport::SubscriberFilter::getTopic
std::string getTopic() const
camera_throttle::RgbdCameraSubscriber::Impl::depthSub
image_transport::SubscriberFilter depthSub
Definition: rgbd_camera_subscriber.cpp:74
message_filters::Subscriber::getTopic
std::string getTopic() const
message_filters::Subscriber< sensor_msgs::CameraInfo >
camera_throttle::RgbdCameraSubscriber::Impl::depthInfoReceived
int depthInfoReceived
Definition: rgbd_camera_subscriber.cpp:84
camera_throttle::RgbdCameraSubscriber::getPclTopic
std::string getPclTopic() const
Get the topic of the pointcloud (can be empty if pointcloud is not processed).
Definition: rgbd_camera_subscriber.cpp:183
camera_throttle::RgbdCameraSubscriber::shutdown
void shutdown()
Unsubscribe the callback associated with this RgbdCameraSubscriber.
Definition: rgbd_camera_subscriber.cpp:212
camera_throttle::RgbdCameraSubscriber::Impl::shutdown
void shutdown()
Definition: rgbd_camera_subscriber.cpp:37
ros::NodeHandle::resolveName
std::string resolveName(const std::string &name, bool remap=true) const
camera_throttle::RgbdCameraSubscriber::Impl::isValid
bool isValid() const
Definition: rgbd_camera_subscriber.cpp:32
string_utils.hpp
rgbd_image_transport.h
subscriber_filter.h
subscriber.h
camera_throttle::RgbdCameraSubscriber::Impl::~Impl
~Impl()
Definition: rgbd_camera_subscriber.cpp:27
camera_throttle::RgbdCameraSubscriber::Impl::depthInfoSub
message_filters::Subscriber< sensor_msgs::CameraInfo > depthInfoSub
Definition: rgbd_camera_subscriber.cpp:76
camera_throttle::RgbdCameraSubscriber::getDepthTransport
std::string getDepthTransport() const
Returns the name of the transport being used for depth.
Definition: rgbd_camera_subscriber.cpp:206
camera_throttle::RgbdCameraSubscriber::getNumPublishers
size_t getNumPublishers() const
Returns the number of publishers this subscriber is connected to.
Definition: rgbd_camera_subscriber.cpp:189
camera_throttle::RgbdCameraSubscriber::Impl::unsubscribed
bool unsubscribed
Definition: rgbd_camera_subscriber.cpp:80
camera_throttle::RgbdCameraSubscriber::impl
std::shared_ptr< Impl > impl
Definition: rgbd_camera_subscriber.h:111
camera_throttle::RgbdCameraSubscriber::getDepthInfoTopic
std::string getDepthInfoTopic() const
Get the depth camera info topic.
Definition: rgbd_camera_subscriber.cpp:177
camera_throttle::RgbdCameraSubscriber::Impl::checkSyncedTimer
ros::WallTimer checkSyncedTimer
Definition: rgbd_camera_subscriber.cpp:83
camera_common.h
camera_throttle::RgbdCameraSubscriber::Impl::sync
message_filters::TimeSynchronizer< sensor_msgs::Image, sensor_msgs::CameraInfo, sensor_msgs::Image, sensor_msgs::CameraInfo > sync
Definition: rgbd_camera_subscriber.cpp:78
camera_throttle
Definition: camera_throttle.h:11
camera_throttle::RgbdCameraSubscriber::Impl::allReceived
int allReceived
Definition: rgbd_camera_subscriber.cpp:84
image_transport::TransportHints::getRosHints
const ros::TransportHints & getRosHints() const
ROS_WARN_NAMED
#define ROS_WARN_NAMED(name,...)
camera_throttle::RgbdCameraSubscriber::Impl
Definition: rgbd_camera_subscriber.cpp:19
camera_throttle::RgbdCameraSubscriber::getRGBInfoTopic
std::string getRGBInfoTopic() const
Get the RGB camera info topic.
Definition: rgbd_camera_subscriber.cpp:165
message_filters::TimeSynchronizer< sensor_msgs::Image, sensor_msgs::CameraInfo, sensor_msgs::Image, sensor_msgs::CameraInfo >
rgbd_camera_subscriber.h
image_transport::TransportHints
ros::WallDuration
camera_throttle::RgbdCameraSubscriber::Impl::hasPcl
bool hasPcl
Definition: rgbd_camera_subscriber.cpp:81
increment
void increment(int *value)
Definition: rgbd_camera_subscriber.cpp:12
ros::NodeHandle::createWallTimer
WallTimer createWallTimer(WallDuration period, const WallTimerCallback &callback, bool oneshot=false, bool autostart=true) const
camera_throttle::RgbdImageTransport
Definition: rgbd_image_transport.h:10
image_transport::SubscriberFilter::unsubscribe
void unsubscribe()
ros::NodeHandle
cras::to_string
inline ::std::string to_string(char *value)
camera_throttle::RgbdCameraSubscriber::Impl::rgbInfoReceived
int rgbInfoReceived
Definition: rgbd_camera_subscriber.cpp:84
camera_throttle::RgbdCameraSubscriber::Impl::rgbSub
image_transport::SubscriberFilter rgbSub
Definition: rgbd_camera_subscriber.cpp:73


camera_throttle
Author(s): Martin Pecka
autogenerated on Sat Dec 14 2024 03:51:15