block_nodelet.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2014, 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 Willow Garage 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 #include <pluginlib/class_list_macros.h>
00035 #include "jsk_topic_tools/block_nodelet.h"
00036 
00037 #include <ros/advertise_options.h>
00038 
00039 namespace jsk_topic_tools
00040 {
00041 
00042   
00043   
00044   void Block::onInit()
00045   {
00046     pnh_ = getPrivateNodeHandle();
00047     pub_input_original_advertised_ = pub_output_advertised_ = false;
00048     sub_input_subscribing_ = sub_output_original_subscribing_ = false;
00049     pnh_.param("check_rate", check_rate_, 1.0); // defaults to 1Hz
00050     sub_input_
00051       = pnh_.subscribe<topic_tools::ShapeShifter>("input", 1,
00052                                                   &Block::inputCallback,
00053                                                   this);
00054     sub_output_original_
00055       = pnh_.subscribe<topic_tools::ShapeShifter>(
00056         "output_original", 1,
00057         &Block::outputOriginalCallback,
00058         this);
00059     timer_ = pnh_.createTimer(ros::Duration(1 / check_rate_),
00060                              &Block::timerCallback, this);
00061   }
00062 
00063   void Block::timerCallback(const ros::TimerEvent& event)
00064   {
00065     NODELET_DEBUG("timerCallback");
00066     if (pub_input_original_advertised_ && pub_output_advertised_) {
00067       // if the publishers are not advertised, we need to wait until
00068       // the subscribers get the first messages
00069 
00070       // next, check the number of the publisher
00071       if (pub_output_.getNumSubscribers() > 0) {
00072         NODELET_DEBUG("subscribe input");
00073         // we need to run all the subscribers
00074         if (!sub_input_subscribing_) {
00075           sub_input_ = pnh_.subscribe<topic_tools::ShapeShifter>(
00076             "input", 1,
00077             &Block::inputCallback,
00078             this);
00079           sub_input_subscribing_ = true;
00080         }
00081         if (!sub_output_original_subscribing_) {
00082           NODELET_DEBUG("subscribe output original");
00083           sub_output_original_
00084             = pnh_.subscribe<topic_tools::ShapeShifter>(
00085               "output_original", 1,
00086               &Block::outputOriginalCallback,
00087               this);
00088           sub_output_original_subscribing_ = true;
00089         }
00090       }
00091       else {
00092         // we need to shutdown the subscribers
00093         if (sub_input_subscribing_) {
00094           NODELET_DEBUG("shutdown input");
00095           sub_input_.shutdown();
00096           sub_input_subscribing_ = false;
00097         }
00098         if (sub_output_original_subscribing_) {
00099           NODELET_DEBUG("shutdown output_original");
00100           sub_output_original_.shutdown();
00101           sub_output_original_subscribing_ = false;
00102         }
00103       }
00104     }
00105   }
00106   
00107   void Block::inputCallback(
00108     const boost::shared_ptr<topic_tools::ShapeShifter const>& msg)
00109   {
00110     NODELET_DEBUG("inputCallback");
00111     // first we need to check is the publisher is advertised or not
00112     if (!pub_input_original_advertised_) {
00113       NODELET_DEBUG("advertise input_original");
00114       pub_input_original_ = msg->advertise(pnh_, "input_original", 1);
00115       pub_input_original_advertised_ = true;
00116       // shutdown subscriber
00117       if (pub_output_advertised_) {
00118         NODELET_DEBUG("shutdown input");
00119         sub_input_.shutdown();
00120       }
00121       else {
00122         NODELET_DEBUG("publish input_original");
00123         pub_input_original_.publish(msg);
00124       }
00125     }
00126     else {
00127       NODELET_DEBUG("publish input_original");
00128       pub_input_original_.publish(msg);
00129     }
00130   }
00131 
00132   void Block::outputOriginalCallback(
00133     const boost::shared_ptr<topic_tools::ShapeShifter const>& msg)
00134   {
00135     NODELET_DEBUG("outputOriginalCallback");
00136     // first we need to check is the publisher is advertised or not
00137     if (!pub_output_advertised_) {
00138       NODELET_DEBUG("advertise output");;
00139       pub_output_ = msg->advertise(pnh_, "output", 1);
00140       pub_output_advertised_ = true;
00141       // shutdown subscriber
00142       sub_output_original_.shutdown();
00143       if (pub_input_original_advertised_) {
00144         NODELET_DEBUG("shutdown input");
00145         sub_input_.shutdown();
00146       }
00147       else {
00148         NODELET_DEBUG("publish output");
00149         pub_output_.publish(msg);
00150       }
00151     }
00152     else {
00153       NODELET_DEBUG("publish output");
00154       pub_output_.publish(msg);
00155     }
00156   }  
00157 }
00158 
00159 typedef jsk_topic_tools::Block Block;
00160 PLUGINLIB_EXPORT_CLASS(Block, nodelet::Nodelet)
00161 


jsk_topic_tools
Author(s): Kei Okada , Yusuke Furuta
autogenerated on Sun Jan 25 2015 12:38:34