当前位置: 首页 > news >正文

学习笔记+ZY_75843《机器人操作系统(ROS2)入门与实践 》_刘相权等+记录

ros2 humble    @  ubuntu22.04   

VScode

 

第3章  ROS2编程基础

3.1 节点与软件包

3.1.1 创建workspace 然后package

ubuntu@ubuntu-Lenovo-XiaoXin-14API-2019:~$ mkdir -p ros2_ws_cha3
ubuntu@ubuntu-Lenovo-XiaoXin-14API-2019:~$ mkdir -p ros2_ws_cha3/src
ubuntu@ubuntu-Lenovo-XiaoXin-14API-2019:~$ cd  ~/ros2_ws-cha3/src
ubuntu@ubuntu-Lenovo-XiaoXin-14API-2019:~/ros2_ws_cha3/src$ ros2 pkg create my_pkg
going to create a new package
package name: my_pkg
destination directory: /home/ubuntu/ros2_ws_cha3/src
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['ubuntu <ubuntu@todo.todo>']
licenses: ['TODO: License declaration']
build type: ament_cmake
dependencies: []
creating folder ./my_pkg
creating ./my_pkg/package.xml
creating source and include folder
creating folder ./my_pkg/src
creating folder ./my_pkg/include/my_pkg
creating ./my_pkg/CMakeLists.txt[WARNING]: Unknown license 'TODO: License declaration'.  This has been set in the package.xml, but no LICENSE file has been created.
It is recommended to use one of the ament license identitifers:
Apache-2.0
BSL-1.0
BSD-2.0
BSD-2-Clause
BSD-3-Clause
GPL-3.0-only
LGPL-3.0-only
MIT
MIT-0

在VScode中打开工作空间 ros2_ws_cha3。在VScode文件树中观察工作空间中的文件结构。

image

 

3.1.2 编写node

3.1.2.1 编写节点代码
// 包含ROS 2 C++客户端库的头文件
#include "rclcpp/rclcpp.hpp"int main(int argc, char * argv[])
{// 初始化ROS 2系统// 处理命令行参数并设置必要的通信环境
  rclcpp::init(argc, argv);// 创建一个共享指针指向ROS 2节点对象// std::make_shared<rclcpp::Node>("my_node") 创建了一个名为"my_node"的节点// auto关键字自动推导节点类型为 std::shared_ptr<rclcpp::Node>auto node = std::make_shared<rclcpp::Node>("my_node");// 使用ROS 2的日志系统输出信息// RCLCPP_INFO宏用于输出INFO级别的日志// node->get_logger() 获取节点的日志记录器// "Hello world!" 是要输出的信息RCLCPP_INFO(node->get_logger(), "Hello world!");// 空循环,保持程序运行// rclcpp::ok() 检查ROS 2系统是否正常运行// 当收到SIGINT信号(如Ctrl+C)或调用rclcpp::shutdown()时返回falsewhile (rclcpp::ok()){;  // 空语句,循环体什么都不做
  }// 关闭ROS 2系统// 清理资源,终止通信
  rclcpp::shutdown();// 程序正常退出return 0;
}
first_node.cpp
ubuntu@ubuntu-Lenovo-XiaoXin-14API-2019:~/ros2_ws_cha3$ colcon build
Starting >>> my_pkg
Finished <<< my_pkg [6.39s]
 
Summary: 1 package finished [6.85s]
ubuntu@ubuntu-Lenovo-XiaoXin-14API-2019:~/ros2_ws_cha3$ source install/setup.bash
ubuntu@ubuntu-Lenovo-XiaoXin-14API-2019:~/ros2_ws_cha3$ ros2 run my_pkg first_node
[INFO] [1773564269.835462423] [my_node]: Hello world!

image

 3.1.2.2设置编译规则

修改CMakeLists.txt

cmake_minimum_required(VERSION 3.8)
project(my_pkg)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)
add_executable(first_node src/first_node.cpp)
ament_target_dependencies(first_node "rclcpp")
install(TARGETSfirst_nodeDESTINATION lib/${PROJECT_NAME})
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()

 3.1.2.3 修改软件包信息

