suppressor.h
Go to the documentation of this file.
00001 /*
00002 Author: Minglong Li
00003 Affiliation: State Key Laboratory of High Performance Computing (HPCL)
00004              College of Computer, National University of Defense Technology
00005 Email: minglong_l@163.com
00006 Created on: June 4th, 2016
00007 */
00008 #ifndef SUPPRESSOR_H_
00009 #define SUPPRESSOR_H_
00010 
00011 #include <ros/ros.h>
00012 #include <nodelet/nodelet.h>
00013 #include <iostream>
00014 #include <pluginlib/class_list_macros.h>
00015 
00016 namespace micros_mars_task_alloc {
00017 
00018 template<typename MsgType>
00019 class Suppressor : public nodelet::Nodelet
00020 {
00021     //typedef typename boost::shared_ptr<MsgType> TMsgPtr;
00022     //typedef typename boost::shared_ptr<const T> TConstPtr;
00023 public:
00024     Suppressor(){}
00025     virtual ~Suppressor(){}
00026     virtual void onInit()
00027     {
00028         nh_ = getPrivateNodeHandle();
00029         std::cout << "Initialising nodelet ..." << std::endl;        
00030         // resolve node(let) name
00031         /*
00032         std::string name = nh_.getUnresolvedNamespace();
00033         int pos = name.find_last_of('/');
00034         name = name.substr(pos + 1);
00035         std::cout << "Initialising nodelet... [" << name << "]"<< std::endl;
00036         */
00037         // get the parameters
00038         if (!(nh_.getParam("upper_topic", upper_topic_)))
00039         {
00040             std::cout << "Fail to get the parameter upper_topic." << std::endl;
00041             return;
00042         }
00043         if (!(nh_.getParam("lower_topic", lower_topic_)))
00044         {
00045             std::cout << "Fail to get the parameter lower_topic" << std::endl;
00046             return;
00047         }
00048         if (!(nh_.getParam("output_topic",output_topic_)))
00049         {
00050             std::cout << "Fail to get the parameter output_topic" << std::endl;
00051             return;
00052         }
00053         nh_.param<double>("time_duration", time_duration_, 1.0);
00054         upper_msg_sub_ = nh_.subscribe(upper_topic_, 10, &Suppressor::upperCB, this);
00055         lower_msg_sub_ = nh_.subscribe(lower_topic_, 10, &Suppressor::lowerCB, this);
00056         pub_ = nh_.advertise<MsgType>(output_topic_, 10);//the typename MsgType represents the type of the suppressed messages
00057         
00058     }
00059     //void upperCB(boost::shared_ptr<MsgType const>& msg);
00060     void upperCB(const boost::shared_ptr<const MsgType>& msg)
00061     {
00062         begin_ = ros::Time::now().toSec();
00063         //pub_ = nh_.advertise<MsgType>(output_topic_, 1000);
00064         pub_.publish(msg);
00065         std::cout << "The upper layer messages output successfully." << std::endl;
00066     };
00067 
00068     //void lowerCB(boost::shared_ptr<MsgType const>& msg);  
00069     void lowerCB(const boost::shared_ptr<const MsgType>& msg)
00070     {
00071         end_ = ros::Time::now().toSec();
00072         if(end_-begin_ > time_duration_)
00073         {
00074             //pub_ = nh_.advertise<MsgType>(output_topic_, 1000);
00075             pub_.publish(msg);
00076             std::cout << "the lower layer messages output successfully." << std::endl;
00077         }
00078     };  
00079     
00080 private:
00081     ros::NodeHandle nh_;
00082     std::string upper_topic_, lower_topic_, output_topic_;
00083     ros::Subscriber upper_msg_sub_, lower_msg_sub_;
00084     ros::Publisher pub_;
00085     double begin_;//to calculate the time duration
00086     double end_;//to calculate the time duration
00087     double time_duration_;
00088 
00089 };
00090 
00091 }//namespace micros_mars_task_alloc
00092 #endif


micros_mars_task_alloc
Author(s): Minglong Li , Xiaodong Yi , Yanzhen Wang , Zhongxuan Cai
autogenerated on Mon Jul 1 2019 19:55:03