-
Notifications
You must be signed in to change notification settings - Fork 0
/
rrt_cv.cpp
402 lines (373 loc) · 13.5 KB
/
rrt_cv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <iostream>
#include "include/rrt_cv.hpp"
using namespace std;
#define thrhold 5
#define distance_object_to_line 0.1
vector<RRT::Vertex*> nodeList;
int main()
{
unsigned int startX, startY, goalX, goalY;
RRT Planner;
while(true)
{
printf("Input startX startY \n");
scanf("%d %d",&startX,&startY);
printf("Input goalX goalY \n");
scanf("%d %d",&goalX,&goalY);
Planner.configure(startX,startY,goalX,goalY);
Planner.createPlan();
}
return 0;
}
double RRT::randSample(int size)
{
std::random_device random_device;
std::mt19937 engine(random_device());
std::uniform_int_distribution<int> distribution(0.0, size);
return distribution(engine);
}
bool RRT::Sampling(Vertex* sample, unsigned int randX, unsigned int randY)
{
//Node *sample = new Node;// Random Sample
//Node *tmpNode;
double dist = 0.0;
double angle =0.0;
double mindist = 1000.0;
int i = -1;
int index = 0;
sample->x = randX;//randSample(width);
sample->y = randY;//randSample(height);
//cout <<"sample:" << sample->x << ", " << sample->y << endl;
bool flagOfValidSample = false;
for (auto a : nodeList)
{
i++;
unsigned int nodeSampleX = a->x;
unsigned int nodeSampleY = a->y;
dist = hypot((double)nodeSampleX - (double)randX,(double)nodeSampleY- (double)randY); // Calc dist to find minimum Qnear to Qrand
angle = atan2f(((int)randY- (int)nodeSampleY), ((int)randX - (int)nodeSampleX)); //x cosangle y sin angle
//cout <<"angle:" << angle <<"dist: "<< dist << endl;
//cout << "diff (" << randX - nodeSampleX << " ," << randY - nodeSampleY << ")"<< endl;
if (dist <= mindist && dist >= sampleDistThr/resolution_)
{
mindist = dist;
index = i;
flagOfValidSample = true;
}
else if(dist > mindist || dist < sampleDistThr/resolution_)
{
continue;
}
}
if(!flagOfValidSample)
{
free(sample);
return false;
}
//Node *nNode = new Node;// New node between Qnear and Qrand
// nNode -> x = nodeList[index]->x + jump*cosf(angle);
// nNode -> y = nodeList[index]->y + jump*sinf(angle);
// nNode -> parent = nodeList[index];
///////////generation New Node//////////////////////////
unsigned int newSampleX = (unsigned int)((double)nodeList[index]->x + jump_ * cosf(angle));
unsigned int newSampleY = (unsigned int)((double)nodeList[index]->y + jump_ * sinf(angle));
// if(seen_[newSampleX][newSampleY])
// {
// cout <<"Seen!" << newSampleX << ", " << newSampleY << endl;
// free(sample);
// return false;
// }
sample->x = newSampleX;
sample->y = newSampleY;
sample->parent = nodeList[index];
//cout <<"index:" << index << ", " << nodeList[index]->x << ", "<< nodeList[index]->x<< endl;
return true;
}
bool RRT::checkFootPrint(int sampleX, int sampleY)
{
int footPrintX = std::abs((int)startX_ - sampleX);
int footPrintY = std::abs((int)startY_ - sampleY);
if(footPrintX <=2 && footPrintY <=2 )
return true;
else
return false;
}
bool RRT::checkValidSample(Vertex* sample)
{
Vertex* parent = sample->parent;
double lineGrad;
unsigned int tmpX, tmpY;
int sampleX = (int)sample->x;
int sampleY = (int)sample->y;
int parentX = (int)parent->x;
int parentY = (int)parent->y;
int diffX = (int)((sampleX - parentX));
int diffY = (int)((sampleY - parentY));
if (diffX != 0)
lineGrad = ((double)diffY / (double)diffX);
else
lineGrad = 0.0;
//interceptY = round(parentY - lineGrad * parentX);
if (sampleX >= (int)sizeX_ || sampleX <= 0)
{
free(sample);
return false;
}
else if (sampleY >= (int)sizeY_ || sampleY <= 0)
{
free(sample);
return false;
}
//unsigned int index2 = costmap_->getIndex(sample->x,sample->y);
//unsigned int getCost2 = costmap_->getCost(index2);
unsigned char getCost2 = map.at<uchar>(sample->y,sample->x);
//cout << " Check 1" << endl;
if(getCost2 > collisionThr && !checkFootPrint(sample->x,sample->y))
{
//cout << " why2" << endl;
free(sample);
//cout << " why3" << endl;
return false;
}
if (diffX == 0)
{
//cout << " Check 2" << endl;
//cout<<"objectX:" << a.first << ", objectY:" << a.second << endl;
//cout<<"sampleX:" << sample->x << ", sampleY:" << sample->y << endl;
//cout<<"parentX:" << parent->x << ", parentY:" << parent->y << endl;
//cout<<"grad:" << lineGrad << ", intercept:" << interceptY << endl;
if (parentY < sampleY)
{
for (int y1 = parentY; y1 < (sampleY); y1++)
{
tmpX = parentX;
tmpY = y1;
//unsigned int index = costmap_->getIndex(tmpX,tmpY);
//unsigned int getCost = costmap_->getCost(index);
unsigned char getCost = map.at<uchar>(tmpY,tmpX);
if(getCost > collisionThr)
{
free(sample);
return false;
}
}
}
else
{
for ( int y1 = parentY; y1 > (sampleY); y1--)
{
tmpX = parentX;
tmpY = y1;
//unsigned int index = costmap_->getIndex(tmpX,tmpY);
unsigned char getCost = map.at<uchar>(tmpY,tmpX);
if(getCost > collisionThr)
{
free(sample);
return false;
}
}
}
}
else
{
//cout << " Check 3" << endl;
if (sampleX > parentX && sampleY >= parentY) // 1
{
for ( int x1 = 0; x1 < diffX; x1++)
{
tmpX = (unsigned int)(parentX+x1);
tmpY = (unsigned int)(lineGrad * (double)x1)+(unsigned int)parentY;
//std::cout << lineGrad <<" **"<< x1 << "+"<<parentY <<"="<< tmpY << std::endl;
//unsigned int index = costmap_->getIndex(tmpX,tmpY);
unsigned char getCost = map.at<uchar>(tmpY,tmpX);
if(getCost > collisionThr && !checkFootPrint(tmpX,tmpY))
{
//std::cout << " Check 3-1" << std::endl;
std::cout << tmpX << ", " << tmpY << ", " << getCost << std::endl;
std::cout <<lineGrad<<","<< parentX << ", " << sampleX << ", " << parentY << ", " << sampleY << std::endl;
free(sample);
return false;
}
}
}
else if (sampleX < parentX && sampleY > parentY) // 2
{
for ( int x1 = diffX; x1 < 0; x1++)
{
tmpX = (unsigned int)(parentX+x1);
tmpY = (unsigned int)(lineGrad * (double)x1) + (unsigned int)parentY;
//std::cout << lineGrad <<" **"<<(double)(x1/5.0) << "+"<<parentY <<"="<< tmpY << std::endl;
//unsigned int index = costmap_->getIndex(tmpX,tmpY);
unsigned char getCost = map.at<uchar>(tmpY,tmpX);
if(getCost > collisionThr && !checkFootPrint(tmpX,tmpY))
{
//cout << " Check 3-2" << endl;
std::cout << tmpX << ", " << tmpY << ", " << getCost << std::endl;
std::cout <<lineGrad<<","<< parentX << ", " << sampleX << ", " << parentY << ", " << sampleY << std::endl;
free(sample);
return false;
}
}
}
else if (sampleX > parentX && sampleY < parentY) // 3
{
for ( int x1 = 0; x1 < diffX; x1++)
{
tmpX = (unsigned int)(parentX+x1);
tmpY = (unsigned int)(lineGrad * (double)x1) + (unsigned int)parentY;
//std::cout << lineGrad <<" **"<<x1 << "+"<<parentY <<"="<< tmpY << std::endl;
//unsigned int index = costmap_->getIndex(tmpX,tmpY);
unsigned char getCost = map.at<uchar>(tmpY,tmpX);
if(getCost > collisionThr && !checkFootPrint(tmpX,tmpY))
{
//cout << " Check 3-3" << endl;
std::cout << tmpX << ", " << tmpY << ", " << getCost << std::endl;
std::cout <<lineGrad<<","<< parentX << ", " << sampleX << ", " << parentY << ", " << sampleY << std::endl;
free(sample);
return false;
}
}
}
else if(sampleX < parentX && sampleY < parentY)// 4
{
for ( int x1 = diffX; x1 < 0; x1++)
{
tmpX = (unsigned int)(parentX+x1);
tmpY = (unsigned int)(lineGrad * (double)x1) + (unsigned int)parentY;
//std::cout << lineGrad <<" **"<<x1 << "+"<<parentY <<"="<< tmpY << std::endl;
//unsigned int index = costmap_->getIndex(tmpX,tmpY);
//std::cout << tmpX <<", "<< tmpY<< "getIndex: "<< index << std::endl;
unsigned char getCost = map.at<uchar>(tmpY,tmpX);
//std::cout << "getCosts"<< std::endl;
if(getCost > collisionThr && !checkFootPrint(tmpX,tmpY))
{
//cout << " Check 3-4" << endl;
std::cout << tmpX << ", " << tmpY << ", " << getCost << std::endl;
std::cout <<lineGrad<<","<< parentX << ", " << sampleX << ", " << parentY << ", " << sampleY << std::endl;
free(sample);
return false;
}
}
}
}
//cout << " Check complete" << endl;
return true;
}
void RRT::conNode(Vertex* nNode)
{
double mindist = 1000.0;
double tempDist = 1000.0;
Vertex* tmpNode = nNode;
//새롭게 연결된 노드
for (auto a : nodeList)
{
tempDist = hypot((double)a->x - (double)tmpNode->x,(double)a->y - (double)tmpNode->y);
//cout << "dist.." << tempDist << ","<< mindist << endl;
//cout << "dist2.." << tempDist << ","<< mindist << endl;
if (tempDist < thrhold)
return;
if (tempDist < mindist)
{
tmpNode->parent = nullptr;
//cout << "dist2.." << tempDist << ","<< mindist << endl;
mindist = tempDist;
tmpNode->parent = a;
//cout << "nNode node.." << nNode->x << ", " << nNode->y << endl;
//cout << "parent node.." << a->x << ", " << a->y << endl;
}
}
//cout << "dist2: " << tempDist << ","<< mindist << endl;
//cout << "push node.." << endl;
//cout << "push node2.." << nNode->x << ", " << nNode->y << endl;
}
void RRT::InitVertex()
{
for (auto a : nodeList)
{
Vertex* remove = a;
free(remove);
}
nodeList = vector<Vertex*>();
}
void RRT::configure(unsigned int startX, unsigned int startY, unsigned int goalX, unsigned int goalY)
{
sizeX_ = 300;
sizeY_= 300;
resolution_ = 0.05;
jump_ = 3;
startX_ = startX;
startY_ = startY;
goalX_ = goalX;
goalY_ = goalY;
sampleDistThr = 0.1;
nodeSize = 0;
goalTolerance = 10;
collisionThr = 1;
map = cv::Mat::zeros(sizeY_,sizeX_,CV_8UC1);
printf("Set Map Size(%d, %d)\n",sizeX_,sizeY_);
printf("Set start point(%d, %d) and goal point(%d, %d)\n",startX_,startY_,goalX_,goalY_);
}
void RRT::adjustGoal(double & goalThr)
{
if(nodeSize < nodeList.size())
{
goalThr = goalThr + 5;
}
nodeSize = nodeList.size();
}
void RRT::createPlan()
{
double tempDist;
bool result;
//std::vector<nav_msgs::msg::Path> samplePaths;
std::mt19937 engine(random_device());
std::uniform_int_distribution<unsigned int> distribution(0.0, sizeX_-1);
std::uniform_int_distribution<unsigned int> distribution2(0.0, sizeY_-1);
//Define StartPose
bool validPathFlag = false;
Vertex* head = new Vertex;
Vertex* ptNode = nullptr;
head->x = startX_;
head->y = startY_;
nodeList.push_back(head);
while (validPathFlag == false)
{
ptNode = new Vertex;
if (!Sampling(ptNode, distribution(engine), distribution2(engine)))
continue;
if (!checkValidSample(ptNode))
continue;
unsigned int sampleX = ptNode->x;
unsigned int sampleY = ptNode->y;
nodeList.push_back(ptNode);
//printf("new Node!");
tempDist = hypot(goalX_ - sampleX, goalY_ - sampleY);
if (tempDist < goalTolerance)
validPathFlag = true;
else if(nodeList.size()>sizeX_*sizeY_ && !validPathFlag)
adjustGoal(goalTolerance);
}
vector<std::pair<int,int>> foundPlan;
while(validPathFlag == true)
{
foundPlan.push_back({ptNode->y,ptNode->x});
map.at<uchar>(ptNode->y,ptNode->x) = 254;
//printf("(%d,%d) \n",ptNode->x,ptNode->y);
if(ptNode->parent == nullptr)
break;
Vertex* tmpNode = ptNode->parent;
//tempDist = sqrt(pow(nodeList[0]->x - ptNode->x, 2) + pow(nodeList[0]->y - ptNode->y, 2));
//printf("(%d,%d)",ptNode->x,ptNode->y);
ptNode = tmpNode;
}
printf("Found Plan. Plan Size %ld \n",foundPlan.size());
cv::imshow("Map",map);
std::cout << "press 'q' Key on cv Window" << std::endl;
int key = cv::waitKey();
if(key == 'q')
{
std::cout << "Exit"<< std::endl;
cv::destroyAllWindows();
}
InitVertex();
}