pointcloud_to_pcd_nodelet.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2016, JSK Lab
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
37 #include <sensor_msgs/PointCloud2.h>
40 
41 
42 #include <pcl/io/io.h>
43 #include <Eigen/Geometry>
44 
45 namespace jsk_pcl_ros_utils
46 {
48  {
49  timer_.stop();
50  }
51 
53  savePCD();
54  }
55 
56  bool PointCloudToPCD::savePCDCallback(std_srvs::Empty::Request& req, std_srvs::Empty::Response& res) {
57  savePCD();
58  return true;
59  }
60 
62  {
64  cloud = ros::topic::waitForMessage<pcl::PCLPointCloud2>("input", *pnh_);
65  if ((cloud->width * cloud->height) == 0)
66  {
67  return;
68  }
69 
70  NODELET_INFO ("Received %d data points in frame %s with the following fields: %s",
71  (int)cloud->width * cloud->height,
72  cloud->header.frame_id.c_str (),
73  pcl::getFieldsList (*cloud).c_str ());
74 
75  Eigen::Vector4f v = Eigen::Vector4f::Zero ();
76  Eigen::Quaternionf q = Eigen::Quaternionf::Identity ();
77  if (!fixed_frame_.empty ()) {
79  NODELET_WARN("Could not get transform!");
80  return;
81  }
82  tf::StampedTransform transform_stamped;
83  tf_listener_->lookupTransform (fixed_frame_, cloud->header.frame_id, pcl_conversions::fromPCL (cloud->header).stamp, transform_stamped);
84  Eigen::Affine3d transform;
85  tf::transformTFToEigen(transform_stamped, transform);
86  v = Eigen::Vector4f::Zero ();
87  v.head<3> () = transform.translation ().cast<float> ();
88  q = transform.rotation ().cast<float> ();
89  }
90 
91  std::stringstream ss;
92  ss << prefix_ << cloud->header.stamp << ".pcd";
93  NODELET_INFO ("Data saved to %s", ss.str ().c_str ());
94 
95  pcl::PCDWriter writer;
96  if(binary_)
97  {
98  if(compressed_)
99  {
100  writer.writeBinaryCompressed (ss.str (), *cloud, v, q);
101  }
102  else
103  {
104  writer.writeBinary (ss.str (), *cloud, v, q);
105  }
106  }
107  else
108  {
109  writer.writeASCII (ss.str (), *cloud, v, q, 8);
110  }
111  }
112 
114  {
115  PCLNodelet::onInit();
116  srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
117  dynamic_reconfigure::Server<Config>::CallbackType f = boost::bind(&PointCloudToPCD::configCallback, this, _1, _2);
118  srv_->setCallback (f);
119  srv_save_pcd_server_ = pnh_->advertiseService("save_pcd", &PointCloudToPCD::savePCDCallback, this);
121  if(binary_)
122  {
123  if(compressed_)
124  {
125  NODELET_INFO("Saving as binary compressed PCD") ;
126  }
127  else
128  {
129  NODELET_INFO("Saving as binary PCD");
130  }
131  }
132  else
133  {
134  NODELET_INFO("Saving as ASCII PCD");
135  }
136  }
137 
138  void PointCloudToPCD::configCallback(Config &config, uint32_t level)
139  {
140  boost::mutex::scoped_lock lock(mutex_);
141  prefix_ = config.prefix;
142  binary_ = config.binary;
143  compressed_ = config.compressed;
144  fixed_frame_ = config.fixed_frame;
145  duration_ = config.duration;
146  timer_.stop();
147  if (duration_ != 0) {
148  timer_ = pnh_->createTimer(ros::Duration(duration_), boost::bind(&PointCloudToPCD::timerCallback, this, _1));
149  }
150  }
151 }
152 
jsk_pcl_ros_utils
Definition: add_point_indices.h:51
jsk_pcl_ros_utils::PointCloudToPCD::savePCD
void savePCD()
Definition: pointcloud_to_pcd_nodelet.cpp:93
nodelet_topic_tools::NodeletLazy::pnh_
boost::shared_ptr< ros::NodeHandle > pnh_
jsk_pcl_ros_utils::PointCloudToPCD::binary_
bool binary_
Definition: pointcloud_to_pcd.h:132
jsk_pcl_ros_utils::PointCloudToPCD
Definition: pointcloud_to_pcd.h:82
jsk_pcl_ros_utils::PointCloudToPCD::~PointCloudToPCD
virtual ~PointCloudToPCD()
Definition: pointcloud_to_pcd_nodelet.cpp:79
boost::shared_ptr
pcl::getFieldsList
std::string getFieldsList(const sensor_msgs::PointCloud2 &cloud)
lock
lock
jsk_pcl_ros_utils::PointCloudToPCD::configCallback
virtual void configCallback(Config &config, uint32_t level)
Definition: pointcloud_to_pcd_nodelet.cpp:170
ros::Timer::stop
void stop()
NODELET_WARN
#define NODELET_WARN(...)
jsk_pcl_ros_utils::PointCloudToPCD::prefix_
std::string prefix_
Definition: pointcloud_to_pcd.h:131
transform
template Halfspace< double > transform(const Halfspace< double > &a, const Transform3< double > &tf)
tf::StampedTransform
sample_camera_info_and_pointcloud_publisher.cloud
cloud
Definition: sample_camera_info_and_pointcloud_publisher.py:30
jsk_pcl_ros_utils::PointCloudToPCD::savePCDCallback
bool savePCDCallback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res)
Definition: pointcloud_to_pcd_nodelet.cpp:88
class_list_macros.h
pcl_conversions::fromPCL
void fromPCL(const pcl::ModelCoefficients &pcl_mc, pcl_msgs::ModelCoefficients &mc)
jsk_pcl_ros_utils::PointCloudToPCD::duration_
double duration_
Definition: pointcloud_to_pcd.h:130
q
q
tf::transformTFToEigen
void transformTFToEigen(const tf::Transform &t, Eigen::Affine3f &eigen)
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros_utils::PointCloudToPCD, nodelet::Nodelet)
jsk_pcl_ros_utils::PointCloudToPCD::srv_save_pcd_server_
ros::ServiceServer srv_save_pcd_server_
Definition: pointcloud_to_pcd.h:136
jsk_pcl_ros_utils::PointCloudToPCD::mutex_
boost::mutex mutex_
Definition: pointcloud_to_pcd.h:125
_1
boost::arg< 1 > _1
tf::Transformer::waitForTransform
bool waitForTransform(const std::string &target_frame, const ros::Time &target_time, const std::string &source_frame, const ros::Time &source_time, const std::string &fixed_frame, const ros::Duration &timeout, const ros::Duration &polling_sleep_duration=ros::Duration(0.01), std::string *error_msg=NULL) const
ros::TimerEvent
pcl_conversion_util.h
jsk_pcl_ros_utils::PointCloudToPCD::onInit
virtual void onInit()
Definition: pointcloud_to_pcd_nodelet.cpp:145
NODELET_INFO
#define NODELET_INFO(...)
f
f
nodelet::Nodelet
jsk_pcl_ros_utils::PointCloudToPCD::srv_
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
Definition: pointcloud_to_pcd.h:126
jsk_pcl_ros_utils::PointCloudToPCD::timerCallback
virtual void timerCallback(const ros::TimerEvent &event)
Definition: pointcloud_to_pcd_nodelet.cpp:84
jsk_pcl_ros_utils::PointCloudToPCD::fixed_frame_
std::string fixed_frame_
Definition: pointcloud_to_pcd.h:134
jsk_recognition_utils::TfListenerSingleton::getInstance
static tf::TransformListener * getInstance()
jsk_pcl_ros_utils::PointCloudToPCD::compressed_
bool compressed_
Definition: pointcloud_to_pcd.h:133
tf_listener_singleton.h
tf::Transformer::lookupTransform
void lookupTransform(const std::string &target_frame, const ros::Time &target_time, const std::string &source_frame, const ros::Time &source_time, const std::string &fixed_frame, StampedTransform &transform) const
pointcloud_to_pcd.h
config
config
ros::Duration
jsk_pcl_ros_utils::PointCloudToPCD::timer_
ros::Timer timer_
Definition: pointcloud_to_pcd.h:127
jsk_pcl_ros_utils::PointCloudToPCD::tf_listener_
tf::TransformListener * tf_listener_
Definition: pointcloud_to_pcd.h:135


jsk_pcl_ros_utils
Author(s): Yohei Kakiuchi
autogenerated on Fri May 16 2025 03:11:43