Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. import os
  2.  
  3. from ament_index_python.packages import get_package_share_directory
  4.  
  5. from launch import LaunchDescription
  6. from launch.substitutions import LaunchConfiguration
  7. from launch.actions import IncludeLaunchDescription, DeclareLaunchArgument
  8. from launch.launch_description_sources import PythonLaunchDescriptionSource
  9. from launch.conditions import IfCondition
  10. from nav2_common.launch import RewrittenYaml
  11. from launch.actions import ExecuteProcess
  12. from launch_ros.actions import Node
  13.  
  14.  
  15. def generate_launch_description():
  16. # Get the launch directory
  17. bringup_dir = get_package_share_directory('nav2_bringup')
  18. launch_dir = os.path.join(bringup_dir, 'launch')
  19. params_dir = os.path.join(bringup_dir, "params")
  20.  
  21. nav2_params = os.path.join(params_dir, "real_truck_nav2_params.yaml")
  22. configured_params = RewrittenYaml(
  23. source_file=nav2_params, root_key="", param_rewrites="", convert_types=True
  24. )
  25.  
  26. # Keep truck static by publishing to /cmd_vel in a loop
  27. keep_truck_static_cmd = ExecuteProcess(
  28. cmd=['bash', '-c', "while true; do ros2 topic pub /cmd_vel geometry_msgs/msg/Twist '{linear: {x: -0.8, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}' -1; sleep 0.1; done"],
  29. output='screen'
  30. )
  31.  
  32. use_rviz = LaunchConfiguration('use_rviz')
  33. use_mapviz = LaunchConfiguration('use_mapviz')
  34.  
  35. declare_use_rviz_cmd = DeclareLaunchArgument(
  36. 'use_rviz',
  37. default_value='False',
  38. description='Whether to start RVIZ')
  39.  
  40. declare_use_mapviz_cmd = DeclareLaunchArgument(
  41. 'use_mapviz',
  42. default_value='True',
  43. description='Whether to start mapviz')
  44.  
  45. robot_localization_cmd = IncludeLaunchDescription(
  46. PythonLaunchDescriptionSource(
  47. os.path.join(launch_dir, 'real_bot_dual_ekf_navsat.launch.py'))
  48. )
  49.  
  50. navigation2_cmd = IncludeLaunchDescription(
  51. PythonLaunchDescriptionSource(
  52. os.path.join(bringup_dir, "launch", "real_navigation_launch.py")
  53. ),
  54. launch_arguments={
  55. "use_sim_time": "False",
  56. "params_file": configured_params,
  57. "autostart": "True",
  58. }.items(),
  59. )
  60.  
  61. rviz_cmd = IncludeLaunchDescription(
  62. PythonLaunchDescriptionSource(
  63. os.path.join(bringup_dir, "launch", 'rviz_launch.py')),
  64. condition=IfCondition(use_rviz)
  65. )
  66.  
  67. mapviz_cmd = IncludeLaunchDescription(
  68. PythonLaunchDescriptionSource(
  69. os.path.join(launch_dir, 'mapviz.launch.py')),
  70. condition=IfCondition(use_mapviz)
  71. )
  72.  
  73. robot_cmd = IncludeLaunchDescription(
  74. PythonLaunchDescriptionSource(
  75. os.path.join(launch_dir, 'real_truck_environment_launch.py'))
  76. )
  77.  
  78. # Node to run broadcast_odom_base_footprint.py
  79. broadcast_odom_base_footprint_node = Node(
  80. package='nav2_bringup',
  81. executable='broadcast_odom_base_footprint.py',
  82. name='broadcast_odom_base_footprint',
  83. output='screen'
  84. )
  85.  
  86. # Node to relay GPS from CAN to proper topic
  87. nmea_to_navsat_converter_node = Node(
  88. package='nav2_bringup',
  89. executable='nmea_to_navsat_converter.py',
  90. name='nmea_to_navsat_converter',
  91. output='screen'
  92. )
  93.  
  94. # Relay command to publish GPS data to the correct topic
  95. relay_gps_fix_cmd = ExecuteProcess(
  96. cmd=['ros2', 'run', 'topic_tools', 'relay', '/real_gps/fix', '/gps/fix'],
  97. output='screen'
  98. )
  99.  
  100. # Create the launch description and populate
  101. ld = LaunchDescription()
  102.  
  103. # Add launch arguments
  104. ld.add_action(declare_use_rviz_cmd)
  105. ld.add_action(declare_use_mapviz_cmd)
  106.  
  107. # Add robot launch
  108. ld.add_action(robot_cmd)
  109.  
  110. # Add robot localization launch
  111. ld.add_action(robot_localization_cmd)
  112.  
  113. # Add navigation2 launch
  114. ld.add_action(navigation2_cmd)
  115.  
  116. # Add viz launch
  117. ld.add_action(rviz_cmd)
  118. ld.add_action(mapviz_cmd)
  119.  
  120. # Add the broadcast_odom_base_footprint node
  121. ld.add_action(broadcast_odom_base_footprint_node)
  122.  
  123. # Add nmea relay
  124. # ld.add_action(nmea_to_navsat_converter_node)
  125.  
  126. # Add the relay GPS fix command
  127. ld.add_action(relay_gps_fix_cmd)
  128.  
  129. # Uncomment when the terrain does not move the truck backwards (plane terrains don't need it)
  130. # ld.add_action(keep_truck_static_cmd) #FAKE BRAKE
  131.  
  132. return ld
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement