nodelet.cpp
Go to the documentation of this file.
00001 
00010 /*****************************************************************************
00011 ** Includes
00012 *****************************************************************************/
00013 #include <string>
00014 #include <ecl/threads/thread.hpp>
00015 #include <nodelet/nodelet.h>
00016 #include <pluginlib/class_list_macros.h>
00017 #include "kobuki_random_walker/random_walker_controller.hpp"
00018 
00019 
00020 namespace kobuki
00021 {
00022 
00026 class RandomWalkerControllerNodelet : public nodelet::Nodelet
00027 {
00028 public:
00029   RandomWalkerControllerNodelet(): shutdown_requested_(false){};
00030   ~RandomWalkerControllerNodelet()
00031   {
00032     shutdown_requested_ = true;
00033     NODELET_DEBUG_STREAM("Waiting for update thread to finish. [" << name_ << "]");
00034     update_thread_.join();
00035     NODELET_INFO_STREAM("Controller is shutting down. [" << name_ << "]");
00036   }
00037 
00043   virtual void onInit()
00044   {
00045     ros::NodeHandle nh_priv = this->getPrivateNodeHandle();
00046 
00047     // resolve node(let) name
00048     std::string name = nh_priv.getUnresolvedNamespace();
00049     int pos = name.find_last_of('/');
00050     name_ = name.substr(pos + 1);
00051 
00052     NODELET_INFO_STREAM("Initialising nodelet ... [" << name_ << "]");
00053     controller_.reset(new RandomWalkerController(nh_priv, name_));
00054 
00055     nh_priv.param("update_rate", update_rate_, 10.0);
00056     NODELET_INFO_STREAM("Controller will spin at " << update_rate_ << " hz. [" << name_ << "]");
00057 
00058     // Initialises the controller
00059     if (controller_->init())
00060     {
00061       NODELET_INFO_STREAM("Nodelet initialised. Spinning up update thread. [" << name_ << "]");
00062       update_thread_.start(&RandomWalkerControllerNodelet::update, *this);
00063     }
00064     else
00065     {
00066       NODELET_ERROR_STREAM("Couldn't initialise nodelet! Please restart. [" << name_ << "]");
00067     }
00068   }
00069 
00070 private:
00072   boost::shared_ptr<RandomWalkerController> controller_;
00074   double update_rate_;
00076   std::string name_;
00078   ecl::Thread update_thread_;
00080   bool shutdown_requested_;
00081 
00083   void update()
00084   {
00085     ros::Rate spin_rate(update_rate_);
00086     while (ros::ok() && !shutdown_requested_)
00087     {
00088       controller_->spin();
00089       spin_rate.sleep();
00090     }
00091   }
00092 };
00093 
00094 } // namespace kobuki
00095 
00096 PLUGINLIB_EXPORT_CLASS(kobuki::RandomWalkerControllerNodelet,
00097                        nodelet::Nodelet);


kobuki_random_walker
Author(s): Marcus Liebhardt
autogenerated on Wed Sep 16 2015 04:35:24