Skip to content

Commit c7e2031

Browse files
committed
fix: cleanup code using lamda function
1 parent ce9b1d2 commit c7e2031

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

moveit_ros/planning/plan_execution/src/plan_execution.cpp

+18-16
Original file line numberDiff line numberDiff line change
@@ -283,39 +283,41 @@ bool plan_execution::PlanExecution::isRemainingPathValid(const ExecutableMotionP
283283
collision_detection::CollisionRequest req;
284284
req.group_name = t.getGroupName();
285285
req.pad_environment_collisions = false;
286-
std::vector<const moveit::core::AttachedBody*> attached_bodies;
287-
plan.planning_scene->getCurrentState().getAttachedBodies(attached_bodies);
288-
std::map<std::string, const moveit::core::AttachedBody*> current_attached_objects;
289-
for (const auto& ab : attached_bodies)
290-
{
291-
current_attached_objects[ab->getName()] = ab;
292-
}
286+
auto getAttachedObjects = [](const moveit::core::RobotState& state) {
287+
std::vector<const moveit::core::AttachedBody*> attached_bodies;
288+
state.getAttachedBodies(attached_bodies);
289+
std::map<std::string, const moveit::core::AttachedBody*> attached_objects;
290+
for (const auto& ab : attached_bodies)
291+
{
292+
attached_objects[ab->getName()] = ab;
293+
}
294+
return attached_objects;
295+
};
296+
293297
moveit::core::RobotState state = plan.planning_scene->getCurrentState();
298+
std::map<std::string, const moveit::core::AttachedBody*> current_attached_objects = getAttachedObjects(state);
294299
for (std::size_t i = std::max(path_segment.second - 1, 0); i < wpc; ++i)
295300
{
296301
state = t.getWayPoint(i);
297302
collision_detection::CollisionResult res;
298-
state.getAttachedBodies(attached_bodies);
299-
std::map<std::string, const moveit::core::AttachedBody*> sample_attached_object;
300-
for (const auto& ab : attached_bodies)
301-
{
302-
sample_attached_object[ab->getName()] = ab;
303-
}
303+
std::map<std::string, const moveit::core::AttachedBody*> sample_attached_object = getAttachedObjects(state);
304+
305+
// If sample state has attached objects that are not in the current state, remove them from the sample state
304306
for (const auto& [name, object] : sample_attached_object)
305307
{
306308
if (current_attached_objects.find(name) == current_attached_objects.end())
307309
{
308-
RCLCPP_INFO(logger_, "Attached object '%s' is not in the current scene. Removing from robot state",
309-
name.c_str());
310+
RCLCPP_DEBUG(logger_, "Attached object '%s' is not in the current scene. Removing it.", name.c_str());
310311
state.clearAttachedBody(name);
311312
}
312313
}
313314

315+
// If current state has attached objects that are not in the sample state, add them to the sample state
314316
for (const auto& [name, object] : current_attached_objects)
315317
{
316318
if (sample_attached_object.find(name) == sample_attached_object.end())
317319
{
318-
RCLCPP_INFO(logger_, "Attached object '%s' is not in the robot state. Adding it.", name.c_str());
320+
RCLCPP_DEBUG(logger_, "Attached object '%s' is not in the robot state. Adding it.", name.c_str());
319321
state.attachBody(std::make_unique<moveit::core::AttachedBody>(*object));
320322
}
321323
}

0 commit comments

Comments
 (0)