00001 /* 00002 * Copyright (c) 2012, Yujin Robot. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of Yujin Robot nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00039 /***************************************************************************** 00040 ** Includes 00041 *****************************************************************************/ 00042 00043 #include <nodelet/nodelet.h> 00044 #include <pluginlib/class_list_macros.h> 00045 #include <ecl/threads/thread.hpp> 00046 #include "kobuki_safety_controller/safety_controller.hpp" 00047 00048 00049 namespace kobuki 00050 { 00051 00052 class SafetyControllerNodelet : public nodelet::Nodelet 00053 { 00054 public: 00055 SafetyControllerNodelet() : shutdown_requested_(false) { }; 00056 ~SafetyControllerNodelet() 00057 { 00058 NODELET_DEBUG_STREAM("Waiting for update thread to finish."); 00059 shutdown_requested_ = true; 00060 update_thread_.join(); 00061 } 00062 virtual void onInit() 00063 { 00064 ros::NodeHandle nh = this->getPrivateNodeHandle(); 00065 // resolve node(let) name 00066 std::string name = nh.getUnresolvedNamespace(); 00067 int pos = name.find_last_of('/'); 00068 name = name.substr(pos + 1); 00069 NODELET_INFO_STREAM("Initialising nodelet... [" << name << "]"); 00070 controller_.reset(new SafetyController(nh, name)); 00071 if (controller_->init()) 00072 { 00073 NODELET_INFO_STREAM("Kobuki initialised. Spinning up update thread ... [" << name << "]"); 00074 update_thread_.start(&SafetyControllerNodelet::update, *this); 00075 NODELET_INFO_STREAM("Nodelet initialised. [" << name << "]"); 00076 } 00077 else 00078 { 00079 NODELET_ERROR_STREAM("Couldn't initialise nodelet! Please restart. [" << name << "]"); 00080 } 00081 } 00082 private: 00083 void update() 00084 { 00085 ros::Rate spin_rate(10); 00086 controller_->enable(); // enable the controller when loading the nodelet 00087 while (! shutdown_requested_ && ros::ok()) 00088 { 00089 controller_->spin(); 00090 spin_rate.sleep(); 00091 } 00092 } 00093 00094 boost::shared_ptr<SafetyController> controller_; 00095 ecl::Thread update_thread_; 00096 bool shutdown_requested_; 00097 }; 00098 00099 } // namespace kobuki 00100 00101 PLUGINLIB_EXPORT_CLASS(kobuki::SafetyControllerNodelet, 00102 nodelet::Nodelet);