navtest.cpp
Go to the documentation of this file.
1 //
2 // simple timing test of the nav fn planner
3 // expects a cost map in maps/willow-full-0.05.pgm
4 //
5 
6 #include <navfn/navfn.h>
7 #ifdef HAVE_SYS_TIME_H
8 #include <sys/time.h>
9 #endif
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdint.h>
13 
14 #include <string>
15 #include <fstream>
16 
17 #include "navwin.h"
18 
19 using namespace navfn;
20 
21 #ifdef __APPLE__
22 # include <netpbm/pgm.h>
23 #else
24 extern "C" {
25 #include <stdio.h>
26 // pgm.h is not very friendly with system headers... need to undef max() and min() afterwards
27 #include <pgm.h>
28 #undef max
29 #undef min
30 }
31 #endif
32 
33 int goal[2];
34 int start[2];
35 
36 double get_ms()
37 {
38  struct timeval t0;
39  gettimeofday(&t0,NULL);
40  double ret = t0.tv_sec * 1000.0;
41  ret += ((double)t0.tv_usec)*0.001;
42  return ret;
43 }
44 
46 
47 void
49 {
50  nwin->drawPot(nav);
51  Fl::check();
52 }
53 
54 
55 // <raw> is true for ROS-generated raw cost maps
56 COSTTYPE *readPGM(const char *fname, int *width, int *height, bool raw = false);
57 
58 int main(int argc, char **argv)
59 {
60  int dispn = 0;
61 
62  int res = 50; // 50 mm resolution
63  double size = 40.0; // 40 m on a side
64  int inc = 2*COST_NEUTRAL; // thin wavefront
65  bool got_start_goal = false;
66  std::string pgm_file_name;
67 
68  start[0] = 420;
69  start[1] = 420;
70 
71  goal[0] = 580;
72  goal[1] = 400;
73 
74  if (argc > 1)
75  {
76  pgm_file_name = std::string( argv[ 1 ]) + ".pgm";
77  std::string txt_file_name = std::string( argv[ 1 ]) + ".txt";
78 
79  std::ifstream txt_stream( txt_file_name.c_str() );
80  if( txt_stream )
81  {
82  std::string name;
83  int x, y;
84  for( int i = 0; i < 2; i++ )
85  {
86  txt_stream >> name >> x >> y;
87  if( txt_stream && name == "Goal:" )
88  {
89  goal[0] = x;
90  goal[1] = y;
91  }
92  else if( txt_stream && name == "Start:" )
93  {
94  start[0] = x;
95  start[1] = y;
96  }
97  }
98  got_start_goal = true;
99  printf( "start is %d, %d, goal is %d, %d.\n", start[ 0 ], start[ 1 ], goal[ 0 ], goal[ 1 ]);
100  }
101  else
102  {
103  printf( "Failed to open file %s, assuming you didn't want to open a file.\n", txt_file_name.c_str() );
104  }
105  }
106 
107  // get resolution (mm) and perhaps size (m)
108  if( !got_start_goal )
109  {
110  if (argc > 1)
111  res = atoi(argv[1]);
112 
113  if (argc > 2)
114  size = atoi(argv[2]);
115 
116  if (argc > 3)
117  inc = atoi(argv[3]);
118 
119  if (argc > 4)
120  dispn = atoi(argv[4]);
121  }
122  NavFn *nav;
123 
124  // try reading in a file
125  int sx,sy;
126  COSTTYPE *cmap = NULL;
127  cmap = readPGM( pgm_file_name.c_str(),&sx,&sy,true);
128  if (cmap)
129  {
130  nav = new NavFn(sx,sy);
131 
132 
133  }
134  else
135  {
136  sx = (int)((.001 + size) / (res*.001));
137  sy = sx;
138  nav = new NavFn(sx,sy); // size in pixels
139  goal[0] = sx-10;
140  goal[1] = sy/2;
141  start[0] = 20;
142  start[1] = sy/2;
143  }
144 
145  // display
146  nwin = new NavWin(sx,sy,"Potential Field");
147  nwin->maxval = 2*sx*COST_NEUTRAL;
148  Fl::visual(FL_RGB);
149  nwin->show();
150 
151 
152  // set goal and robot poses
153  int *gg = goal;
154  nav->setGoal(gg);
155  int *ss = start;
156  nav->setStart(ss);
157 
158  // set display function
159  nav->display(dispPot,dispn);
160 
161 
162  nav->priInc = inc;
163  printf("[NavTest] priority increment: %d\n", inc);
164 
165  double t0 = get_ms();
166  // set up cost map from file, if it exists
167  if (cmap)
168  {
169  // nav->setCostMap(cmap);
170  memcpy(nav->costarr,cmap,sx*sy);
171  nav->setupNavFn(true);
172  }
173  else
174  {
175  nav->setupNavFn(false);
176  }
177  double t1 = get_ms();
178  // nav->calcNavFnAstar();
179  nav->calcNavFnDijkstra(true);
180  double t2 = get_ms();
181  printf("Setup: %d ms Plan: %d ms Total: %d ms\n",
182  (int)(t1-t0), (int)(t2-t1), (int)(t2-t0));
183 
184  // draw potential field
185  float mmax = 0.0;
186  float *pp = nav->potarr;
187  int ntot = 0;
188  for (int i=0; i<nav->ny*nav->nx; i++, pp++)
189  {
190  if (*pp < 10e7 && *pp > mmax)
191  mmax = *pp;
192  if (*pp > 10e7)
193  ntot++; // number of uncalculated cells
194  }
195  printf("[NavFn] Cells not touched: %d/%d\n", ntot, nav->nx*nav->ny);
196  nwin->maxval = 4*mmax/3/15;
197  dispPot(nav);
198  while (Fl::check()) {
199  if( Fl::event_key( 'q' ))
200  {
201  break;
202  }
203  }
204 
205  return 0;
206 }
207 
208 
209 // read in a PGM file for obstacles
210 // no expansion yet...
211 
212 static int CS;
213 
214 void
215 setcostobs(COSTTYPE *cmap, int n, int w)
216 {
217  CS = 11;
218  for (int i=-CS/2; i<CS/2; i++)
219  {
220  COSTTYPE *cm = i*w + &cmap[n];
221  for (int j=-CS/2; j<CS/2; j++)
222  cm[j] = COST_NEUTRAL + 50;
223  }
224  CS = 7;
225  for (int i=-CS/2; i<CS/2; i++)
226  {
227  COSTTYPE *cm = i*w + &cmap[n];
228  for (int j=-CS/2; j<CS/2; j++)
229  cm[j] = COST_OBS;
230  }
231 }
232 
233 void setcostunk(COSTTYPE *cmap, int n, int w)
234 {
235  cmap[n] = COST_OBS;
236 }
237 
238 #define unknown_gray 0xCC // seems to be the value of "unknown" in maps
239 
240 COSTTYPE *
241 readPGM(const char *fname, int *width, int *height, bool raw)
242 {
243  pm_init("navtest",0);
244 
245  FILE *pgmfile;
246  pgmfile = fopen(fname,"r");
247  if (!pgmfile)
248  {
249  printf("[NavTest] Can't find file %s\n", fname);
250  return NULL;
251  }
252 
253  printf("[NavTest] Reading costmap file %s\n", fname);
254  int ncols, nrows;
255  gray maxval;
256  int format;
257  pgm_readpgminit(pgmfile, &ncols, &nrows, &maxval, &format);
258  printf("[NavTest] Size: %d x %d\n", ncols, nrows);
259 
260  // set up cost map
261  COSTTYPE *cmap = (COSTTYPE *)malloc(ncols*nrows*sizeof(COSTTYPE));
262  if (!raw)
263  for (int i=0; i<ncols*nrows; i++)
264  cmap[i] = COST_NEUTRAL;
265 
266  gray * row(pgm_allocrow(ncols));
267  int otot = 0;
268  int utot = 0;
269  int ftot = 0;
270  for (int ii = 0; ii < nrows; ii++) {
271  pgm_readpgmrow(pgmfile, row, ncols, maxval, format);
272  if (raw) // raw costmap from ROS
273  {
274  for (int jj(ncols - 1); jj >= 0; --jj)
275  {
276  int v = row[jj];
277  cmap[ii*ncols+jj] = v;
278  if (v >= COST_OBS_ROS)
279  otot++;
280  if (v == 0)
281  ftot++;
282  }
283  }
284  else
285  {
286  ftot = ncols*nrows;
287  for (int jj(ncols - 1); jj >= 0; --jj)
288  {
289  if (row[jj] < unknown_gray && ii < nrows-7 && ii > 7)
290  {
291  setcostobs(cmap,ii*ncols+jj,ncols);
292  otot++;
293  ftot--;
294  }
295  else if (row[jj] <= unknown_gray)
296  {
297  setcostunk(cmap,ii*ncols+jj,ncols);
298  utot++;
299  ftot--;
300  }
301  }
302  }
303  }
304  printf("[NavTest] Found %d obstacle cells, %d free cells, %d unknown cells\n", otot, ftot, utot);
305  pgm_freerow(row);
306  *width = ncols;
307  *height = nrows;
308  return cmap;
309 }
navfn::NavFn::calcNavFnDijkstra
bool calcNavFnDijkstra(bool atStart=false)
Caclulates the full navigation function using Dijkstra.
Definition: navfn.cpp:322
navfn::NavFn::setGoal
void setGoal(int *goal)
Sets the goal position for the planner. Note: the navigation cost field computed gives the cost to ge...
Definition: navfn.cpp:205
get_ms
double get_ms()
Definition: navtest.cpp:36
navfn::NavWin::maxval
float maxval
Definition: navwin.h:29
navfn::NavFn
Navigation function class. Holds buffers for costmap, navfn map. Maps are pixel-based....
Definition: navfn.h:106
COST_OBS
#define COST_OBS
Definition: navfn.h:51
navfn
Definition: navfn.h:81
COSTTYPE
#define COSTTYPE
Definition: navfn.h:71
setcostunk
void setcostunk(COSTTYPE *cmap, int n, int w)
Definition: navtest.cpp:233
navfn.h
COST_OBS_ROS
#define COST_OBS_ROS
Definition: navfn.h:52
setcostobs
void setcostobs(COSTTYPE *cmap, int n, int w)
Definition: navtest.cpp:215
navfn::NavFn::potarr
float * potarr
Definition: navfn.h:171
CS
static int CS
Definition: navtest.cpp:212
COST_NEUTRAL
#define COST_NEUTRAL
Definition: navfn.h:64
start
int start[2]
Definition: navtest.cpp:34
nwin
NavWin * nwin
Definition: navtest.cpp:45
goal
int goal[2]
Definition: navtest.cpp:33
navfn::NavWin::drawPot
void drawPot(NavFn *nav)
Definition: navwin.cpp:30
navfn::NavWin
Definition: navwin.h:18
navfn::NavFn::priInc
float priInc
Definition: navfn.h:182
main
int main(int argc, char **argv)
Definition: navtest.cpp:58
navfn::NavFn::setupNavFn
void setupNavFn(bool keepit=false)
Definition: navfn.cpp:397
navwin.h
navfn::NavFn::nx
int nx
Definition: navfn.h:124
navfn::NavFn::display
void display(void fn(NavFn *nav), int n=100)
Definition: navfn.cpp:1046
dispPot
void dispPot(NavFn *nav)
Definition: navtest.cpp:48
navfn::NavFn::setStart
void setStart(int *start)
Sets the start position for the planner. Note: the navigation cost field computed gives the cost to g...
Definition: navfn.cpp:213
readPGM
COSTTYPE * readPGM(const char *fname, int *width, int *height, bool raw=false)
Definition: navtest.cpp:241
navfn::NavFn::ny
int ny
Definition: navfn.h:124
navfn::NavFn::costarr
COSTTYPE * costarr
Definition: navfn.h:170
costmap_2d
unknown_gray
#define unknown_gray
Definition: navtest.cpp:238


navfn
Author(s): Kurt Konolige, Eitan Marder-Eppstein, contradict@gmail.com
autogenerated on Mon Mar 6 2023 03:50:37