tf_transform_bounding_box_array_nodelet.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2015, 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 
00037 #include "jsk_pcl_ros_utils/tf_transform_bounding_box_array.h"
00038 #include "jsk_recognition_utils/pcl_conversion_util.h"
00039 
00040 namespace jsk_pcl_ros_utils
00041 {
00042 
00043   void TfTransformBoundingBoxArray::onInit()
00044   {
00045     DiagnosticNodelet::onInit();
00046     if (!pnh_->getParam("target_frame_id", target_frame_id_)) {
00047       ROS_FATAL("~target_frame_id is not specified");
00048       return;
00049     }
00050 
00051     pnh_->param("use_latest_tf", use_latest_tf_, false);
00052     pnh_->param("tf_queue_size", tf_queue_size_, 10);
00053     tf_listener_ = jsk_recognition_utils::TfListenerSingleton::getInstance();
00054     pub_ = advertise<jsk_recognition_msgs::BoundingBoxArray>(*pnh_, "output", 1);
00055 
00056     onInitPostProcess();
00057   }
00058 
00059   void TfTransformBoundingBoxArray::subscribe()
00060   {
00061     if (use_latest_tf_) {
00062       sub_ = pnh_->subscribe("input", 1, &TfTransformBoundingBoxArray::transform, this);
00063     }
00064     else {
00065       sub_filter_.subscribe(*pnh_, "input", 10);
00066       tf_filter_.reset(new tf::MessageFilter<jsk_recognition_msgs::BoundingBoxArray>(
00067                          sub_filter_,
00068                          *tf_listener_,
00069                          target_frame_id_,
00070                          tf_queue_size_));
00071       tf_filter_->registerCallback(boost::bind(&TfTransformBoundingBoxArray::transform, this, _1));
00072     }
00073   }
00074 
00075   void TfTransformBoundingBoxArray::unsubscribe()
00076   {
00077     if (use_latest_tf_) {
00078       sub_.shutdown();
00079     }
00080     else {
00081       sub_filter_.unsubscribe();
00082     }
00083   }
00084 
00085   void TfTransformBoundingBoxArray::transform(
00086     const jsk_recognition_msgs::BoundingBoxArray::ConstPtr& msg)
00087   {
00088     vital_checker_->poke();
00089     try
00090     {
00091       jsk_recognition_msgs::BoundingBoxArray transformed_box;
00092       transformed_box.header.stamp = msg->header.stamp;
00093       transformed_box.header.frame_id = target_frame_id_;
00094       tf::StampedTransform tf_transform;
00095       if (use_latest_tf_) {
00096         tf_listener_->lookupTransform(target_frame_id_, msg->header.frame_id,
00097                                       ros::Time(0), tf_transform);
00098       }
00099       else {
00100         tf_listener_->lookupTransform(target_frame_id_, msg->header.frame_id,
00101                                       msg->header.stamp, tf_transform);
00102       }
00103       
00104       Eigen::Affine3f transform;
00105       tf::transformTFToEigen(tf_transform, transform);
00106       for (size_t i = 0; i < msg->boxes.size(); i++) {
00107         jsk_recognition_msgs::BoundingBox box = jsk_recognition_msgs::BoundingBox(msg->boxes[i]);
00108         Eigen::Affine3f pose;
00109         tf::poseMsgToEigen(box.pose, pose);
00110         Eigen::Affine3f new_pose = transform * pose;
00111         tf::poseEigenToMsg(new_pose, box.pose);
00112         box.header.frame_id = target_frame_id_;
00113         transformed_box.boxes.push_back(box);
00114       }
00115       pub_.publish(transformed_box);
00116     }
00117     catch (tf2::ConnectivityException &e)
00118     {
00119       NODELET_ERROR("Transform error: %s", e.what());
00120     }
00121     catch (tf2::InvalidArgumentException &e)
00122     {
00123       NODELET_ERROR("Transform error: %s", e.what());
00124     }
00125     catch (...)
00126     {
00127       NODELET_ERROR("Unknown transform error");
00128     }
00129   }
00130 
00131 }
00132 #include <pluginlib/class_list_macros.h>
00133 PLUGINLIB_EXPORT_CLASS (jsk_pcl_ros_utils::TfTransformBoundingBoxArray, nodelet::Nodelet);


jsk_pcl_ros_utils
Author(s): Yohei Kakiuchi
autogenerated on Sun Oct 8 2017 02:43:05