-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathRPathPlan_astar.cpp
More file actions
574 lines (438 loc) · 14.1 KB
/
Copy pathRPathPlan_astar.cpp
File metadata and controls
574 lines (438 loc) · 14.1 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <vector>
#include "RPathPlan_astar.h"
namespace rtk {
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
typedef struct AStarNode
{
int s_x; // 坐标(最终输出路径需要)
int s_y;
int s_g; // 起点到此点的距离( 由g和h可以得到f,此处f省略,f=g+h )
int s_h; // 启发函数预测的此点到终点的距离
int8_t s_style; // 结点类型:起始点,终点,障碍物
AStarNode *s_parent; // 父节点
int in_closetable; // 是否在close表中
int in_opentable; // 是否在open表中
} *pAStarNode;
typedef std::vector<pAStarNode> nodeTable;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
struct AStarData
{
public:
AStarData();
~AStarData();
int setMap(RMap *map);
int findRoute();
int getPath(RMap *map);
private:
int open_table_push(AStarNode *pn);
void open_table_print(void);
void swap(int idx1, int idx2);
void adjust_heap(int nIndex);
int insert_to_opentable(int x, int y,
pAStarNode curr_node,
pAStarNode end_node,
int w);
void add_neighbors(pAStarNode curr_node, pAStarNode end_node);
private:
RMap *m_map;
int nx, ny; // map size
AStarNode *map_maze; // 结点数组
nodeTable path_stack; // 保存路径的栈
pAStarNode *open_table; // open表
int open_table_size; // open-list pre-allocated size
int open_node_count; // open表中节点数量
AStarNode *start_node; // 起始点
AStarNode *end_node; // 结束点
AStarNode *curr_node; // 当前点
};
AStarData::AStarData()
{
map_maze = NULL;
open_node_count = 0;
open_table_size = 2048;
open_table = new pAStarNode[open_table_size];
}
AStarData::~AStarData()
{
if( map_maze != NULL ) {
delete [] map_maze;
map_maze = NULL;
}
if( open_table != NULL ) {
delete [] open_table;
open_table = NULL;
open_table_size = 0;
}
path_stack.clear();
}
int AStarData::setMap(RMap *map)
{
uint32_t i;
int8_t *p;
int ix, iy;
uint32_t idx;
m_map = map;
nx = map->getSizeX();
ny = map->getSizeY();
p = map->getMap();
if( nx <= 0 || ny <= 0 ) {
printf("AStarData::setMap input map size error! (%d %d)\n", nx, ny);
return -1;
}
// create inner map
if( map_maze != NULL ) delete [] map_maze;
map_maze = new AStarNode[nx*ny];
for(i=0; i<nx*ny; i++) {
ix = i % nx;
iy = i / nx;
map_maze[i].s_g = 0;
map_maze[i].s_h = 0;
map_maze[i].in_closetable = 0;
map_maze[i].in_opentable = 0;
map_maze[i].s_style = p[i];
map_maze[i].s_x = ix;
map_maze[i].s_y = iy;
map_maze[i].s_parent = NULL;
}
// set start/end point
map->getStart(&ix, &iy);
if( ix < 0 || iy < 0 ) return -1;
if( ix >= nx || iy >= ny ) return -1;
idx = iy*nx + ix;
start_node = &(map_maze[idx]);
map->getEnd(&ix, &iy);
if( ix < 0 || iy < 0 ) return -1;
if( ix >= nx || iy >= ny ) return -1;
idx = iy*nx + ix;
end_node = &(map_maze[idx]);
// clear path stack
path_stack.clear();
return 0;
}
int AStarData::getPath(RMap *map)
{
int ix, iy;
// clear old path
map->clearPath();
if( path_stack.size() == 0 ) return -1;
// reverse the path
nodeTable::reverse_iterator rit;
for(rit=path_stack.rbegin(); rit!=path_stack.rend(); rit++) {
ix = (*rit)->s_x;
iy = (*rit)->s_y;
map->pushPathNode(ix, iy);
//printf("(%d,%d) -> ", ix, iy);
}
//printf("\n");
return 0;
}
int AStarData::open_table_push(AStarNode *pn)
{
int ns, i;
pAStarNode *a;
// adjust open_table size dynamically
if( open_node_count+1 >= open_table_size ) {
ns = open_table_size*2;
a = new pAStarNode[ns];
for(i=0; i<open_node_count; i++) a[i] = open_table[i];
delete [] open_table;
open_table = a;
open_table_size = ns;
}
// push to table last
open_table[open_node_count++] = pn;
return 0;
}
void AStarData::open_table_print(void)
{
int i;
printf("ot (%6d),", open_node_count);
for(i=0; i<open_node_count; i++) {
printf(" (%d %d - %d %d)",
open_table[i]->s_x, open_table[i]->s_y,
open_table[i]->s_g, open_table[i]->s_h);
}
printf("\n");
}
// 交换两个元素
void AStarData::swap(int idx1, int idx2)
{
pAStarNode tmp = open_table[idx1];
open_table[idx1] = open_table[idx2];
open_table[idx2] = tmp;
}
// 堆调整
void AStarData::adjust_heap(int nIndex)
{
int curr = nIndex;
int child = curr * 2 + 1; // 得到左孩子idx( 下标从0开始,所有做孩子是curr*2+1 )
int parent = ( curr - 1 ) / 2; // 得到双亲idx
if (nIndex < 0 || nIndex >= open_node_count)
return;
// 往下调整( 要比较左右孩子和cuur parent )
while ( child < open_node_count ) {
// 小根堆是双亲值小于孩子值
if ( child + 1 < open_node_count &&
open_table[child]->s_g + open_table[child]->s_h >
open_table[child+1]->s_g + open_table[child+1]->s_h ) {
++child; // 判断左右孩子大小
}
if (open_table[curr]->s_g + open_table[curr]->s_h <=
open_table[child]->s_g + open_table[child]->s_h) {
break;
} else {
swap(child, curr); // 交换节点
curr = child; // 再判断当前孩子节点
child = curr * 2 + 1; // 再判断左孩子
}
}
if (curr != nIndex)
return;
// 往上调整( 只需要比较curr child和parent )
while (curr != 0) {
if (open_table[curr]->s_g + open_table[curr]->s_h >=
open_table[parent]->s_g + open_table[parent]->s_h) {
break;
} else {
swap(curr, parent);
curr = parent;
parent = (curr-1)/2;
}
}
}
// insert neighbor nodes to open_table
int AStarData::insert_to_opentable(
int x, int y,
pAStarNode curr_node,
pAStarNode end_node,
int w )
{
int i;
pAStarNode cn;
// check range
if( x < 0 || x >= nx || y < 0 || y >= ny )
return -1;
// check whether pass the obstacle
{
int dx, dy, ix1, iy1, ix2, iy2;
uint32_t idx1, idx2;
dx = x - curr_node->s_x;
dy = y - curr_node->s_y;
if( abs(dx) == 1 || abs(dy) == 1 ) {
ix1 = x;
iy1 = curr_node->s_y;
idx1 = iy1*nx + ix1;
ix2 = curr_node->s_x;
iy2 = y;
idx2 = iy2*nx + ix2;
// if two diag nodes are obstacles then return
if( map_maze[idx1].s_style == 1 && map_maze[idx2].s_style == 1 )
return -1;
}
}
cn = &(map_maze[y*nx + x]);
if( cn->s_style != 1 ) { // 不是障碍物
if ( !cn->in_closetable ) { // 不在闭表中
if ( cn->in_opentable ) {
// 在open表中
// 需要判断是否是一条更优化的路径
if ( cn->s_g > curr_node->s_g + w ) { // 如果更优化
cn->s_g = curr_node->s_g + w;
cn->s_parent = curr_node;
for ( i = 0; i < open_node_count; ++i ) {
if ( open_table[i]->s_x == cn->s_x &&
open_table[i]->s_y == cn->s_y ) {
break;
}
}
adjust_heap( i ); // 下面调整点
}
} else {
// 不在open中
cn->s_g = curr_node->s_g + w;
cn->s_h = abs(end_node->s_x - x ) + abs(end_node->s_y - y);
cn->s_parent = curr_node;
cn->in_opentable = 1;
open_table_push(cn);
}
}
}
return 0;
}
// add 8 neighbor nodes
void AStarData::add_neighbors(pAStarNode curr_node, pAStarNode end_node)
{
int x = curr_node->s_x;
int y = curr_node->s_y;
// add 8 neighbor nodes
insert_to_opentable( x+1, y, curr_node, end_node, 10 );
insert_to_opentable( x-1, y, curr_node, end_node, 10 );
insert_to_opentable( x, y+1, curr_node, end_node, 10 );
insert_to_opentable( x, y-1, curr_node, end_node, 10 );
insert_to_opentable( x+1, y+1, curr_node, end_node, 14 );
insert_to_opentable( x+1, y-1, curr_node, end_node, 14 );
insert_to_opentable( x-1, y+1, curr_node, end_node, 14 );
insert_to_opentable( x-1, y-1, curr_node, end_node, 14 );
}
int AStarData::findRoute(void)
{
int is_found;
open_node_count = 0;
// push start point to open_table
start_node->in_opentable = 1;
open_table_push(start_node);
start_node->s_g = 0;
start_node->s_h = abs(end_node->s_x - start_node->s_x) +
abs(end_node->s_y - start_node->s_y);
start_node->s_parent = NULL;
is_found = 0;
// if start point is end point then return
if ( start_node->s_x == end_node->s_x && start_node->s_y == end_node->s_y ) {
printf("ERR: start == end\n");
return -1;
}
while( 1 ) {
curr_node = open_table[0]; // open表的第一个点一定是f值最小的点(通过堆排序得到的)
open_table[0] = open_table[--open_node_count]; // 最后一个点放到第一个点,然后进行堆调整
adjust_heap( 0 ); // 调整堆
curr_node->in_closetable = 1; // 已经在close表中了
if ( curr_node->s_x == end_node->s_x &&
curr_node->s_y == end_node->s_y ) { // 终点在close中,结束
is_found = 1;
break;
}
add_neighbors(curr_node, end_node); // add neighbor nodes
if ( open_node_count == 0 ) { // 没有路径到达
is_found = 0;
break;
}
}
if ( is_found ) {
curr_node = end_node;
while( curr_node ) {
path_stack.push_back(curr_node);
curr_node = curr_node->s_parent;
}
} else {
printf("ERR: No route can be found!\n");
return -1;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
RPathPlan_astar::RPathPlan_astar()
{
m_dat = NULL;
}
RPathPlan_astar::~RPathPlan_astar()
{
if( m_dat != NULL ) {
AStarData *d = (AStarData *) m_dat;
delete d;
m_dat = NULL;
}
}
int RPathPlan_astar::pathPlan(void)
{
AStarData *d;
int ret = 0;
if( m_dat != NULL )
d = (AStarData *) m_dat;
else {
d = new AStarData;
m_dat = d;
}
m_map->clearPath();
ret = d->setMap(m_map);
if( ret != 0 ) {
printf("ERR: RPathPlan_astar::pathPlan failed to set map! (%d)\n", ret);
return -1;
}
ret = d->findRoute();
ret = d->getPath(m_map);
return ret;
}
int RPathPlan_astar::planBeg(void)
{
AStarData *d;
int ret;
if( m_dat != NULL )
d = (AStarData *) m_dat;
else {
d = new AStarData;
m_dat = d;
}
// prepare map data
m_map->convMapCellValue(RMAP_OBSTACLE_SCANNED, RMAP_OBSTACLE);
m_map->convMapCellValue(RMAP_OBSTACLE_UNDESCOVERED, RMAP_OBSTACLE);
m_map->clearPath();
// perform path plan
ret = d->setMap(m_map);
if( ret != 0 ) {
printf("ERR: RPathPlan_astar::pathBeg failed to set map!\n");
return -1;
}
ret = d->findRoute();
ret = d->getPath(m_map);
if( ret != 0 ) return ret;
// get initial position
m_robStep = 0;
m_map->getPathNode(m_robStep, &m_robX, &m_robY, &m_robT);
// scan environment
m_scan.setMap(m_map);
m_scan.setRobot(m_robX, m_robY, m_robT);
m_scan.scan();
// set results to map
m_scan.setScanRes2Map(m_map, RMAP_OBSTACLE_SCANNED);
return 0;
}
int RPathPlan_astar::planStep(void)
{
// prepare map data
m_map->convMapCellValue(RMAP_OBSTACLE_SCANNED, RMAP_OBSTACLE);
m_robStep ++;
if( m_robStep >= m_map->getPathNodeNum() ) {
m_robStep = 0;
return -1;
}
// get current position
m_map->getPathNode(m_robStep, &m_robX, &m_robY, &m_robT);
m_map->setRobPos(m_robX, m_robY, m_robT);
// scan environment
m_scan.setMap(m_map);
m_scan.setRobot(m_robX, m_robY, m_robT);
m_scan.scan();
// set results to map
m_scan.setScanRes2Map(m_map, RMAP_OBSTACLE_SCANNED);
return 0;
}
int RPathPlan_astar::planStepBackward(void)
{
// prepare map data
m_map->convMapCellValue(RMAP_OBSTACLE_SCANNED, RMAP_OBSTACLE);
m_robStep --;
if( m_robStep < 0 ) {
m_robStep = 0;
return -1;
}
// get current position
m_map->getPathNode(m_robStep, &m_robX, &m_robY, &m_robT);
m_map->setRobPos(m_robX, m_robY, m_robT);
// scan environment
m_scan.setMap(m_map);
m_scan.setRobot(m_robX, m_robY, m_robT);
m_scan.scan();
// set results to map
m_scan.setScanRes2Map(m_map, RMAP_OBSTACLE_SCANNED);
return 0;
}
} // end of namespace rtk