joint_state_static_filter_nodelet.cpp
Go to the documentation of this file.
00001 // -*- mode: c++ -*-
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2014, JSK Lab
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/o2r other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the JSK Lab nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *********************************************************************/
00035 
00036 #include "jsk_pcl_ros/joint_state_static_filter.h"
00037 #include <jsk_topic_tools/rosparam_utils.h>
00038 
00039 namespace jsk_pcl_ros
00040 {
00041   void JointStateStaticFilter::onInit()
00042   {
00043     DiagnosticNodelet::onInit();
00044 
00045     double vital_rate;
00046     pnh_->param("vital_rate", vital_rate, 1.0);
00047     joint_vital_.reset(
00048       new jsk_topic_tools::VitalChecker(1 / vital_rate));
00049     if (!jsk_topic_tools::readVectorParameter(*pnh_,
00050                                               "joint_names",
00051                                               joint_names_) ||
00052         joint_names_.size() == 0) {
00053       JSK_NODELET_FATAL("NO ~joint_names is specified");
00054       return;
00055     }
00056     pub_ = advertise<sensor_msgs::PointCloud2>(*pnh_, "output", 1);
00057   }
00058 
00059   void JointStateStaticFilter::subscribe()
00060   {
00061     sub_joint_ = pnh_->subscribe("input_joint_state", 1,
00062                                  &JointStateStaticFilter::jointStateCallback,
00063                                  this);
00064     sub_input_ = pnh_->subscribe("input", 1,
00065                                  &JointStateStaticFilter::filter,
00066                                  this);
00067   }
00068 
00069   void JointStateStaticFilter::unsubscribe()
00070   {
00071     sub_joint_.shutdown();
00072     sub_input_.shutdown();
00073   }
00074 
00075   void JointStateStaticFilter::filter(
00076     const sensor_msgs::PointCloud2::ConstPtr& msg)
00077   {
00078     boost::mutex::scoped_lock lock(mutex_);
00079     JSK_NODELET_DEBUG("Pointcloud Callback");
00080     vital_checker_->poke();
00081     if (isStatic(msg->header.stamp)) {
00082       JSK_ROS_DEBUG("static");
00083       pub_.publish(msg);
00084     }
00085     else {
00086       JSK_ROS_DEBUG("not static");
00087     }
00088     diagnostic_updater_->update();
00089   }
00090 
00091   std::vector<double>
00092   JointStateStaticFilter::filterJointState(
00093     const sensor_msgs::JointState::ConstPtr& msg)
00094   {
00095     std::vector<double> ret;
00096     for (size_t i = 0; i < joint_names_.size(); i++) {
00097       std::string target_joint = joint_names_[i];
00098       // lookup target_joint from msg
00099       bool find_joint = false;
00100       for (size_t j = 0; j < msg->name.size(); j++) {
00101         if (target_joint == msg->name[j]) {
00102           ret.push_back(msg->position[j]);
00103           find_joint = true;
00104           break;
00105         }
00106       }
00107       if (!find_joint) {
00108         return std::vector<double>();
00109       }
00110     }
00111     return ret;
00112   }
00113 
00114   bool JointStateStaticFilter::isStatic(
00115     const ros::Time& stamp)
00116   {
00117     double min_diff = DBL_MAX;
00118     bool min_value = false;
00119     for (boost::circular_buffer<StampedBool>::iterator it = buf_.begin();
00120          it != buf_.end();
00121          ++it) {
00122       double diff = fabs((it->get<0>() - stamp).toSec());
00123       if (diff < min_diff) {
00124         min_value = it->get<1>();
00125         min_diff = diff;
00126       }
00127     }
00128     JSK_NODELET_DEBUG("min_diff: %f", min_diff);
00129     return min_value;
00130   }
00131   
00132   void JointStateStaticFilter::jointStateCallback(
00133     const sensor_msgs::JointState::ConstPtr& msg)
00134   {
00135     boost::mutex::scoped_lock lock(mutex_);
00136     JSK_NODELET_DEBUG("jointCallback");
00137     // filter out joints based on joint names
00138     std::vector<double> joints = filterJointState(msg);
00139     if (joints.size() == 0) {
00140       JSK_NODELET_DEBUG("cannot find the joints from the input message");
00141       return;
00142     }
00143     joint_vital_->poke();
00144 
00145     // check the previous state...
00146     if (previous_joints_.size() > 0) {
00147       // compute velocity
00148       for (size_t i = 0; i < previous_joints_.size(); i++) {
00149         // JSK_NODELET_INFO("[%s] diff: %f", joint_names_[i].c_str(),
00150         //              fabs(previous_joints_[i] - joints[i]));
00151         if (fabs(previous_joints_[i] - joints[i]) > eps_) {
00152           buf_.push_front(boost::make_tuple<ros::Time, bool>(
00153                             msg->header.stamp, false));
00154           previous_joints_ = joints;
00155           return;
00156         }
00157       }
00158       buf_.push_front(boost::make_tuple<ros::Time, bool>(
00159                         msg->header.stamp, true));
00160     }
00161     previous_joints_ = joints;
00162   }
00163   
00164   void JointStateStaticFilter::updateDiagnostic(
00165     diagnostic_updater::DiagnosticStatusWrapper &stat)
00166   {
00167     if (vital_checker_->isAlive()) {
00168       if (joint_vital_->isAlive()) {
00169         stat.summary(diagnostic_msgs::DiagnosticStatus::OK,
00170                      name_ + " running");
00171       }
00172       else {
00173         addDiagnosticErrorSummary(
00174           name_, joint_vital_, stat);
00175       }
00176     }
00177     else {
00178       addDiagnosticErrorSummary(
00179         name_, vital_checker_, stat);
00180     }
00181 
00182   }
00183 }
00184 
00185 #include <pluginlib/class_list_macros.h>
00186 PLUGINLIB_EXPORT_CLASS (jsk_pcl_ros::JointStateStaticFilter, nodelet::Nodelet);


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