Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
navfn.hpp
1 // Copyright (c) 2008, Willow Garage, Inc.
2 // All rights reserved.
3 //
4 // Software License Agreement (BSD License 2.0)
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 // * Neither the name of the Willow Garage nor the names of its
17 // contributors may be used to endorse or promote products derived
18 // from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 
33 //
34 // Navigation function computation
35 // Uses Dijkstra's method
36 // Modified for Euclidean-distance computation
37 //
38 
39 #ifndef NAV2_NAVFN_PLANNER__NAVFN_HPP_
40 #define NAV2_NAVFN_PLANNER__NAVFN_HPP_
41 
42 #include <math.h>
43 #include <stdint.h>
44 #include <string.h>
45 #include <stdio.h>
46 #include <functional>
47 
48 namespace nav2_navfn_planner
49 {
50 
51 // cost defs
52 #define COST_UNKNOWN_ROS 255 // 255 is unknown cost
53 #define COST_OBS 254 // 254 for forbidden regions
54 #define COST_OBS_ROS 253 // ROS values of 253 are obstacles
55 
56 // navfn cost values are set to
57 // COST_NEUTRAL + COST_FACTOR * costmap_cost_value.
58 // Incoming costmap cost values are in the range 0 to 252.
59 // With COST_NEUTRAL of 50, the COST_FACTOR needs to be about 0.8 to
60 // ensure the input values are spread evenly over the output range, 50
61 // to 253. If COST_FACTOR is higher, cost values will have a plateau
62 // around obstacles and the planner will then treat (for example) the
63 // whole width of a narrow hallway as equally undesirable and thus
64 // will not plan paths down the center.
65 
66 #define COST_NEUTRAL 50 // Set this to "open space" value
67 #define COST_FACTOR 0.8 // Used for translating costs in NavFn::setCostmap()
68 
69 // Define the cost type in the case that it is not set. However, this allows
70 // clients to modify it without changing the file. Arguably, it is better to require it to
71 // be defined by a user explicitly
72 #ifndef COSTTYPE
73 #define COSTTYPE unsigned char // Whatever is used...
74 #endif
75 
76 // potential defs
77 #define POT_HIGH 1.0e10 // unassigned cell potential
78 
79 // priority buffers
80 #define PRIORITYBUFSIZE 10000
81 
94 int create_nav_plan_astar(
95  const COSTTYPE * costmap, int nx, int ny,
96  int * goal, int * start,
97  float * plan, int nplan);
98 
104 class NavFn
105 {
106 public:
112  NavFn(int nx, int ny);
113 
114  ~NavFn();
115 
121  void setNavArr(int nx, int ny);
122  int nx, ny, ns;
131  void setCostmap(const COSTTYPE * cmap, bool isROS = true, bool allow_unknown = true);
132 
138  bool calcNavFnAstar(std::function<bool()> cancelChecker);
139 
144  bool calcNavFnDijkstra(std::function<bool()> cancelChecker, bool atStart = false);
145 
150  float * getPathX();
151 
156  float * getPathY();
157 
162  int getPathLen();
163 
168  float getLastPathCost();
169 
171  COSTTYPE * costarr;
172  float * potarr;
173  bool * pending;
174  int nobs;
177  int * pb1, * pb2, * pb3;
178  int * curP, * nextP, * overP;
179  int curPe, nextPe, overPe;
182  float curT;
183  float priInc;
186  static constexpr int terminal_checking_interval = 5000;
187 
195  void setGoal(int * goal);
196 
203  void setStart(int * start);
204 
205  int goal[2];
206  int start[2];
212  void initCost(int k, float v);
213 
220  void updateCell(int n);
221 
226  void updateCellAstar(int n);
227 
232  void setupNavFn(bool keepit = false);
233 
242  bool propNavFnDijkstra(int cycles, std::function<bool()> cancelChecker, bool atStart = false);
243 
251  bool propNavFnAstar(int cycles, std::function<bool()> cancelChecker);
252 
254  float * gradx, * grady;
255  float * pathx, * pathy;
256  int npath;
257  int npathbuf;
266  int calcPath(int n, int * st = NULL);
267 
273  float gradCell(int n);
275  float pathStep;
279  // void display(void fn(NavFn * nav), int n = 100);
280  // int displayInt; /**< save second argument of display() above */
281  // void (* displayFn)(NavFn * nav); /**< display function itself */
282 
285  // void savemap(const char * fname);
286 };
287 
288 } // namespace nav2_navfn_planner
289 
290 #endif // NAV2_NAVFN_PLANNER__NAVFN_HPP_
Navigation function class. Holds buffers for costmap, navfn map. Maps are pixel-based....
Definition: navfn.hpp:105
int getPathLen()
Accessor for the length of a path.
Definition: navfn.cpp:325
float * getPathX()
Accessor for the x-coordinates of a path.
Definition: navfn.cpp:323
bool calcNavFnAstar(std::function< bool()> cancelChecker)
Calculates a plan using the A* heuristic, returns true if one is found.
Definition: navfn.cpp:311
float gradCell(int n)
Calculate gradient at a cell.
Definition: navfn.cpp:935
void updateCell(int n)
Updates the cell at index n.
Definition: navfn.cpp:422
bool propNavFnDijkstra(int cycles, std::function< bool()> cancelChecker, bool atStart=false)
Run propagation for <cycles> iterations, or until start is reached using breadth-first Dijkstra metho...
Definition: navfn.cpp:575
int calcPath(int n, int *st=NULL)
Calculates the path for at most <n> cycles.
Definition: navfn.cpp:763
float getLastPathCost()
Gets the cost of the path found the last time a navigation function was computed.
Definition: navfn.cpp:745
bool calcNavFnDijkstra(std::function< bool()> cancelChecker, bool atStart=false)
Calculates the full navigation function using Dijkstra.
Definition: navfn.cpp:297
bool propNavFnAstar(int cycles, std::function< bool()> cancelChecker)
Run propagation for <cycles> iterations, or until start is reached using the best-first A* method wit...
Definition: navfn.cpp:660
void updateCellAstar(int n)
Updates the cell at index n using the A* heuristic.
Definition: navfn.cpp:496
void initCost(int k, float v)
Initialize cell k with cost v for propagation.
Definition: navfn.cpp:401
void setNavArr(int nx, int ny)
Sets or resets the size of the map.
Definition: navfn.cpp:207
NavFn(int nx, int ny)
Constructs the planner.
Definition: navfn.cpp:112
void setCostmap(const COSTTYPE *cmap, bool isROS=true, bool allow_unknown=true)
Set up the cost array for the planner, usually from ROS.
Definition: navfn.cpp:247
void setupNavFn(bool keepit=false)
Set up navigation potential arrays for new propagation.
Definition: navfn.cpp:342
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:185
float * getPathY()
Accessor for the y-coordinates of a path.
Definition: navfn.cpp:324
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:193