look.cpp
Go to the documentation of this file.
00001 /*************************************************************************
00002         > File Name: pathplan.cpp
00003         > Author: Minglong Li
00004         > Mail: minglong_l@163.com
00005         > Created on: July 18th, 2016
00006  ************************************************************************/
00007 
00008 #include <iostream>
00009 #include <ros/ros.h>
00010 #include <nodelet/nodelet.h>
00011 
00012 #include <limits> 
00013 #include <std_msgs/Bool.h>
00014 #include <sensor_msgs/LaserScan.h>
00015 #include <micros_mars_task_alloc/Path.h>
00016 #include <math.h>
00017 #include <vector>
00018 
00019 #include <pluginlib/class_list_macros.h>
00020 
00021 namespace micros_mars_task_alloc{
00022     using namespace std;
00023     class Look : public nodelet::Nodelet
00024     {
00025     public:
00026         Look():max_range_(8.0), start_look_(false), travel_max_distance_(10.0), pi_(std::acos(-1)){}
00027         ~Look(){}
00028 
00029         virtual void onInit();
00030         
00031         void callback_0(const sensor_msgs::LaserScanConstPtr & msg);
00032         void callback_1(const std_msgs::BoolConstPtr & msg);
00033 
00034     private:    
00035         ros::NodeHandle nh_;
00036         ros::Subscriber sub_0_; 
00037         ros::Subscriber sub_1_;
00038         ros::Publisher pub_;
00039         
00040         float max_range_;
00041         bool start_look_;
00042         float travel_max_distance_;
00043         float pi_;
00044     };
00045 
00046     void Look::onInit()
00047     {
00048         nh_ = getPrivateNodeHandle();
00049         sub_0_ = nh_.subscribe("/robot0/laser_12", 10, &Look::callback_0, this);//This is an absolute topic.
00050         sub_1_ = nh_.subscribe("/whenlook/startlook", 10, &Look::callback_1, this);//This is an absolute topic.
00051         pub_ = nh_.advertise<micros_mars_task_alloc::Path>("path", 10);//This is a relative topic, the prefix look will be added automaically.
00052     }
00053 
00054     void Look::callback_0(const sensor_msgs::LaserScanConstPtr & msg)
00055     {
00056         if(start_look_)
00057         {
00058             int i =0;
00059             float turn_angle = 0.0;
00060             float forward_magnitude = 0.0;
00061             float max_distance = 0.0;
00062             int len = msg->ranges.size();//ranges is a vector, get the lenth of ranges
00063             float ranges[len];
00064             for(i=0; i<len; i++)
00065             {
00066                 ranges[i] = msg->ranges[i];
00067             }
00068             for(i=0; i<len; i++)
00069             {
00070                 if (ranges[i] == std::numeric_limits<float>::infinity())
00071                     ranges[i] = max_range_;
00072             }
00073             //get the max value of ranges
00074             max_distance = ranges[0];
00075             for(i = 1; i < len; i++)
00076             {
00077                 if (ranges[i] > max_distance)
00078                     max_distance = ranges[i];
00079             } 
00080             
00081             //choose a direction
00082             int count_a = 0;
00083             int count_b = 0;
00084             int count_c = 0;
00085             int count_d = 0;
00086             int angle_index = 0;
00087             
00088             for(i=0; i<len; i++)
00089             {
00090                 if(ranges[i] == max_distance)
00091                 {
00092                     count_a = i;
00093                     break;
00094                 }
00095             }
00096             for(i=0; i<len; i++)
00097             {
00098                 if((ranges[i] == max_distance) && (ranges[i+1] != max_distance))
00099                 {
00100                     count_b = i;
00101                     break;
00102                 }
00103             }
00104             for(i=count_b+1; i<len; i++)
00105             {
00106                 if(ranges[i] == max_distance)
00107                 {
00108                     count_c = i;
00109                     break;
00110                 }
00111             }
00112             for(i=count_b+1; i<len; i++)
00113             {
00114                 if((ranges[i] == max_distance) && (ranges[i+1] != max_distance))
00115                 {
00116                     count_d = i;
00117                     break;
00118                 }
00119             }
00120             
00121             //calculate the turn angle
00122             if (abs(count_b - count_a) > abs(count_c - count_d))
00123             {
00124                 angle_index = (count_a + count_b)/2;
00125                 if((angle_index == 0) || ((angle_index > 0) && (angle_index < 135)) )
00126                     turn_angle = float((225 + angle_index)) * float((pi_ / 180.0));
00127                 else
00128                     turn_angle = float(angle_index - 135) * float(pi_ / 180.0);
00129             }   
00130             else
00131             {
00132                 angle_index = (count_c + count_d) / 2;
00133                 if ((angle_index == 0) || ((angle_index > 0) && (angle_index < 135)))
00134                     turn_angle = float(225 + angle_index) * float(pi_ / 180);
00135                 else
00136                     turn_angle = (angle_index - 135) * (pi_ / 180);
00137              }
00138              //publish the path message
00139              micros_mars_task_alloc::PathPtr path_ptr(new micros_mars_task_alloc::Path);
00140              path_ptr -> path_angle = turn_angle;
00141              path_ptr -> path_distance = max_distance;
00142              pub_.publish(path_ptr);
00143              cout << "Look module is working." << endl; 
00144              start_look_ = false;
00145         }
00146         else
00147             cout << "Look module is waiting for the trigger message." << endl;
00148     }
00149 
00150     void Look::callback_1(const std_msgs::BoolConstPtr & msg)
00151     {
00152         start_look_ = msg->data; 
00153         /*
00154         if(start_look_)
00155         {
00156             ros::Duration(0.5).sleep();
00157             start_look_ = false;
00158         }
00159         */
00160     }
00161 }//namespace micros_mars_task_alloc
00162 PLUGINLIB_EXPORT_CLASS(micros_mars_task_alloc::Look, 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