rgbd_throttle.cpp
Go to the documentation of this file.
4 
5 namespace camera_throttle
6 {
7 
9 {
10  auto pnh = this->getPrivateNodeHandle();
11  if (pnh.hasParam("rate"))
12  this->rate = this->getParam(pnh, "rate", ros::Rate(10.0), "Hz");
13  else
14  NODELET_INFO("No rate limiting");
15  this->queueSize = this->getParam(pnh, "queue_size", 10u, "messages");
16  this->subRGBBaseName = this->getParam(pnh, "sub_rgb_base_name", "image_raw");
17  this->pubRGBBaseName = this->getParam(pnh, "pub_rgb_base_name", this->subRGBBaseName);
18  this->subDepthBaseName = this->getParam(pnh, "sub_depth_base_name", "depth");
19  this->pubDepthBaseName = this->getParam(pnh, "pub_depth_base_name", this->subDepthBaseName);
20  this->subscribePcl = this->getParam(pnh, "subscribe_pcl", true);
21 
22  if (pnh.hasParam("fix_rgb_frame_id"))
23  this->rgbFrameId = this->getParam(pnh, "fix_rgb_frame_id", "");
24  if (this->rgbFrameId.has_value() && this->rgbFrameId.value().empty())
25  this->rgbFrameId.reset();
26  if (this->rgbFrameId)
27  NODELET_INFO("Fixing RGB frame_id to %s", this->rgbFrameId->c_str());
28 
29  if (pnh.hasParam("fix_depth_frame_id"))
30  this->depthFrameId = this->getParam(pnh, "fix_depth_frame_id", "");
31  if (this->depthFrameId.has_value() && this->depthFrameId.value().empty())
32  this->depthFrameId.reset();
33  if (this->depthFrameId)
34  NODELET_INFO("Fixing depth frame_id to %s", this->depthFrameId->c_str());
35 
36  this->subRgbNh = ros::NodeHandle(this->getNodeHandle(), "camera_rgb_in");
37  this->subDepthNh = ros::NodeHandle(this->getNodeHandle(), "camera_depth_in");
38  this->subPclNh = this->getNodeHandle();
39  this->subTransport = std::make_unique<RgbdImageTransport>(this->subRgbNh, this->subDepthNh, this->subPclNh);
40 
41  this->pubRgbNh = ros::NodeHandle(this->getNodeHandle(), "camera_rgb_out");
42  this->pubDepthNh = ros::NodeHandle(this->getNodeHandle(), "camera_depth_out");
43  this->pubPclNh = this->getNodeHandle();
44  this->pubTransport = std::make_unique<RgbdImageTransport>(this->pubRgbNh, this->pubDepthNh, this->pubPclNh);
45 
46  if (this->subscribePcl)
47  this->pub = this->pubTransport->advertiseRgbdCamera(
48  this->pubRGBBaseName, this->pubDepthBaseName, "points_out", this->queueSize,
49  boost::bind(&RgbdCameraThrottleNodelet::img_connect_cb, this, _1),
50  boost::bind(&RgbdCameraThrottleNodelet::img_disconnect_cb, this, _1),
51  boost::bind(&RgbdCameraThrottleNodelet::img_connect_cb, this, _1),
52  boost::bind(&RgbdCameraThrottleNodelet::img_disconnect_cb, this, _1),
53  boost::bind(&RgbdCameraThrottleNodelet::info_connect_cb, this, _1),
54  boost::bind(&RgbdCameraThrottleNodelet::info_disconnect_cb, this, _1),
55  boost::bind(&RgbdCameraThrottleNodelet::info_connect_cb, this, _1),
56  boost::bind(&RgbdCameraThrottleNodelet::info_disconnect_cb, this, _1),
57  boost::bind(&RgbdCameraThrottleNodelet::info_connect_cb, this, _1),
58  boost::bind(&RgbdCameraThrottleNodelet::info_disconnect_cb, this, _1));
59  else
60  this->pub = this->pubTransport->advertiseRgbdCamera(
61  this->pubRGBBaseName, this->pubDepthBaseName, this->queueSize,
62  boost::bind(&RgbdCameraThrottleNodelet::img_connect_cb, this, _1),
63  boost::bind(&RgbdCameraThrottleNodelet::img_disconnect_cb, this, _1),
64  boost::bind(&RgbdCameraThrottleNodelet::img_connect_cb, this, _1),
65  boost::bind(&RgbdCameraThrottleNodelet::img_disconnect_cb, this, _1),
66  boost::bind(&RgbdCameraThrottleNodelet::info_connect_cb, this, _1),
67  boost::bind(&RgbdCameraThrottleNodelet::info_disconnect_cb, this, _1),
68  boost::bind(&RgbdCameraThrottleNodelet::info_connect_cb, this, _1),
69  boost::bind(&RgbdCameraThrottleNodelet::info_disconnect_cb, this, _1));
70 }
71 
72 void RgbdCameraThrottleNodelet::cb(const sensor_msgs::ImageConstPtr& rgbImg, const sensor_msgs::CameraInfoConstPtr& rgbInfo, const sensor_msgs::ImageConstPtr& depthImg, const sensor_msgs::CameraInfoConstPtr& depthInfo)
73 {
74  if (this->rate)
75  {
76  if (this->lastUpdate + this->rate.value().expectedCycleTime() > ros::Time::now())
77  {
78  NODELET_DEBUG("throttle last update at %f skipping", this->lastUpdate.toSec());
79  return;
80  }
81  }
82 
83  this->lastUpdate = ros::Time::now();
84 
85  if (!this->rgbFrameId.has_value() && !this->depthFrameId.has_value())
86  {
87  this->pub.publish(rgbImg, rgbInfo, depthImg, depthInfo);
88  } else {
89  sensor_msgs::ImageConstPtr pubRgbImg = rgbImg;
90  sensor_msgs::CameraInfoConstPtr pubRgbInfo = rgbInfo;
91  sensor_msgs::ImageConstPtr pubDepthImg = depthImg;
92  sensor_msgs::CameraInfoConstPtr pubDepthInfo = depthInfo;
93 
94  if (this->rgbFrameId)
95  {
96  sensor_msgs::ImagePtr newRgbImg(new sensor_msgs::Image);
97  sensor_msgs::CameraInfoPtr newRgbInfo(new sensor_msgs::CameraInfo);
98  *newRgbImg = *rgbImg;
99  *newRgbInfo = *rgbInfo;
100  newRgbImg->header.frame_id = this->rgbFrameId.value();
101  newRgbInfo->header.frame_id = this->rgbFrameId.value();
102 
103  pubRgbImg = newRgbImg;
104  pubRgbInfo = newRgbInfo;
105  }
106 
107  if (this->depthFrameId)
108  {
109  sensor_msgs::ImagePtr newDepthImg(new sensor_msgs::Image);
110  sensor_msgs::CameraInfoPtr newDepthInfo(new sensor_msgs::CameraInfo);
111  *newDepthImg = *depthImg;
112  *newDepthInfo = *depthInfo;
113  newDepthImg->header.frame_id = this->depthFrameId.value();
114  newDepthInfo->header.frame_id = this->depthFrameId.value();
115 
116  pubDepthImg = newDepthImg;
117  pubDepthInfo = newDepthInfo;
118  }
119 
120  this->pub.publish(pubRgbImg, pubRgbInfo, pubDepthImg, pubDepthInfo);
121  }
122 }
123 
124 void RgbdCameraThrottleNodelet::cbPcl(const sensor_msgs::ImageConstPtr& rgbImg, const sensor_msgs::CameraInfoConstPtr& rgbInfo, const sensor_msgs::ImageConstPtr& depthImg, const sensor_msgs::CameraInfoConstPtr& depthInfo, const sensor_msgs::PointCloud2ConstPtr& pcl)
125 {
126  if (this->rate)
127  {
128  if (this->lastUpdate + this->rate.value().expectedCycleTime() > ros::Time::now())
129  {
130  NODELET_DEBUG("throttle last update at %f skipping", this->lastUpdate.toSec());
131  return;
132  }
133  }
134 
135  this->lastUpdate = ros::Time::now();
136 
137  if (!this->rgbFrameId.has_value() && !this->depthFrameId.has_value())
138  {
139  this->pub.publish(rgbImg, rgbInfo, depthImg, depthInfo, pcl);
140  } else {
141  sensor_msgs::ImageConstPtr pubRgbImg = rgbImg;
142  sensor_msgs::CameraInfoConstPtr pubRgbInfo = rgbInfo;
143  sensor_msgs::ImageConstPtr pubDepthImg = depthImg;
144  sensor_msgs::CameraInfoConstPtr pubDepthInfo = depthInfo;
145 
146  if (this->rgbFrameId)
147  {
148  sensor_msgs::ImagePtr newRgbImg(new sensor_msgs::Image);
149  sensor_msgs::CameraInfoPtr newRgbInfo(new sensor_msgs::CameraInfo);
150  *newRgbImg = *rgbImg;
151  *newRgbInfo = *rgbInfo;
152  newRgbImg->header.frame_id = this->rgbFrameId.value();
153  newRgbInfo->header.frame_id = this->rgbFrameId.value();
154 
155  pubRgbImg = newRgbImg;
156  pubRgbInfo = newRgbInfo;
157  }
158 
159  if (this->depthFrameId)
160  {
161  sensor_msgs::ImagePtr newDepthImg(new sensor_msgs::Image);
162  sensor_msgs::CameraInfoPtr newDepthInfo(new sensor_msgs::CameraInfo);
163  *newDepthImg = *depthImg;
164  *newDepthInfo = *depthInfo;
165  newDepthImg->header.frame_id = this->depthFrameId.value();
166  newDepthInfo->header.frame_id = this->depthFrameId.value();
167 
168  pubDepthImg = newDepthImg;
169  pubDepthInfo = newDepthInfo;
170  }
171 
172  this->pub.publish(pubRgbImg, pubRgbInfo, pubDepthImg, pubDepthInfo, pcl);
173  }
174 }
175 
177 {
178  std::lock_guard<std::mutex> g(this->publishersMutex);
179  if (this->pub.getNumSubscribers() == 1 && !this->sub)
180  this->onFirstConnect();
181 }
182 
184 {
185  std::lock_guard<std::mutex> g(this->publishersMutex);
186  if (this->pub.getNumSubscribers() == 1 && !this->sub)
187  this->onFirstConnect();
188 }
189 
191 {
192  std::lock_guard<std::mutex> g(this->publishersMutex);
193  if (this->pub.getNumSubscribers() == 0 && this->sub)
194  this->onLastDisconnect();
195 }
196 
198 {
199  std::lock_guard<std::mutex> g(this->publishersMutex);
200  if (this->pub.getNumSubscribers() == 0 && this->sub)
201  this->onLastDisconnect();
202 }
203 
205 {
206  const auto& defaultTransport = this->getPrivateNodeHandle().param("image_transport", std::string("raw"));
207  image_transport::TransportHints hintsRgb(defaultTransport, {}, this->getPrivateNodeHandle(), "image_transport_rgb");
208  image_transport::TransportHints hintsDepth(defaultTransport, {}, this->getPrivateNodeHandle(), "image_transport_depth");
209  if (this->subscribePcl)
210  {
211  NODELET_DEBUG("Started lazy-subscription to %s, %s and %s", this->subRgbNh.resolveName(this->subRGBBaseName).c_str(),
212  this->subDepthNh.resolveName(this->subDepthBaseName).c_str(),
213  this->subPclNh.resolveName("points_in").c_str());
214  this->sub = this->subTransport->subscribeRgbdCamera(this->subRGBBaseName, this->subDepthBaseName, "points_in", this->queueSize, &RgbdCameraThrottleNodelet::cbPcl, this, hintsRgb, hintsDepth);
215  } else
216  {
217  NODELET_DEBUG("Started lazy-subscription to %s and %s", this->subRgbNh.resolveName(this->subRGBBaseName).c_str(),
218  this->subDepthNh.resolveName(this->subDepthBaseName).c_str());
219  this->sub = this->subTransport->subscribeRgbdCamera(this->subRGBBaseName, this->subDepthBaseName, this->queueSize, &RgbdCameraThrottleNodelet::cb, this, hintsRgb, hintsDepth);
220  }
221 }
222 
224 {
225  if (this->subscribePcl)
226  NODELET_DEBUG("Stopped lazy-subscription to %s, %s and %s", this->sub.value().getRGBTopic().c_str(), this->sub.value().getDepthTopic().c_str(), this->sub.value().getPclTopic().c_str());
227  else
228  NODELET_DEBUG("Stopped lazy-subscription to %s and %s", this->sub.value().getRGBTopic().c_str(), this->sub.value().getDepthTopic().c_str());
229  this->sub.value().shutdown();
230  this->sub.reset();
231 }
232 
233 }
234 
rgbd_throttle.h
camera_throttle::RgbdCameraThrottleNodelet::subRgbNh
ros::NodeHandle subRgbNh
Definition: rgbd_throttle.h:62
camera_throttle::RgbdCameraThrottleNodelet::depthFrameId
std::optional< std::string > depthFrameId
Definition: rgbd_throttle.h:74
nodelet::Nodelet::getNodeHandle
ros::NodeHandle & getNodeHandle() const
camera_throttle::RgbdCameraThrottleNodelet::cbPcl
virtual void cbPcl(const sensor_msgs::ImageConstPtr &rgbImg, const sensor_msgs::CameraInfoConstPtr &rgbInfo, const sensor_msgs::ImageConstPtr &depthImg, const sensor_msgs::CameraInfoConstPtr &depthInfo, const sensor_msgs::PointCloud2ConstPtr &pcl)
Definition: rgbd_throttle.cpp:124
camera_throttle::RgbdCameraThrottleNodelet::pubDepthNh
ros::NodeHandle pubDepthNh
Definition: rgbd_throttle.h:66
camera_throttle::RgbdCameraThrottleNodelet::subDepthBaseName
std::string subDepthBaseName
Definition: rgbd_throttle.h:79
camera_throttle::RgbdCameraThrottleNodelet::lastUpdate
ros::Time lastUpdate
Definition: rgbd_throttle.h:76
camera_throttle::RgbdCameraThrottleNodelet::sub
std::optional< RgbdCameraSubscriber > sub
Definition: rgbd_throttle.h:70
camera_throttle::RgbdCameraThrottleNodelet::pubPclNh
ros::NodeHandle pubPclNh
Definition: rgbd_throttle.h:67
TimeBase< Time, Duration >::toSec
double toSec() const
camera_throttle::RgbdCameraThrottleNodelet
Definition: rgbd_throttle.h:49
camera_throttle::RgbdCameraThrottleNodelet::img_disconnect_cb
void img_disconnect_cb(const image_transport::SingleSubscriberPublisher &)
Definition: rgbd_throttle.cpp:190
camera_throttle::RgbdCameraPublisher::publish
void publish(const sensor_msgs::Image &rgb_image, const sensor_msgs::CameraInfo &rgb_info, const sensor_msgs::Image &depth_image, const sensor_msgs::CameraInfo &depth_info) const
Publish an RGB and depth (image, info) pair on the topics associated with this RgbdCameraPublisher.
Definition: rgbd_camera_publisher.cpp:155
camera_throttle::RgbdCameraThrottleNodelet::onFirstConnect
virtual void onFirstConnect()
Definition: rgbd_throttle.cpp:204
camera_throttle::RgbdCameraThrottleNodelet::publishersMutex
std::mutex publishersMutex
Definition: rgbd_throttle.h:83
nodelet::Nodelet::getPrivateNodeHandle
ros::NodeHandle & getPrivateNodeHandle() const
camera_throttle::RgbdCameraThrottleNodelet::subRGBBaseName
std::string subRGBBaseName
Definition: rgbd_throttle.h:77
camera_throttle::RgbdCameraThrottleNodelet::rgbFrameId
std::optional< std::string > rgbFrameId
Definition: rgbd_throttle.h:73
ros::NodeHandle::resolveName
std::string resolveName(const std::string &name, bool remap=true) const
ros::SingleSubscriberPublisher
rgbd_image_transport.h
camera_throttle::RgbdCameraThrottleNodelet::onInit
void onInit() override
Definition: rgbd_throttle.cpp:8
PLUGINLIB_EXPORT_CLASS
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
camera_throttle::RgbdCameraThrottleNodelet::img_connect_cb
void img_connect_cb(const image_transport::SingleSubscriberPublisher &)
Definition: rgbd_throttle.cpp:176
camera_throttle::RgbdCameraThrottleNodelet::cb
virtual void cb(const sensor_msgs::ImageConstPtr &rgbImg, const sensor_msgs::CameraInfoConstPtr &rgbIinfo, const sensor_msgs::ImageConstPtr &depthImg, const sensor_msgs::CameraInfoConstPtr &depthInfo)
Definition: rgbd_throttle.cpp:72
camera_throttle::RgbdCameraThrottleNodelet::pubRgbNh
ros::NodeHandle pubRgbNh
Definition: rgbd_throttle.h:65
camera_throttle::RgbdCameraThrottleNodelet::info_disconnect_cb
void info_disconnect_cb(const ros::SingleSubscriberPublisher &)
Definition: rgbd_throttle.cpp:197
camera_throttle::RgbdCameraThrottleNodelet::rate
std::optional< ros::Rate > rate
Definition: rgbd_throttle.h:72
camera_throttle::RgbdCameraThrottleNodelet::pubRGBBaseName
std::string pubRGBBaseName
Definition: rgbd_throttle.h:78
camera_throttle::RgbdCameraThrottleNodelet::subPclNh
ros::NodeHandle subPclNh
Definition: rgbd_throttle.h:64
camera_throttle::RgbdCameraThrottleNodelet::queueSize
size_t queueSize
Definition: rgbd_throttle.h:75
camera_throttle::RgbdCameraThrottleNodelet::subscribePcl
bool subscribePcl
Definition: rgbd_throttle.h:81
camera_throttle::RgbdCameraThrottleNodelet::subTransport
std::unique_ptr< RgbdImageTransport > subTransport
Definition: rgbd_throttle.h:68
image_transport::SingleSubscriberPublisher
camera_throttle::RgbdCameraThrottleNodelet::info_connect_cb
void info_connect_cb(const ros::SingleSubscriberPublisher &)
Definition: rgbd_throttle.cpp:183
NODELET_INFO
#define NODELET_INFO(...)
camera_throttle::RgbdCameraThrottleNodelet::subDepthNh
ros::NodeHandle subDepthNh
Definition: rgbd_throttle.h:63
nodelet::Nodelet
camera_throttle::RgbdCameraThrottleNodelet::pub
RgbdCameraPublisher pub
Definition: rgbd_throttle.h:71
NodeletParamHelper< ::nodelet::Nodelet >::getParam
inline ::std::string getParam(const ::ros::NodeHandle &node, const ::std::string &name, const ::cras::optional< const char * > &defaultValue, const ::std::string &unit="", const ::cras::GetParamOptions<::std::string > &options={})
camera_throttle
Definition: camera_throttle.h:11
class_list_macros.hpp
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
camera_throttle::RgbdCameraThrottleNodelet::pubTransport
std::unique_ptr< RgbdImageTransport > pubTransport
Definition: rgbd_throttle.h:69
ros::Rate
image_transport::TransportHints
camera_throttle::RgbdCameraThrottleNodelet::pubDepthBaseName
std::string pubDepthBaseName
Definition: rgbd_throttle.h:80
ros::NodeHandle
camera_throttle::RgbdCameraPublisher::getNumSubscribers
size_t getNumSubscribers() const
Returns the number of subscribers that are currently connected to this CameraPublisher.
Definition: rgbd_camera_publisher.cpp:115
NODELET_DEBUG
#define NODELET_DEBUG(...)
ros::Time::now
static Time now()
camera_throttle::RgbdCameraThrottleNodelet::onLastDisconnect
virtual void onLastDisconnect()
Definition: rgbd_throttle.cpp:223


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