00001 00006 #include <ecl/threads/thread.hpp> 00007 #include <nodelet/nodelet.h> 00008 #include <pluginlib/class_list_macros.h> 00009 #include "yocs_safety_controller/safety_controller.hpp" 00010 00011 00012 namespace yocs_safety_controller 00013 { 00014 00015 class SafetyControllerNodelet : public nodelet::Nodelet 00016 { 00017 public: 00018 SafetyControllerNodelet() : shutdown_requested_(false) { }; 00019 ~SafetyControllerNodelet() 00020 { 00021 NODELET_DEBUG_STREAM("Waiting for update thread to finish."); 00022 shutdown_requested_ = true; 00023 update_thread_.join(); 00024 } 00025 virtual void onInit() 00026 { 00027 ros::NodeHandle nh = this->getPrivateNodeHandle(); 00028 // resolve node(let) name 00029 std::string name = nh.getUnresolvedNamespace(); 00030 int pos = name.find_last_of('/'); 00031 name = name.substr(pos + 1); 00032 NODELET_INFO_STREAM("Initialising nodelet... [" << name << "]"); 00033 controller_.reset(new SafetyController(nh, name)); 00034 if (controller_->init()) 00035 { 00036 NODELET_INFO_STREAM("Safety controller initialised. Spinning up update thread ... [" << name << "]"); 00037 update_thread_.start(&SafetyControllerNodelet::update, *this); 00038 NODELET_INFO_STREAM("Nodelet initialised. [" << name << "]"); 00039 } 00040 else 00041 { 00042 NODELET_ERROR_STREAM("Couldn't initialise nodelet! Please restart. [" << name << "]"); 00043 } 00044 } 00045 private: 00046 void update() 00047 { 00048 ros::Rate spin_rate(10); 00049 controller_->enable(); // enable the controller when loading the nodelet 00050 while (! shutdown_requested_ && ros::ok()) 00051 { 00052 controller_->spinOnce(); 00053 spin_rate.sleep(); 00054 } 00055 } 00056 00057 boost::shared_ptr<SafetyController> controller_; 00058 ecl::Thread update_thread_; 00059 bool shutdown_requested_; 00060 }; 00061 00062 } // namespace yocs_safety_controller 00063 00064 PLUGINLIB_EXPORT_CLASS(yocs_safety_controller::SafetyControllerNodelet, 00065 nodelet::Nodelet);