moveit_task_constructor: ComputeIK fails when any collision objects are in the scene.
I’ve got a tool swapping function using moveit task constructor:
bool swap_tool(std::string tool)
{
std::shared_ptr<moveit::task_constructor::solvers::CartesianPath>
cartesian_planner(
new moveit::task_constructor::solvers::CartesianPath());
cartesian_planner->setMaxVelocityScalingFactor(1.0);
cartesian_planner->setMaxAccelerationScalingFactor(1.0);
cartesian_planner->setStepSize(.01);
cartesian_planner->setIKFrame(this->get_parameter("link_name").as_string());
std::shared_ptr<moveit::task_constructor::solvers::PipelinePlanner>
sampling_planner(new moveit::task_constructor::solvers::PipelinePlanner(
std::make_shared<rclcpp::Node>(this->get_name())));
sampling_planner->setProperty("goal_joint_tolerance", 1e-5);
sampling_planner->setMaxVelocityScalingFactor(1.0);
sampling_planner->setMaxAccelerationScalingFactor(1.0);
moveit::task_constructor::TaskPtr task(
new moveit::task_constructor::Task());
task->stages()->setName("tool change");
task->loadRobotModel(std::make_shared<rclcpp::Node>(this->get_name()));
task->setProperty("group", this->get_parameter("group_name").as_string());
task->setProperty("eef", this->get_parameter("link_name").as_string());
task->setProperty("ik_frame", this->get_parameter("link_name").as_string());
std::unique_ptr<moveit::task_constructor::stages::CurrentState>
current_state(new moveit::task_constructor::stages::CurrentState(
"current state"));
moveit::task_constructor::Stage *monitor_ptr = current_state.get();
geometry_msgs::msg::Vector3Stamped vec;
vec.header.frame_id = this->get_parameter("link_name").as_string();
double tool_approach_offset =
this->get_parameter("tool_approach_offset").as_double();
tf2::Quaternion orientation;
orientation.setRPY(0.0f, M_PI, M_PI);
std::vector<double> tool_box_origin =
this->get_parameter("tool_box_origin").as_double_array();
std::unique_ptr<moveit::task_constructor::stages::MoveRelative>
tool_approach(new moveit::task_constructor::stages::MoveRelative(
"approach tool", cartesian_planner));
vec.vector.z = tool_approach_offset;
tool_approach->setIKFrame(this->get_parameter("link_name").as_string());
tool_approach->setForwardedProperties({"ik_frame"});
tool_approach->setDirection(vec);
tool_approach->properties().configureInitFrom(
moveit::task_constructor::Stage::PARENT);
tool_approach->setMinMaxDistance(tool_approach_offset,
tool_approach_offset + 0.05);
std::unique_ptr<moveit::task_constructor::stages::MoveRelative>
tool_retract(new moveit::task_constructor::stages::MoveRelative(
"retract tool", cartesian_planner));
vec.vector.z = -tool_approach_offset;
tool_retract->setIKFrame(this->get_parameter("link_name").as_string());
tool_retract->setForwardedProperties({"ik_frame"});
tool_retract->setDirection(vec);
tool_retract->properties().configureInitFrom(
moveit::task_constructor::Stage::PARENT);
tool_retract->setMinMaxDistance(tool_approach_offset - 0.05,
tool_approach_offset);
std::unique_ptr<moveit::task_constructor::stages::GeneratePose>
tool_location(new moveit::task_constructor::stages::GeneratePose(
"tool location"));
geometry_msgs::msg::PoseStamped pose;
pose.header.frame_id = "world";
geometry_msgs::msg::Point tool_origin =
(!active_tool_.empty()) ? tools_.at(active_tool_).tool_origin
: tools_.at(tool).tool_origin;
pose.pose.position.x = tool_box_origin[0] + tool_origin.x;
pose.pose.position.y = tool_box_origin[1] + tool_origin.y;
pose.pose.position.z = tool_box_origin[2] + tool_origin.z;
pose.pose.orientation = tf2::toMsg(orientation);
tool_location->setPose(pose);
tool_location->setMonitoredStage(monitor_ptr);
std::unique_ptr<moveit::task_constructor::stages::ComputeIK>
tool_location_ik(new moveit::task_constructor::stages::ComputeIK(
"tool location IK", std::move(tool_location)));
tool_location_ik->setMaxIKSolutions(8);
tool_location_ik->setMinSolutionDistance(1.0);
tool_location_ik->setIKFrame(Eigen::Isometry3d().Identity(),
this->get_parameter("link_name").as_string());
tool_location_ik->properties().configureInitFrom(
moveit::task_constructor::Stage::PARENT, {"eef", "group"});
tool_location_ik->properties().configureInitFrom(
moveit::task_constructor::Stage::INTERFACE, {"target_pose"});
std::unique_ptr<moveit::task_constructor::stages::Connect>
location_connector(new moveit::task_constructor::stages::Connect(
"move to tool",
moveit::task_constructor::stages::Connect::GroupPlannerVector{
{this->get_parameter("group_name").as_string(),
sampling_planner}}));
location_connector->setTimeout(5.0);
location_connector->properties().configureInitFrom(
moveit::task_constructor::Stage::PARENT);
task->add(std::move(current_state));
task->add(std::move(location_connector));
task->add(std::move(tool_approach));
task->add(std::move(tool_location_ik));
if (!plan_and_execute_task(task))
{
// result->success = false;
// goal_handle->abort(result);
return false;
}
auto msg = std_msgs::msg::Bool();
msg.data = (!active_tool_.empty()) ? false : true;
this->chuck_magnet_switch_->publish(msg);
rclcpp::Clock().sleep_for(rclcpp::Duration(5, 0));
moveit_task_constructor_msgs::msg::Solution solution;
task->solutions().front()->toMsg(solution, &task->introspection());
std::unique_ptr<moveit::task_constructor::stages::FixedState> end_state(
new moveit::task_constructor::stages::FixedState("end state"));
planning_scene::PlanningScenePtr current_plan(
new planning_scene::PlanningScene(task->getRobotModel()));
current_plan->setCurrentState(solution.start_scene.robot_state);
end_state->setState(current_plan);
if (!active_tool_.empty() && !tool.empty())
{ // Pick new tool up.
RCLCPP_INFO(this->get_logger(), "pick new tool up.");
task->clear();
current_state.reset(
new moveit::task_constructor::stages::CurrentState("current state"));
monitor_ptr = current_state.get();
std::unique_ptr<moveit::task_constructor::stages::Connect>
pick_place_connector(new moveit::task_constructor::stages::Connect(
"move to next tool",
moveit::task_constructor::stages::Connect::GroupPlannerVector{
{this->get_parameter("group_name").as_string(),
cartesian_planner}}));
pick_place_connector->setTimeout(5.0);
pick_place_connector->properties().configureInitFrom(
moveit::task_constructor::Stage::PARENT);
tool_location.reset(
new moveit::task_constructor::stages::GeneratePose("tool location"));
geometry_msgs::msg::PoseStamped pose;
pose.header.frame_id = "world";
geometry_msgs::msg::Point tool_origin = tools_.at(tool).tool_origin;
pose.pose.position.x = tool_box_origin[0] + tool_origin.x;
pose.pose.position.y = tool_box_origin[1] + tool_origin.y;
pose.pose.position.z = tool_box_origin[2] + tool_origin.z;
pose.pose.orientation = tf2::toMsg(orientation);
tool_location->setMonitoredStage(monitor_ptr);
tool_location->setPose(pose);
tool_location_ik.reset(new moveit::task_constructor::stages::ComputeIK(
"tool location IK", std::move(tool_location)));
tool_location_ik->setMaxIKSolutions(8);
tool_location_ik->setMinSolutionDistance(1.0);
tool_location_ik->setIKFrame(
Eigen::Isometry3d().Identity(),
this->get_parameter("link_name").as_string());
tool_location_ik->properties().configureInitFrom(
moveit::task_constructor::Stage::PARENT, {"eef", "group"});
tool_location_ik->properties().configureInitFrom(
moveit::task_constructor::Stage::INTERFACE, {"target_pose"});
tool_approach.reset(new moveit::task_constructor::stages::MoveRelative(
"approach tool", cartesian_planner));
vec.vector.z = tool_approach_offset;
tool_approach->setIKFrame(this->get_parameter("link_name").as_string());
tool_approach->setForwardedProperties({"ik_frame"});
tool_approach->setDirection(vec);
tool_approach->properties().configureInitFrom(
moveit::task_constructor::Stage::PARENT);
tool_approach->setMinMaxDistance(tool_approach_offset,
tool_approach_offset + 0.05);
task->add(std::move(current_state));
task->add(std::move(tool_retract));
task->add(std::move(pick_place_connector));
task->add(std::move(tool_approach));
task->add(std::move(tool_location_ik));
if (!plan_and_execute_task(task))
{
// result->success = false;
// goal_handle->abort(result);
return false;
}
auto msg = std_msgs::msg::Bool();
msg.data = true;
this->chuck_magnet_switch_->publish(msg);
rclcpp::Clock().sleep_for(rclcpp::Duration(5, 0));
tool_retract.reset(new moveit::task_constructor::stages::MoveRelative(
"retract tool", cartesian_planner));
vec.vector.z = -tool_approach_offset;
tool_retract->setIKFrame(this->get_parameter("link_name").as_string());
tool_retract->setForwardedProperties({"ik_frame"});
tool_retract->setDirection(vec);
tool_retract->properties().configureInitFrom(
moveit::task_constructor::Stage::PARENT);
tool_retract->setMinMaxDistance(tool_approach_offset - 0.05,
tool_approach_offset);
}
task->clear();
current_state.reset(
new moveit::task_constructor::stages::CurrentState("current state"));
monitor_ptr = current_state.get();
std::unique_ptr<moveit::task_constructor::stages::Connect> end_connector(
new moveit::task_constructor::stages::Connect(
"move to end",
moveit::task_constructor::stages::Connect::GroupPlannerVector{
{this->get_parameter("group_name").as_string(),
sampling_planner}}));
end_connector->setTimeout(5.0);
end_connector->properties().configureInitFrom(
moveit::task_constructor::Stage::PARENT);
task->add(std::move(current_state));
task->add(std::move(tool_retract));
task->add(std::move(end_connector));
task->add(std::move(end_state));
if (!plan_and_execute_task(task))
{
return false;
}
active_tool_ = tool;
vaccuum_gripper_ = this->create_publisher<std_msgs::msg::Bool>(
"/tools/" + tool + "/" + tool + "_gripper/turn_on", 10);
return true;
}
It works perfectly when I have no collision objects in the planning scene. However, I need the arm to avoid colliding with static objects in the scene when performing the tool change.
But when I add an object into the planning scene using the following in the class constructor:
rclcpp::Publisher<moveit_msgs::msg::PlanningScene>::SharedPtr
planning_scene_diff_publisher =
this->create_publisher<moveit_msgs::msg::PlanningScene>(
"/planning_scene", 1);
moveit_msgs::msg::PlanningScene planning_scene;
planning_scene.is_diff = true;
moveit_msgs::msg::CollisionObject boundaries;
boundaries.header.frame_id = "world";
boundaries.id = "boundary";
boundaries.operation = boundaries.ADD;
shape_msgs::msg::SolidPrimitive rail_box;
geometry_msgs::msg::Pose rail_box_pose;
rail_box_pose.position.x = -1.355 / 2;
rail_box_pose.position.y = 0.640 / 2;
rail_box_pose.position.z = 0.082 / 2;
rail_box_pose.orientation.x = 0.0;
rail_box_pose.orientation.y = 0.0;
rail_box_pose.orientation.z = 0.0;
rail_box_pose.orientation.w = 1.0;
rail_box.type = rail_box.BOX;
rail_box.dimensions.resize(3);
rail_box.dimensions[0] = 1.355;
rail_box.dimensions[1] = 0.640;
rail_box.dimensions[2] = 0.01;
boundaries.primitives.push_back(rail_box);
boundaries.primitive_poses.push_back(rail_box_pose);
planning_scene.world.collision_objects.push_back(boundaries);
planning_scene_diff_publisher->publish(planning_scene);
ComputeIK fails every time, regardless of where the collision object is, even if it’s nowhere near the end effector or the arm and no IK plan could possibly collide with it. Here’s the relevant logs:
[robot_arm_core-17] 0 - ← 0 → - 0 / tool change
[robot_arm_core-17] 1 - ← 1 → - 1 / current state
[robot_arm_core-17] - 1 → 0 ← 0 - / move to tool
[robot_arm_core-17] 0 - ← 0 ← 0 - / approach tool
[robot_arm_core-17] 0 - ← 0 → - 0 / tool location IK
[robot_arm_core-17] 1 - ← 1 → - 1 / tool location
[robot_arm_core-17] Failing stage(s):
[robot_arm_core-17] tool location IK (0/1)
[robot_arm_core-17] [ERROR] [1738260711.215896384] [robot_arm_core]: Task constructor plan failed: PLANNING_FAILED
Thanks!
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (10 by maintainers)
I think I’ve fixed the problem, it seems to be an issue with the
end_statestage, which is aFixedStatethat captures theCurrentStateat the start, to allow the robot to return to the position it started from. When creating the stage aPlanningSceneis created fromtask->getRobotModel()which doesn’t include the collision objects:The solution in my case was to add the
worldargument to the constructor ofPlanningScenefrom aPlanningSceneMonitorI was already using for something else:This doesn’t feel like the best way to do things for me, perhaps there is a more elegant solution. It would be nice if there was a function to create a
FixedStatethat is a “capture” of aCurrentStateso the robot can return to this saved state later on. Perhaps as an additional constructor. I’ll look into it when I have time. Or maybe there’s already a better solution I don’t know about. Thanks @rhaschke and @DaniGarciaLopez for your help, I know it was a struggle at times and apologies for not being as on the ball as I should have been.@ralphieraccoon: Looking at https://github.com/moveit/moveit_task_constructor/issues/650#issuecomment-2626000813, a possible reason for your failure might be incompatible states feeding into both ends of the
Connectstage. These are silently filtered out. You may enable debug-level logging and see some warnings on the command-line.@DaniGarciaLopez, thanks for offering help. I already implemented the change quickly: #651.