functions.cpp
Go to the documentation of this file.
1 #include "functions.h"
2 
3 
4 // rdm class, for gentaring random flot numbers
5 rdm::rdm() {i=time(0);}
6 float rdm::randomize() { i=i+1; srand (i); return float(rand())/float(RAND_MAX);}
7 
8 
9 
10 //Norm function
11 float Norm(std::vector<float> x1,std::vector<float> x2)
12 {
13 return pow( (pow((x2[0]-x1[0]),2)+pow((x2[1]-x1[1]),2)) ,0.5);
14 }
15 
16 
17 //sign function
18 float sign(float n)
19 {
20 if (n<0.0){return -1.0;}
21 else{return 1.0;}
22 }
23 
24 
25 //Nearest function
26 std::vector<float> Nearest( std::vector< std::vector<float> > V, std::vector<float> x){
27 
28 float min=Norm(V[0],x);
29 int min_index;
30 float temp;
31 
32 for (int j=0;j<V.size();j++)
33 {
34 temp=Norm(V[j],x);
35 if (temp<=min){
36 min=temp;
37 min_index=j;}
38 
39 }
40 
41 return V[min_index];
42 }
43 
44 
45 
46 //Steer function
47 std::vector<float> Steer( std::vector<float> x_nearest , std::vector<float> x_rand, float eta){
48 std::vector<float> x_new;
49 
50 if (Norm(x_nearest,x_rand)<=eta){
51 x_new=x_rand;
52 }
53 else{
54 
55 
56 float m=(x_rand[1]-x_nearest[1])/(x_rand[0]-x_nearest[0]);
57 
58 x_new.push_back( (sign(x_rand[0]-x_nearest[0]))* ( sqrt( (pow(eta,2)) / ((pow(m,2))+1) ) )+x_nearest[0] );
59 x_new.push_back( m*(x_new[0]-x_nearest[0])+x_nearest[1] );
60 
61 if(x_rand[0]==x_nearest[0]){
62 x_new[0]=x_nearest[0];
63 x_new[1]=x_nearest[1]+eta;
64 }
65 
66 
67 
68 }
69 return x_new;
70 }
71 
72 
73 
74 
75 
76 //gridValue function
77 int gridValue(nav_msgs::OccupancyGrid &mapData,std::vector<float> Xp){
78 
79 float resolution=mapData.info.resolution;
80 float Xstartx=mapData.info.origin.position.x;
81 float Xstarty=mapData.info.origin.position.y;
82 
83 float width=mapData.info.width;
84 std::vector<signed char> Data=mapData.data;
85 
86 //returns grid value at "Xp" location
87 //map data: 100 occupied -1 unknown 0 free
88 float indx=( floor((Xp[1]-Xstarty)/resolution)*width)+( floor((Xp[0]-Xstartx)/resolution) );
89 int out;
90 out=Data[int(indx)];
91 return out;
92 }
93 
94 
95 
96 
97 // ObstacleFree function-------------------------------------
98 
99 char ObstacleFree(std::vector<float> xnear, std::vector<float> &xnew, nav_msgs::OccupancyGrid mapsub){
100 float rez=float(mapsub.info.resolution)*.2;
101 float stepz=int(ceil(Norm(xnew,xnear))/rez);
102 std::vector<float> xi=xnear;
103 char obs=0; char unk=0;
104 
105 geometry_msgs::Point p;
106 for (int c=0;c<stepz;c++){
107  xi=Steer(xi,xnew,rez);
108 
109 
110  if (gridValue(mapsub,xi) ==100){ obs=1; }
111 
112  if (gridValue(mapsub,xi) ==-1){ unk=1; break;}
113  }
114 char out=0;
115  xnew=xi;
116  if (unk==1){ out=-1;}
117 
118  if (obs==1){ out=0;}
119 
120  if (obs!=1 && unk!=1){ out=1;}
121 
122 
123 
124 
125  return out;
126 
127 
128  }
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
std::vector< float > Steer(std::vector< float > x_nearest, std::vector< float > x_rand, float eta)
Definition: functions.cpp:47
int i
Definition: functions.h:14
std::vector< float > Nearest(std::vector< std::vector< float > > V, std::vector< float > x)
Definition: functions.cpp:26
float Norm(std::vector< float > x1, std::vector< float > x2)
Definition: functions.cpp:11
int gridValue(nav_msgs::OccupancyGrid &mapData, std::vector< float > Xp)
Definition: functions.cpp:77
float Xstartx
char ObstacleFree(std::vector< float > xnear, std::vector< float > &xnew, nav_msgs::OccupancyGrid mapsub)
Definition: functions.cpp:99
float randomize()
Definition: functions.cpp:6
int min(int a, int b)
float Xstarty
float resolution
float sign(float n)
Definition: functions.cpp:18
rdm()
Definition: functions.cpp:5


rrt_exploration
Author(s): Hassan Umari
autogenerated on Mon Jun 10 2019 14:57:44