在MoveIt规划场景中添加碰撞物体并绕行规划
该教程在hello_moveit项目基础上,演示如何向MoveIt规划场景插入碰撞物体,并让机械臂规划到新目标位姿时绕开该物体,涉及碰撞对象消息构建与规划场景接口的使用。
## 围绕对象进行规划[](<https://moveit.picknik.ai/main/doc/tutorials/planning_around_objects/planning_around_objects.html#planning-around-objects>) 这个教程会介绍您将对象插入到规划场景中,并围绕它们进行规划. ## 先决条件[](<https://moveit.picknik.ai/main/doc/tutorials/planning_around_objects/planning_around_objects.html#prerequisites>) 若您还没有这样做, 请确保您已完成步骤 [在 RViz 可视化](<https://moveit.picknik.ai/main/doc/tutorials/visualizing_in_rviz/visualizing_in_rviz.html>)。这个项目假设您要从 `hello_moveit` 选项,上一个教程从何处离开。如果您想要运行教程,可以跟随 [嵌入器指南](<https://moveit.picknik.ai/main/doc/how_to_guides/how_to_setup_docker_containers_in_ubuntu.html>) 以完成的教程启动容器。 ## 步骤[](<https://moveit.picknik.ai/main/doc/tutorials/planning_around_objects/planning_around_objects.html#steps>) ### 1 添加规划场景界面[](<https://moveit.picknik.ai/main/doc/tutorials/planning_around_objects/planning_around_objects.html#add-include-for-planning-scene-interface>) 在您的源文件的顶端, 将此添加到列表中 : ``` #include <moveit/planning_scene_interface/planning_scene_interface.hpp> ``` ### 2 改变目标Pose[](<https://moveit.picknik.ai/main/doc/tutorials/planning_around_objects/planning_around_objects.html#change-the-target-pose>) 首先,将目标姿势更新如下修改,使机器人计划到达不同位置: 1. ``` // Set a target Pose with updated values !!! auto const target_pose = [] { geometry_msgs::msg::Pose msg; msg.orientation.y = 0.8; msg.orientation.w = 0.6; msg.position.x = 0.1; msg.position.y = 0.4; msg.position.z = 0.4; return msg; }(); move_group_interface.setPoseTarget(target_pose); ``` ### 3 创建碰撞对象[](<https://moveit.picknik.ai/main/doc/tutorials/planning_around_objects/planning_around_objects.html#create-a-collision-object>) 在下一个代码块中,我们创建了一个碰撞对象。首先要注意的是它被放置在机器人的坐标框中。如果我们有一个感知系统来报告我们场景中障碍的位置,那么这就是它将构建的信息。因为这只是一个例子,所以我们正在手动创建它。在这个代码的结尾 需要注意的一件事是 我们把操作 这个消息到 `ADD`。这会导致物体被添加到碰撞场景中。将这个代码块放在设定目标从上一步起的姿势和创建计划之间。 ``` // Create collision object for the robot to avoid auto const collision_object = [frame_id = move_group_interface.getPlanningFrame()] { moveit_msgs::msg::CollisionObject collision_object; collision_object.header.frame_id = frame_id; collision_object.id = "box1"; shape_msgs::msg::SolidPrimitive primitive; // Define the size of the box in meters primitive.type = primitive.BOX; primitive.dimensions.resize(3); primitive.dimensions[primitive.BOX_X] = 0.5; primitive.dimensions[primitive.BOX_Y] = 0.1; primitive.dimensions[primitive.BOX_Z] = 0.5; // Define the pose of the box (relative to the frame_id) geometry_msgs::msg::Pose box_pose; box_pose.orientation.w = 1.0; // We can leave out the x, y, and z components of the quaternion since they are initialized to 0 box_pose.position.x = 0.2; box_pose.position.y = 0.2; box_pose.position.z = 0.25; collision_object.primitives.push_back(primitive); collision_object.primitive_poses.push_back(box_pose); collision_object.operation = collision_object.ADD; return collision_object; }(); ``` ### 4 将对象添加到规划场景[](<https://moveit.picknik.ai/main/doc/tutorials/planning_around_objects/planning_around_objects.html#add-the-object-to-the-planning-scene>) 最后,我们需要把这个物体添加到碰撞场景中。要做到这一点,我们使用一个叫做“碰撞场景”的物体。 `PlanningSceneInterface` 使用 ROS 接口向规划场景传达更改 `MoveGroup`。这个代码块应当直接跟随创建碰撞对象的代码块。 ``` // Add the collision object to the scene moveit::planning_interface::PlanningSceneInterface planning_scene_interface; planning_scene_interface.applyCollisionObject(collision_object); ``` ### 5 运行程序并观察变化[](<https://moveit.picknik.ai/main/doc/tutorials/planning_around_objects/planning_around_objects.html#run-the-program-and-observe-the-change>) 就像我们在上一个教程中所做的那样, 开始使用 RViz `demo.launch.py` 脚本并运行我们的程序。 如果您正在使用一个 Docker 教程容器, 您可以指定一个不同的 RViz 配置, 该配置已经添加了 RvizVisualToolsGui 面板, 使用 : ``` ros2 launch moveit2_tutorials demo.launch.py rviz_config:=kinova_hello_moveit.rviz ```  ## 内容提要[](<https://moveit.picknik.ai/main/doc/tutorials/planning_around_objects/planning_around_objects.html#summary>) - 你扩展了你和MoveIt一起写的程序,以规划在现场的某个物体. - [以下是完整hello_moveit.cpp源的复制件.](<https://github.com/moveit/moveit2_tutorials/blob/main/doc/tutorials/planning_around_objects/hello_moveit.cpp>). ## 进一步阅读[](<https://moveit.picknik.ai/main/doc/tutorials/planning_around_objects/planning_around_objects.html#further-reading>) - [利用规划场景进行碰撞和制约检查的例子](<https://moveit.picknik.ai/main/doc/examples/planning_scene/planning_scene_tutorial.html>). - [使用规划场景 ROS API的例子](<https://moveit.picknik.ai/main/doc/examples/planning_scene_ros_api/planning_scene_ros_api_tutorial.html>). - [可视化碰撞物体的例子](<https://moveit.picknik.ai/main/doc/examples/visualizing_collisions/visualizing_collisions_tutorial.html>). - [用于对对象进行规划的子框架示例](<https://moveit.picknik.ai/main/doc/examples/subframes/subframes_tutorial.html>). ## 下一个步骤[](<https://moveit.picknik.ai/main/doc/tutorials/planning_around_objects/planning_around_objects.html#next-step>) 在下一个教程中 [用 MoveIt 任务构建器选择和放置](<https://moveit.picknik.ai/main/doc/tutorials/pick_and_place_with_moveit_task_constructor/pick_and_place_with_moveit_task_constructor.html>)中,您将被引入一个旨在解决更硬运动计划的更高层工具。在此下一个教程中,您将创建一个程序来选择和放置一个对象。
查看原始来源