用C++创建首个MoveIt程序
该教程面向已掌握ROS 2基础的开发者,演示创建ROS 2软件包并编写首个C++ MoveIt应用,借助MoveGroupInterface完成运动规划与执行,同时提醒完整Move Group功能还需补充参数。
## 您的第一个 C++ MoveIt 项目[](<https://moveit.picknik.ai/main/doc/tutorials/your_first_project/your_first_project.html#your-first-c-moveit-project>) 此教程会通过使用 MoveIt 写入您的第一个 C++ 应用程序来一步 。 警告: MoveIt 中的大多数功能都不会正常工作, 因为需要额外的参数来完成全移动组的功能。 对于一个完整的设置, 请继续 [移动组 C++ 界面教程](<https://moveit.picknik.ai/main/doc/examples/move_group_interface/move_group_interface_tutorial.html>). ## 先决条件[](<https://moveit.picknik.ai/main/doc/tutorials/your_first_project/your_first_project.html#prerequisites>) 若您还没有这样做, 请确保您已完成步骤 [开始](<https://moveit.picknik.ai/main/doc/tutorials/getting_started/getting_started.html>). 此教程假设您理解 ROS 2 的基本原理。 为了做好这个准备, 请完成 [官方 ROS 2 教学](<https://docs.ros.org/en/rolling/Tutorials.html>) 直至“写一个简单的出版商和订户(C++)”。 ## 步骤[](<https://moveit.picknik.ai/main/doc/tutorials/your_first_project/your_first_project.html#steps>) ### 1 创建软件包[](<https://moveit.picknik.ai/main/doc/tutorials/your_first_project/your_first_project.html#create-a-package>) 打开终端 [源代码 ROS 2 安装](<https://docs.ros.org/en/rolling/Tutorials/Configuring-ROS2-Environment.html>) 这样, `ros2` 命令会起作用的。 导航到您的 `ws_moveit` 您创建的目录 [开始教学](<https://moveit.picknik.ai/main/doc/tutorials/getting_started/getting_started.html>). 将目录更改为 `src` 编目,因为我们把源代码放在那里。 使用 ROS 2 命令行工具创建新软件包 : ``` ros2 pkg create \ --build-type ament_cmake \ --dependencies moveit_ros_planning_interface rclcpp \ --node-name hello_moveit hello_moveit ``` 此选项的输出将显示它在新目录中创建了一些文件 。 请注意,我们添加了 `moveit_ros_planning_interface` 财务报告和财务报告 `rclcpp` 这将对数据进行必要的修改。 `package.xml` 财务报告和财务报告 `CMakeLists.txt` 文档,这样我们就可以依赖这两个软件包。 打开您创建的新源文件 `ws_moveit/src/hello_moveit/src/hello_moveit.cpp` 在你最喜欢的编辑。 ### 2 创建 ROS 节点和执行器[](<https://moveit.picknik.ai/main/doc/tutorials/your_first_project/your_first_project.html#create-a-ros-node-and-executor>) 这第一块是锅炉板 但你应该习惯于从ROS 2教程中看到这个. ``` #include <memory> #include <rclcpp/rclcpp.hpp> #include <moveit/move_group_interface/move_group_interface.hpp> int main(int argc, char * argv[]) { // Initialize ROS and create the Node rclcpp::init(argc, argv); auto const node = std::make_shared<rclcpp::Node>( "hello_moveit", rclcpp::NodeOptions().automatically_declare_parameters_from_overrides(true) ); // Create a ROS logger auto const logger = rclcpp::get_logger("hello_moveit"); // Next step goes here // Shutdown ROS rclcpp::shutdown(); return 0; } ``` #### 2.1 构建和运行[](<https://moveit.picknik.ai/main/doc/tutorials/your_first_project/your_first_project.html#build-and-run>) 我们将建立和运行这个程序, 以确保一切是正确的之前,我们前进。 将目录更改为工作空间目录 `ws_moveit` 运行此命令 : ``` colcon build --mixin debug ``` 成功之后, **打开新终端**,然后将工作空间环境脚本来源于那个新的终端,这样我们就可以运行我们的程序。 ``` cd ~/ws_moveit source install/setup.bash ``` 运行您的程序并查看输出 。 ``` ros2 run hello_moveit hello_moveit ``` 程序应该运行和退出而不会出错. #### 2.2 检查代码[](<https://moveit.picknik.ai/main/doc/tutorials/your_first_project/your_first_project.html#examine-the-code>) 包含在顶部的标头只是一些标准的C++标头,以及我们稍后会使用的ROS和MoveIt标头. 之后,我们有正常的呼叫 初始化rclcpp, 然后我们创建我们的节点。 ``` auto const node = std::make_shared<rclcpp::Node>( "hello_moveit", rclcpp::NodeOptions().automatically_declare_parameters_from_overrides(true) ); ``` 第一个参数是一个字符串,ROS将使用它来命名一个独特的节点. 第二个是MoveIt,因为我们是如何使用ROS参数的. 接下来,我们 [创建日志](<https://docs.ros.org/en/humble/Tutorials/Demos/Logging-and-logger-configuration.html>) 名为“hello_moveit ”以保持我们的日志产出的组织和可配置性。 ``` // Create a ROS logger auto const logger = rclcpp::get_logger("hello_moveit"); ``` 最后,我们得到了关闭 ROS的代码。 ``` // Shutdown ROS rclcpp::shutdown(); return 0; ``` ### 3 计划与执行使用 MoveGroupInterface[](<https://moveit.picknik.ai/main/doc/tutorials/your_first_project/your_first_project.html#plan-and-execute-using-movegroupinterface>) 代替“下一步”的注释,加上这个代码: ``` // Create the MoveIt MoveGroup Interface using moveit::planning_interface::MoveGroupInterface; auto move_group_interface = MoveGroupInterface(node, "manipulator"); // Set a target Pose auto const target_pose = []{ geometry_msgs::msg::Pose msg; msg.orientation.w = 1.0; msg.position.x = 0.28; msg.position.y = -0.2; msg.position.z = 0.5; return msg; }(); move_group_interface.setPoseTarget(target_pose); // Create a plan to that target pose auto const [success, plan] = [&move_group_interface]{ moveit::planning_interface::MoveGroupInterface::Plan msg; auto const ok = static_cast<bool>(move_group_interface.plan(msg)); return std::make_pair(ok, msg); }(); // Execute the plan if(success) { move_group_interface.execute(plan); } else { RCLCPP_ERROR(logger, "Planning failed!"); } ``` #### 3.1 构建和运行[](<https://moveit.picknik.ai/main/doc/tutorials/your_first_project/your_first_project.html#id1>) 和以前一样,我们需要在运行之前先建立代码. 在工作空间目录中, `ws_moveit`,运行此命令 : ``` colcon build --mixin debug ``` 在此成功之后, 我们需要重新使用上一个教程中的演示启动文件来启动 RViz 和 MoveGroup 节点。 在单独的终端中, 源代码为工作空间, 然后执行 : ``` ros2 launch moveit2_tutorials demo.launch.py ``` 然后,在那个 `Displays` 窗口下方 `MotionPlanning/Planning Request`中,取消选中框 `Query Goal State`. [ 在第三个终端,源代码和运行程序。 ``` ros2 run hello_moveit hello_moveit ``` 这应该会让RViz的机器人移动到这个姿势中: [ 请注意,如果你运行节点 `hello_moveit` 在不首先启动演示发射文件的情况下,它会等待10秒,然后打印这个错误并退出. ``` [ERROR] [1644181704.350825487] [hello_moveit]: Could not find parameter robot_description and did not receive robot_description via std_msgs::msg::String subscription within 10.000000 seconds. ``` 这是因为 `demo.launch.py` 发射开始 `MoveGroup` 提供机器人描述的节点。 `MoveGroupInterface` 构建时,它会寻找一个节点,发布一个带有机器人描述的主题。如果它无法在 10 秒内找到,它会打印这个错误并终止程序。 #### 3.2 检查代码[](<https://moveit.picknik.ai/main/doc/tutorials/your_first_project/your_first_project.html#id2>) 我们做的第一件事就是创建 `MoveGroupInterface`。此对象将用来与 `move_group`,它允许我们计划并执行轨迹。请注意,这是我们在此程序中创建的唯一可变对象。另一个需要注意的是第二个参数。 `MoveGroupInterface` 在此创建对象 : `"manipulator"`这就是机器人描述中定义的关节组 我们要用这个操作 `MoveGroupInterface`. ``` using moveit::planning_interface::MoveGroupInterface; auto move_group_interface = MoveGroupInterface(node, "manipulator"); ``` 然后,我们设定了目标姿势和计划。请注意,只有目标姿势是设定的。 `setPoseTarget`开始的姿势是隐含的州联合出版商公布的立场,该立场可以使用 `MoveGroupInterface::setStartState*` 函数家族(但不在此教程中)。 下一节还有一件事需要注意 使用羊肉来构建信件类型 `target_pose` 和规划。这是现代 C++ 代码库中的一种模式,它能够以更公开的风格写作。关于此模式的更多信息,此教程结尾处有几条链接。 ``` // Set a target Pose auto const target_pose = []{ geometry_msgs::msg::Pose msg; msg.orientation.w = 1.0; msg.position.x = 0.28; msg.position.y = -0.2; msg.position.z = 0.5; return msg; }(); move_group_interface.setPoseTarget(target_pose); // Create a plan to that target pose auto const [success, plan] = [&move_group_interface]{ moveit::planning_interface::MoveGroupInterface::Plan msg; auto const ok = static_cast<bool>(move_group_interface.plan(msg)); return std::make_pair(ok, msg); }(); ``` 最后,如果规划成功,我们执行我们的计划,否则,我们记录一个错误: ``` // Execute the plan if(success) { move_group_interface.execute(plan); } else { RCLCPP_ERROR(logger, "Planning failed!"); } ``` ## 内容提要[](<https://moveit.picknik.ai/main/doc/tutorials/your_first_project/your_first_project.html#summary>) - 你创建了ROS 2软件包,并使用MoveIt写下了你的第一个程序. - 你学会了使用MoveGroupInterface来计划和执行动作. - [以下是本教程末尾的完整hello_moveit.cpp源的复制件.](<https://github.com/moveit/moveit2_tutorials/blob/main/doc/tutorials/your_first_project/kinova_hello_moveit.cpp>). ## 进一步阅读[](<https://moveit.picknik.ai/main/doc/tutorials/your_first_project/your_first_project.html#further-reading>) - 我们用羊肉来初始化物体作为常数。 这被称为一种叫做 IIFE 的技术 。 [更多从 C++ 故事读取此模式](<https://www.cppstories.com/2016/11/iife-for-complex-initialization/>). - 我们还宣布了我们能做的一切都是Const [更多关于康斯特的实用性](<https://www.cppstories.com/2016/12/please-declare-your-variables-as-const/>). ## 下一个步骤[](<https://moveit.picknik.ai/main/doc/tutorials/your_first_project/your_first_project.html#next-step>) 在下一个教程中 [在 RViz 可视化](<https://moveit.picknik.ai/main/doc/tutorials/visualizing_in_rviz/visualizing_in_rviz.html>),你会扩展你在这里构建的程序,以创建视觉标记,从而更容易理解MoveIt在做什么.
查看原始来源