环境:Ubuntu22.04,ROS2-humble
通过修改.yaml
配置文件中的参数,可以不用重新编译源代码进行软件调试。
1.yaml文件格式
bag_to_image_node
:运行的ROS2节点名称
参数格式参考如下:
bag_to_image_node:ros__parameters:greeting: "Hello"name: "BUDING DUODUO"ExposureTime: 8888
2.launch文件
- launch中要找到YAML文件,可以使用绝对路径
import launch
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, LogInfo
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Nodedef generate_launch_description():current_dir = os.getcwd() # 获取当前工作目录print(f"Current directory: {current_dir}") # 打印当前目录# 声明参数文件路径的启动参数return LaunchDescription([# 声明一个启动参数,指定参数文件的路径DeclareLaunchArgument('config_file',default_value='/home/boss-dog/001_test/bag_to_image/src/bag_to_image/config/params.yaml',description='Path to the YAML parameter file'),# 启动节点并加载参数文件Node(package='bag_to_image', # 你的包名executable='bag_to_image_node', # 你的可执行文件名name='bag_to_image_node', # 节点名称output='screen',parameters=[LaunchConfiguration('config_file')], # 加载指定的 YAML 配置文件),# 打印日志确认节点启动LogInfo(condition=launch.conditions.LaunchConfigurationEquals('config_file', ''),msg="Starting with default parameter file")])
- launch使用相对路径
import launch
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, LogInfo
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
import osdef generate_launch_description():current_dir = os.getcwd() # 获取当前工作目录print(f"Current directory: {current_dir}") # 打印当前目录# 拼接文件路径config_file_path = os.path.join(current_dir, 'install', 'bag_to_image', 'share', 'bag_to_image', 'config', 'params.yaml')# 声明参数文件路径的启动参数return LaunchDescription([# 启动节点并加载参数文件Node(package='bag_to_image', # 你的包名executable='bag_to_image_node', # 你的可执行文件名name='bag_to_image_node', # 节点名称output='screen',parameters=[config_file_path], # 加载指定的 YAML 配置文件),# 打印日志确认节点启动LogInfo(condition=launch.conditions.LaunchConfigurationEquals('config_file', ''),msg="Starting with default parameter file")])
3.CMakeList中需要将配置文件和launch文件拷贝到install下
# 复制launch文件
install(DIRECTORY launch/DESTINATION share/${PROJECT_NAME}/launch
)# Install config files
install(DIRECTORY config/DESTINATION share/${PROJECT_NAME}/config
)
4.源码
项目结构
bag_to_image
├── CMakeLists.txt
├── config
│ └── params.yaml
├── include
│ └── bag_to_image
├── launch
│ └── start_node.launch.py
├── package.xml
└── src└── bag_to_image_node.cpp
- CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(bag_to_image)if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")add_compile_options(-Wall -Wextra -Wpedantic)
endif()find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)add_executable(bag_to_image_node src/bag_to_image_node.cpp)target_include_directories(bag_to_image_nodePUBLIC ${PCL_INCLUDE_DIRS}
)# ROS2中指定节点或库的依赖项
ament_target_dependencies(bag_to_image_node rclcpp) install(TARGETSbag_to_image_nodeDESTINATION lib/${PROJECT_NAME})# 复制launch文件
install(DIRECTORY launch/DESTINATION share/${PROJECT_NAME}/launch
)# Install config files
install(DIRECTORY config/DESTINATION share/${PROJECT_NAME}/config
)if(BUILD_TESTING)find_package(ament_lint_auto REQUIRED)set(ament_cmake_copyright_FOUND TRUE)set(ament_cmake_cpplint_FOUND TRUE)ament_lint_auto_find_test_dependencies()
endif()ament_package()
- params.yaml
bag_to_image_node:ros__parameters:greeting: "Hello"name: "BUDING DUODUO"ExposureTime: 12345
- start_node.launch.py
import launch
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, LogInfo
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
import osdef generate_launch_description():current_dir = os.getcwd() # 获取当前工作目录print(f"Current directory: {current_dir}") # 打印当前目录# 拼接文件路径config_file_path = os.path.join(current_dir, 'install', 'bag_to_image', 'share', 'bag_to_image', 'config', 'params.yaml')# 声明参数文件路径的启动参数return LaunchDescription([# 启动节点并加载参数文件Node(package='bag_to_image', # 你的包名executable='bag_to_image_node', # 你的可执行文件名name='bag_to_image_node', # 节点名称output='screen',parameters=[config_file_path], # 加载指定的 YAML 配置文件),# 打印日志确认节点启动LogInfo(condition=launch.conditions.LaunchConfigurationEquals('config_file', ''),msg="Starting with default parameter file")])
- 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>bag_to_image</name><version>0.0.0</version><description>TODO: Package description</description><maintainer email="boss_dog@qq.cn">qq</maintainer><license>TODO: License declaration</license><buildtool_depend>ament_cmake</buildtool_depend><test_depend>ament_lint_auto</test_depend><test_depend>ament_lint_common</test_depend><export><build_type>ament_cmake</build_type></export>
</package>
- bag_to_image_node.cpp
#include <rclcpp/rclcpp.hpp>
#include <iostream>class ImageSaverNode : public rclcpp::Node
{
public:ImageSaverNode() : Node("image_saver_node"){std::cout << "node..." << std::endl;// 获取参数this->declare_parameter<std::string>("greeting", "Hello");this->declare_parameter<std::string>("name", "ROS2");this->declare_parameter<int>("ExposureTime", 50000);// 读取参数std::string greeting = this->get_parameter("greeting").as_string();std::string name = this->get_parameter("name").as_string();int ExposureTime = this->get_parameter("ExposureTime").as_int();// 打印信息std::cout << greeting << ", " << name << std::endl;std::cout << "ExposureTime: " << ExposureTime << std::endl;}~ImageSaverNode() {}
};int main(int argc, char **argv)
{rclcpp::init(argc, argv);rclcpp::spin(std::make_shared<ImageSaverNode>());rclcpp::shutdown();std::cout << "run ..." << std::endl;return 0;
}
运行结果
- 不通过读取.yaml文件直接启动节点
$ source install/setup.bash
$ ros2 run bag_to_image bag_to_image_node node...
Hello, ROS2
ExposureTime: 50000
- 通过launch读取.yaml文件启动节点
$ source install/setup.bash
$ ros2 launch bag_to_image start_node.launch.py [INFO] [launch]: All log files can be found below /home/boss-dog/.ros/log/2024-12-24-20-34-00-600200-boss-dog-106611
[INFO] [launch]: Default logging verbosity is set to INFO
Current directory: /home/boss-dog/001_test/bag_to_image
[INFO] [bag_to_image_node-1]: process started with pid [106612]
[bag_to_image_node-1] node...
[bag_to_image_node-1] Hello, BUDING DUODUO
[bag_to_image_node-1] ExposureTime: 12345