Robot Operating System——创建可执行文件项目的步骤

2024-08-21 06:28

本文主要是介绍Robot Operating System——创建可执行文件项目的步骤,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

大纲

  • 初始化环境
  • 创建Package
  • 代码
  • 添加依赖(package.xml)
  • 修改编译描述
    • find_package寻找依赖库
    • 指定代码路径和编译类型(可执行文件/动态库)
    • 链接依赖的库
    • 完整文件
  • 编译
  • 测试
  • 总结
  • 参考资料

之前我们看到ROS2中,有的Node的实现逻辑存在于动态库中,而动态库又有隐式加载和手动加载等几种模式。这些例子都比较复杂。本文将介绍如何从0到1创建一个最简单的工程,其Node都在可执行文件中,以保证过程的清晰。

初始化环境

在《Robot Operating System——深度解析自动隐式加载动态库的运行模式》一文中,我们展现了ROS2可执行文件的链接指令。可以看到它依赖了很多ROS2环境相关的动态库,所以我们在创建工程之前也要初始化环境。

source /opt/ros/jazzy/setup.bash

关于环境的安装可以参见《Robot Operating System——Ubuntu上以二进制形式安装环境》。

创建Package

ros2 pkg create --build-type ament_cmake --license Apache-2.0 two_node_pipeline

在这里插入图片描述
可以看到ros2帮我们创建好了相关的目录结构
在这里插入图片描述
剩下的事就是我们要填充这个工程。

代码

在two_node_pipeline/src目录下新建一个main.cpp文件。
代码我们直接借用https://github.com/ros2/demos/blob/jazzy/intra_process_demo/src/cyclic_pipeline/cyclic_pipeline.cpp的源码。它直接在一个可执行文件中编译了两个Node。

