Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
navfn.hpp
1 // Copyright (c) 2008, Willow Garage, Inc.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 //
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 //
13 // * Neither the name of the copyright holder nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 
29 //
30 // Navigation function computation
31 // Uses Dijkstra's method
32 // Modified for Euclidean-distance computation
33 //
34 
35 #ifndef NAV2_NAVFN_PLANNER__NAVFN_HPP_
36 #define NAV2_NAVFN_PLANNER__NAVFN_HPP_
37 
38 #include <math.h>
39 #include <stdint.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <functional>
43 
44 namespace nav2_navfn_planner
45 {
46 
47 // cost defs
48 #define COST_UNKNOWN_ROS 255 // 255 is unknown cost
49 #define COST_OBS 254 // 254 for forbidden regions
50 #define COST_OBS_ROS 253 // ROS values of 253 are obstacles
51 
52 // navfn cost values are set to
53 // COST_NEUTRAL + COST_FACTOR * costmap_cost_value.
54 // Incoming costmap cost values are in the range 0 to 252.
55 // With COST_NEUTRAL of 50, the COST_FACTOR needs to be about 0.8 to
56 // ensure the input values are spread evenly over the output range, 50
57 // to 253. If COST_FACTOR is higher, cost values will have a plateau
58 // around obstacles and the planner will then treat (for example) the
59 // whole width of a narrow hallway as equally undesirable and thus
60 // will not plan paths down the center.
61 
62 #define COST_NEUTRAL 50 // Set this to "open space" value
63 #define COST_FACTOR 0.8 // Used for translating costs in NavFn::setCostmap()
64 
65 // Define the cost type in the case that it is not set. However, this allows
66 // clients to modify it without changing the file. Arguably, it is better to require it to
67 // be defined by a user explicitly
68 #ifndef COSTTYPE
69 #define COSTTYPE unsigned char // Whatever is used...
70 #endif
71 
72 // potential defs
73 #define POT_HIGH 1.0e10 // unassigned cell potential
74 
75 // priority buffers
76 #define PRIORITYBUFSIZE 10000
77 
90 int create_nav_plan_astar(
91  const COSTTYPE * costmap, int nx, int ny,
92  int * goal, int * start,
93  float * plan, int nplan);
94 
100 class NavFn
101 {
102 public:
108  NavFn(int nx, int ny);
109 
110  ~NavFn();
111 
117  void setNavArr(int nx, int ny);
118  int nx, ny, ns;
127  void setCostmap(const COSTTYPE * cmap, bool isROS = true, bool allow_unknown = true);
128 
134  bool calcNavFnAstar(std::function<bool()> cancelChecker);
135 
140  bool calcNavFnDijkstra(std::function<bool()> cancelChecker, bool atStart = false);
141 
146  float * getPathX();
147 
152  float * getPathY();
153 
158  int getPathLen();
159 
164  float getLastPathCost();
165 
167  COSTTYPE * costarr;
168  float * potarr;
169  bool * pending;
170  int nobs;
173  int * pb1, * pb2, * pb3;
174  int * curP, * nextP, * overP;
175  int curPe, nextPe, overPe;
178  float curT;
179  float priInc;
182  static constexpr int terminal_checking_interval = 5000;
183 
191  void setGoal(int * goal);
192 
199  void setStart(int * start);
200 
201  int goal[2];
202  int start[2];
208  void initCost(int k, float v);
209 
216  void updateCell(int n);
217 
222  void updateCellAstar(int n);
223 
228  void setupNavFn(bool keepit = false);
229 
238  bool propNavFnDijkstra(int cycles, std::function<bool()> cancelChecker, bool atStart = false);
239 
247  bool propNavFnAstar(int cycles, std::function<bool()> cancelChecker);
248 
250  float * gradx, * grady;
251  float * pathx, * pathy;
252  int npath;
253  int npathbuf;
262  int calcPath(int n, int * st = NULL);
263 
269  float gradCell(int n);
271  float pathStep;
275  // void display(void fn(NavFn * nav), int n = 100);
276  // int displayInt; /**< save second argument of display() above */
277  // void (* displayFn)(NavFn * nav); /**< display function itself */
278 
281  // void savemap(const char * fname);
282 };
283 
284 } // namespace nav2_navfn_planner
285 
286 #endif // NAV2_NAVFN_PLANNER__NAVFN_HPP_
Navigation function class. Holds buffers for costmap, navfn map. Maps are pixel-based....
Definition: navfn.hpp:101
int getPathLen()
Accessor for the length of a path.
Definition: navfn.cpp:321
float * getPathX()
Accessor for the x-coordinates of a path.
Definition: navfn.cpp:319
bool calcNavFnAstar(std::function< bool()> cancelChecker)
Calculates a plan using the A* heuristic, returns true if one is found.
Definition: navfn.cpp:307
float gradCell(int n)
Calculate gradient at a cell.
Definition: navfn.cpp:931
void updateCell(int n)
Updates the cell at index n.
Definition: navfn.cpp:418
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:571
int calcPath(int n, int *st=NULL)
Calculates the path for at most <n> cycles.
Definition: navfn.cpp:759
float getLastPathCost()
Gets the cost of the path found the last time a navigation function was computed.
Definition: navfn.cpp:741
bool calcNavFnDijkstra(std::function< bool()> cancelChecker, bool atStart=false)
Calculates the full navigation function using Dijkstra.
Definition: navfn.cpp:293
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:656
void updateCellAstar(int n)
Updates the cell at index n using the A* heuristic.
Definition: navfn.cpp:492
void initCost(int k, float v)
Initialize cell k with cost v for propagation.
Definition: navfn.cpp:397
void setNavArr(int nx, int ny)
Sets or resets the size of the map.
Definition: navfn.cpp:203
NavFn(int nx, int ny)
Constructs the planner.
Definition: navfn.cpp:108
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:243
void setupNavFn(bool keepit=false)
Set up navigation potential arrays for new propagation.
Definition: navfn.cpp:338
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:181
float * getPathY()
Accessor for the y-coordinates of a path.
Definition: navfn.cpp:320
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:189