range_to_cloud.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (C) 2014, 2015, Jack O'Quin
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the author nor other contributors may be
00018 *     used to endorse or promote products derived from this software
00019 *     without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 
00050 #include <string>
00051 #include <limits>
00052 #include <sensor_msgs/PointCloud2.h>
00053 #include <sensor_msgs/Range.h>
00054 #include <pcl_ros/point_cloud.h>
00055 #include "range_to_cloud.h"
00056 
00057 namespace segbot_sensors
00058 {
00060   RangeToCloud::RangeToCloud(ros::NodeHandle node,
00061                                ros::NodeHandle private_nh)
00062   {
00063     // advertise output point cloud (before subscribing to input data)
00064     points_ =
00065       node.advertise<sensor_msgs::PointCloud2>("range_points", 1);
00066 
00067     // subscribe to input ranges
00068     ranges_ =
00069       node.subscribe("sonar", 10,
00070                      &RangeToCloud::processRange, (RangeToCloud *) this,
00071                      ros::TransportHints().tcpNoDelay(true));
00072   }
00073 
00074   void RangeToCloud::processRange(const sensor_msgs::Range::ConstPtr
00075                                   &range_msg)
00076   {
00077     // allocate a point cloud message
00078     pcl::PointCloud<pcl::PointXYZ>::Ptr
00079         cloud(new pcl::PointCloud<pcl::PointXYZ>());
00080     // cloud's header is a pcl::PCLHeader, convert it before stamp assignment
00081     cloud->header.stamp =
00082       pcl_conversions::toPCL(range_msg->header).stamp;
00083     cloud->header.frame_id = "sensor_base"; // TODO: make this a parameter
00084     cloud->height = 1;
00085 
00086     // Skip readings with no object within range (see: REP-0117).
00087     if (range_msg->range < std::numeric_limits<float>::infinity())
00088       {
00089         // Transform this range point into the "sensor_base" frame.
00090         tf::StampedTransform transform;
00091         try
00092           {
00093             listener_.lookupTransform(cloud->header.frame_id,
00094                                       range_msg->header.frame_id,
00095                                       ros::Time(0), transform);
00096           }
00097         catch (tf::TransformException ex)
00098           {
00099             ROS_WARN_STREAM(ex.what());
00100             return;                     // skip this reading
00101           }
00102 
00103         tf::Point pt(range_msg->range, 0.0, 0.0);
00104         pt = transform * pt;
00105 
00106         pcl::PointXYZ pcl_point;
00107         pcl_point.x = pt.m_floats[0];
00108         pcl_point.y = pt.m_floats[1];
00109         pcl_point.z = pt.m_floats[2];
00110         cloud->points.push_back(pcl_point);
00111         ++cloud->width;
00112 
00113         // publish the accumulated cloud message
00114         points_.publish(cloud);
00115       }
00116   }
00117 
00118 
00119 }; // end namespace segbot_sensors


segbot_sensors
Author(s): Piyush Khandelwal
autogenerated on Fri Aug 28 2015 13:03:00