ROS2 launch文件与参数设置

ROS2 launch文件与参数设置

在ros2创建的C++语言的package中,通过launch文件启动多个节点,并从yaml文件中读取订阅的topic名称。

项目架构
working_space/
|
└───build/
|
└───install/
|
└───log/
|
└───src/
	└───package_1/
	|	|
	|	└───config/
	|	|	└───***.rviz
	|	|	└───***.yaml
	|	|
    |   └───include/
	|	|
    |   └───package_1/
	|	|	└───***.h
	|	|	└───***.hpp
	|	|
    |   └───launch/
	|	|	└───***.launch.py
	|	|
	|	└───src/
	|	|	└───***.cpp
	|	|	└───***.cpp
	|	|
	|	└───CMakeLists.txt
	|	└───package.xml
	|
	└───package_2/
	|
	└───package_3/


launch文件编写
from ament_index_python.packages import get_package_share_directory
import launch
import launch.actions
import launch.substitutions
import launch_ros.actions
import os.path

def generate_launch_description():
    rviz_config_dir = os.path.join(get_package_share_directory('sensor_fusion'), 'config', 'fusion.rviz')
    para_dir = os.path.join(get_package_share_directory('sensor_fusion'), 'config', 'sensor_fusion_subscribed_topic.yaml')
    return launch.LaunchDescription([
        launch.actions.DeclareLaunchArgument(
            'node_prefix',
            # if launch file's prefix is "_lanuch.py", change '.' into '_'
            default_value=[launch.substitutions.EnvironmentVariable('USER'), '.'],
            description='Prefix for node names'
        ),
        launch_ros.actions.Node(
            package='rviz2',
            node_namespace='rviz2',
            node_executable='rviz2',
            arguments=['-d', rviz_config_dir]
        ),
        launch_ros.actions.Node(
            package='kitti_pub',
            node_namespace='kitti_pub',
            node_executable='kitti_pub',
            output='screen'
        ),
        launch_ros.actions.Node(
            package='sensor_fusion',
            node_namespace='sensor_fusion',
            node_executable='sensor_fusion',
            parameters=[para_dir],
            output='screen'
        )
    ])
yaml文件编写
# namespace
sensor_fusion:
  # node_name
  sensor_fusion:
    # parameters
    ros__parameters:
      # topic's name will be namespace setted in the launch file plus original publisher topic.
      point_cloud_topic: "/kitti_pub/kitti_points"
      image_topic: "/kitti_pub/kitti_cam02"
      detect_box2d_topic: "/kitti_pub/yolo_det"
CMakeLists.txt文件修改

ament_package() 前面加上以下内容,才能在share文件夹中找到launch文件和config文件。

install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}/
)
install(DIRECTORY
  config
  DESTINATION share/${PROJECT_NAME}/
)
源文件内读取yaml文件参数并初始化subscriber
// Same type with the parameters in yaml to read the params
string point_cloud_topic, image_topic, detect_box2d_topic;
// Declare and get parameters from yaml or use alternative parameters
this->declare_parameter<string>("point_cloud_topic", "/kitti_pub/kitti_points");
this->declare_parameter<string>("image_topic", "/kitti_pub/kitti_cam02");
this->declare_parameter<string>("detect_box2d_topic", "/kitti_pub/yolo_det");
this->get_parameter_or<string>("point_cloud_topic", point_cloud_topic, "/kitti_pub/kitti_points");
this->get_parameter_or<string>("image_topic", image_topic, "/kitti_pub/kitti_cam02");
this->get_parameter_or<string>("detect_box2d_topic", detect_box2d_topic, "/kitti_pub/yolo_det");
// Declare subscribers
message_filters::Subscriber<sensor_msgs::msg::PointCloud2> pcl_sub;
message_filters::Subscriber<sensor_msgs::msg::Image> img_sub;
message_filters::Subscriber<darknet_ros_msgs::msg::BoundingBoxes> det_sub;
// Initialize subscribers
pcl_sub.subscribe(this, point_cloud_topic);
img_sub.subscribe(this, image_topic);
det_sub.subscribe(this, detect_box2d_topic);

参考链接:

https://docs.ros.org/en/dashing/Tutorials/Launch-Files/Creating-Launch-Files.html

https://docs.ros.org/en/dashing/Tutorials/Launch-system.html

https://blog.csdn.net/qq_33399315/article/details/106863743?spm=1001.2014.3001.5501

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个ROS中的launch文件示例,用于启动一个简单的机器人模拟器(使用Gazebo仿真),并启动相应的ROS节点: ``` <launch> <!-- 启动Gazebo仿真器 --> <include file="$(find gazebo_ros)/launch/empty_world.launch"> <arg name="world_name" value="$(find my_robot)/worlds/my_world.world"/> <arg name="paused" value="false"/> <arg name="use_sim_time" value="true"/> <arg name="gui" value="true"/> <arg name="headless" value="false"/> <arg name="debug" value="false"/> </include> <!-- 启动机器人控制节点 --> <node name="robot_control" pkg="my_robot" type="robot_control_node.py" output="screen"> <param name="robot_name" value="my_robot"/> <param name="control_rate" value="10"/> </node> <!-- 启动传感器数据处理节点 --> <node name="sensor_processing" pkg="my_robot" type="sensor_processing_node.py" output="screen"> <param name="robot_name" value="my_robot"/> </node> </launch> ``` 上述launch文件中包含了三个节点: 1. Gazebo仿真器:使用Gazebo仿真器启动机器人模拟器,并设置仿真世界和一些参数。 2. 机器人控制节点:启动一个ROS节点,用于控制机器人的运动。 3. 传感器数据处理节点:启动一个ROS节点,用于处理机器人传感器的数据。 其中,节点的名称、包名、类型、参数等信息都需要根据实际情况进行修改。在终端中运行launch文件时,可以使用以下命令: ``` $ roslaunch my_robot my_robot.launch ``` 此时,launch文件中的节点会依次启动,完成机器人模拟器的启动和相关节点的启动。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值