Skip to content

Instantly share code, notes, and snippets.

@rfzeg
Created March 23, 2023 14:44
Show Gist options
  • Save rfzeg/514a455798b4f920871d41337aaaf338 to your computer and use it in GitHub Desktop.
Save rfzeg/514a455798b4f920871d41337aaaf338 to your computer and use it in GitHub Desktop.
Spawn two robots from URDF files, without robot_state_publisher node (spawn_entity.py script with argument -file instead of -topic)
import os
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, ExecuteProcess, IncludeLaunchDescription
from launch.substitutions import Command, LaunchConfiguration
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
def generate_launch_description():
# This is to find the models inside the models folder in my_box_bot_gazebo package
gazebo_models_path = os.path.join(pkg_box_bot_gazebo, 'models')
if 'GAZEBO_MODEL_PATH' in os.environ:
os.environ['GAZEBO_MODEL_PATH'] = os.environ['GAZEBO_MODEL_PATH'] + \
':' + install_dir + '/share' + ':' + gazebo_models_path
else:
os.environ['GAZEBO_MODEL_PATH'] = install_dir + \
"/share" + ':' + gazebo_models_path
if 'GAZEBO_PLUGIN_PATH' in os.environ:
os.environ['GAZEBO_PLUGIN_PATH'] = os.environ['GAZEBO_PLUGIN_PATH'] + \
':' + install_dir + '/lib'
else:
os.environ['GAZEBO_PLUGIN_PATH'] = install_dir + '/lib'
print("GAZEBO MODELS PATH=="+str(os.environ["GAZEBO_MODEL_PATH"]))
print("GAZEBO PLUGINS PATH=="+str(os.environ["GAZEBO_PLUGIN_PATH"]))
# Declare a new launch argument for the world file
world_file_arg = DeclareLaunchArgument(
'world',
default_value=[get_package_share_directory(
'my_box_bot_gazebo'), '/worlds/box_bot_empty.world'],
description='Path to the Gazebo world file'
)
# Define the launch arguments for the Gazebo launch file
gazebo_launch_args = {
'verbose': 'false',
'pause': 'false',
'world': LaunchConfiguration('world')
}
# Include the Gazebo launch file with the modified launch arguments
gazebo = IncludeLaunchDescription(
PythonLaunchDescriptionSource([os.path.join(
get_package_share_directory('gazebo_ros'), 'launch'), '/gazebo.launch.py']),
launch_arguments=gazebo_launch_args.items(),
)
spawn_robot1 = Node(
package='gazebo_ros',
executable='spawn_entity.py',
arguments=['-entity', 'robot1', '-x', '0.0', '-y', '0.0', '-z', '0.0',
'-file', get_package_share_directory('my_box_bot_description') + '/urdf/box_bot_final.urdf'],
)
spawn_robot2 = Node(
package='gazebo_ros',
executable='spawn_entity.py',
arguments=['-entity', 'robot2', '-x', '1.0', '-y', '1.0', '-z', '0.0',
'-file', get_package_share_directory('my_box_bot_description') + '/urdf/box_bot_final.urdf'],
)
return LaunchDescription([
world_file_arg,
gazebo,
spawn_robot1,
spawn_robot2
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment