-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathnavfn.cpp
1039 lines (891 loc) · 26.2 KB
/
navfn.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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2008, Willow Garage, Inc.
// All rights reserved.
//
// Software License Agreement (BSD License 2.0)
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of the Willow Garage nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Navigation function computation
// Uses Dijkstra's method
// Modified for Euclidean-distance computation
//
// Path calculation uses no interpolation when pot field is at max in
// nearby cells
//
// Path calc has sanity check that it succeeded
//
#include "nav2_navfn_planner/navfn.hpp"
#include <algorithm>
#include "nav2_core/planner_exceptions.hpp"
#include "rclcpp/rclcpp.hpp"
namespace nav2_navfn_planner
{
//
// function to perform nav fn calculation
// keeps track of internal buffers, will be more efficient
// if the size of the environment does not change
//
// Example usage:
/*
int
create_nav_plan_astar(
COSTTYPE * costmap, int nx, int ny,
int * goal, int * start,
float * plan, int nplan)
{
static NavFn * nav = NULL;
if (nav == NULL) {
nav = new NavFn(nx, ny);
}
if (nav->nx != nx || nav->ny != ny) { // check for compatibility with previous call
delete nav;
nav = new NavFn(nx, ny);
}
nav->setGoal(goal);
nav->setStart(start);
nav->costarr = costmap;
nav->setupNavFn(true);
// calculate the nav fn and path
nav->priInc = 2 * COST_NEUTRAL;
nav->propNavFnAstar(std::max(nx * ny / 20, nx + ny));
// path
int len = nav->calcPath(nplan);
if (len > 0) { // found plan
RCLCPP_DEBUG(rclcpp::get_logger("rclcpp"), "[NavFn] Path found, %d steps\n", len);
} else {
RCLCPP_DEBUG(rclcpp::get_logger("rclcpp"), "[NavFn] No path found\n");
}
if (len > 0) {
for (int i = 0; i < len; i++) {
plan[i * 2] = nav->pathx[i];
plan[i * 2 + 1] = nav->pathy[i];
}
}
return len;
}
*/
//
// create nav fn buffers
//
NavFn::NavFn(int xs, int ys)
{
// create cell arrays
costarr = NULL;
potarr = NULL;
pending = NULL;
gradx = grady = NULL;
setNavArr(xs, ys);
// priority buffers
pb1 = new int[PRIORITYBUFSIZE];
pb2 = new int[PRIORITYBUFSIZE];
pb3 = new int[PRIORITYBUFSIZE];
// for Dijkstra (breadth-first), set to COST_NEUTRAL
// for A* (best-first), set to COST_NEUTRAL
priInc = 2 * COST_NEUTRAL;
// goal and start
goal[0] = goal[1] = 0;
start[0] = start[1] = 0;
// display function
// displayFn = NULL;
// displayInt = 0;
// path buffers
npathbuf = npath = 0;
pathx = pathy = NULL;
pathStep = 0.5;
}
NavFn::~NavFn()
{
if (costarr) {
delete[] costarr;
}
if (potarr) {
delete[] potarr;
}
if (pending) {
delete[] pending;
}
if (gradx) {
delete[] gradx;
}
if (grady) {
delete[] grady;
}
if (pathx) {
delete[] pathx;
}
if (pathy) {
delete[] pathy;
}
if (pb1) {
delete[] pb1;
}
if (pb2) {
delete[] pb2;
}
if (pb3) {
delete[] pb3;
}
}
//
// set goal, start positions for the nav fn
//
void
NavFn::setGoal(int * g)
{
goal[0] = g[0];
goal[1] = g[1];
RCLCPP_DEBUG(rclcpp::get_logger("rclcpp"), "[NavFn] Setting goal to %d,%d\n", goal[0], goal[1]);
}
void
NavFn::setStart(int * g)
{
start[0] = g[0];
start[1] = g[1];
RCLCPP_DEBUG(
rclcpp::get_logger("rclcpp"), "[NavFn] Setting start to %d,%d\n", start[0],
start[1]);
}
//
// Set/Reset map size
//
void
NavFn::setNavArr(int xs, int ys)
{
RCLCPP_DEBUG(rclcpp::get_logger("rclcpp"), "[NavFn] Array is %d x %d\n", xs, ys);
nx = xs;
ny = ys;
ns = nx * ny;
if (costarr) {
delete[] costarr;
}
if (potarr) {
delete[] potarr;
}
if (pending) {
delete[] pending;
}
if (gradx) {
delete[] gradx;
}
if (grady) {
delete[] grady;
}
costarr = new COSTTYPE[ns]; // cost array, 2d config space
memset(costarr, 0, ns * sizeof(COSTTYPE));
potarr = new float[ns]; // navigation potential array
pending = new bool[ns];
memset(pending, 0, ns * sizeof(bool));
gradx = new float[ns];
grady = new float[ns];
}
//
// set up cost array, usually from ROS
//
void
NavFn::setCostmap(const COSTTYPE * cmap, bool isROS, bool allow_unknown)
{
COSTTYPE * cm = costarr;
if (isROS) { // ROS-type cost array
for (int i = 0; i < ny; i++) {
int k = i * nx;
for (int j = 0; j < nx; j++, k++, cmap++, cm++) {
// This transforms the incoming cost values:
// COST_OBS -> COST_OBS (incoming "lethal obstacle")
// COST_OBS_ROS -> COST_OBS (incoming "inscribed inflated obstacle")
// values in range 0 to 252 -> values from COST_NEUTRAL to COST_OBS_ROS.
*cm = COST_OBS;
int v = *cmap;
if (v < COST_OBS_ROS) {
v = COST_NEUTRAL + COST_FACTOR * v;
if (v >= COST_OBS) {
v = COST_OBS - 1;
}
*cm = v;
} else if (v == COST_UNKNOWN_ROS && allow_unknown) {
v = COST_OBS - 1;
*cm = v;
}
}
}
} else { // not a ROS map, just a PGM
for (int i = 0; i < ny; i++) {
int k = i * nx;
for (int j = 0; j < nx; j++, k++, cmap++, cm++) {
*cm = COST_OBS;
if (i < 7 || i > ny - 8 || j < 7 || j > nx - 8) {
continue; // don't do borders
}
int v = *cmap;
if (v < COST_OBS_ROS) {
v = COST_NEUTRAL + COST_FACTOR * v;
if (v >= COST_OBS) {
v = COST_OBS - 1;
}
*cm = v;
} else if (v == COST_UNKNOWN_ROS) {
v = COST_OBS - 1;
*cm = v;
}
}
}
}
}
bool
NavFn::calcNavFnDijkstra(std::function<bool()> cancelChecker, bool atStart)
{
setupNavFn(true);
// calculate the nav fn and path
return propNavFnDijkstra(std::max(nx * ny / 20, nx + ny), cancelChecker, atStart);
}
//
// calculate navigation function, given a costmap, goal, and start
//
bool
NavFn::calcNavFnAstar(std::function<bool()> cancelChecker)
{
setupNavFn(true);
// calculate the nav fn and path
return propNavFnAstar(std::max(nx * ny / 20, nx + ny), cancelChecker);
}
//
// returning values
//
float * NavFn::getPathX() {return pathx;}
float * NavFn::getPathY() {return pathy;}
int NavFn::getPathLen() {return npath;}
// inserting onto the priority blocks
#define push_cur(n) {if (n >= 0 && n < ns && !pending[n] && \
costarr[n] < COST_OBS && curPe < PRIORITYBUFSIZE) \
{curP[curPe++] = n; pending[n] = true;}}
#define push_next(n) {if (n >= 0 && n < ns && !pending[n] && \
costarr[n] < COST_OBS && nextPe < PRIORITYBUFSIZE) \
{nextP[nextPe++] = n; pending[n] = true;}}
#define push_over(n) {if (n >= 0 && n < ns && !pending[n] && \
costarr[n] < COST_OBS && overPe < PRIORITYBUFSIZE) \
{overP[overPe++] = n; pending[n] = true;}}
// Set up navigation potential arrays for new propagation
void
NavFn::setupNavFn(bool keepit)
{
// reset values in propagation arrays
for (int i = 0; i < ns; i++) {
potarr[i] = POT_HIGH;
if (!keepit) {
costarr[i] = COST_NEUTRAL;
}
gradx[i] = grady[i] = 0.0;
}
// outer bounds of cost array
COSTTYPE * pc;
pc = costarr;
for (int i = 0; i < nx; i++) {
*pc++ = COST_OBS;
}
pc = costarr + (ny - 1) * nx;
for (int i = 0; i < nx; i++) {
*pc++ = COST_OBS;
}
pc = costarr;
for (int i = 0; i < ny; i++, pc += nx) {
*pc = COST_OBS;
}
pc = costarr + nx - 1;
for (int i = 0; i < ny; i++, pc += nx) {
*pc = COST_OBS;
}
// priority buffers
curT = COST_OBS;
curP = pb1;
curPe = 0;
nextP = pb2;
nextPe = 0;
overP = pb3;
overPe = 0;
memset(pending, 0, ns * sizeof(bool));
// set goal
int k = goal[0] + goal[1] * nx;
initCost(k, 0);
// find # of obstacle cells
pc = costarr;
int ntot = 0;
for (int i = 0; i < ns; i++, pc++) {
if (*pc >= COST_OBS) {
ntot++; // number of cells that are obstacles
}
}
nobs = ntot;
}
// initialize a goal-type cost for starting propagation
void
NavFn::initCost(int k, float v)
{
potarr[k] = v;
push_cur(k + 1);
push_cur(k - 1);
push_cur(k - nx);
push_cur(k + nx);
}
//
// Critical function: calculate updated potential value of a cell,
// given its neighbors' values
// Planar-wave update calculation from two lowest neighbors in a 4-grid
// Quadratic approximation to the interpolated value
// No checking of bounds here, this function should be fast
//
#define INVSQRT2 0.707106781
inline void
NavFn::updateCell(int n)
{
// get neighbors
const float l = potarr[n - 1];
const float r = potarr[n + 1];
const float u = potarr[n - nx];
const float d = potarr[n + nx];
// ROS_INFO("[Update] c: %0.1f l: %0.1f r: %0.1f u: %0.1f d: %0.1f\n",
// potarr[n], l, r, u, d);
// ROS_INFO("[Update] cost: %d\n", costarr[n]);
// find lowest, and its lowest neighbor
float ta, tc;
if (l < r) {tc = l;} else {tc = r;}
if (u < d) {ta = u;} else {ta = d;}
// do planar wave update
if (costarr[n] < COST_OBS) { // don't propagate into obstacles
float hf = static_cast<float>(costarr[n]); // traversability factor
float dc = tc - ta; // relative cost between ta,tc
if (dc < 0) { // ta is lowest
dc = -dc;
ta = tc;
}
// calculate new potential
float pot;
if (dc >= hf) { // if too large, use ta-only update
pot = ta + hf;
} else { // two-neighbor interpolation update
// use quadratic approximation
// might speed this up through table lookup, but still have to
// do the divide
const float div = dc / hf;
const float v = -0.2301 * div * div + 0.5307 * div + 0.7040;
pot = ta + hf * v;
}
// ROS_INFO("[Update] new pot: %d\n", costarr[n]);
// now add affected neighbors to priority blocks
if (pot < potarr[n]) {
float le = INVSQRT2 * static_cast<float>(costarr[n - 1]);
float re = INVSQRT2 * static_cast<float>(costarr[n + 1]);
float ue = INVSQRT2 * static_cast<float>(costarr[n - nx]);
float de = INVSQRT2 * static_cast<float>(costarr[n + nx]);
potarr[n] = pot;
if (pot < curT) { // low-cost buffer block
if (l > pot + le) {push_next(n - 1);}
if (r > pot + re) {push_next(n + 1);}
if (u > pot + ue) {push_next(n - nx);}
if (d > pot + de) {push_next(n + nx);}
} else { // overflow block
if (l > pot + le) {push_over(n - 1);}
if (r > pot + re) {push_over(n + 1);}
if (u > pot + ue) {push_over(n - nx);}
if (d > pot + de) {push_over(n + nx);}
}
}
}
}
//
// Use A* method for setting priorities
// Critical function: calculate updated potential value of a cell,
// given its neighbors' values
// Planar-wave update calculation from two lowest neighbors in a 4-grid
// Quadratic approximation to the interpolated value
// No checking of bounds here, this function should be fast
//
#define INVSQRT2 0.707106781
inline void
NavFn::updateCellAstar(int n)
{
// get neighbors
float l = potarr[n - 1];
float r = potarr[n + 1];
float u = potarr[n - nx];
float d = potarr[n + nx];
// ROS_INFO("[Update] c: %0.1f l: %0.1f r: %0.1f u: %0.1f d: %0.1f\n",
// potarr[n], l, r, u, d);
// ROS_INFO("[Update] cost of %d: %d\n", n, costarr[n]);
// find lowest, and its lowest neighbor
float ta, tc;
if (l < r) {tc = l;} else {tc = r;}
if (u < d) {ta = u;} else {ta = d;}
// do planar wave update
if (costarr[n] < COST_OBS) { // don't propagate into obstacles
float hf = static_cast<float>(costarr[n]); // traversability factor
float dc = tc - ta; // relative cost between ta,tc
if (dc < 0) { // ta is lowest
dc = -dc;
ta = tc;
}
// calculate new potential
float pot;
if (dc >= hf) { // if too large, use ta-only update
pot = ta + hf;
} else { // two-neighbor interpolation update
// use quadratic approximation
// might speed this up through table lookup, but still have to
// do the divide
const float div = dc / hf;
const float v = -0.2301 * div * div + 0.5307 * div + 0.7040;
pot = ta + hf * v;
}
// ROS_INFO("[Update] new pot: %d\n", costarr[n]);
// now add affected neighbors to priority blocks
if (pot < potarr[n]) {
float le = INVSQRT2 * static_cast<float>(costarr[n - 1]);
float re = INVSQRT2 * static_cast<float>(costarr[n + 1]);
float ue = INVSQRT2 * static_cast<float>(costarr[n - nx]);
float de = INVSQRT2 * static_cast<float>(costarr[n + nx]);
// calculate distance
int x = n % nx;
int y = n / nx;
float dist = hypot(x - start[0], y - start[1]) * static_cast<float>(COST_NEUTRAL);
potarr[n] = pot;
pot += dist;
if (pot < curT) { // low-cost buffer block
if (l > pot + le) {push_next(n - 1);}
if (r > pot + re) {push_next(n + 1);}
if (u > pot + ue) {push_next(n - nx);}
if (d > pot + de) {push_next(n + nx);}
} else {
if (l > pot + le) {push_over(n - 1);}
if (r > pot + re) {push_over(n + 1);}
if (u > pot + ue) {push_over(n - nx);}
if (d > pot + de) {push_over(n + nx);}
}
}
}
}
//
// main propagation function
// Dijkstra method, breadth-first
// runs for a specified number of cycles,
// or until it runs out of cells to update,
// or until the Start cell is found (atStart = true)
//
bool
NavFn::propNavFnDijkstra(int cycles, std::function<bool()> cancelChecker, bool atStart)
{
int nwv = 0; // max priority block size
int nc = 0; // number of cells put into priority blocks
int cycle = 0; // which cycle we're on
// set up start cell
int startCell = start[1] * nx + start[0];
for (; cycle < cycles; cycle++) { // go for this many cycles, unless interrupted
if (cycle % terminal_checking_interval == 0 && cancelChecker()) {
throw nav2_core::PlannerCancelled("Planner was cancelled");
}
if (curPe == 0 && nextPe == 0) { // priority blocks empty
break;
}
// stats
nc += curPe;
if (curPe > nwv) {
nwv = curPe;
}
// reset pending flags on current priority buffer
int * pb = curP;
int i = curPe;
while (i-- > 0) {
pending[*(pb++)] = false;
}
// process current priority buffer
pb = curP;
i = curPe;
while (i-- > 0) {
updateCell(*pb++);
}
// if (displayInt > 0 && (cycle % displayInt) == 0) {
// displayFn(this);
// }
// swap priority blocks curP <=> nextP
curPe = nextPe;
nextPe = 0;
pb = curP; // swap buffers
curP = nextP;
nextP = pb;
// see if we're done with this priority level
if (curPe == 0) {
curT += priInc; // increment priority threshold
curPe = overPe; // set current to overflow block
overPe = 0;
pb = curP; // swap buffers
curP = overP;
overP = pb;
}
// check if we've hit the Start cell
if (atStart) {
if (potarr[startCell] < POT_HIGH) {
break;
}
}
}
RCLCPP_DEBUG(
rclcpp::get_logger("rclcpp"),
"[NavFn] Used %d cycles, %d cells visited (%d%%), priority buf max %d\n",
cycle, nc, (int)((nc * 100.0) / (ns - nobs)), nwv);
return (cycle < cycles) ? true : false;
}
//
// main propagation function
// A* method, best-first
// uses Euclidean distance heuristic
// runs for a specified number of cycles,
// or until it runs out of cells to update,
// or until the Start cell is found (atStart = true)
//
bool
NavFn::propNavFnAstar(int cycles, std::function<bool()> cancelChecker)
{
int nwv = 0; // max priority block size
int nc = 0; // number of cells put into priority blocks
int cycle = 0; // which cycle we're on
// set initial threshold, based on distance
float dist = hypot(goal[0] - start[0], goal[1] - start[1]) * static_cast<float>(COST_NEUTRAL);
curT = dist + curT;
// set up start cell
int startCell = start[1] * nx + start[0];
// do main cycle
for (; cycle < cycles; cycle++) { // go for this many cycles, unless interrupted
if (cycle % terminal_checking_interval == 0 && cancelChecker()) {
throw nav2_core::PlannerCancelled("Planner was cancelled");
}
if (curPe == 0 && nextPe == 0) { // priority blocks empty
break;
}
// stats
nc += curPe;
if (curPe > nwv) {
nwv = curPe;
}
// reset pending flags on current priority buffer
int * pb = curP;
int i = curPe;
while (i-- > 0) {
pending[*(pb++)] = false;
}
// process current priority buffer
pb = curP;
i = curPe;
while (i-- > 0) {
updateCellAstar(*pb++);
}
// if (displayInt > 0 && (cycle % displayInt) == 0) {
// displayFn(this);
// }
// swap priority blocks curP <=> nextP
curPe = nextPe;
nextPe = 0;
pb = curP; // swap buffers
curP = nextP;
nextP = pb;
// see if we're done with this priority level
if (curPe == 0) {
curT += priInc; // increment priority threshold
curPe = overPe; // set current to overflow block
overPe = 0;
pb = curP; // swap buffers
curP = overP;
overP = pb;
}
// check if we've hit the Start cell
if (potarr[startCell] < POT_HIGH) {
break;
}
}
last_path_cost_ = potarr[startCell];
RCLCPP_DEBUG(
rclcpp::get_logger("rclcpp"),
"[NavFn] Used %d cycles, %d cells visited (%d%%), priority buf max %d\n",
cycle, nc, (int)((nc * 100.0) / (ns - nobs)), nwv);
if (potarr[startCell] < POT_HIGH) {
return true; // finished up here}
} else {
return false;
}
}
float NavFn::getLastPathCost()
{
return last_path_cost_;
}
//
// Path construction
// Find gradient at array points, interpolate path
// Use step size of pathStep, usually 0.5 pixel
//
// Some sanity checks:
// 1. Stuck at same index position
// 2. Doesn't get near goal
// 3. Surrounded by high potentials
//
int
NavFn::calcPath(int n, int * st)
{
// test write
// savemap("test");
// check path arrays
if (npathbuf < n) {
if (pathx) {delete[] pathx;}
if (pathy) {delete[] pathy;}
pathx = new float[n];
pathy = new float[n];
npathbuf = n;
}
// set up start position at cell
// st is always upper left corner for 4-point bilinear interpolation
if (st == NULL) {st = start;}
int stc = st[1] * nx + st[0];
// set up offset
float dx = 0;
float dy = 0;
npath = 0;
// go for <n> cycles at most
for (int i = 0; i < n; i++) {
// check if near goal
int nearest_point = std::max(
0,
std::min(
nx * ny - 1, stc + static_cast<int>(round(dx)) +
static_cast<int>(nx * round(dy))));
if (potarr[nearest_point] < COST_NEUTRAL) {
pathx[npath] = static_cast<float>(goal[0]);
pathy[npath] = static_cast<float>(goal[1]);
return ++npath; // done!
}
if (stc < nx || stc > ns - nx) { // would be out of bounds
RCLCPP_DEBUG(rclcpp::get_logger("rclcpp"), "[PathCalc] Out of bounds");
return 0;
}
// add to path
pathx[npath] = stc % nx + dx;
pathy[npath] = stc / nx + dy;
npath++;
bool oscillation_detected = false;
if (npath > 2 &&
pathx[npath - 1] == pathx[npath - 3] &&
pathy[npath - 1] == pathy[npath - 3])
{
RCLCPP_DEBUG(
rclcpp::get_logger("rclcpp"),
"[PathCalc] oscillation detected, attempting fix.");
oscillation_detected = true;
}
int stcnx = stc + nx;
int stcpx = stc - nx;
// check for potentials at eight positions near cell
if (potarr[stc] >= POT_HIGH ||
potarr[stc + 1] >= POT_HIGH ||
potarr[stc - 1] >= POT_HIGH ||
potarr[stcnx] >= POT_HIGH ||
potarr[stcnx + 1] >= POT_HIGH ||
potarr[stcnx - 1] >= POT_HIGH ||
potarr[stcpx] >= POT_HIGH ||
potarr[stcpx + 1] >= POT_HIGH ||
potarr[stcpx - 1] >= POT_HIGH ||
oscillation_detected)
{
RCLCPP_DEBUG(
rclcpp::get_logger("rclcpp"),
"[Path] Pot fn boundary, following grid (%0.1f/%d)", potarr[stc], npath);
// check eight neighbors to find the lowest
int minc = stc;
int minp = potarr[stc];
int sti = stcpx - 1;
if (potarr[sti] < minp) {minp = potarr[sti]; minc = sti;}
sti++;
if (potarr[sti] < minp) {minp = potarr[sti]; minc = sti;}
sti++;
if (potarr[sti] < minp) {minp = potarr[sti]; minc = sti;}
sti = stc - 1;
if (potarr[sti] < minp) {minp = potarr[sti]; minc = sti;}
sti = stc + 1;
if (potarr[sti] < minp) {minp = potarr[sti]; minc = sti;}
sti = stcnx - 1;
if (potarr[sti] < minp) {minp = potarr[sti]; minc = sti;}
sti++;
if (potarr[sti] < minp) {minp = potarr[sti]; minc = sti;}
sti++;
if (potarr[sti] < minp) {minp = potarr[sti]; minc = sti;}
stc = minc;
dx = 0;
dy = 0;
RCLCPP_DEBUG(
rclcpp::get_logger("rclcpp"), "[Path] Pot: %0.1f pos: %0.1f,%0.1f",
potarr[stc], pathx[npath - 1], pathy[npath - 1]);
if (potarr[stc] >= POT_HIGH) {
RCLCPP_DEBUG(rclcpp::get_logger("rclcpp"), "[PathCalc] No path found, high potential");
// savemap("navfn_highpot");
return 0;
}
} else { // have a good gradient here
// get grad at four positions near cell
gradCell(stc);
gradCell(stc + 1);
gradCell(stcnx);
gradCell(stcnx + 1);
// get interpolated gradient
float x1 = (1.0 - dx) * gradx[stc] + dx * gradx[stc + 1];
float x2 = (1.0 - dx) * gradx[stcnx] + dx * gradx[stcnx + 1];
float x = (1.0 - dy) * x1 + dy * x2; // interpolated x
float y1 = (1.0 - dx) * grady[stc] + dx * grady[stc + 1];
float y2 = (1.0 - dx) * grady[stcnx] + dx * grady[stcnx + 1];
float y = (1.0 - dy) * y1 + dy * y2; // interpolated y
#if 0
// show gradients
RCLCPP_DEBUG(
rclcpp::get_logger("rclcpp"),
"[Path] %0.2f,%0.2f %0.2f,%0.2f %0.2f,%0.2f %0.2f,%0.2f; final x=%.3f, y=%.3f\n",
gradx[stc], grady[stc], gradx[stc + 1], grady[stc + 1],
gradx[stcnx], grady[stcnx], gradx[stcnx + 1], grady[stcnx + 1],
x, y);
#endif
// check for zero gradient, failed
if (x == 0.0 && y == 0.0) {
RCLCPP_DEBUG(rclcpp::get_logger("rclcpp"), "[PathCalc] Zero gradient");
return 0;
}
// move in the right direction
float ss = pathStep / hypot(x, y);
dx += x * ss;
dy += y * ss;
// check for overflow
if (dx > 1.0) {stc++; dx -= 1.0;}
if (dx < -1.0) {stc--; dx += 1.0;}
if (dy > 1.0) {stc += nx; dy -= 1.0;}
if (dy < -1.0) {stc -= nx; dy += 1.0;}
}
// ROS_INFO("[Path] Pot: %0.1f grad: %0.1f,%0.1f pos: %0.1f,%0.1f\n",
// potarr[stc], x, y, pathx[npath-1], pathy[npath-1]);
}
// return npath; // out of cycles, return failure
RCLCPP_DEBUG(rclcpp::get_logger("rclcpp"), "[PathCalc] No path found, path too long");
// savemap("navfn_pathlong");
return 0; // out of cycles, return failure
}
//
// gradient calculations
//
// calculate gradient at a cell
// positive value are to the right and down
float
NavFn::gradCell(int n)
{
if (gradx[n] + grady[n] > 0.0) { // check this cell
return 1.0;
}
if (n < nx || n > ns - nx) { // would be out of bounds
return 0.0;
}
float cv = potarr[n];
float dx = 0.0;
float dy = 0.0;
// check for in an obstacle
if (cv >= POT_HIGH) {
if (potarr[n - 1] < POT_HIGH) {
dx = -COST_OBS;
} else if (potarr[n + 1] < POT_HIGH) {
dx = COST_OBS;
}
if (potarr[n - nx] < POT_HIGH) {
dy = -COST_OBS;
} else if (potarr[n + nx] < POT_HIGH) {
dy = COST_OBS;
}
} else { // not in an obstacle
// dx calc, average to sides
if (potarr[n - 1] < POT_HIGH) {
dx += potarr[n - 1] - cv;
}
if (potarr[n + 1] < POT_HIGH) {
dx += cv - potarr[n + 1];
}
// dy calc, average to sides
if (potarr[n - nx] < POT_HIGH) {
dy += potarr[n - nx] - cv;
}
if (potarr[n + nx] < POT_HIGH) {
dy += cv - potarr[n + nx];
}
}
// normalize
float norm = hypot(dx, dy);
if (norm > 0) {
norm = 1.0 / norm;
gradx[n] = norm * dx;
grady[n] = norm * dy;
}
return norm;
}
//
// display function setup
// <n> is the number of cycles to wait before displaying,
// use 0 to turn it off
// void
// NavFn::display(void fn(NavFn * nav), int n)
// {
// displayFn = fn;
// displayInt = n;
// }