88MoveItErrorCode
restateInNewFrame(
const std::shared_ptr<tf2_ros::Buffer>& tf,
const std::string& target_frame,
89 const std::string& source_frame, geometry_msgs::msg::Point* translation,
90 geometry_msgs::msg::Quaternion* rotation,
const tf2::TimePoint& lookup_time)
92 if (target_frame == source_frame)
94 return MoveItErrorCode::SUCCESS;
96 if (translation ==
nullptr && rotation ==
nullptr)
98 return MoveItErrorCode::SUCCESS;
102 geometry_msgs::msg::TransformStamped transform;
105 transform = tf->lookupTransform(target_frame, source_frame, lookup_time);
107 catch (tf2::TransformException& ex)
109 std::stringstream ss;
110 ss <<
"Could not get transform for " << source_frame <<
" to " << target_frame <<
": " << ex.what();
115 if (translation !=
nullptr)
117 translation->x += transform.transform.translation.x;
118 translation->y += transform.transform.translation.y;
119 translation->z += transform.transform.translation.z;
123 if (rotation !=
nullptr)
125 tf2::Quaternion input_quat(rotation->x, rotation->y, rotation->z, rotation->w);
126 tf2::Quaternion transform_quat(transform.transform.rotation.x, transform.transform.rotation.y,
127 transform.transform.rotation.z, transform.transform.rotation.w);
129 input_quat.normalize();
130 transform_quat.normalize();
132 auto final_quat = input_quat * transform_quat;
133 final_quat.normalize();
135 rotation->x = final_quat.getX();
136 rotation->y = final_quat.getY();
137 rotation->z = final_quat.getZ();
138 rotation->w = final_quat.getW();
141 return moveit::core::MoveItErrorCode::SUCCESS;
213 const MoveGroupInterface&
move_group,
double match_tolerance,
214 const std::string& reference_frame_id,
const std::string& prefix)
216 const std::shared_ptr<tf2_ros::Buffer> tf_buffer =
move_group.getTF();
220 bool emit_position_constraint_warning =
false;
221 for (
const auto& constraint : constraints)
223 for (
const auto& position_constraint : constraint.position_constraints)
225 if (!position_constraint.constraint_region.primitives.empty())
227 emit_position_constraint_warning =
true;
231 if (emit_position_constraint_warning)
236 if (emit_position_constraint_warning)
238 RCLCPP_WARN_STREAM(getLogger(),
"Ignoring " << prefix <<
".position_constraints.constraint_region: Not supported.");
241 bool emit_visibility_constraint_warning =
false;
242 for (
const auto& constraint : constraints)
244 if (!constraint.visibility_constraints.empty())
246 emit_visibility_constraint_warning =
true;
250 if (emit_visibility_constraint_warning)
252 RCLCPP_WARN_STREAM(getLogger(),
"Ignoring " << prefix <<
".visibility_constraints: Not supported.");
257 size_t constraint_idx = 0;
258 for (
auto& constraint : constraints)
265 std::string constraint_prefix = prefix +
"_" + std::to_string(constraint_idx++);
268 size_t joint_idx = 0;
269 for (
const auto& joint_constraint : constraint.joint_constraints)
271 std::string query_name = constraint_prefix +
".joint_constraints_" + std::to_string(joint_idx++);
272 query.append(query_name +
".joint_name", joint_constraint.joint_name);
274 query.appendGTE(query_name +
".tolerance_above", joint_constraint.tolerance_above);
275 query.appendLTE(query_name +
".tolerance_below", joint_constraint.tolerance_below);
279 if (!constraint.position_constraints.empty())
282 query.append(constraint_prefix +
".position_constraints.header.frame_id", reference_frame_id);
284 size_t position_idx = 0;
285 for (
const auto& position_constraint : constraint.position_constraints)
287 std::string query_name = constraint_prefix +
".position_constraints_" + std::to_string(position_idx++);
289 geometry_msgs::msg::Point canonical_position;
290 canonical_position.x = position_constraint.target_point_offset.x;
291 canonical_position.y = position_constraint.target_point_offset.y;
292 canonical_position.z = position_constraint.target_point_offset.z;
295 if (position_constraint.header.frame_id != reference_frame_id)
297 if (MoveItErrorCode status =
299 &canonical_position,
nullptr, tf2::TimePointZero);
300 status != MoveItErrorCode::SUCCESS)
305 std::stringstream ss;
306 ss <<
"Skipping " << prefix <<
":" << query_name <<
" query append: " << status.message;
307 return MoveItErrorCode(status.val, status.message);
311 query.append(query_name +
".link_name", position_constraint.link_name);
322 if (!constraint.orientation_constraints.empty())
325 query.append(constraint_prefix +
".orientation_constraints.header.frame_id", reference_frame_id);
328 for (
const auto& orientation_constraint : constraint.orientation_constraints)
330 std::string query_name = constraint_prefix +
".orientation_constraints_" + std::to_string(ori_idx++);
331 geometry_msgs::msg::Quaternion canonical_orientation = orientation_constraint.orientation;
334 if (orientation_constraint.header.frame_id != reference_frame_id)
336 if (MoveItErrorCode status =
338 nullptr, &canonical_orientation, tf2::TimePointZero);
339 status != MoveItErrorCode::SUCCESS)
344 std::stringstream ss;
345 ss <<
"Skipping " << prefix <<
":" << query_name <<
" query append: " << status.message;
346 return MoveItErrorCode(status.val, status.message);
350 query.append(query_name +
".link_name", orientation_constraint.link_name);
359 return moveit::core::MoveItErrorCode::SUCCESS;
363 std::vector<moveit_msgs::msg::Constraints> constraints,
365 const std::string& workspace_frame_id,
366 const std::string& prefix)
368 const std::shared_ptr<tf2_ros::Buffer> tf_buffer =
move_group.getTF();
372 bool emit_position_constraint_warning =
false;
373 for (
const auto& constraint : constraints)
375 for (
const auto& position_constraint : constraint.position_constraints)
377 if (!position_constraint.constraint_region.primitives.empty())
379 emit_position_constraint_warning =
true;
383 if (emit_position_constraint_warning)
388 if (emit_position_constraint_warning)
390 RCLCPP_WARN_STREAM(getLogger(),
"Ignoring " << prefix <<
".position_constraints.constraint_region: Not supported.");
393 bool emit_visibility_constraint_warning =
false;
394 for (
const auto& constraint : constraints)
396 if (!constraint.visibility_constraints.empty())
398 emit_visibility_constraint_warning =
true;
402 if (emit_visibility_constraint_warning)
404 RCLCPP_WARN_STREAM(getLogger(),
"Ignoring " << prefix <<
".visibility_constraints: Not supported.");
409 size_t constraint_idx = 0;
410 for (
auto& constraint : constraints)
417 std::string constraint_prefix = prefix +
"_" + std::to_string(constraint_idx++);
420 size_t joint_idx = 0;
421 for (
const auto& joint_constraint : constraint.joint_constraints)
423 std::string meta_name = constraint_prefix +
".joint_constraints_" + std::to_string(joint_idx++);
424 metadata.append(meta_name +
".joint_name", joint_constraint.joint_name);
425 metadata.append(meta_name +
".position", joint_constraint.position);
426 metadata.append(meta_name +
".tolerance_above", joint_constraint.tolerance_above);
427 metadata.append(meta_name +
".tolerance_below", joint_constraint.tolerance_below);
431 if (!constraint.position_constraints.empty())
434 metadata.append(constraint_prefix +
".position_constraints.header.frame_id", workspace_frame_id);
436 size_t position_idx = 0;
437 for (
const auto& position_constraint : constraint.position_constraints)
439 std::string meta_name = constraint_prefix +
".position_constraints_" + std::to_string(position_idx++);
441 geometry_msgs::msg::Point canonical_position;
442 canonical_position.x = position_constraint.target_point_offset.x;
443 canonical_position.y = position_constraint.target_point_offset.y;
444 canonical_position.z = position_constraint.target_point_offset.z;
447 if (position_constraint.header.frame_id != workspace_frame_id)
449 if (MoveItErrorCode status =
451 &canonical_position,
nullptr, tf2::TimePointZero);
452 status != MoveItErrorCode::SUCCESS)
457 std::stringstream ss;
458 ss <<
"Skipping " << prefix <<
":" << meta_name <<
" metadata append: " << status.message;
459 return MoveItErrorCode(status.val, status.message);
463 metadata.append(meta_name +
".link_name", position_constraint.link_name);
464 metadata.append(meta_name +
".target_point_offset.x", canonical_position.x);
465 metadata.append(meta_name +
".target_point_offset.y", canonical_position.y);
466 metadata.append(meta_name +
".target_point_offset.z", canonical_position.z);
471 if (!constraint.orientation_constraints.empty())
474 metadata.append(constraint_prefix +
".orientation_constraints.header.frame_id", workspace_frame_id);
477 for (
const auto& orientation_constraint : constraint.orientation_constraints)
479 std::string meta_name = constraint_prefix +
".orientation_constraints_" + std::to_string(ori_idx++);
480 geometry_msgs::msg::Quaternion canonical_orientation = orientation_constraint.orientation;
483 if (orientation_constraint.header.frame_id != workspace_frame_id)
485 if (MoveItErrorCode status =
487 nullptr, &canonical_orientation, tf2::TimePointZero);
488 status != MoveItErrorCode::SUCCESS)
493 std::stringstream ss;
494 ss <<
"Skipping " << prefix <<
":" << meta_name <<
" metadata append: " << status.message;
495 return MoveItErrorCode(status.val, status.message);
499 metadata.append(meta_name +
".link_name", orientation_constraint.link_name);
500 metadata.append(meta_name +
".orientation.x", canonical_orientation.x);
501 metadata.append(meta_name +
".orientation.y", canonical_orientation.y);
502 metadata.append(meta_name +
".orientation.z", canonical_orientation.z);
503 metadata.append(meta_name +
".orientation.w", canonical_orientation.w);
508 return moveit::core::MoveItErrorCode::SUCCESS;
515 const MoveGroupInterface&
move_group,
double match_tolerance,
516 const std::string& prefix)
520 if (!robot_state.multi_dof_joint_state.joint_names.empty())
522 RCLCPP_WARN_STREAM(getLogger(),
"Ignoring " << prefix <<
".multi_dof_joint_states: Not supported.");
524 if (!robot_state.attached_collision_objects.empty())
526 RCLCPP_WARN_STREAM(getLogger(),
"Ignoring " << prefix <<
".attached_collision_objects: Not supported.");
531 if (robot_state.is_diff)
547 moveit::core::RobotStatePtr current_state =
move_group.getCurrentState();
553 std::stringstream ss;
554 ss <<
"Skipping " << prefix <<
" query append: "
555 <<
"Could not get robot state.";
556 return MoveItErrorCode(MoveItErrorCode::UNABLE_TO_AQUIRE_SENSOR_DATA, ss.str());
559 moveit_msgs::msg::RobotState current_state_msg;
560 robotStateToRobotStateMsg(*current_state, current_state_msg);
562 for (
size_t i = 0; i < current_state_msg.joint_state.name.size(); ++i)
564 query.append(prefix +
".joint_state.name_" + std::to_string(i), current_state_msg.joint_state.name.at(i));
566 current_state_msg.joint_state.position.at(i), match_tolerance);
571 for (
size_t i = 0; i < robot_state.joint_state.name.size(); ++i)
573 query.append(prefix +
".joint_state.name_" + std::to_string(i), robot_state.joint_state.name.at(i));
575 robot_state.joint_state.position.at(i), match_tolerance);
579 return moveit::core::MoveItErrorCode::SUCCESS;
584 const MoveGroupInterface&
move_group,
const std::string& prefix)
588 if (!robot_state.multi_dof_joint_state.joint_names.empty())
590 RCLCPP_WARN_STREAM(getLogger(),
"Ignoring " << prefix <<
".multi_dof_joint_states: Not supported.");
592 if (!robot_state.attached_collision_objects.empty())
594 RCLCPP_WARN_STREAM(getLogger(),
"Ignoring " << prefix <<
".attached_collision_objects: Not supported.");
599 if (robot_state.is_diff)
615 moveit::core::RobotStatePtr current_state =
move_group.getCurrentState();
621 std::stringstream ss;
622 ss <<
"Skipping " << prefix <<
" metadata append: "
623 <<
"Could not get robot state.";
624 return MoveItErrorCode(MoveItErrorCode::UNABLE_TO_AQUIRE_SENSOR_DATA, ss.str());
627 moveit_msgs::msg::RobotState current_state_msg;
628 robotStateToRobotStateMsg(*current_state, current_state_msg);
630 for (
size_t i = 0; i < current_state_msg.joint_state.name.size(); ++i)
632 metadata.append(prefix +
".joint_state.name_" + std::to_string(i), current_state_msg.joint_state.name.at(i));
633 metadata.append(prefix +
".joint_state.position_" + std::to_string(i),
634 current_state_msg.joint_state.position.at(i));
639 for (
size_t i = 0; i < robot_state.joint_state.name.size(); ++i)
641 metadata.append(prefix +
".joint_state.name_" + std::to_string(i), robot_state.joint_state.name.at(i));
642 metadata.append(prefix +
".joint_state.position_" + std::to_string(i), robot_state.joint_state.position.at(i));
646 return moveit::core::MoveItErrorCode::SUCCESS;