functions.cpp
Go to the documentation of this file.
00001 #include "functions.h"
00002 
00003 
00004 // rdm class, for gentaring random flot numbers
00005 rdm::rdm() {i=time(0);}
00006 float rdm::randomize() { i=i+1;  srand (i);  return float(rand())/float(RAND_MAX);}
00007 
00008 
00009 
00010 //Norm function 
00011 float Norm(std::vector<float> x1,std::vector<float> x2)
00012 {
00013 return pow(     (pow((x2[0]-x1[0]),2)+pow((x2[1]-x1[1]),2))     ,0.5);
00014 }
00015 
00016 
00017 //sign function
00018 float sign(float n)
00019 {
00020 if (n<0.0){return -1.0;}
00021 else{return 1.0;}
00022 }
00023 
00024 
00025 //Nearest function
00026 std::vector<float> Nearest(  std::vector< std::vector<float>  > V, std::vector<float>  x){
00027 
00028 float min=Norm(V[0],x);
00029 int min_index;
00030 float temp;
00031 
00032 for (int j=0;j<V.size();j++)
00033 {
00034 temp=Norm(V[j],x);
00035 if (temp<=min){
00036 min=temp;
00037 min_index=j;}
00038 
00039 }
00040 
00041 return V[min_index];
00042 }
00043 
00044 
00045 
00046 //Steer function
00047 std::vector<float> Steer(  std::vector<float> x_nearest , std::vector<float> x_rand, float eta){
00048 std::vector<float> x_new;
00049 
00050 if (Norm(x_nearest,x_rand)<=eta){
00051 x_new=x_rand;
00052 }
00053 else{
00054 
00055 
00056 float m=(x_rand[1]-x_nearest[1])/(x_rand[0]-x_nearest[0]);
00057 
00058 x_new.push_back(  (sign(x_rand[0]-x_nearest[0]))* (   sqrt( (pow(eta,2)) / ((pow(m,2))+1) )   )+x_nearest[0] );
00059 x_new.push_back(  m*(x_new[0]-x_nearest[0])+x_nearest[1] );
00060 
00061 if(x_rand[0]==x_nearest[0]){
00062 x_new[0]=x_nearest[0];
00063 x_new[1]=x_nearest[1]+eta;
00064 }
00065 
00066 
00067 
00068 }
00069 return x_new;
00070 }
00071 
00072 
00073 
00074 
00075 
00076 //gridValue function
00077 int gridValue(nav_msgs::OccupancyGrid &mapData,std::vector<float> Xp){
00078 
00079 float resolution=mapData.info.resolution;
00080 float Xstartx=mapData.info.origin.position.x;
00081 float Xstarty=mapData.info.origin.position.y;
00082 
00083 float width=mapData.info.width;
00084 std::vector<signed char> Data=mapData.data;
00085 
00086 //returns grid value at "Xp" location
00087 //map data:  100 occupied      -1 unknown       0 free
00088 float indx=(  floor((Xp[1]-Xstarty)/resolution)*width)+( floor((Xp[0]-Xstartx)/resolution) );
00089 int out;
00090 out=Data[int(indx)];
00091 return out;
00092 }
00093 
00094 
00095 
00096 
00097 // ObstacleFree function-------------------------------------
00098 
00099 char ObstacleFree(std::vector<float> xnear, std::vector<float> &xnew, nav_msgs::OccupancyGrid mapsub){
00100 float rez=float(mapsub.info.resolution)*.2;
00101 float stepz=int(ceil(Norm(xnew,xnear))/rez); 
00102 std::vector<float> xi=xnear;
00103 char  obs=0; char unk=0;
00104  
00105 geometry_msgs::Point p;
00106 for (int c=0;c<stepz;c++){
00107   xi=Steer(xi,xnew,rez);
00108                 
00109 
00110    if (gridValue(mapsub,xi) ==100){     obs=1; }
00111    
00112    if (gridValue(mapsub,xi) ==-1){      unk=1;  break;}
00113   }
00114 char out=0;
00115  xnew=xi;
00116  if (unk==1){  out=-1;}
00117         
00118  if (obs==1){  out=0;}
00119                 
00120  if (obs!=1 && unk!=1){   out=1;}
00121 
00122  
00123  
00124  
00125  return out;
00126  
00127 
00128  }
00129  
00130 
00131 
00132      
00133    
00134 
00135 
00136   
00137  
00138  
00139  
00140  
00141 
00142 
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155 
00156 
00157 
00158 
00159 
00160 
00161 
00162 
00163 
00164 
00165 
00166 
00167 
00168 
00169 


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