plane_rejector_nodelet.cpp
Go to the documentation of this file.
1 // -*- mode: C++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2013, Ryohei Ueda and JSK Lab
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
38 
39 #include <geometry_msgs/Vector3Stamped.h>
41 
42 namespace jsk_pcl_ros_utils
43 {
45  {
46  ConnectionBasedNodelet::onInit();
49  double vital_rate;
50  pnh_->param("vital_rate", vital_rate, 1.0);
51  vital_checker_.reset(
52  new jsk_topic_tools::VitalChecker(1 / vital_rate));
53 
55  diagnostic_updater_->setHardwareID(getName());
57  getName() + "::PlaneRejector",
58  boost::bind(
60  this, _1));
61  if (!pnh_->getParam("processing_frame_id", processing_frame_id_)) {
62  NODELET_FATAL("You need to specify ~processing_frame_id");
63  return;
64  }
65  pnh_->param("use_inliers", use_inliers_, false);
66  pnh_->param("allow_flip", allow_flip_, false);
67 
68  std::vector<double> reference_axis;
69  if (!jsk_topic_tools::readVectorParameter(
70  *pnh_, "reference_axis", reference_axis)) {
71  NODELET_FATAL("you need to specify ~reference_axis");
72  return;
73  }
74  else if (reference_axis.size() != 3){
75  NODELET_FATAL("~reference_axis is not 3 length vector");
76  return;
77  }
78  else {
80  reference_axis_.normalize();
81  }
82 
83  srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
84  dynamic_reconfigure::Server<Config>::CallbackType f =
85  boost::bind (&PlaneRejector::configCallback, this, _1, _2);
86  srv_->setCallback (f);
87 
88  polygons_pub_ = advertise<jsk_recognition_msgs::PolygonArray>(
89  *pnh_, "output_polygons", 1);
90  coefficients_pub_ = advertise<jsk_recognition_msgs::ModelCoefficientsArray>(
91  *pnh_, "output_coefficients", 1);
92  if (use_inliers_) {
93  inliers_pub_ = advertise<jsk_recognition_msgs::ClusterPointIndices>(*pnh_, "output_inliers", 1);
94  }
95  else {
96  }
97  diagnostics_timer_ = pnh_->createTimer(
98  ros::Duration(1.0),
100  this,
101  _1));
102  onInitPostProcess();
103  }
104 
106  // message_filters::Synchronizer needs to be called reset
107  // before message_filters::Subscriber is freed.
108  // Calling reset fixes the following error on shutdown of the nodelet:
109  // terminate called after throwing an instance of
110  // 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >'
111  // what(): boost: mutex lock failed in pthread_mutex_lock: Invalid argument
112  // Also see https://github.com/ros/ros_comm/issues/720 .
113  sync_.reset();
114  sync_inlier_.reset();
115  }
116 
118  {
119  if (use_inliers_) {
120  sync_inlier_ = boost::make_shared<message_filters::Synchronizer<SyncInlierPolicy> >(100);
121  sub_polygons_.subscribe(*pnh_, "input_polygons", 1);
122  sub_coefficients_.subscribe(*pnh_, "input_coefficients", 1);
123  sub_inliers_.subscribe(*pnh_, "input_inliers", 1);
125  sync_inlier_->registerCallback(boost::bind(&PlaneRejector::reject, this, _1, _2, _3));
126  }
127  else {
128  sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(100);
129  sub_polygons_.subscribe(*pnh_, "input_polygons", 1);
130  sub_coefficients_.subscribe(*pnh_, "input_coefficients", 1);
131  sync_->connectInput(sub_polygons_, sub_coefficients_);
132  sync_->registerCallback(boost::bind(&PlaneRejector::reject, this, _1, _2));
133  }
134  }
135 
137  {
140  }
141 
144  {
145  bool alivep = vital_checker_->isAlive();
146  // check tf successeed or not
147  if (alivep) {
148  if (tf_success_->getValue()) {
149  stat.summary(diagnostic_msgs::DiagnosticStatus::OK,
150  "PlaneRejector running");
151  }
152  else {
153  stat.summary(diagnostic_msgs::DiagnosticStatus::ERROR,
154  "failed to tf transform");
155  }
156  stat.add("Input planes (Avg.)", input_plane_counter_.mean());
157  stat.add("Rejected Planes (Avg.)", rejected_plane_counter_.mean());
158  stat.add("Passed Planes (Avg.)", passed_plane_counter_.mean());
159  stat.add("Angular Threahold", angle_thr_);
160  stat.add("Reference Axis",
161  (boost::format("[%f, %f, %f]")
162  % (reference_axis_[0])
163  % (reference_axis_[1])
164  % (reference_axis_[2])).str());
165  stat.add("Processing Frame ID", processing_frame_id_);
166  }
167  else {
168  stat.summary(diagnostic_msgs::DiagnosticStatus::ERROR,
169  "PlaneRejector not running");
170  }
171 
172  }
173 
175  {
176  boost::mutex::scoped_lock lock(mutex_);
177  diagnostic_updater_->update();
178  }
179 
180  void PlaneRejector::configCallback (Config &config, uint32_t level)
181  {
182  boost::mutex::scoped_lock lock(mutex_);
183  angle_thr_ = config.angle_thr;
184  angle_ = config.angle;
185  }
186 
188  const jsk_recognition_msgs::PolygonArray::ConstPtr& polygons,
189  const jsk_recognition_msgs::ModelCoefficientsArray::ConstPtr& coefficients)
190  {
191  reject(polygons, coefficients, jsk_recognition_msgs::ClusterPointIndices::ConstPtr());
192  }
193 
195  const jsk_recognition_msgs::PolygonArray::ConstPtr& polygons,
196  const jsk_recognition_msgs::ModelCoefficientsArray::ConstPtr& coefficients,
197  const jsk_recognition_msgs::ClusterPointIndices::ConstPtr& inliers)
198  {
199  boost::mutex::scoped_lock lock(mutex_);
200  vital_checker_->poke();
201  jsk_recognition_msgs::PolygonArray result_polygons;
202  jsk_recognition_msgs::ModelCoefficientsArray result_coefficients;
203  jsk_recognition_msgs::ClusterPointIndices result_inliers;
204  result_polygons.header = polygons->header;
205  result_coefficients.header = coefficients->header;
206  result_inliers.header = polygons->header;
207  input_plane_counter_.add(polygons->polygons.size());
208  int rejected_plane_counter = 0;
209  int passed_plane_counter = 0;
210  for (size_t i = 0; i < polygons->polygons.size(); i++) {
211  geometry_msgs::PolygonStamped polygon = polygons->polygons[i];
212  PCLModelCoefficientMsg coefficient = coefficients->coefficients[i];
213  // transform the coefficients to processing_frame_id_
214  if (listener_->canTransform(coefficient.header.frame_id,
216  coefficient.header.stamp)) {
217  tf_success_->addValue(true);
218  geometry_msgs::Vector3Stamped plane_axis;
219  plane_axis.header = coefficient.header;
220  plane_axis.vector.x = coefficient.values[0];
221  plane_axis.vector.y = coefficient.values[1];
222  plane_axis.vector.z = coefficient.values[2];
223  geometry_msgs::Vector3Stamped transformed_plane_axis;
225  plane_axis,
226  transformed_plane_axis);
227  Eigen::Vector3d eigen_transformed_plane_axis;
228  tf::vectorMsgToEigen(transformed_plane_axis.vector,
229  eigen_transformed_plane_axis);
230  if (allow_flip_ && eigen_transformed_plane_axis.normalized().dot(reference_axis_) < 0) {
231  // flip normal vector to make its direction same as reference axis
232  eigen_transformed_plane_axis = -eigen_transformed_plane_axis;
233  }
234  double ang = std::abs(acos(eigen_transformed_plane_axis.normalized().dot(reference_axis_)) - angle_);
235  if (ang < angle_thr_) {
236  ++passed_plane_counter;
237  result_polygons.polygons.push_back(polygons->polygons[i]);
238  result_coefficients.coefficients.push_back(coefficient);
239  if (use_inliers_) {
240  result_inliers.cluster_indices.push_back(inliers->cluster_indices[i]);
241  }
242  }
243  else {
244  ++rejected_plane_counter;
245  }
246  }
247  else {
248  tf_success_->addValue(false);
249  }
250  }
251  rejected_plane_counter_.add(rejected_plane_counter);
252  passed_plane_counter_.add(passed_plane_counter);
253  polygons_pub_.publish(result_polygons);
254  coefficients_pub_.publish(result_coefficients);
255  if (use_inliers_) {
256  inliers_pub_.publish(result_inliers);
257  }
258  diagnostic_updater_->update();
259  }
260 
261 }
262 
jsk_pcl_ros_utils
Definition: add_point_indices.h:51
jsk_pcl_ros_utils::PlaneRejector::configCallback
virtual void configCallback(Config &config, uint32_t level)
Definition: plane_rejector_nodelet.cpp:212
jsk_pcl_ros_utils::PlaneRejector::diagnostics_timer_
ros::Timer diagnostics_timer_
Definition: plane_rejector.h:180
polygons
polygons
NODELET_FATAL
#define NODELET_FATAL(...)
jsk_pcl_ros_utils::PlaneRejector::sync_
boost::shared_ptr< message_filters::Synchronizer< SyncPolicy > > sync_
Definition: plane_rejector.h:166
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros_utils::PlaneRejector, nodelet::Nodelet)
i
int i
diagnostic_updater::DiagnosticStatusWrapper::add
void add(const std::string &key, const bool &b)
jsk_pcl_ros_utils::PlaneRejector::sub_coefficients_
message_filters::Subscriber< jsk_recognition_msgs::ModelCoefficientsArray > sub_coefficients_
Definition: plane_rejector.h:164
jsk_pcl_ros_utils::PlaneRejector::unsubscribe
virtual void unsubscribe()
Definition: plane_rejector_nodelet.cpp:168
jsk_pcl_ros_utils::PlaneRejector
Definition: plane_rejector.h:104
jsk_pcl_ros_utils::PlaneRejector::input_plane_counter_
jsk_recognition_utils::Counter input_plane_counter_
Definition: plane_rejector.h:187
jsk_recognition_utils::SeriesedBoolean
jsk_pcl_ros_utils::PlaneRejector::diagnostic_updater_
boost::shared_ptr< diagnostic_updater::Updater > diagnostic_updater_
Definition: plane_rejector.h:181
lock
lock
jsk_recognition_utils::pointFromVectorToVector
void pointFromVectorToVector(const FromT &from, ToT &to)
jsk_pcl_ros_utils::PlaneRejector::angle_thr_
double angle_thr_
Definition: plane_rejector.h:175
getName
ROSCONSOLE_CONSOLE_IMPL_DECL std::string getName(void *handle)
jsk_pcl_ros_utils::PlaneRejector::use_inliers_
bool use_inliers_
Definition: plane_rejector.h:170
jsk_pcl_ros_utils::PlaneRejector::updateDiagnostics
virtual void updateDiagnostics(const ros::TimerEvent &event)
Definition: plane_rejector_nodelet.cpp:206
diagnostic_updater::Updater
str
string str
jsk_pcl_ros_utils::PlaneRejector::tf_success_
jsk_recognition_utils::SeriesedBoolean::Ptr tf_success_
Definition: plane_rejector.h:183
tf::TransformListener::transformVector
void transformVector(const std::string &target_frame, const geometry_msgs::Vector3Stamped &stamped_in, geometry_msgs::Vector3Stamped &stamped_out) const
jsk_pcl_ros_utils::PlaneRejector::listener_
tf::TransformListener * listener_
Definition: plane_rejector.h:176
jsk_pcl_ros_utils::PlaneRejector::vital_checker_
jsk_topic_tools::VitalChecker::Ptr vital_checker_
Definition: plane_rejector.h:182
jsk_pcl_ros_utils::PlaneRejector::angle_
double angle_
Definition: plane_rejector.h:188
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
message_filters::Subscriber::unsubscribe
void unsubscribe()
jsk_pcl_ros_utils::PlaneRejector::~PlaneRejector
virtual ~PlaneRejector()
Definition: plane_rejector_nodelet.cpp:137
jsk_pcl_ros_utils::PlaneRejector::srv_
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
Definition: plane_rejector.h:178
class_list_macros.h
jsk_pcl_ros_utils::PlaneRejector::allow_flip_
bool allow_flip_
Definition: plane_rejector.h:171
jsk_pcl_ros_utils::PlaneRejector::coefficients_pub_
ros::Publisher coefficients_pub_
Definition: plane_rejector.h:179
jsk_pcl_ros_utils::PlaneRejector::onInit
virtual void onInit()
Definition: plane_rejector_nodelet.cpp:76
diagnostic_updater::DiagnosticStatusWrapper::summary
void summary(const diagnostic_msgs::DiagnosticStatus &src)
PCLModelCoefficientMsg
pcl::ModelCoefficients PCLModelCoefficientMsg
jsk_recognition_utils::Counter::add
virtual void add(double v)
jsk_pcl_ros_utils::PlaneRejector::subscribe
virtual void subscribe()
Definition: plane_rejector_nodelet.cpp:149
tf::Transformer::canTransform
bool canTransform(const std::string &target_frame, const ros::Time &target_time, const std::string &source_frame, const ros::Time &source_time, const std::string &fixed_frame, std::string *error_msg=NULL) const
eigen_msg.h
tf::vectorMsgToEigen
void vectorMsgToEigen(const geometry_msgs::Vector3 &m, Eigen::Vector3d &e)
plane_rejector.h
jsk_pcl_ros_utils::PlaneRejector::sync_inlier_
boost::shared_ptr< message_filters::Synchronizer< SyncInlierPolicy > > sync_inlier_
Definition: plane_rejector.h:167
ros::TimerEvent
jsk_pcl_ros_utils::PlaneRejector::passed_plane_counter_
jsk_recognition_utils::Counter passed_plane_counter_
Definition: plane_rejector.h:186
message_filters::Subscriber::subscribe
void subscribe()
jsk_pcl_ros_utils::PlaneRejector::inliers_pub_
ros::Publisher inliers_pub_
Definition: plane_rejector.h:179
jsk_pcl_ros_utils::PlaneRejector::mutex_
boost::mutex mutex_
Definition: plane_rejector.h:177
f
f
nodelet::Nodelet
jsk_pcl_ros_utils::PlaneRejector::updateDiagnosticsPlaneRejector
virtual void updateDiagnosticsPlaneRejector(diagnostic_updater::DiagnosticStatusWrapper &stat)
Definition: plane_rejector_nodelet.cpp:174
coefficients
coefficients
jsk_recognition_utils::Counter::mean
virtual double mean()
diagnostic_updater::DiagnosticStatusWrapper
jsk_pcl_ros_utils::PlaneRejector::sub_polygons_
message_filters::Subscriber< jsk_recognition_msgs::PolygonArray > sub_polygons_
Definition: plane_rejector.h:163
jsk_pcl_ros_utils::PlaneRejector::polygons_pub_
ros::Publisher polygons_pub_
Definition: plane_rejector.h:179
jsk_recognition_utils::TfListenerSingleton::getInstance
static tf::TransformListener * getInstance()
jsk_pcl_ros_utils::PlaneRejector::rejected_plane_counter_
jsk_recognition_utils::Counter rejected_plane_counter_
Definition: plane_rejector.h:185
jsk_pcl_ros_utils::PlaneRejector::reference_axis_
Eigen::Vector3d reference_axis_
Definition: plane_rejector.h:174
config
config
ros::Duration
jsk_pcl_ros_utils::PlaneRejector::processing_frame_id_
std::string processing_frame_id_
Definition: plane_rejector.h:172
jsk_pcl_ros_utils::PlaneRejector::reject
virtual void reject(const jsk_recognition_msgs::PolygonArray::ConstPtr &polygons, const jsk_recognition_msgs::ModelCoefficientsArray::ConstPtr &coefficients)
Definition: plane_rejector_nodelet.cpp:219
jsk_pcl_ros_utils::PlaneRejector::sub_inliers_
message_filters::Subscriber< jsk_recognition_msgs::ClusterPointIndices > sub_inliers_
Definition: plane_rejector.h:165


jsk_pcl_ros_utils
Author(s): Yohei Kakiuchi
autogenerated on Fri May 16 2025 03:11:43