MoveIt! 学习笔记1- MoveGroup C++ Interface

此博文主要是用来记录ROS-Kinetic 中,用于机器人轨迹规划的MoveIt功能包的学习记录。 

英文原版教程见此链接:http://docs.ros.org/en/kinetic/api/moveit_tutorials/html/doc/move_group_interface/move_group_interface_tutorial.html

 引: MoveIt是基于MoveGroup这个类,MoveIt提供了一个相对简单的方式,令操作人员可以较为容易的操作机器人。 操作人员仅需发送各个关节的指定角度或者TCP的目标位置,即可控制机器人执行指令,移动到位。 MoveIt是通过ROS内部的Topic/Service和Action机制,向MoveGroup的节点发送指令。

主要内容:在上面的链接教程中,主要涉及到了C++的MoveIt!的接口。

                  以及如何创建规划组Move_Group,如何创建关节类型/目标点类型的轨迹,并使用Moveit自带规划器进行轨迹规划。 并且在RVIZ中显示出来规划完成的轨迹。

官方教程主要以代码实例为主,所以,在下边的代码中,主要通过注释的方式,解释代码含义,通过代码实例,学习MoveIt内部内容。

/*********************************************************************
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2013, SRI International
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of SRI International nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *********************************************************************/

/* Author: Sachin Chitta, Dave Coleman, Mike Lautman */

#include <moveit/move_group_interface/move_group_interface.h>
#include <moveit/planning_scene_interface/planning_scene_interface.h>

#include <moveit_msgs/DisplayRobotState.h>
#include <moveit_msgs/DisplayTrajectory.h>

#include <moveit_msgs/AttachedCollisionObject.h>
#include <moveit_msgs/CollisionObject.h>

#include <moveit_visual_tools/moveit_visual_tools.h>

