-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathRPathPlan_astarDyn.cpp
294 lines (225 loc) · 6.54 KB
/
RPathPlan_astarDyn.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <vector>
#include "RPathPlan_astar.h"
#include "RPathPlan_astarDyn.h"
namespace rtk {
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
RPathPlan_astarDyn::RPathPlan_astarDyn()
{
m_dat = NULL;
m_mapDyn = NULL;
m_mapScan = NULL;
m_dynPlanState = 0;
}
RPathPlan_astarDyn::~RPathPlan_astarDyn()
{
if( m_dat != NULL ) {
RPathPlan_astar *as = (RPathPlan_astar *) m_dat;
delete as;
m_dat = NULL;
}
if( m_mapDyn != NULL ) {
delete m_mapDyn;
m_mapDyn = NULL;
}
if( m_mapScan != NULL ) {
delete m_mapScan;
m_mapScan = NULL;
}
m_dynPlanState = 0;
}
int RPathPlan_astarDyn::pathPlan(void)
{
RPathPlan_astar *as;
int ret;
if( m_dat != NULL )
as = (RPathPlan_astar *) m_dat;
else {
as = new RPathPlan_astar;
m_dat = as;
}
m_map->clearPath();
ret = as->setMap(m_map);
ret = as->pathPlan();
if( ret != 0 ) {
printf("ERR: RPathPlan_astarDyn::pathPlan failed to plan path!\n");
return -1;
}
return ret;
}
int RPathPlan_astarDyn::planBeg(void)
{
int ret = 0;
RPathPlan_astar *as;
int mx, my;
int ix, iy, ix2, iy2;
// get map size
mx = m_map->getSizeX();
my = m_map->getSizeY();
if( mx <= 0 || my <= 0 ) {
printf("ERR: input map is empty!\n");
return -1;
}
// get start/end point
m_map->getStart(&ix, &iy);
if( ix == -1 || iy == -1 ) {
printf("ERR: start point not defined!\n");
return -2;
}
m_map->getEnd(&ix2, &iy2);
if( ix2 == -1 || iy2 == -1 ) {
printf("ERR: end point not defined!\n");
return -2;
}
// get a* obj
if( m_dat != NULL )
as = (RPathPlan_astar *) m_dat;
else {
as = new RPathPlan_astar;
m_dat = as;
}
// copy map
if( m_mapDyn == NULL ) m_mapDyn = new RMap;
m_map->deepCopy(m_mapDyn);
if( m_mapScan == NULL ) m_mapScan = new RMap;
m_map->deepCopy(m_mapScan);
// set visual map data
m_map->convMapCellValue(RMAP_OBSTACLE, RMAP_OBSTACLE_UNDESCOVERED);
m_map->convMapCellValue(RMAP_OBSTACLE_SCANNED, RMAP_OBSTACLE_UNDESCOVERED);
m_map->clearPath();
m_map->clearTrack();
// set dynamic plan map
m_mapDyn->clear(0);
m_mapDyn->clearPath();
m_mapDyn->clearTrack();
// set scan map
m_mapScan->convMapCellValue(RMAP_OBSTACLE_UNDESCOVERED, RMAP_OBSTACLE);
m_mapScan->convMapCellValue(RMAP_OBSTACLE_SCANNED, RMAP_OBSTACLE);
m_mapScan->clearPath();
m_mapScan->clearTrack();
// set scanner's map & perform first scan
m_scan.setMap(m_mapScan);
m_mapScan->setRobPos(ix, iy, 0);
m_scan.setRobot(ix, iy, 0);
m_scan.scan();
m_scan.setScanRes2Map(m_map, RMAP_OBSTACLE_SCANNED);
m_scan.setScanRes2Map(m_mapDyn, RMAP_OBSTACLE);
// perform path plan
m_mapDyn->setStart(ix, iy);
m_mapDyn->setEnd(ix2, iy2);
ret = as->setMap(m_mapDyn);
if( ret != 0 ) {
printf("ERR: RPathPlan_astarDyn::pathBeg failed to set dynamic map!\n");
return -3;
}
ret = as->pathPlan();
if( ret != 0 ) {
printf("ERR: RPathPlan_astarDyn::pathBeg failed to do a* (%d)\n", ret);
m_dynPlanState = 0;
m_mapDynStep = 0;
return -3;
}
as->getPath(m_map->getPath());
m_map->getTrack()->push_back(iy*mx+ix);
m_dynPlanState = 1;
m_mapDynStep = 0;
return 0;
}
int RPathPlan_astarDyn::planStep(void)
{
int mx, my;
double fx, fy;
int ix, iy;
double d, t;
RPathPlan_astar *as;
RPath *rp;
// check current state
if( m_dynPlanState != 1 ) {
printf("ERR: dynamic path plan not started!\n");
return -1;
}
// get a* obj
if( m_dat != NULL )
as = (RPathPlan_astar *) m_dat;
else {
as = new RPathPlan_astar;
m_dat = as;
}
// get map size
mx = m_map->getSizeX();
my = m_map->getSizeY();
// get next position
m_scan.getRobot(&fx, &fy, &t);
ix = (int)(fx);
iy = (int)(fy);
// get faced obstalce distance
m_scan.getFacedObstacle(&d);
printf("faced distance: %g\n", d);
if( d <= 4 ) {
// perform path planning
m_mapDyn->setStart(ix, iy);
as->setMap(m_mapDyn);
as->pathPlan();
rp = m_mapDyn->getPath();
if( rp->size() <= 0 ) {
printf("ERR: a* failed to plan path!\n");
m_dynPlanState = 0;
return -2;
}
m_map->setPath(rp);
m_mapDynStep = 0;
// check whether it can go forward
m_mapDyn->getPathNode(1, &ix, &iy, &t);
if( m_mapScan->getMapCell(ix, iy) != RMAP_OBSTACLE ) {
m_mapDynStep ++;
m_scan.setRobot(ix, iy, t);
m_scan.scan();
m_scan.setScanRes2Map(m_map, RMAP_OBSTACLE_SCANNED);
m_scan.setScanRes2Map(m_mapDyn, RMAP_OBSTACLE);
m_map->getTrack()->push_back(iy*mx+ix);
m_map->setRobPos(ix, iy, t);
}
} else {
rp = m_mapDyn->getPath();
m_mapDynStep ++;
if( m_mapDynStep >= rp->size() ) {
// perform path planning
m_mapDyn->setStart(ix, iy);
as->setMap(m_mapDyn);
as->pathPlan();
rp = m_mapDyn->getPath();
if( rp->size() <= 0 ) {
printf("ERR: a* failed to plan path!\n");
m_dynPlanState = 0;
return -3;
}
m_map->setPath(rp);
m_mapDynStep = 0;
} else {
// get next position
m_mapDyn->getPathNode(m_mapDynStep, &ix, &iy, &t);
m_map->setRobPos(ix, iy, t);
m_map->convMapCellValue(RMAP_OBSTACLE_SCANNED, RMAP_OBSTACLE);
// perform scan
m_scan.setRobot(ix, iy, t);
m_scan.scan();
m_scan.setScanRes2Map(m_map, RMAP_OBSTACLE_SCANNED);
m_scan.setScanRes2Map(m_mapDyn, RMAP_OBSTACLE);
m_map->getTrack()->push_back(iy*mx+ix);
m_map->setPath(rp);
}
}
return 0;
}
int RPathPlan_astarDyn::planStepBackward(void)
{
printf("ERR: RPathPlan_astarDyn::planStepBackward not implemented!\n");
return -1;
}
} // end of namespace rtk