collide.cpp
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: July 16th, 2016
00007 */
00008 #include <ros/ros.h>
00009 #include <nodelet/nodelet.h>
00010 #include <iostream>
00011 #include <pluginlib/class_list_macros.h>
00012 #include <limits> 
00013 
00014 #include <message_filters/subscriber.h>
00015 #include <message_filters/synchronizer.h>
00016 #include <message_filters/sync_policies/approximate_time.h>
00017 
00018 #include <sensor_msgs/Range.h>
00019 #include <std_msgs/Bool.h>
00020 
00021 namespace micros_mars_task_alloc {
00022 using namespace sensor_msgs;
00023 using namespace message_filters;
00024 using namespace std;
00025 
00026 class Collide : public nodelet::Nodelet
00027 {
00028 public:
00029     Collide(): halt_flag_(false), n_inf_(-std::numeric_limits<float>::infinity()), maximum_queue_size_(10){}
00030     ~Collide(){}
00031     virtual void onInit();
00032     void callback(const RangeConstPtr& msg_0, const RangeConstPtr& msg_1, const RangeConstPtr& msg_2, const RangeConstPtr& msg_10, const RangeConstPtr& msg_11);
00033                                                   
00034 private:
00035     //every force has a range and an angle.
00036     bool halt_flag_;
00037     float n_inf_;
00038     int maximum_queue_size_;//control the queue size of the message filters
00039     typedef sync_policies::ApproximateTime<Range, Range, Range, Range, Range> ApproximatePolicy;
00040     typedef message_filters::Synchronizer<ApproximatePolicy> ApproximateSync;
00041     
00042     //Because message_filters support 9 topics at most, use two filters and two callbacks to handle the sonar messages.
00043     boost::shared_ptr<ApproximateSync> approximate_sync_;    
00044 
00045     message_filters::Subscriber<Range> sub_0_, sub_1_, sub_2_, sub_10_, sub_11_;
00046     ros::NodeHandle nh_;
00047     ros::Publisher pub_;
00048 
00049 };
00050 
00051 void Collide::onInit()
00052 {
00053     nh_ = getPrivateNodeHandle();
00054     cout << "Initialising nodelet ..." << endl;        
00055 
00056     //The topics should be absolute.
00057     sub_0_.subscribe(nh_, "/robot0/sonar_0", 1);
00058     sub_1_.subscribe(nh_, "/robot0/sonar_1", 1);
00059     sub_2_.subscribe(nh_, "/robot0/sonar_2", 1);
00060     sub_10_.subscribe(nh_, "/robot0/sonar_10", 1);
00061     sub_11_.subscribe(nh_, "/robot0/sonar_11", 1);   
00062     
00063     pub_ = nh_.advertise<std_msgs::Bool>("halt", 10);
00064     
00065     approximate_sync_.reset( new ApproximateSync(ApproximatePolicy(maximum_queue_size_), sub_0_, sub_1_, sub_2_, sub_10_, sub_11_) );
00066     approximate_sync_->registerCallback(boost::bind(&Collide::callback, this, _1, _2, _3, _4, _5));
00067 }
00068 
00069 void Collide::callback(const RangeConstPtr& msg_0, const RangeConstPtr& msg_1, const RangeConstPtr& msg_2, const RangeConstPtr& msg_10, const RangeConstPtr& msg_11)
00070 {
00071     
00072     if ((msg_0->range!=n_inf_)&&(msg_1->range!=n_inf_)&&(msg_2->range!=n_inf_)&&(msg_10->range!=n_inf_)&&(msg_11->range!=n_inf_))
00073         halt_flag_ = false;
00074     else
00075         halt_flag_ = true;
00076     
00077     std_msgs::BoolPtr bool_ptr(new std_msgs::Bool);
00078     bool_ptr->data = halt_flag_;
00079     pub_.publish(bool_ptr);
00080 }
00081 }//namespace micros_mars_task_alloc
00082 PLUGINLIB_EXPORT_CLASS(micros_mars_task_alloc::Collide, nodelet::Nodelet)


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