// Copyright 2015 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.#include <chrono>
#include <cinttypes>
#include <cstdio>
#include <memory>
#include <string>
#include <utility>#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/int32.hpp"using namespace std::chrono_literals;// Node that produces messages.
struct Producer : public rclcpp::Node
{Producer(const std::string & name, const std::string & output): Node(name, rclcpp::NodeOptions().use_intra_process_comms(true)){// Create a publisher on the output topic.pub_ = this->create_publisher<std_msgs::msg::Int32>(output, 10);std::weak_ptr<std::remove_pointer<decltype(pub_.get())>::type> captured_pub = pub_;// Create a timer which publishes on the output topic at ~1Hz.auto callback = [captured_pub]() -> void {auto pub_ptr = captured_pub.lock();if (!pub_ptr) {return;}static int32_t count = 0;std_msgs::msg::Int32::UniquePtr msg(new std_msgs::msg::Int32());msg->data = count++;printf("Published message with value: %d, and address: 0x%" PRIXPTR "\n", msg->data,reinterpret_cast<std::uintptr_t>(msg.get()));pub_ptr->publish(std::move(msg));};timer_ = this->create_wall_timer(1s, callback);}rclcpp::Publisher<std_msgs::msg::Int32>::SharedPtr pub_;rclcpp::TimerBase::SharedPtr timer_;
};// Node that consumes messages.
struct Consumer : public rclcpp::Node
{Consumer(const std::string & name, const std::string & input): Node(name, rclcpp::NodeOptions().use_intra_process_comms(true)){// Create a subscription on the input topic which prints on receipt of new messages.sub_ = this->create_subscription<std_msgs::msg::Int32>(input,10,[](std_msgs::msg::Int32::UniquePtr msg) {printf(" Received message with value: %d, and address: 0x%" PRIXPTR "\n", msg->data,reinterpret_cast<std::uintptr_t>(msg.get()));});}rclcpp::Subscription<std_msgs::msg::Int32>::SharedPtr sub_;
};int main(int argc, char * argv[])
{setvbuf(stdout, NULL, _IONBF, BUFSIZ);rclcpp::init(argc, argv);rclcpp::executors::SingleThreadedExecutor executor;auto producer = std::make_shared<Producer>("producer", "number");auto consumer = std::make_shared<Consumer>("consumer", "number");executor.add_node(producer);executor.add_node(consumer);executor.spin();rclcpp::shutdown();return 0;
}

添加依赖(package.xml)

package.xml 是 ROS 2 包的元数据文件,定义了包的基本信息和依赖关系。

打开two_node_pipeline/package.xml,添加代码中include头文件(#include "rclcpp/rclcpp.hpp" #include "std_msgs/msg/int32.hpp")所依赖的其他库。

  <depend>rclcpp</depend><depend>std_msgs</depend>

完整文件如下

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3"><name>two_node_pipeline</name><version>0.0.0</version><description>TODO: Package description</description><maintainer email="f304646673@gmail.com">fangliang</maintainer><license>Apache-2.0</license><buildtool_depend>ament_cmake</buildtool_depend><depend>rclcpp</depend><depend>std_msgs</depend><test_depend>ament_lint_auto</test_depend><test_depend>ament_lint_common</test_depend><export><build_type>ament_cmake</build_type></export>
</package>

修改编译描述

find_package寻找依赖库

再打开two_node_pipeline/CMakeLists.txt中添加上述头文件(#include "rclcpp/rclcpp.hpp" #include "std_msgs/msg/int32.hpp")所依赖的库

find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

在 CMake 中,find_package 命令用于查找并加载外部包或库。它的主要作用如下:

  • 查找包:find_package 会在系统中查找指定的包或库,并加载其配置文件。这些配置文件通常包含包的路径、库文件、头文件等信息。

  • 设置变量:如果找到包,find_package 会设置一些变量,这些变量可以在后续的 CMake 脚本中使用。例如,<PackageName>_FOUND 变量会被设置为 TRUE,表示找到了包。

  • 导入目标:一些包会定义 CMake 导入目标(imported targets),这些目标可以直接在 target_link_libraries 等命令中使用。

指定代码路径和编译类型(可执行文件/动态库)

设定编译结果名称

set(EXECUTABLE_NAME "two_node_pipeline")

指定编译结果是可执行文件

# Collect all source files in this directory
file(GLOB SOURCES "src/*.cpp")# Add the executable target
add_executable(${EXECUTABLE_NAME} ${SOURCES})

链接依赖的库

最后将代码中用到的库(#include "rclcpp/rclcpp.hpp" #include "std_msgs/msg/int32.hpp")链接到可执行文件中。

ament_target_dependencies(${EXECUTABLE_NAME} rclcpp std_msgs)

完整文件

cmake_minimum_required(VERSION 3.8)
project(two_node_pipeline)if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")add_compile_options(-Wall -Wextra -Wpedantic)
endif()# find dependencies
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)set(EXECUTABLE_NAME "two_node_pipeline")# Collect all source files in this directory
file(GLOB SOURCES "src/*.cpp")# Add the executable target
add_executable(${EXECUTABLE_NAME} ${SOURCES})ament_target_dependencies(${EXECUTABLE_NAME} rclcpp std_msgs)if(BUILD_TESTING)find_package(ament_lint_auto REQUIRED)# the following line skips the linter which checks for copyrights# comment the line when a copyright and license is added to all source filesset(ament_cmake_copyright_FOUND TRUE)# the following line skips cpplint (only works in a git repo)# comment the line when this package is in a git repo and when# a copyright and license is added to all source filesset(ament_cmake_cpplint_FOUND TRUE)ament_lint_auto_find_test_dependencies()
endif()ament_package()

编译

cd two_node_pipeline/build
cmake ..
make

在这里插入图片描述

测试

./two_node_pipeline 

在这里插入图片描述

总结

  1. 初始化环境source /opt/ros/jazzy/setup.bash,以保证ROS2的基础动态库路径在系统PATH中。
  2. 通过`ros2 pkg create --build-type ament_cmake --license Apache-2.0 <YOUR-Folder-Name>'创建目录结构和基础文件。
  3. 填充代码。
  4. package.xml中添加代码中显式依赖的库。
  5. CMakeLists.txt中寻找和链接代码中显式依赖的库。

参考资料

  • https://docs.ros.org/en/rolling/Tutorials/Beginner-Client-Libraries/Creating-A-Workspace/Creating-A-Workspace.html#new-directory

这篇关于Robot Operating System——创建可执行文件项目的步骤的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1092378

相关文章

idea中创建新类时自动添加注释的实现

《idea中创建新类时自动添加注释的实现》在每次使用idea创建一个新类时,过了一段时间发现看不懂这个类是用来干嘛的,为了解决这个问题,我们可以设置在创建一个新类时自动添加注释,帮助我们理解这个类的用... 目录前言:详细操作:步骤一:点击上方的 文件(File),点击&nbmyHIgsp;设置(Setti

Spring Boot3虚拟线程的使用步骤详解

《SpringBoot3虚拟线程的使用步骤详解》虚拟线程是Java19中引入的一个新特性,旨在通过简化线程管理来提升应用程序的并发性能,:本文主要介绍SpringBoot3虚拟线程的使用步骤,... 目录问题根源分析解决方案验证验证实验实验1:未启用keep-alive实验2:启用keep-alive扩展建

tomcat多实例部署的项目实践

《tomcat多实例部署的项目实践》Tomcat多实例是指在一台设备上运行多个Tomcat服务,这些Tomcat相互独立,本文主要介绍了tomcat多实例部署的项目实践,具有一定的参考价值,感兴趣的可... 目录1.创建项目目录,测试文China编程件2js.创建实例的安装目录3.准备实例的配置文件4.编辑实例的

Python下载Pandas包的步骤

《Python下载Pandas包的步骤》:本文主要介绍Python下载Pandas包的步骤,在python中安装pandas库,我采取的方法是用PIP的方法在Python目标位置进行安装,本文给大... 目录安装步骤1、首先找到我们安装python的目录2、使用命令行到Python安装目录下3、我们回到Py

springboot集成Deepseek4j的项目实践

《springboot集成Deepseek4j的项目实践》本文主要介绍了springboot集成Deepseek4j的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录Deepseek4j快速开始Maven 依js赖基础配置基础使用示例1. 流式返回示例2. 进阶

SpringBoot项目启动报错"找不到或无法加载主类"的解决方法

《SpringBoot项目启动报错找不到或无法加载主类的解决方法》在使用IntelliJIDEA开发基于SpringBoot框架的Java程序时,可能会出现找不到或无法加载主类com.example.... 目录一、问题描述二、排查过程三、解决方案一、问题描述在使用 IntelliJ IDEA 开发基于

SpringBoot项目使用MDC给日志增加唯一标识的实现步骤

《SpringBoot项目使用MDC给日志增加唯一标识的实现步骤》本文介绍了如何在SpringBoot项目中使用MDC(MappedDiagnosticContext)为日志增加唯一标识,以便于日... 目录【Java】SpringBoot项目使用MDC给日志增加唯一标识,方便日志追踪1.日志效果2.实现步

Spring 中使用反射创建 Bean 实例的几种方式

《Spring中使用反射创建Bean实例的几种方式》文章介绍了在Spring框架中如何使用反射来创建Bean实例,包括使用Class.newInstance()、Constructor.newI... 目录1. 使用 Class.newInstance() (仅限无参构造函数):2. 使用 Construc

Linux系统中配置静态IP地址的详细步骤

《Linux系统中配置静态IP地址的详细步骤》本文详细介绍了在Linux系统中配置静态IP地址的五个步骤,包括打开终端、编辑网络配置文件、配置IP地址、保存并重启网络服务,这对于系统管理员和新手都极具... 目录步骤一:打开终端步骤二:编辑网络配置文件步骤三:配置静态IP地址步骤四:保存并关闭文件步骤五:重

Java导入、导出excel用法步骤保姆级教程(附封装好的工具类)

《Java导入、导出excel用法步骤保姆级教程(附封装好的工具类)》:本文主要介绍Java导入、导出excel的相关资料,讲解了使用Java和ApachePOI库将数据导出为Excel文件,包括... 目录前言一、引入Apache POI依赖二、用法&步骤2.1 创建Excel的元素2.3 样式和字体2.