int main(int argc, char** argv)
{
  ros::init(argc, argv, "move_group_interface_tutorial");
  ros::NodeHandle node_handle;
  ros::AsyncSpinner spinner(1);
  spinner.start();

  // BEGIN_TUTORIAL
  //
  // Setup
  // ^^^^^
  //
  // MoveIt! operates on sets of joints called "planning groups" and stores them in an object called
  // the `JointModelGroup`. Throughout MoveIt! the terms "planning group" and "joint model group"
  // are used interchangably.设定运动规划组名称
  static const std::string PLANNING_GROUP = "panda_arm";

  // The :move_group_interface:`MoveGroup` class can be easily
  // setup using just the name of the planning group you would like to control and plan for.
  //初始化运动规划组,并将前面设置的名称输入进来
  moveit::planning_interface::MoveGroupInterface move_group(PLANNING_GROUP);

  // We will use the :planning_scene_interface:`PlanningSceneInterface`
  // class to add and remove collision objects in our "virtual world" scene
  // 创建一个对象,用于添加和移除在仿真环境中的‘碰撞元件’(用来进行避障的轨迹仿真)
  moveit::planning_interface::PlanningSceneInterface planning_scene_interface;

  // Raw pointers are frequently used to refer to the planning group for improved performance.
  // 创建一个指针类型变量,用
要通过 **MoveIt!** 控制 **TR-Arm(一个机械臂)** 实现路径规划,你需要完成一系列 ROS(Robot Operating System)相关的配置和编程工作。下面我将详细说明整个流程,并提供示例代码。 --- ## ✅ 系统要求 - Ubuntu 20.04 / 22.04 - ROS Noetic / ROS2 Humble(本例以 ROS Noetic 为例) - MoveIt! - TR-Arm URDF 模型 - Gazebo(可选用于仿真) --- ## ✅ 步骤概览 1. 创建或获取 TR-Arm 的 URDF 模型 2. 使用 MoveIt Setup Assistant 配置 MoveIt 包 3. 启动 MoveIt 和 RViz 进行可视化 4. 编写 ROS 节点实现路径规划 --- ## ✅ Step-by-step 操作指南 ### 📌 Step 1: 获取或创建 TR-Arm 的 URDF 模型 确保你有一个完整的 URDF 文件描述 TR-Arm,例如 `tr_arm.urdf` 或 `tr_arm.xacro`。通常结构如下: ``` tr_arm_description/ ├── urdf/ │ └── tr_arm.urdf ├── meshes/ └── launch/ └── display.launch ``` 你可以使用 `xacro` 宏文件来简化建模。 --- ### 📌 Step 2: 使用 MoveIt Setup Assistant 配置 MoveIt 包 ```bash sudo apt-get install ros-noetic-moveit-setup-assistant ``` 运行助手并导入你的 URDF 模型: ```bash roslaunch moveit_setup_assistant setup_assistant.launch ``` 在 GUI 中完成以下步骤: 1. 导入 URDF 模型 2. 自动识别运动链(Kinematic Chain) 3. 设置虚拟关节(如 base_link -> world) 4. 添加自碰撞矩阵(Self-Collisions) 5. 生成配置包 最终会生成一个名为 `tr_arm_moveit_config` 的包。 --- ### 📌 Step 3: 启动 MoveIt 并进行路径规划测试 #### 启动 MoveIt 与 RViz: ```bash roslaunch tr_arm_moveit_config demo.launch ``` 你会看到 RViz 显示 TR-Arm 模型,并可以手动拖动目标位姿进行路径规划。 --- ### 📌 Step 4: 编写 ROS 节点进行自动路径规划 以下是使用 MoveIt C++ API 控制 TR-Arm 实现路径规划的示例代码: #### 示例代码:`plan_arm_motion.cpp` ```cpp #include <moveit/move_group_interface/move_group_interface.h> #include <moveit/planning_scene_interface/planning_scene_interface.h> #include <moveit_msgs/DisplayRobotState.h> #include <moveit_msgs/DisplayTrajectory.h> #include <moveit_msgs/AttachedCollisionObject.h> #include <moveit_msgs/CollisionObject.h> #include <moveit_visual_tools/moveit_visual_tools.h> int main(int argc, char** argv) { ros::init(argc, argv, "tr_arm_move_group"); ros::NodeHandle node_handle; ros::AsyncSpinner spinner(1); spinner.start(); // 设置规划组名称,需与 SRDF 中一致 static const std::string PLANNING_GROUP = "arm"; // 创建 MoveGroupInterface 对象 moveit::planning_interface::MoveGroupInterface move_group(PLANNING_GROUP); // 获取当前位姿 geometry_msgs::Pose target_pose = move_group.getCurrentPose().pose; // 设置目标位姿(示例中只改变 z 坐标) target_pose.position.z += 0.1; // 设置目标位姿 move_group.setPoseTarget(target_pose); // 调用规划器 moveit::planning_interface::MoveGroupInterface::Plan my_plan; bool success = (move_group.plan(my_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS); ROS_INFO("Visualizing plan %s", success ? "SUCCEEDED" : "FAILED"); if (success) { // 执行轨迹 move_group.execute(my_plan); } ros::shutdown(); return 0; } ``` #### 在 `CMakeLists.txt` 中添加编译规则: ```cmake add_executable(plan_arm_motion src/plan_arm_motion.cpp) target_link_libraries(plan_arm_motion ${catkin_LIBRARIES}) ``` #### 编译并运行: ```bash catkin_make source devel/setup.bash rosrun your_package_name plan_arm_motion ``` --- ## ✅ 注意事项 - 确保 TR-Arm 的关节名、规划组名与 MoveIt 配置一致。 - 如果你在真实硬件上控制,请将 MoveIt 与控制器接口连接(如 `controller_manager`)。 - 可以通过 `Rviz` 设置目标姿态,然后调用 `getCurrentPose()` 获取目标值。 - 如需避障,可在 `planning_scene` 中添加障碍物。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值