local_rrt_detector.cpp
Go to the documentation of this file.
00001 #include "ros/ros.h"
00002 #include "std_msgs/String.h"
00003 #include <sstream>
00004 #include <iostream>
00005 #include <string>
00006 #include <vector>
00007 #include "stdint.h"
00008 #include "functions.h"
00009 #include "mtrand.h"
00010 
00011 
00012 #include "nav_msgs/OccupancyGrid.h"
00013 #include "geometry_msgs/PointStamped.h"
00014 #include "std_msgs/Header.h"
00015 #include "nav_msgs/MapMetaData.h"
00016 #include "geometry_msgs/Point.h"
00017 #include "visualization_msgs/Marker.h"
00018 #include <tf/transform_listener.h>
00019 
00020 
00021 
00022 // global variables
00023 nav_msgs::OccupancyGrid mapData;
00024 geometry_msgs::PointStamped clickedpoint;
00025 geometry_msgs::PointStamped exploration_goal;
00026 visualization_msgs::Marker points,line;
00027 float xdim,ydim,resolution,Xstartx,Xstarty,init_map_x,init_map_y;
00028 
00029 rdm r; // for genrating random numbers
00030 
00031 
00032 
00033 //Subscribers callback functions---------------------------------------
00034 void mapCallBack(const nav_msgs::OccupancyGrid::ConstPtr& msg)
00035 {
00036 mapData=*msg;
00037 }
00038 
00039 
00040  
00041 void rvizCallBack(const geometry_msgs::PointStamped::ConstPtr& msg)
00042 { 
00043 
00044 geometry_msgs::Point p;  
00045 p.x=msg->point.x;
00046 p.y=msg->point.y;
00047 p.z=msg->point.z;
00048 
00049 points.points.push_back(p);
00050 
00051 }
00052 
00053 
00054 
00055 
00056 int main(int argc, char **argv)
00057 {
00058 
00059   unsigned long init[4] = {0x123, 0x234, 0x345, 0x456}, length = 7;
00060   MTRand_int32 irand(init, length); // 32-bit int generator
00061 // this is an example of initializing by an array
00062 // you may use MTRand(seed) with any 32bit integer
00063 // as a seed for a simpler initialization
00064   MTRand drand; // double in [0, 1) generator, already init
00065 
00066 // generate the same numbers as in the original C test program
00067   ros::init(argc, argv, "local_rrt_frontier_detector");
00068   ros::NodeHandle nh;
00069   
00070   // fetching all parameters
00071   float eta,init_map_x,init_map_y,range;
00072   std::string map_topic,base_frame_topic;
00073   
00074   std::string ns;
00075   ns=ros::this_node::getName();
00076 
00077   ros::param::param<float>(ns+"/eta", eta, 0.5);
00078   ros::param::param<std::string>(ns+"/map_topic", map_topic, "/robot_1/map"); 
00079   ros::param::param<std::string>(ns+"/robot_frame", base_frame_topic, "/robot_1/base_link"); 
00080 //---------------------------------------------------------------
00081 ros::Subscriber sub= nh.subscribe(map_topic, 100 ,mapCallBack); 
00082 ros::Subscriber rviz_sub= nh.subscribe("/clicked_point", 100 ,rvizCallBack);    
00083 
00084 ros::Publisher targetspub = nh.advertise<geometry_msgs::PointStamped>("/detected_points", 10);
00085 ros::Publisher pub = nh.advertise<visualization_msgs::Marker>(ns+"_shapes", 10);
00086 
00087 ros::Rate rate(100); 
00088  
00089  
00090 // wait until map is received, when a map is received, mapData.header.seq will not be < 1  
00091 while (mapData.header.seq<1 or mapData.data.size()<1)  {  ros::spinOnce();  ros::Duration(0.1).sleep();}
00092 
00093 
00094 
00095 //visualizations  points and lines..
00096 points.header.frame_id=mapData.header.frame_id;
00097 line.header.frame_id=mapData.header.frame_id;
00098 points.header.stamp=ros::Time(0);
00099 line.header.stamp=ros::Time(0);
00100         
00101 points.ns=line.ns = "markers";
00102 points.id = 0;
00103 line.id =1;
00104 
00105 
00106 points.type = points.POINTS;
00107 line.type=line.LINE_LIST;
00108 
00109 //Set the marker action.  Options are ADD, DELETE, and new in ROS Indigo: 3 (DELETEALL)
00110 points.action =points.ADD;
00111 line.action = line.ADD;
00112 points.pose.orientation.w =1.0;
00113 line.pose.orientation.w = 1.0;
00114 line.scale.x =  0.03;
00115 line.scale.y= 0.03;
00116 points.scale.x=0.3; 
00117 points.scale.y=0.3; 
00118 
00119 line.color.r =255.0/255.0;
00120 line.color.g= 0.0/255.0;
00121 line.color.b =0.0/255.0;
00122 points.color.r = 255.0/255.0;
00123 points.color.g = 0.0/255.0;
00124 points.color.b = 0.0/255.0;
00125 points.color.a=0.3;
00126 line.color.a = 1.0;
00127 points.lifetime = ros::Duration();
00128 line.lifetime = ros::Duration();
00129 
00130 geometry_msgs::Point p;  
00131 
00132 
00133 while(points.points.size()<5)
00134 {
00135 ros::spinOnce();
00136 
00137 pub.publish(points) ;
00138 }
00139 
00140 
00141 
00142 
00143 std::vector<float> temp1;
00144 temp1.push_back(points.points[0].x);
00145 temp1.push_back(points.points[0].y);
00146         
00147 std::vector<float> temp2; 
00148 temp2.push_back(points.points[2].x);
00149 temp2.push_back(points.points[0].y);
00150 
00151 
00152 init_map_x=Norm(temp1,temp2);
00153 temp1.clear();          temp2.clear();
00154 
00155 temp1.push_back(points.points[0].x);
00156 temp1.push_back(points.points[0].y);
00157 
00158 temp2.push_back(points.points[0].x);
00159 temp2.push_back(points.points[2].y);
00160 
00161 init_map_y=Norm(temp1,temp2);
00162 temp1.clear();          temp2.clear();
00163 
00164 Xstartx=(points.points[0].x+points.points[2].x)*.5;
00165 Xstarty=(points.points[0].y+points.points[2].y)*.5;
00166 
00167 
00168 
00169 
00170 
00171 geometry_msgs::Point trans;
00172 trans=points.points[4];
00173 std::vector< std::vector<float>  > V; 
00174 std::vector<float> xnew; 
00175 xnew.push_back( trans.x);xnew.push_back( trans.y);  
00176 V.push_back(xnew);
00177 
00178 points.points.clear();
00179 pub.publish(points) ;
00180 
00181 
00182 
00183 
00184 
00185 
00186 
00187 std::vector<float> frontiers;
00188 int i=0;
00189 float xr,yr;
00190 std::vector<float> x_rand,x_nearest,x_new;
00191 
00192 tf::TransformListener listener;
00193 // Main loop
00194 while (ros::ok()){
00195 
00196 
00197 // Sample free
00198 x_rand.clear();
00199 xr=(drand()*init_map_x)-(init_map_x*0.5)+Xstartx;
00200 yr=(drand()*init_map_y)-(init_map_y*0.5)+Xstarty;
00201 
00202 
00203 x_rand.push_back( xr ); x_rand.push_back( yr );
00204 
00205 
00206 // Nearest
00207 x_nearest=Nearest(V,x_rand);
00208 
00209 // Steer
00210 
00211 x_new=Steer(x_nearest,x_rand,eta);
00212 
00213 
00214 // ObstacleFree    1:free     -1:unkown (frontier region)      0:obstacle
00215 char   checking=ObstacleFree(x_nearest,x_new,mapData);
00216 
00217           if (checking==-1){
00218 
00219                         exploration_goal.header.stamp=ros::Time(0);
00220                 exploration_goal.header.frame_id=mapData.header.frame_id;
00221                 exploration_goal.point.x=x_new[0];
00222                 exploration_goal.point.y=x_new[1];
00223                 exploration_goal.point.z=0.0;
00224                 p.x=x_new[0]; 
00225                         p.y=x_new[1]; 
00226                         p.z=0.0;
00227                                         
00228                 points.points.push_back(p);
00229                 pub.publish(points) ;
00230                 targetspub.publish(exploration_goal);
00231                         points.points.clear();
00232                         V.clear();
00233                         
00234                         
00235                         tf::StampedTransform transform;
00236                         int  temp=0;
00237                         while (temp==0){
00238                         try{
00239                         temp=1;
00240                         listener.lookupTransform(map_topic, base_frame_topic , ros::Time(0), transform);
00241                         }
00242                         catch (tf::TransformException ex){
00243                         temp=0;
00244                         ros::Duration(0.1).sleep();
00245                         }}
00246                         
00247                         x_new[0]=transform.getOrigin().x();
00248                         x_new[1]=transform.getOrigin().y();
00249                 V.push_back(x_new);
00250                 line.points.clear();
00251                 }
00252                 
00253           
00254           else if (checking==1){
00255                 V.push_back(x_new);
00256                 
00257                 p.x=x_new[0]; 
00258                 p.y=x_new[1]; 
00259                 p.z=0.0;
00260                 line.points.push_back(p);
00261                 p.x=x_nearest[0]; 
00262                 p.y=x_nearest[1]; 
00263                 p.z=0.0;
00264                 line.points.push_back(p);
00265 
00266                 }
00267 
00268 
00269 
00270 pub.publish(line);  
00271 
00272 
00273    
00274 
00275 ros::spinOnce();
00276 rate.sleep();
00277   }return 0;}


rrt_exploration
Author(s): Hassan Umari
autogenerated on Thu Jun 6 2019 20:54:03