camera_throttle.cpp
Go to the documentation of this file.
4 #include <memory>
5 
6 #include <cv_bridge/cv_bridge.h>
7 
8 namespace camera_throttle
9 {
10 
12 {
13  cv::Mat opencvMat;
14 };
15 
17 {
18 
19 }
20 
22 {
23  auto pnh = this->getPrivateNodeHandle();
24  if (pnh.hasParam("rate"))
25  this->rate = this->getParam(pnh, "rate", ros::Rate(10.0), "Hz");
26  else
27  NODELET_INFO("No rate limiting");
28  this->queueSize = this->getParam(pnh, "queue_size", 10u, "messages");
29  this->subBaseName = this->getParam(pnh, "sub_base_name", "image_raw");
30  this->pubBaseName = this->getParam(pnh, "pub_base_name", this->subBaseName);
31 
32  if (pnh.hasParam("fix_frame_id"))
33  this->frameId = this->getParam(pnh, "fix_frame_id", "");
34  if (this->frameId.has_value() && this->frameId.value().empty())
35  this->frameId.reset();
36  if (this->frameId)
37  NODELET_INFO("Fixing RGB frame_id to %s", this->frameId->c_str());
38 
39  this->flipHorizontal = this->getParam(pnh, "flip_horizontal", false);
40  this->flipVertical = this->getParam(pnh, "flip_vertical", false);
41 
42  this->subNh = ros::NodeHandle(this->getNodeHandle(), "camera_in");
43  this->subTransport = std::make_unique<image_transport::ImageTransport>(this->subNh);
44 
45  this->pubNh = ros::NodeHandle(this->getNodeHandle(), "camera_out");
46  this->pubTransport = std::make_unique<image_transport::ImageTransport>(this->pubNh);
47  this->pub = this->pubTransport->advertiseCamera(this->pubBaseName, this->queueSize,
48  boost::bind(&CameraThrottleNodelet::img_connect_cb, this, _1),
49  boost::bind(&CameraThrottleNodelet::img_disconnect_cb, this, _1),
50  boost::bind(&CameraThrottleNodelet::info_connect_cb, this, _1),
51  boost::bind(&CameraThrottleNodelet::info_disconnect_cb, this, _1));
52 }
53 
54 void CameraThrottleNodelet::cb(const sensor_msgs::ImageConstPtr& img, const sensor_msgs::CameraInfoConstPtr& info)
55 {
56  if (this->rate)
57  {
58  if (this->lastUpdate + this->rate.value().expectedCycleTime() > ros::Time::now())
59  {
60  NODELET_DEBUG("throttle last update at %f skipping", this->lastUpdate.toSec());
61  return;
62  }
63  }
64 
65  this->lastUpdate = ros::Time::now();
66  if (!this->frameId.has_value() && !this->flipHorizontal && !this->flipVertical)
67  {
68  this->pub.publish(img, info);
69  } else {
70  sensor_msgs::ImagePtr newImg(new sensor_msgs::Image);
71  sensor_msgs::CameraInfoPtr newInfo(new sensor_msgs::CameraInfo);
72 
73  *newImg = *img;
74  *newInfo = *info;
75 
76  if (this->flipVertical || this->flipHorizontal)
77  {
78  int flipValue;
79  if (this->flipVertical && this->flipHorizontal)
80  flipValue = -1;
81  else if (this->flipVertical)
82  flipValue = 0;
83  else
84  flipValue = 1;
85 
86  cv_bridge::CvImagePtr cvImage;
87  try
88  {
90  }
91  catch (cv_bridge::Exception& e)
92  {
93  ROS_ERROR("cv_bridge exception: %s", e.what());
94  return;
95  }
96 
97  cv::flip(cvImage->image, this->data->opencvMat, flipValue);
98 
99  newImg = cv_bridge::CvImage(img->header, "bgr8", this->data->opencvMat).toImageMsg();
100  }
101 
102  if (this->frameId)
103  {
104  newImg->header.frame_id = this->frameId.value();
105  newInfo->header.frame_id = this->frameId.value();
106  }
107 
108  this->pub.publish(newImg, newInfo);
109  }
110 }
111 
113 {
114  std::lock_guard<std::mutex> g(this->publishersMutex);
115  if (this->pub.getNumSubscribers() == 1 && !this->sub)
116  this->onFirstConnect();
117 }
118 
120 {
121  std::lock_guard<std::mutex> g(this->publishersMutex);
122  if (this->pub.getNumSubscribers() == 1 && !this->sub)
123  this->onFirstConnect();
124 }
125 
127 {
128  std::lock_guard<std::mutex> g(this->publishersMutex);
129  if (this->pub.getNumSubscribers() == 0 && this->sub)
130  this->onLastDisconnect();
131 }
132 
134 {
135  std::lock_guard<std::mutex> g(this->publishersMutex);
136  if (this->pub.getNumSubscribers() == 0 && this->sub)
137  this->onLastDisconnect();
138 }
139 
141 {
142  NODELET_DEBUG("Started lazy-subscription to %s", this->subNh.resolveName(this->subBaseName).c_str());
143  image_transport::TransportHints hints("raw", {}, this->getPrivateNodeHandle());
144  this->sub = this->subTransport->subscribeCamera(this->subBaseName, this->queueSize, &CameraThrottleNodelet::cb, this, hints);
145 }
146 
148 {
149  NODELET_DEBUG("Stopped lazy-subscription to %s", this->sub.value().getTopic().c_str());
150  this->sub.value().shutdown();
151  this->sub.reset();
152 }
153 
154 }
155 
nodelet::Nodelet::getNodeHandle
ros::NodeHandle & getNodeHandle() const
camera_throttle::CameraThrottleNodelet::pubBaseName
std::string pubBaseName
Definition: camera_throttle.h:61
boost::shared_ptr
image_transport::CameraPublisher::getNumSubscribers
uint32_t getNumSubscribers() const
camera_throttle::CameraThrottleNodelet::pub
image_transport::CameraPublisher pub
Definition: camera_throttle.h:53
cv_bridge::CvImage::toImageMsg
sensor_msgs::ImagePtr toImageMsg() const
camera_throttle::CameraThrottleNodelet::subTransport
std::unique_ptr< image_transport::ImageTransport > subTransport
Definition: camera_throttle.h:50
camera_throttle::CameraThrottleNodelet::publishersMutex
std::mutex publishersMutex
Definition: camera_throttle.h:63
camera_throttle::CameraThrottleNodelet::info_connect_cb
void info_connect_cb(const ros::SingleSubscriberPublisher &)
Definition: camera_throttle.cpp:119
camera_throttle::CameraThrottleNodelet::subNh
ros::NodeHandle subNh
Definition: camera_throttle.h:48
camera_throttle::CameraThrottleNodelet
Definition: camera_throttle.h:36
camera_throttle.h
TimeBase< Time, Duration >::toSec
double toSec() const
image_transport::CameraPublisher::publish
void publish(const sensor_msgs::Image &image, const sensor_msgs::CameraInfo &info) const
camera_throttle::CameraThrottleNodelet::cb
virtual void cb(const sensor_msgs::ImageConstPtr &img, const sensor_msgs::CameraInfoConstPtr &info)
Definition: camera_throttle.cpp:54
cv_bridge::Exception
nodelet::Nodelet::getPrivateNodeHandle
ros::NodeHandle & getPrivateNodeHandle() const
data
data
camera_throttle::CameraThrottleNodelet::pubTransport
std::unique_ptr< image_transport::ImageTransport > pubTransport
Definition: camera_throttle.h:51
camera_throttle::CameraThrottleNodelet::img_connect_cb
void img_connect_cb(const image_transport::SingleSubscriberPublisher &)
Definition: camera_throttle.cpp:112
ros::NodeHandle::resolveName
std::string resolveName(const std::string &name, bool remap=true) const
ros::SingleSubscriberPublisher
camera_throttle::CameraThrottleNodelet::flipHorizontal
bool flipHorizontal
Definition: camera_throttle.h:56
PLUGINLIB_EXPORT_CLASS
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
cv_bridge::toCvCopy
CvImagePtr toCvCopy(const sensor_msgs::CompressedImage &source, const std::string &encoding=std::string())
camera_throttle::CameraThrottleNodelet::img_disconnect_cb
void img_disconnect_cb(const image_transport::SingleSubscriberPublisher &)
Definition: camera_throttle.cpp:126
camera_throttle::CameraThrottleNodelet::onInit
void onInit() override
Definition: camera_throttle.cpp:21
camera_throttle::CameraThrottleNodelet::onFirstConnect
virtual void onFirstConnect()
Definition: camera_throttle.cpp:140
camera_throttle::CameraThrottleNodelet::CameraThrottleNodelet
CameraThrottleNodelet()
Definition: camera_throttle.cpp:16
camera_throttle::CameraThrottleNodelet::rate
std::optional< ros::Rate > rate
Definition: camera_throttle.h:54
camera_throttle::CameraThrottleNodelet::info_disconnect_cb
void info_disconnect_cb(const ros::SingleSubscriberPublisher &)
Definition: camera_throttle.cpp:133
image_transport::SingleSubscriberPublisher
image_transport.h
camera_throttle::CameraThrottleNodelet::flipVertical
bool flipVertical
Definition: camera_throttle.h:57
NODELET_INFO
#define NODELET_INFO(...)
nodelet::Nodelet
camera_throttle::CameraThrottleNodelet::pubNh
ros::NodeHandle pubNh
Definition: camera_throttle.h:49
camera_throttle::CameraThrottleNodelet::CameraThrottlePrivate
Definition: camera_throttle.cpp:11
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={})
cv_bridge.h
camera_throttle
Definition: camera_throttle.h:11
ROS_ERROR
#define ROS_ERROR(...)
cv_bridge::CvImage
class_list_macros.hpp
ros::Rate
sensor_msgs::image_encodings::BGR8
const std::string BGR8
camera_throttle::CameraThrottleNodelet::frameId
std::optional< std::string > frameId
Definition: camera_throttle.h:55
image_transport::TransportHints
camera_throttle::CameraThrottleNodelet::lastUpdate
ros::Time lastUpdate
Definition: camera_throttle.h:59
camera_throttle::CameraThrottleNodelet::subBaseName
std::string subBaseName
Definition: camera_throttle.h:60
camera_throttle::CameraThrottleNodelet::onLastDisconnect
virtual void onLastDisconnect()
Definition: camera_throttle.cpp:147
camera_throttle::CameraThrottleNodelet::queueSize
size_t queueSize
Definition: camera_throttle.h:58
camera_throttle::CameraThrottleNodelet::sub
std::optional< image_transport::CameraSubscriber > sub
Definition: camera_throttle.h:52
ros::NodeHandle
NODELET_DEBUG
#define NODELET_DEBUG(...)
ros::Time::now
static Time now()
camera_throttle::CameraThrottleNodelet::CameraThrottlePrivate::opencvMat
cv::Mat opencvMat
Definition: camera_throttle.cpp:13


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