centroid_publisher_nodelet.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2013, Ryohei Ueda and JSK Lab
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/o2r other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the JSK Lab nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software 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 #define BOOST_PARAMETER_MAX_ARITY 7
00036 #include "jsk_pcl_ros/centroid_publisher.h"
00037 
00038 #include <geometry_msgs/PoseStamped.h>
00039 #include <geometry_msgs/PointStamped.h>
00040 
00041 #include <pcl/common/centroid.h>
00042 #include "jsk_pcl_ros/pcl_conversion_util.h"
00043 
00044 namespace jsk_pcl_ros
00045 {
00046   void CentroidPublisher::extract(const sensor_msgs::PointCloud2ConstPtr& input)
00047   {
00048     pcl::PointCloud<pcl::PointXYZ> cloud_xyz;
00049     pcl::fromROSMsg(*input, cloud_xyz);
00050     Eigen::Vector4f center;
00051     pcl::compute3DCentroid(cloud_xyz, center);
00052     if (publish_tf_) {
00053       tf::Transform transform;
00054       transform.setOrigin(tf::Vector3(center[0], center[1], center[2]));
00055       transform.setRotation(tf::createIdentityQuaternion());
00056       br_.sendTransform(tf::StampedTransform(transform, input->header.stamp,
00057                                              input->header.frame_id, frame_));
00058     }
00059     geometry_msgs::PoseStamped pose;
00060     pose.pose.orientation.w = 1.0;
00061     pose.pose.position.x = center[0];
00062     pose.pose.position.y = center[1];
00063     pose.pose.position.z = center[2];
00064     pose.header = input->header;
00065     pub_pose_.publish(pose);
00066     geometry_msgs::PointStamped point;
00067     point.point.x = center[0];
00068     point.point.y = center[1];
00069     point.point.z = center[2];
00070     point.header = input->header;
00071     pub_point_.publish(point);
00072   }
00073 
00074   void CentroidPublisher::subscribe()
00075   {
00076     sub_input_ = pnh_->subscribe("input", 1, &CentroidPublisher::extract, this);
00077   }
00078 
00079   void CentroidPublisher::unsubscribe()
00080   {
00081     sub_input_.shutdown();
00082   }
00083   
00084   void CentroidPublisher::onInit(void)
00085   {
00086     DiagnosticNodelet::onInit();
00087     pnh_->param("publish_tf", publish_tf_, false);
00088     if (publish_tf_) {
00089       if (!pnh_->getParam("frame", frame_))
00090       {
00091         JSK_ROS_WARN("~frame is not specified, using %s", getName().c_str());
00092         frame_ = getName();
00093       }
00094       // do not use DiagnosticNodelet functionality when ~publish_tf is false
00095       pub_pose_ = pnh_->advertise<geometry_msgs::PoseStamped>("output/pose", 1);
00096       pub_point_ = pnh_->advertise<geometry_msgs::PointStamped>(
00097         "output/point", 1);
00098       subscribe();
00099     }
00100     else {
00101       pub_pose_ = advertise<geometry_msgs::PoseStamped>(*pnh_, "output/pose", 1);
00102       pub_point_ = advertise<geometry_msgs::PointStamped>(
00103         *pnh_, "output/point", 1);
00104     }
00105   }
00106 }
00107 
00108 #include <pluginlib/class_list_macros.h>
00109 PLUGINLIB_EXPORT_CLASS (jsk_pcl_ros::CentroidPublisher, nodelet::Nodelet);


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Wed Sep 16 2015 04:36:47