从配置文件读取串口名

This commit is contained in:
lyq 2026-02-28 13:25:32 +08:00
parent 101bc924aa
commit c55105c9bf
5 changed files with 41 additions and 1 deletions

View File

@ -78,6 +78,13 @@ def generate_launch_description():
package="rtk", package="rtk",
executable="rtk_node", executable="rtk_node",
name="rtk_node", name="rtk_node",
parameters=[
os.path.join(
get_package_share_directory("rtk"),
"config",
"rtk_params.yaml"
)
], # 从YAML文件加载参数
output="screen", output="screen",
respawn=True, respawn=True,
respawn_delay=2 respawn_delay=2

View File

@ -40,6 +40,9 @@ ament_target_dependencies(
install(TARGETS rtk_node DESTINATION lib/${PROJECT_NAME}) install(TARGETS rtk_node DESTINATION lib/${PROJECT_NAME})
#
install(DIRECTORY config launch DESTINATION share/${PROJECT_NAME})
if(BUILD_TESTING) if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED) find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights # the following line skips the linter which checks for copyrights

View File

@ -0,0 +1,4 @@
rtk:
ros__parameters:
# 串口设备名称
serial_port: "/dev/ttyTHS1"

View File

@ -0,0 +1,23 @@
from launch import LaunchDescription
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
import os
def generate_launch_description():
# 获取配置文件路径
config_dir = os.path.join(
get_package_share_directory("rtk"), "config", "rtk_params.yaml"
)
return LaunchDescription(
[
Node(
package="rtk",
executable="rtk_node",
name="rtk_node",
parameters=[config_dir], # 从YAML文件加载参数
output="screen",
),
]
)

View File

@ -23,7 +23,10 @@ class rtk_node : public rclcpp::Node
// 初始化串口读取 // 初始化串口读取
boost_serial = new Boost_serial(); boost_serial = new Boost_serial();
boost_serial->init("/dev/ttyTHS1"); std::string serial_port;
this->declare_parameter("serial_port", "/dev/ttyTHS1");
this->get_parameter("serial_port", serial_port);
boost_serial->init(serial_port.c_str());
// 创建发布者 // 创建发布者
command_publisher_ = this->create_publisher<sweeper_interfaces::msg::Rtk>("rtk_message", 10); command_publisher_ = this->create_publisher<sweeper_interfaces::msg::Rtk>("rtk_message", 10);