MoveIt2-humble】入门教程----第一个 C++ MoveIt 程序

devtools/2024/10/18 12:24:14/

四节教程会手把手带你写一个完整的 Moveit 控制程序,包括轨迹规划、RViz可视化、添加碰撞物体、抓取和放置。

1 创建依赖包

进入到教程所在工作空间下的src目录,创建一个新的依赖包。

ros2 pkg create \--build-type ament_cmake \--dependencies moveit_ros_planning_interface rclcpp \--node-name hello_moveit hello_moveit

在新建的这个包中添加hello_moveit.cpp,准备开始编码

2 创建ROS节点和执行器
#include <memory>#include <rclcpp/rclcpp.hpp>
#include <moveit/move_group_interface/move_group_interface.h>int main(int argc, char * argv[])
{// 初始化 ROS 并创建节点rclcpp::init(argc, argv);auto const node = std::make_shared<rclcpp::Node>("hello_moveit",rclcpp::NodeOptions().automatically_declare_parameters_from_overrides(true));// 创建一个 ROS loggerauto const logger = rclcpp::get_logger("hello_moveit");// Next step goes here// 创建 MoveIt 的 MoveGroup 接口
using moveit::planning_interface::MoveGroupInterface;
auto move_group_interface = MoveGroupInterface(node, "panda_arm");// 设置目标位姿
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);// 创建一个到目标位姿的规划
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);
}();// 计算这个规划
if(success) {move_group_interface.execute(plan);
} else {RCLCPP_ERROR(logger, "Planing failed!");
}// 关闭 ROSrclcpp::shutdown();return 0;
}

 

2.1 编译和运行

我们可以尝试编译并运行一下,看看有无出现问题。

2.2 代码说明

最上面的是标准C++头文件,随后是为使用 ROS 和 Moveit 所添加的头文件。

在这之后,我们初始化 rclcpp,并创建了节点。

auto const node = std::make_shared<rclcpp::Node>("hello_moveit",rclcpp::NodeOptions().automatically_declare_parameters_from_overrides(true)
);

第一个参数是节点名称。第二个参数对于 Moveit 很重要,因为我们要使用 ROS 参数。

最后,关闭这个节点。

3 使用MoveGroupInterface来规划和执行

在代码中“Next step goes here,”的后面添加以下节点:

// 创建 MoveIt 的 MoveGroup 接口
using moveit::planning_interface::MoveGroupInterface;
auto move_group_interface = MoveGroupInterface(node, "panda_arm");

// 设置目标位姿
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);

// 创建一个到目标位姿的规划
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);
}();

// 计算这个规划
if(success) {
  move_group_interface.execute(plan);
} else {
  RCLCPP_ERROR(logger, "Planing failed!");
}

3.1 编译和运行

打开教程中的launch文件去启动rviz和 MoveGroup 节点,在另一个终端中source这个工作空间并执行:

ros2 launch moveit2_tutorials demo.launch.py

然后在Displays窗口下面的MotionPlanning/Planning Request中,取消选择Query Goal State

在第三个终端中 source 并允许我们本节的程序:

ros2 run hello_moveit hello_moveit

注:

如果你没有运行launch文件就运行了 hello_moveit 节点,等待10s后将会报如下错误:

[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 代码说明

首先要创建 MoveGroupInterface,这个对象用来与 move_group 交互,从而是我们能够去规划和执行轨迹。注意这是我们在程序中创建的唯一的可变对象。

其次是我们创建的 MoveGroupInterface 的第二个接口 "panda_arm",这是在机器人描述中定义的关节组,我们将通过 MoveGroupInterface 去操作它。

using moveit::planning_interface::MoveGroupInterface;
auto move_group_interface = MoveGroupInterface(node, "panda_arm");

之后,设置目标位姿和规划。起始点位姿通过 joint state publisher 来发布,它能够使用MoveGroupInterface::setStartState*中的函数来被改变(本教程无)

通过使用 lambda 表达式来构建信息类型 target_pose与规划。

// 利用 lambda 函数来创建目标位姿
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);// 利用 lambda 函数来创建到目标位姿的规划
// 注:这里使用了 C++ 20标准的新特性
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!");
}


http://www.ppmy.cn/devtools/124234.html

相关文章

nginx 资料整理(二)- HTTP

nginx 资料整理&#xff08;二&#xff09; 1. HTTP1. HTTP是什么&#xff1f;2. 什么是超文本&#xff1f;3. 协议版本概述4. 协议版本详解HTTP/0.9HTTP/1.0HTTP/1.1HTTPSSPDYHTTP/2.0 2. HTTP请求与响应1. HTTP请求1. 请求起始行2. 请求头(Request Headers)3. 空行(Blank Lin…

成都睿明智科技有限公司抖音电商新蓝海的领航者

在当今这个数字化浪潮汹涌的时代&#xff0c;电商行业正以惊人的速度迭代升级&#xff0c;而抖音电商作为新兴势力&#xff0c;更是凭借其庞大的用户基数、精准的算法推荐和高度互动的社区氛围&#xff0c;成为了众多商家竞相追逐的蓝海市场。在这片充满机遇与挑战的海洋中&…

网优学习干货:王者荣耀游戏用户体验洞察及质差识别(1)

一、课题背景 二、课题目的 针对热点游戏&#xff08;王者荣耀&#xff09;进行业务质量评估&#xff0c;并通过对端到端定界分析&#xff0c;从无线、核心网、互联网维度识别影响用户体验关键因素&#xff0c;为游戏用户的体验优化提供依据。 三、课题实施进度 王者荣耀卡顿特…

C++并发编程实战—单例模式与线程池实现

线程池 C线程池是一种用于管理和复用线程的机制&#xff0c;它可以提高程序的性能和效率&#xff0c;特别是在处理大量并发任务时。以下是C线程池的具体细节&#xff1a; 一、定义与功能 定义&#xff1a;线程池是一种设计模式&#xff0c;它预先创建并维护一定数量的线程&a…

Java基础(下)

泛型 Java 泛型&#xff08;Generics&#xff09; 是 JDK 5 中引入的一个新特性。使用泛型参数&#xff0c;可以增强代码的可读性以及稳定性。 编译器可以对泛型参数进行检测&#xff0c;并且通过泛型参数可以指定传入的对象类型 ArrayList<Person> persons new Arra…

k8s为什么用Calico

‌Calico是一种开源的网络和安全解决方案&#xff0c;主要用于容器、虚拟机、宿主机之间的网络连接。‌ 它支持Kubernetes、OpenShift、Docker EE、OpenStack等PaaS或IaaS平台&#xff0c;提供高效的网络通信和安全控制功能‌12。 Calico的核心组件包括Felix、etcd、BIRD等。F…

Label Studio 半自动化标注

引言 Label Studio ML 后端是一个 SDK,用于包装您的机器学习代码并将其转换为 Web 服务器。Web 服务器可以连接到正在运行的 Label Studio 实例,以自动执行标记任务。我们提供了一个示例模型库,您可以在自己的工作流程中使用这些模型,也可以根据需要进行扩展和自定义。 1…

【golang】gorm 使用map实现in 条件查询用法

当 where 字典的值为数组时 gorm 会自动转换为条件 IN 查询 where : map[string]interface{}{} where["id"] [1,2,3] where["name"] "zhangsan"type userList struct {Id int "gorm:id"Name string "gorm:name" } Table.…