找回密码
 会员注册
查看: 40|回复: 0

ROS2launch启动文件编写(重点探讨python方式)

[复制链接]

2万

主题

0

回帖

6万

积分

超级版主

积分
69864
发表于 2024-9-10 09:15:18 | 显示全部楼层 |阅读模式
ros2launch启动文件编写一.运行方式1.启动方式2.设置参数二、瞻仰:官网示例(不想看的可直接看目录三^.^)1.python编写launch示例2.xml编写launch示例3.yaml编写launch示例三、自己操刀编写一个launch.pyros1和ros2其中一个很大区别之一就是launch的编写方式。在ros1中采用xml格式编写launch,而ros2保留了xml格式launch,还另外引入了python和yaml编写方式。选择哪种编写取决于每位开发人员的爱好,但是ros2官方推荐使用python方式编写,理由如下:1.Python是一种脚本语言,使用灵活,python库强大,可以直接引入。2.ros2launch框架本身是用python编写,由于亲和力高python可以直接调用一些高级特性,而XML和YAML可能无法调用。当然了,用python编写也不是全部都是优点没有缺点,不然还要引入xml,yaml干嘛对吧?答案就是用Python编写的启动文件可能比XML或YAML编写的启动文件更复杂、更冗长。一.运行方式1.启动方式而不管是哪种格式的launch,启动方式一样:ros2launch 或者还可以直接启动指定路径文件:ros2launch 2.设置参数ros2launch background_r:=255或者这样:ros2launch background_r:=255二、瞻仰:官网示例(不想看的可直接看目录三^.^)1.python编写launch示例#example.launch.pyimportosfromament_index_pythonimportget_package_share_directoryfromlaunchimportLaunchDescriptionfromlaunch.actionsimportDeclareLaunchArgumentfromlaunch.actionsimportIncludeLaunchDescriptionfromlaunch.actionsimportGroupActionfromlaunch.launch_description_sourcesimportPythonLaunchDescriptionSourcefromlaunch.substitutionsimportLaunchConfigurationfromlaunch.substitutionsimportTextSubstitutionfromlaunch_ros.actionsimportNodefromlaunch_ros.actionsimportPushRosNamespacedefgenerate_launch_description():#argsthatcanbesetfromthecommandlineoradefaultwillbeusedbackground_r_launch_arg=DeclareLaunchArgument("background_r",default_value=TextSubstitution(text="0"))background_g_launch_arg=DeclareLaunchArgument("background_g",default_value=TextSubstitution(text="255"))background_b_launch_arg=DeclareLaunchArgument("background_b",default_value=TextSubstitution(text="0"))chatter_ns_launch_arg=DeclareLaunchArgument("chatter_ns",default_value=TextSubstitution(text="my/chatter/ns"))#includeanotherlaunchfilelaunch_include=IncludeLaunchDescription(PythonLaunchDescriptionSource(os.path.join(get_package_share_directory('demo_nodes_cpp'),'launch/topics/talker_listener.launch.py')))#includeanotherlaunchfileinthechatter_nsnamespacelaunch_include_with_namespace=GroupAction(actions=[#push_ros_namespacetosetnamespaceofincludednodesPushRosNamespace('chatter_ns'),IncludeLaunchDescription(PythonLaunchDescriptionSource(os.path.join(get_package_share_directory('demo_nodes_cpp'),'launch/topics/talker_listener.launch.py'))),])#startaturtlesim_nodeintheturtlesim1namespaceturtlesim_node=Node(package='turtlesim',namespace='turtlesim1',executable='turtlesim_node',name='sim')#startanotherturtlesim_nodeintheturtlesim2namespace#anduseargstosetparametersturtlesim_node_with_parameters=Node(package='turtlesim',namespace='turtlesim2',executable='turtlesim_node',name='sim',parameters=[{"background_r"aunchConfiguration('background_r'),"background_g"aunchConfiguration('background_g'),"background_b"aunchConfiguration('background_b'),}])#performremapsobothturtleslistentothesamecommandtopicforward_turtlesim_commands_to_second_turtlesim_node=Node(package='turtlesim',executable='mimic',name='mimic',remappings=[('/input/pose','/turtlesim1/turtle1/pose'),('/output/cmd_vel','/turtlesim2/turtle1/cmd_vel'),])returnLaunchDescription([background_r_launch_arg,background_g_launch_arg,background_b_launch_arg,chatter_ns_launch_arg,launch_include,launch_include_with_namespace,turtlesim_node,turtlesim_node_with_parameters,forward_turtlesim_commands_to_second_turtlesim_node,])12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697982.xml编写launch示例
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-7 06:03 , Processed in 0.472470 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表