Advertisement
Guest User

Untitled

a guest
Jul 17th, 2024
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # Copyright (c) 2022 Samsung R&D Institute Russia
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16.  
  17. import os
  18.  
  19. from ament_index_python.packages import get_package_share_directory
  20.  
  21. from launch import LaunchDescription
  22. from launch.actions import DeclareLaunchArgument, GroupAction
  23. from launch.conditions import IfCondition
  24. from launch.substitutions import LaunchConfiguration, PythonExpression
  25. from launch.substitutions import NotEqualsSubstitution
  26. from launch_ros.actions import LoadComposableNodes, SetParameter
  27. from launch_ros.actions import Node
  28. from launch_ros.actions import PushROSNamespace
  29. from launch_ros.descriptions import ComposableNode, ParameterFile
  30. from nav2_common.launch import RewrittenYaml
  31.  
  32.  
  33. def generate_launch_description():
  34. # Environment
  35. package_dir = get_package_share_directory('nav2_collision_monitor')
  36.  
  37. # Constant parameters
  38. lifecycle_nodes = ['collision_monitor']
  39. autostart = False #True
  40. remappings = [('/tf', 'tf'),
  41. ('/tf_static', 'tf_static')]
  42.  
  43. # Launch arguments
  44. # 1. Create the launch configuration variables
  45. namespace = LaunchConfiguration('namespace')
  46. use_sim_time = LaunchConfiguration('use_sim_time')
  47. params_file = LaunchConfiguration('params_file')
  48. use_composition = LaunchConfiguration('use_composition')
  49. container_name = LaunchConfiguration('container_name')
  50. container_name_full = (namespace, '/', container_name)
  51.  
  52. # 2. Declare the launch arguments
  53. declare_namespace_cmd = DeclareLaunchArgument(
  54. 'namespace',
  55. default_value='',
  56. description='Top-level namespace')
  57.  
  58. declare_use_sim_time_cmd = DeclareLaunchArgument(
  59. 'use_sim_time',
  60. default_value='True',
  61. description='Use simulation (Gazebo) clock if true')
  62.  
  63. declare_params_file_cmd = DeclareLaunchArgument(
  64. 'params_file',
  65. default_value=os.path.join(package_dir, 'params', 'collision_monitor_params.yaml'),
  66. description='Full path to the ROS2 parameters file to use for all launched nodes')
  67.  
  68. declare_use_composition_cmd = DeclareLaunchArgument(
  69. 'use_composition', default_value='True',
  70. description='Use composed bringup if True')
  71.  
  72. declare_container_name_cmd = DeclareLaunchArgument(
  73. 'container_name', default_value='nav2_container',
  74. description='the name of conatiner that nodes will load in if use composition')
  75.  
  76. configured_params = ParameterFile(
  77. RewrittenYaml(
  78. source_file=params_file,
  79. root_key=namespace,
  80. param_rewrites={},
  81. convert_types=True),
  82. allow_substs=True)
  83.  
  84. # Declare node launching commands
  85. load_nodes = GroupAction(
  86. condition=IfCondition(PythonExpression(['not ', use_composition])),
  87. actions=[
  88. SetParameter('use_sim_time', use_sim_time),
  89. PushROSNamespace(
  90. condition=IfCondition(NotEqualsSubstitution(LaunchConfiguration('namespace'), '')),
  91. namespace=namespace),
  92. Node(
  93. package='nav2_lifecycle_manager',
  94. executable='lifecycle_manager',
  95. name='lifecycle_manager_collision_monitor',
  96. output='screen',
  97. emulate_tty=True, # https://github.com/ros2/launch/issues/188
  98. parameters=[{'autostart': autostart},
  99. {'node_names': lifecycle_nodes}],
  100. remappings=remappings),
  101. Node(
  102. package='nav2_collision_monitor',
  103. executable='collision_monitor',
  104. output='screen',
  105. emulate_tty=True, # https://github.com/ros2/launch/issues/188
  106. parameters=[configured_params],
  107. remappings=remappings)
  108. ]
  109. )
  110.  
  111. load_composable_nodes = GroupAction(
  112. condition=IfCondition(use_composition),
  113. actions=[
  114. SetParameter('use_sim_time', use_sim_time),
  115. PushROSNamespace(
  116. condition=IfCondition(NotEqualsSubstitution(LaunchConfiguration('namespace'), '')),
  117. namespace=namespace),
  118. LoadComposableNodes(
  119. target_container=container_name_full,
  120. composable_node_descriptions=[
  121. ComposableNode(
  122. package='nav2_lifecycle_manager',
  123. plugin='nav2_lifecycle_manager::LifecycleManager',
  124. name='lifecycle_manager_collision_monitor',
  125. parameters=[{'autostart': autostart},
  126. {'node_names': lifecycle_nodes}],
  127. remappings=remappings),
  128. ComposableNode(
  129. package='nav2_collision_monitor',
  130. plugin='nav2_collision_monitor::CollisionMonitor',
  131. name='collision_monitor',
  132. parameters=[configured_params],
  133. remappings=remappings)
  134. ],
  135. )
  136. ]
  137. )
  138.  
  139. ld = LaunchDescription()
  140.  
  141. # Launch arguments
  142. ld.add_action(declare_namespace_cmd)
  143. ld.add_action(declare_use_sim_time_cmd)
  144. ld.add_action(declare_params_file_cmd)
  145. ld.add_action(declare_use_composition_cmd)
  146. ld.add_action(declare_container_name_cmd)
  147.  
  148. # Node launching commands
  149. ld.add_action(load_nodes)
  150. ld.add_action(load_composable_nodes)
  151.  
  152. return ld
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement