|
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示例
|
|