camera_subscriber.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2009, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
40 
41 inline void increment(int* value)
42 {
43  ++(*value);
44 }
45 
46 namespace image_transport {
47 
49 {
50  Impl(uint32_t queue_size)
51  : sync_(queue_size),
52  unsubscribed_(false),
54  {}
55 
57  {
58  shutdown();
59  }
60 
61  bool isValid() const
62  {
63  return !unsubscribed_;
64  }
65 
66  void shutdown()
67  {
68  if (!unsubscribed_) {
69  unsubscribed_ = true;
72  }
73  }
74 
76  {
77  int threshold = 3 * both_received_;
78  if (image_received_ > threshold || info_received_ > threshold) {
79  ROS_WARN_NAMED("sync", // Can suppress ros.image_transport.sync independent of anything else
80  "[image_transport] Topics '%s' and '%s' do not appear to be synchronized. "
81  "In the last 10s:\n"
82  "\tImage messages received: %d\n"
83  "\tCameraInfo messages received: %d\n"
84  "\tSynchronized pairs: %d",
85  image_sub_.getTopic().c_str(), info_sub_.getTopic().c_str(),
87  }
89  }
90 
95  // For detecting when the topics aren't synchronized
98 };
99 
101  const std::string& base_topic, uint32_t queue_size,
102  const Callback& callback, const ros::VoidPtr& tracked_object,
103  const TransportHints& transport_hints)
104  : impl_(new Impl(queue_size))
105 {
106  // Must explicitly remap the image topic since we then do some string manipulation on it
107  // to figure out the sibling camera_info topic.
108  std::string image_topic = info_nh.resolveName(base_topic);
109  std::string info_topic = getCameraInfoTopic(image_topic);
110  impl_->image_sub_.subscribe(image_it, image_topic, queue_size, transport_hints);
111  impl_->info_sub_ .subscribe(info_nh, info_topic, queue_size, transport_hints.getRosHints());
112  impl_->sync_.connectInput(impl_->image_sub_, impl_->info_sub_);
113  // need for Boost.Bind here is kind of broken
114  impl_->sync_.registerCallback(boost::bind(callback, _1, _2));
115 
116  // Complain every 10s if it appears that the image and info topics are not synchronized
117  impl_->image_sub_.registerCallback(boost::bind(increment, &impl_->image_received_));
118  impl_->info_sub_.registerCallback(boost::bind(increment, &impl_->info_received_));
119  impl_->sync_.registerCallback(boost::bind(increment, &impl_->both_received_));
120  impl_->check_synced_timer_ = info_nh.createWallTimer(ros::WallDuration(10.0),
121  boost::bind(&Impl::checkImagesSynchronized, impl_.get()));
122 }
123 
124 std::string CameraSubscriber::getTopic() const
125 {
126  if (impl_) return impl_->image_sub_.getTopic();
127  return std::string();
128 }
129 
131 {
132  if (impl_) return impl_->info_sub_.getTopic();
133  return std::string();
134 }
135 
137 {
139  //if (impl_) return std::max(impl_->image_sub_.getNumPublishers(), impl_->info_sub_.getNumPublishers());
140  if (impl_) return impl_->image_sub_.getNumPublishers();
141  return 0;
142 }
143 
145 {
146  if (impl_) return impl_->image_sub_.getTransport();
147  return std::string();
148 }
149 
151 {
152  if (impl_) impl_->shutdown();
153 }
154 
155 CameraSubscriber::operator void*() const
156 {
157  return (impl_ && impl_->isValid()) ? (void*)1 : (void*)0;
158 }
159 
160 } //namespace image_transport
image_transport::SubscriberFilter
Image subscription filter.
Definition: subscriber_filter.h:96
image_transport::CameraSubscriber::Impl::image_received_
int image_received_
Definition: camera_subscriber.cpp:97
increment
void increment(int *value)
Definition: camera_subscriber.cpp:41
image_transport::getCameraInfoTopic
IMAGE_TRANSPORT_DECL std::string getCameraInfoTopic(const std::string &base_topic)
Form the camera info topic name, sibling to the base topic.
Definition: camera_common.cpp:75
image_transport::ImageTransport
Advertise and subscribe to image topics.
Definition: image_transport.h:84
boost::shared_ptr< void >
camera_subscriber.h
image_transport::CameraSubscriber::Impl::info_sub_
message_filters::Subscriber< sensor_msgs::CameraInfo > info_sub_
Definition: camera_subscriber.cpp:92
image_transport::CameraSubscriber::Impl::info_received_
int info_received_
Definition: camera_subscriber.cpp:97
image_transport::CameraSubscriber::Impl::Impl
Impl(uint32_t queue_size)
Definition: camera_subscriber.cpp:50
image_transport::CameraSubscriber::CameraSubscriber
CameraSubscriber()
Definition: camera_subscriber.h:101
image_transport::CameraSubscriber::Impl::both_received_
int both_received_
Definition: camera_subscriber.cpp:97
ros::WallTimer
time_synchronizer.h
image_transport::CameraSubscriber::getTransport
std::string getTransport() const
Returns the name of the transport being used.
Definition: camera_subscriber.cpp:144
image_transport::CameraSubscriber::Impl::~Impl
~Impl()
Definition: camera_subscriber.cpp:56
message_filters::Subscriber::unsubscribe
void unsubscribe()
image_transport::SubscriberFilter::getTopic
std::string getTopic() const
Definition: subscriber_filter.h:186
message_filters::Subscriber::getTopic
std::string getTopic() const
message_filters::Subscriber< sensor_msgs::CameraInfo >
ros::NodeHandle::resolveName
std::string resolveName(const std::string &name, bool remap=true) const
subscriber_filter.h
subscriber.h
image_transport::CameraSubscriber::impl_
ImplPtr impl_
Definition: camera_subscriber.h:144
image_transport::CameraSubscriber::shutdown
void shutdown()
Unsubscribe the callback associated with this CameraSubscriber.
Definition: camera_subscriber.cpp:150
image_transport::CameraSubscriber::Impl
Definition: camera_subscriber.cpp:48
image_transport::CameraSubscriber::Impl::check_synced_timer_
ros::WallTimer check_synced_timer_
Definition: camera_subscriber.cpp:96
camera_common.h
image_transport::CameraSubscriber::Callback
boost::function< void(const sensor_msgs::ImageConstPtr &, const sensor_msgs::CameraInfoConstPtr &)> Callback
Definition: camera_subscriber.h:99
image_transport::CameraSubscriber::Impl::image_sub_
SubscriberFilter image_sub_
Definition: camera_subscriber.cpp:91
image_transport::CameraSubscriber::Impl::isValid
bool isValid() const
Definition: camera_subscriber.cpp:61
image_transport::CameraSubscriber::Impl::shutdown
void shutdown()
Definition: camera_subscriber.cpp:66
image_transport::CameraSubscriber::Impl::sync_
message_filters::TimeSynchronizer< sensor_msgs::Image, sensor_msgs::CameraInfo > sync_
Definition: camera_subscriber.cpp:93
image_transport::CameraSubscriber::Impl::unsubscribed_
bool unsubscribed_
Definition: camera_subscriber.cpp:94
image_transport::CameraSubscriber::getNumPublishers
uint32_t getNumPublishers() const
Returns the number of publishers this subscriber is connected to.
Definition: camera_subscriber.cpp:136
image_transport::TransportHints::getRosHints
const ros::TransportHints & getRosHints() const
Definition: transport_hints.h:140
ROS_WARN_NAMED
#define ROS_WARN_NAMED(name,...)
image_transport
Definition: camera_common.h:41
image_transport::CameraSubscriber::getTopic
std::string getTopic() const
Get the base topic (on which the raw image is published).
Definition: camera_subscriber.cpp:124
message_filters::TimeSynchronizer< sensor_msgs::Image, sensor_msgs::CameraInfo >
image_transport::CameraSubscriber::getInfoTopic
std::string getInfoTopic() const
Get the camera info topic.
Definition: camera_subscriber.cpp:130
image_transport::CameraSubscriber::Impl::checkImagesSynchronized
void checkImagesSynchronized()
Definition: camera_subscriber.cpp:75
image_transport::TransportHints
Stores transport settings for an image topic subscription.
Definition: transport_hints.h:77
ros::WallDuration
ros::NodeHandle::createWallTimer
WallTimer createWallTimer(WallDuration period, const WallTimerCallback &callback, bool oneshot=false, bool autostart=true) const
image_transport::SubscriberFilter::unsubscribe
void unsubscribe()
Force immediate unsubscription of this subscriber from its topic.
Definition: subscriber_filter.h:181
ros::NodeHandle


image_transport
Author(s): Patrick Mihelich
autogenerated on Sun Apr 5 2020 03:15:28