laserscan_kinect_node.cpp
Go to the documentation of this file.
2 #include <boost/bind.hpp>
3 #include <functional>
4 
5 namespace laserscan_kinect {
6 
8  pnh_(pnh), it_(pnh), srv_(pnh) {
9  std::lock_guard<std::mutex> lock(connect_mutex_);
10 
11  // Dynamic reconfigure server callback
12  srv_.setCallback(boost::bind(&LaserScanKinectNode::reconfigureCb, this, _1, _2));
13 
14  // Subscription to depth image topic
15  pub_ = pnh.advertise<sensor_msgs::LaserScan>("scan", 10,
16  std::bind(&LaserScanKinectNode::connectCb, this),
17  std::bind(&LaserScanKinectNode::disconnectCb, this));
18 
19  // New depth image publisher
20  pub_dbg_img_ = it_.advertise("debug_image", 1,
21  std::bind(&LaserScanKinectNode::connectCb, this),
22  std::bind(&LaserScanKinectNode::disconnectCb, this));
23 }
24 
26  sub_.shutdown();
27 }
28 
29 void LaserScanKinectNode::depthCb(const sensor_msgs::ImageConstPtr& depth_msg,
30  const sensor_msgs::CameraInfoConstPtr& info_msg) {
31  try {
32  sensor_msgs::LaserScanPtr laserscan_msg = converter_.getLaserScanMsg(depth_msg, info_msg);
33  pub_.publish(laserscan_msg);
34 
35  // Publish debug image
37  auto dbg_image = converter_.getDbgImage();
38  if (dbg_image != nullptr) {
39  pub_dbg_img_.publish(dbg_image);
40  }
41  }
42  }
43  catch (std::runtime_error& e) {
44  ROS_ERROR_THROTTLE(1.0, "Could not convert depth image to laserscan: %s", e.what());
45  }
46 }
47 
49  std::lock_guard<std::mutex> lock(connect_mutex_);
50 
51  if (sub_ == nullptr && (pub_.getNumSubscribers() > 0 || pub_dbg_img_.getNumSubscribers() > 0)) {
52  ROS_DEBUG("Connecting to depth topic.");
54  sub_ = it_.subscribeCamera("image", 10, &LaserScanKinectNode::depthCb, this, hints);
55  }
56 }
57 
59  std::lock_guard<std::mutex> lock(connect_mutex_);
60 
62  ROS_DEBUG("Unsubscribing from depth topic.");
63  sub_.shutdown();
64  }
65 }
66 
67 void LaserScanKinectNode::reconfigureCb(laserscan_kinect::LaserscanKinectConfig& config,
68  [[maybe_unused]] uint32_t level) {
69  converter_.setOutputFrame(config.output_frame_id);
70  converter_.setRangeLimits(config.range_min, config.range_max);
71  converter_.setScanHeight(config.scan_height);
72  converter_.setDepthImgRowStep(config.depth_img_row_step);
73  converter_.setCamModelUpdate(config.cam_model_update);
74 
75  converter_.setSensorMountHeight(config.sensor_mount_height);
76  converter_.setSensorTiltAngle(config.sensor_tilt_angle);
77  converter_.setGroundRemove(config.ground_remove_en);
78  converter_.setGroundMargin(config.ground_margin);
79  converter_.setTiltCompensation(config.tilt_compensation_en);
80 
82  converter_.setPublishDbgImgEnable(config.publish_dbg_info);
83  converter_.setThreadsNum(config.threads_num);
84 }
85 
86 } // namespace laserscan_kinect
void setPublishDbgImgEnable(const bool enable)
setPublishDbgImgEnable
void setRangeLimits(const float rmin, const float rmax)
setRangeLimits sets depth sensor min and max ranges
void setOutputFrame(const std::string &frame)
setOutputFrame sets the frame to output laser scan
ros::NodeHandle pnh_
Private node handler used to generate the transport hints in the connectCb.
void setDepthImgRowStep(const int row_step)
setDepthImgRowStep
void setScanConfigurated(const bool configured)
setScanConfigurated sets the configuration status
void publish(const boost::shared_ptr< M > &message) const
void setTiltCompensation(const bool enable)
setTiltCompensation enables or disables the feature which compensates sensor tilt ...
sensor_msgs::ImageConstPtr getDbgImage() const
image_transport::ImageTransport it_
Subscribes to synchronized Image CameraInfo pairs.
LaserScanKinectNode(ros::NodeHandle &pnh)
LaserScanKinectNode constructor.
std::mutex connect_mutex_
Prevents the connectCb and disconnectCb from being called until everything is initialized.
CameraSubscriber subscribeCamera(const std::string &base_topic, uint32_t queue_size, const CameraSubscriber::Callback &callback, const ros::VoidPtr &tracked_object=ros::VoidPtr(), const TransportHints &transport_hints=TransportHints())
void setSensorTiltAngle(const float angle)
setSensorTiltAngle sets the sensor tilt angle (in degrees)
void reconfigureCb(laserscan_kinect::LaserscanKinectConfig &config, uint32_t level)
reconfigureCb is dynamic reconfigure callback
Publisher advertise(const std::string &base_topic, uint32_t queue_size, bool latch=false)
uint32_t getNumSubscribers() const
void connectCb()
connectCb is callback which is called when new subscriber connected.
void setThreadsNum(unsigned threads_num)
void setSensorMountHeight(const float height)
setSensorMountHeight sets the height of sensor mount (in meters)
#define ROS_ERROR_THROTTLE(rate,...)
image_transport::Publisher pub_dbg_img_
Publisher for image_transport.
dynamic_reconfigure::Server< laserscan_kinect::LaserscanKinectConfig > srv_
Dynamic reconfigure server.
void disconnectCb()
disconnectCb is called when a subscriber stop subscribing
ros::Publisher pub_
Publisher for output LaserScan messages.
void publish(const sensor_msgs::Image &message) const
void setGroundRemove(const bool enable)
setGroundRemove enables or disables the feature which remove ground from scan
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
laserscan_kinect::LaserScanKinect converter_
Object which convert depth image to laserscan and store all parameters.
sensor_msgs::LaserScanPtr getLaserScanMsg(const sensor_msgs::ImageConstPtr &depth_msg, const sensor_msgs::CameraInfoConstPtr &info_msg)
prepareLaserScanMsg converts depthimage and prepare new LaserScan message
uint32_t getNumSubscribers() const
image_transport::CameraSubscriber sub_
Subscriber for image_transport.
void setCamModelUpdate(const bool enable)
setCamModelUpdate sets the camera parameters
void setScanHeight(const int scan_height)
setScanHeight sets height of depth image which will be used in conversion process ...
void setGroundMargin(const float margin)
setGroundMargin sets the floor margin (in meters)
void depthCb(const sensor_msgs::ImageConstPtr &depth_msg, const sensor_msgs::CameraInfoConstPtr &info_msg)
depthCb is callback which is called when new depth image appear
#define ROS_DEBUG(...)


laserscan_kinect
Author(s): Michal Drwiega (http://www.mdrwiega.com)
autogenerated on Wed May 5 2021 02:56:14