Skip to content

Commit bc76734

Browse files
authored
Direction change index computation during initialization (ros-navigation#4026)
* modified direction change index computation point prior the direction change index was being computed in ever getNeighbor call, now its done at parse time. Signed-off-by: Anil Kumar Chavali <[email protected]> * changed computation point of direction_change_index * moved computation to getMotionPrimitives function * updated getMotionPrimitives function to return index * updated node lattice tests with modified function * removed obsolete code Signed-off-by: Anil Kumar Chavali <[email protected]> * updated docstring for getMotionPrimitives func. Signed-off-by: Anil Kumar Chavali <[email protected]> * linting fixes using ament_uncrustify Signed-off-by: Anil Kumar Chavali <[email protected]> --------- Signed-off-by: Anil Kumar Chavali <[email protected]>
1 parent 305f7e0 commit bc76734

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

nav2_smac_planner/include/nav2_smac_planner/node_lattice.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,12 @@ struct LatticeMotionTable
6767
/**
6868
* @brief Get projections of motion models
6969
* @param node Ptr to NodeLattice
70+
* @param Reference direction change index
7071
* @return A set of motion poses
7172
*/
72-
MotionPrimitivePtrs getMotionPrimitives(const NodeLattice * node);
73+
MotionPrimitivePtrs getMotionPrimitives(
74+
const NodeLattice * node,
75+
unsigned int & direction_change_index);
7376

7477
/**
7578
* @brief Get file metadata needed

nav2_smac_planner/src/node_lattice.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,19 @@ void LatticeMotionTable::initMotionModel(
111111
}
112112
}
113113

114-
MotionPrimitivePtrs LatticeMotionTable::getMotionPrimitives(const NodeLattice * node)
114+
MotionPrimitivePtrs LatticeMotionTable::getMotionPrimitives(
115+
const NodeLattice * node,
116+
unsigned int & direction_change_index)
115117
{
116118
MotionPrimitives & prims_at_heading = motion_primitives[node->pose.theta];
117119
MotionPrimitivePtrs primitive_projection_list;
118120
for (unsigned int i = 0; i != prims_at_heading.size(); i++) {
119121
primitive_projection_list.push_back(&prims_at_heading[i]);
120122
}
121123

124+
// direction change index
125+
direction_change_index = static_cast<unsigned int>(primitive_projection_list.size());
126+
122127
if (allow_reverse_expansion) {
123128
// Find normalized heading bin of the reverse expansion
124129
double reserve_heading = node->pose.theta - (num_angle_quantization / 2);
@@ -470,17 +475,12 @@ void NodeLattice::getNeighbors(
470475
bool backwards = false;
471476
NodePtr neighbor = nullptr;
472477
Coordinates initial_node_coords, motion_projection;
473-
MotionPrimitivePtrs motion_primitives = motion_table.getMotionPrimitives(this);
478+
unsigned int direction_change_index = 0;
479+
MotionPrimitivePtrs motion_primitives = motion_table.getMotionPrimitives(
480+
this,
481+
direction_change_index);
474482
const float & grid_resolution = motion_table.lattice_metadata.grid_resolution;
475483

476-
unsigned int direction_change_idx = 1e9;
477-
for (unsigned int i = 0; i != motion_primitives.size(); i++) {
478-
if (motion_primitives[0]->start_angle != motion_primitives[i]->start_angle) {
479-
direction_change_idx = i;
480-
break;
481-
}
482-
}
483-
484484
for (unsigned int i = 0; i != motion_primitives.size(); i++) {
485485
const MotionPose & end_pose = motion_primitives[i]->poses.back();
486486
motion_projection.x = this->pose.x + (end_pose._x / grid_resolution);
@@ -502,7 +502,7 @@ void NodeLattice::getNeighbors(
502502
// account in case the robot base footprint is asymmetric.
503503
backwards = false;
504504
angle = motion_projection.theta;
505-
if (i >= direction_change_idx) {
505+
if (i >= direction_change_index) {
506506
backwards = true;
507507
angle = motion_projection.theta - (motion_table.num_angle_quantization / 2);
508508
if (angle < 0) {

nav2_smac_planner/test/test_nodelattice.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,22 @@ TEST(NodeLatticeTest, test_node_lattice_neighbors_and_parsing)
120120
nav2_smac_planner::MotionModel::STATE_LATTICE, x, y, angle_quantization, info);
121121

122122
nav2_smac_planner::NodeLattice aNode(0);
123+
unsigned int direction_change_index = 0;
123124
aNode.setPose(nav2_smac_planner::NodeHybrid::Coordinates(0, 0, 0));
124125
nav2_smac_planner::MotionPrimitivePtrs projections =
125-
nav2_smac_planner::NodeLattice::motion_table.getMotionPrimitives(&aNode);
126+
nav2_smac_planner::NodeLattice::motion_table.getMotionPrimitives(
127+
&aNode,
128+
direction_change_index);
126129

127130
EXPECT_NEAR(projections[0]->poses.back()._x, 0.5, 0.01);
128131
EXPECT_NEAR(projections[0]->poses.back()._y, -0.35, 0.01);
129132
EXPECT_NEAR(projections[0]->poses.back()._theta, 5.176, 0.01);
130133

131134
EXPECT_NEAR(
132135
nav2_smac_planner::NodeLattice::motion_table.getLatticeMetadata(
133-
filePath).grid_resolution, 0.05, 0.005);
136+
filePath)
137+
.grid_resolution,
138+
0.05, 0.005);
134139
}
135140

136141
TEST(NodeLatticeTest, test_node_lattice_conversions)
@@ -248,7 +253,6 @@ TEST(NodeLatticeTest, test_node_lattice)
248253
delete costmapA;
249254
}
250255

251-
252256
TEST(NodeLatticeTest, test_get_neighbors)
253257
{
254258
auto lnode = std::make_shared<rclcpp_lifecycle::LifecycleNode>("test");
@@ -356,9 +360,12 @@ TEST(NodeLatticeTest, test_node_lattice_custom_footprint)
356360
node.pose.x = 20;
357361
node.pose.y = 20;
358362
node.pose.theta = 0;
363+
364+
// initialize direction change index
365+
unsigned int direction_change_index = 0;
359366
// Test that the node is valid though all motion primitives poses for custom footprint
360367
nav2_smac_planner::MotionPrimitivePtrs motion_primitives =
361-
nav2_smac_planner::NodeLattice::motion_table.getMotionPrimitives(&node);
368+
nav2_smac_planner::NodeLattice::motion_table.getMotionPrimitives(&node, direction_change_index);
362369
EXPECT_GT(motion_primitives.size(), 0u);
363370
for (unsigned int i = 0; i < motion_primitives.size(); i++) {
364371
EXPECT_EQ(node.isNodeValid(true, checker.get(), motion_primitives[i], false), true);

0 commit comments

Comments
 (0)