修改package.xml

<?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>my_pkg</name><version>0.0.0</version><description>TODO: Package description</description><maintainer email="ubuntu@todo.todo">ubuntu</maintainer><license>TODO: License declaration</license><buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend> <test_depend>ament_lint_auto</test_depend><test_depend>ament_lint_common</test_depend><export><build_type>ament_cmake</build_type></export>
</package>

 3.1.2.4 编译软件包

ubuntu@ubuntu-Lenovo-XiaoXin-14API-2019:~/ros2_ws_cha3$ colcon build
Starting >>> my_pkg  
Finished <<< my_pkg [6.39s]                   

Summary: 1 package finished [6.85s]

3.1.3 运行节点

ubuntu@ubuntu-Lenovo-XiaoXin-14API-2019:~/ros2_ws_cha3$ source install/setup.bash
ubuntu@ubuntu-Lenovo-XiaoXin-14API-2019:~/ros2_ws_cha3$ ros2 run my_pkg first_node
[INFO] [1773564269.835462423] [my_node]: Hello world!

 

http://www.jsqmd.com/news/481814/

相关文章:

  • 告别学术焦虑:百考通AI,覆盖从“降AI痕迹”到“降重复率”的全场景需求
  • 网络流 学习笔记(施工中)
  • 守住学术原创底线!百考通AIGC检测,筑牢学术原创防线,为论文合规性保驾护航
  • 直接上结论:10个AI论文网站测评!继续教育毕业论文写作必备工具推荐
  • 百考通AI:让文献综述从繁琐的体力劳动,转变为高效的学术洞察过程
  • LongFact:评估LLM长文本事实性的基准测试
  • 稳压泵实力厂家2026年新动态,一文速览,排污泵/恒压变频供水设备/消防泵/消防水箱/玻璃钢水箱,稳压泵公司有哪些 - 品牌推荐师
  • 百考通精准贴合不同学历层次的学术需求,实现了从选题到成文的全流程赋能
  • cpp的模块配置
  • EasyCPP2
  • 关于HTML5的一些基础认知
  • 深圳宝珀维修、上海朗格保养、南京积家检修|6城高端腕表维修科普指南 - 时光修表匠
  • 阅读进度管理程序,设定目标自动计算每日页数,提醒打卡,提高读完率,不半途而废。
  • 北京格拉苏蒂维修、杭州雅克德罗保养、无锡法穆兰检修|6城高端腕表维修科普指南 - 时光修表匠
  • 台州宠物腹腔镜绝育:这些医院值得一试,异宠/宠物眼科/宠物腹腔镜绝育/狗狗体检/宠物内科/宠物骨科,宠物绝育医生选哪家 - 品牌推荐师
  • QQ机器人接入OpenClaw完整指南:从零开始打造你的智能助手
  • KDT 小记
  • 杭州宝玑维修、无锡帝舵保养、北京朗格检修|6城高端腕表维修科普指南 - 时光修表匠
  • [20260313]深入探究max_idle_time(21c).txt
  • java+vue+SpringBoot校园外卖服务系统(程序+数据库+报告+部署教程+答辩指导)
  • java+vue+SpringBoot学生用品采购系统(程序+数据库+报告+部署教程+答辩指导)
  • java+vue+SpringBoot火车票订票系统(程序+数据库+报告+部署教程+答辩指导)
  • [20260309]关于db_file_multiblock_read_count参数疑问3.txt
  • ABC449
  • 图形学:重心坐标与纹理渲染核心技术解析
  • [20260310]理解db file parallel read等待事件与异步IO.txt
  • 无根仪式:当AI时代的时间加速膨胀
  • [20260308]关于db_file_multiblock_read_count参数疑问1.txt
  • 本月市场口碑好的篷布生产厂家排行,不容错过,市面上篷布甄选实力品牌 - 品牌推荐师
  • 2FSK-RRC处理随机信号——GNU radio