Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef FORWARDER_H_
00017 #define FORWARDER_H_
00018
00019 #include <ros/ros.h>
00020 #include <nodelet/nodelet.h>
00021 #include <iostream>
00022 #include <pluginlib/class_list_macros.h>
00023 #include <std_msgs/Bool.h>
00024 #include <actionlib_msgs/GoalID.h>
00025 #include <move_base_msgs/MoveBaseActionGoal.h>
00026
00027 namespace micros_mars_task_alloc {
00028
00029 using namespace std;
00030
00031 template<typename MsgType_0, typename MsgType_1 = std_msgs::Bool>
00032 class Forwarder : public nodelet::Nodelet
00033 {
00034 public:
00035 Forwarder():activating_flag_(false){}
00036 virtual ~Forwarder(){}
00037 virtual void onInit()
00038 {
00039 nh_ = getMTPrivateNodeHandle();
00040 nh_.param<std::string>("activating_topic", activating_topic_, "activating_topic");
00041 nh_.param<std::string>("input_topic_0", input_topic_0_, "input_topic_0");
00042 nh_.param<std::string>("input_topic_1", input_topic_1_, "input_topic_1");
00043 nh_.param<std::string>("output_topic_0", output_topic_0_, "output_topic_0");
00044 nh_.param<std::string>("output_topic_1", output_topic_1_, "output_topic_1");
00045 nh_.param<bool>("action_mode", action_mode_, false);
00046 nh_.param<std::string>("sub_action_name", sub_action_name_, "sub_action_name");
00047 nh_.param<std::string>("pub_action_name", pub_action_name_, "pub_action_name");
00048
00049 sub_0_ = nh_.subscribe(input_topic_0_, 10, &Forwarder::callback_0, this);
00050 sub_1_ = nh_.subscribe(input_topic_1_, 10, &Forwarder::callback_1, this);
00051 sub_2_ = nh_.subscribe(activating_topic_, 10, &Forwarder::activating_callback, this);
00052 sub_action_cancel_ = nh_.subscribe(sub_action_name_ + "/goal", 10, &Forwarder::goal_callback, this);
00053
00054 pub_0_ = nh_.advertise<MsgType_0>(output_topic_0_, 10);
00055 pub_1_ = nh_.advertise<MsgType_1>(output_topic_1_, 10);
00056 pub_cancel_ = nh_.advertise<actionlib_msgs::GoalID>(pub_action_name_ +"/cancel", 10);
00057 }
00058
00059 void callback_0(const boost::shared_ptr<const MsgType_0>& msg)
00060 {
00061 cout << "callback_0 start!" << endl;
00062 if (activating_flag_ == true)
00063 {
00064 pub_0_.publish(msg);
00065 }
00066 }
00067
00068 void callback_1(const boost::shared_ptr<const MsgType_1>& msg)
00069 {
00070 cout << "callback_1 start!" << endl;
00071 activating_flag_ = msg -> data;
00072 if (activating_flag_ == true)
00073 {
00074 pub_1_.publish(msg);
00075 }
00076 }
00077
00078 void activating_callback(const std_msgs::BoolConstPtr & msg)
00079 {
00080 cout << "activating_callback start!" << endl;
00081 activating_flag_ = msg -> data;
00082 if (msg -> data == false)
00083 {
00084 std::cout << "This forwarder is off" << std::endl;
00085 if(action_mode_)
00086 {
00087 pub_cancel_.publish(last_goal_);
00088 std::cout << "cancel goal" << std::endl;
00089 }
00090 }
00091 }
00092
00093 void goal_callback(const move_base_msgs::MoveBaseActionGoal & msg)
00094 {
00095 std::cout << "goal received" << std::endl;
00096 last_goal_.stamp.sec = msg.goal_id.stamp.sec;
00097 last_goal_.stamp.nsec = msg.goal_id.stamp.nsec;
00098 last_goal_.id = msg.goal_id.id;
00099 }
00100
00101 private:
00102 std::string activating_topic_;
00103 std::string input_topic_0_;
00104 std::string input_topic_1_;
00105 std::string output_topic_0_;
00106 std::string output_topic_1_;
00107 bool action_mode_;
00108 bool activating_flag_;
00109 std::string sub_action_name_;
00110 std::string pub_action_name_;
00111
00112 ros::NodeHandle nh_;
00113 ros::Subscriber sub_0_, sub_1_, sub_2_, sub_action_cancel_;
00114 ros::Publisher pub_0_, pub_1_, pub_cancel_;
00115 actionlib_msgs::GoalID last_goal_;
00116 };
00117 }
00118 #endif