Skip to content

Commit 32dcd9d

Browse files
MarqRazzmergify[bot]
authored andcommitted
Fix seg fault with attached objects during motion execution (#3466)
(cherry picked from commit c81ac91)
1 parent 89506ab commit 32dcd9d

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

moveit_core/robot_state/src/robot_state.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -1217,14 +1217,24 @@ void RobotState::getAttachedBodies(std::vector<const AttachedBody*>& attached_bo
12171217
attached_bodies.clear();
12181218
attached_bodies.reserve(attached_body_map_.size());
12191219
for (const auto& it : attached_body_map_)
1220-
attached_bodies.push_back(it.second.get());
1220+
{
1221+
if (it.second)
1222+
{
1223+
attached_bodies.push_back(it.second.get());
1224+
}
1225+
}
12211226
}
12221227

12231228
void RobotState::getAttachedBodies(std::map<std::string, const AttachedBody*>& attached_bodies) const
12241229
{
12251230
attached_bodies.clear();
12261231
for (const auto& it : attached_body_map_)
1227-
attached_bodies[it.first] = it.second.get();
1232+
{
1233+
if (it.second && !it.first.empty())
1234+
{
1235+
attached_bodies[it.first] = it.second.get();
1236+
}
1237+
}
12281238
}
12291239

12301240
void RobotState::getAttachedBodies(std::vector<const AttachedBody*>& attached_bodies, const JointModelGroup* group) const

moveit_ros/planning/plan_execution/src/plan_execution.cpp

+19-15
Original file line numberDiff line numberDiff line change
@@ -283,17 +283,20 @@ 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-
moveit::core::RobotState state = plan.planning_scene->getCurrentState();
286+
moveit::core::RobotState start_state = plan.planning_scene->getCurrentState();
287287
std::map<std::string, const moveit::core::AttachedBody*> current_attached_objects, waypoint_attached_objects;
288-
state.getAttachedBodies(current_attached_objects);
289-
waypoint_attached_objects = plan_components_attached_objects_[path_segment.first];
288+
start_state.getAttachedBodies(current_attached_objects);
289+
if (plan_components_attached_objects_.size() > static_cast<size_t>(path_segment.first))
290+
waypoint_attached_objects = plan_components_attached_objects_[path_segment.first];
291+
moveit::core::RobotState waypoint_state(start_state);
290292
for (std::size_t i = std::max(path_segment.second - 1, 0); i < wpc; ++i)
291293
{
292294
collision_detection::CollisionResult res;
293-
state = t.getWayPoint(i);
295+
waypoint_attached_objects.clear(); // clear out the last waypoints attached objects
296+
waypoint_state = t.getWayPoint(i);
294297
if (plan_components_attached_objects_[path_segment.first].empty())
295298
{
296-
state.getAttachedBodies(waypoint_attached_objects);
299+
waypoint_state.getAttachedBodies(waypoint_attached_objects);
297300
}
298301

299302
// If sample state has attached objects that are not in the current state, remove them from the sample state
@@ -302,7 +305,7 @@ bool plan_execution::PlanExecution::isRemainingPathValid(const ExecutableMotionP
302305
if (current_attached_objects.find(name) == current_attached_objects.end())
303306
{
304307
RCLCPP_DEBUG(logger_, "Attached object '%s' is not in the current scene. Removing it.", name.c_str());
305-
state.clearAttachedBody(name);
308+
waypoint_state.clearAttachedBody(name);
306309
}
307310
}
308311

@@ -312,35 +315,35 @@ bool plan_execution::PlanExecution::isRemainingPathValid(const ExecutableMotionP
312315
if (waypoint_attached_objects.find(name) == waypoint_attached_objects.end())
313316
{
314317
RCLCPP_DEBUG(logger_, "Attached object '%s' is not in the robot state. Adding it.", name.c_str());
315-
state.attachBody(std::make_unique<moveit::core::AttachedBody>(*object));
318+
waypoint_state.attachBody(std::make_unique<moveit::core::AttachedBody>(*object));
316319
}
317320
}
318321

319322
if (acm)
320323
{
321-
plan.planning_scene->checkCollision(req, res, state, *acm);
324+
plan.planning_scene->checkCollision(req, res, waypoint_state, *acm);
322325
}
323326
else
324327
{
325-
plan.planning_scene->checkCollision(req, res, state);
328+
plan.planning_scene->checkCollision(req, res, waypoint_state);
326329
}
327330

328-
if (res.collision || !plan.planning_scene->isStateFeasible(state, false))
331+
if (res.collision || !plan.planning_scene->isStateFeasible(waypoint_state, false))
329332
{
330333
RCLCPP_INFO(logger_, "Trajectory component '%s' is invalid for waypoint %ld out of %ld",
331334
plan.plan_components[path_segment.first].description.c_str(), i, wpc);
332335

333336
// call the same functions again, in verbose mode, to show what issues have been detected
334-
plan.planning_scene->isStateFeasible(state, true);
337+
plan.planning_scene->isStateFeasible(waypoint_state, true);
335338
req.verbose = true;
336339
res.clear();
337340
if (acm)
338341
{
339-
plan.planning_scene->checkCollision(req, res, state, *acm);
342+
plan.planning_scene->checkCollision(req, res, waypoint_state, *acm);
340343
}
341344
else
342345
{
343-
plan.planning_scene->checkCollision(req, res, state);
346+
plan.planning_scene->checkCollision(req, res, waypoint_state);
344347
}
345348
return false;
346349
}
@@ -469,7 +472,7 @@ moveit_msgs::msg::MoveItErrorCodes plan_execution::PlanExecution::executeAndMoni
469472
{
470473
const auto& trajectory = component.trajectory;
471474
std::map<std::string, const moveit::core::AttachedBody*> trajectory_attached_objects;
472-
if (trajectory)
475+
if (trajectory && trajectory->getWayPointCount() > 0)
473476
{
474477
std::map<std::string, const moveit::core::AttachedBody*> attached_objects;
475478
trajectory->getWayPoint(0).getAttachedBodies(trajectory_attached_objects);
@@ -483,7 +486,8 @@ moveit_msgs::msg::MoveItErrorCodes plan_execution::PlanExecution::executeAndMoni
483486
}
484487
}
485488
}
486-
plan_components_attached_objects_.push_back(trajectory_attached_objects);
489+
if (!trajectory_attached_objects.empty())
490+
plan_components_attached_objects_.push_back(trajectory_attached_objects);
487491
}
488492

489493
while (rclcpp::ok() && !execution_complete_ && !path_became_invalid_)

0 commit comments

Comments
 (0)