Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
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
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
00146 if (previous_joints_.size() > 0) {
00147
00148 for (size_t i = 0; i < previous_joints_.size(); i++) {
00149
00150
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);