Last active
November 30, 2022 14:25
-
-
Save rfzeg/c23bf0d57074934c4c68034a61a02413 to your computer and use it in GitHub Desktop.
Python launch file to start a simulated world and include sdf models (does not spawn robot)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.8) | |
project(my_package) | |
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") | |
add_compile_options(-Wall -Wextra -Wpedantic) | |
endif() | |
# find dependencies | |
find_package(ament_cmake REQUIRED) | |
# uncomment the following section in order to fill in | |
# further dependencies manually. | |
# find_package(<dependency> REQUIRED) | |
install(DIRECTORY | |
launch | |
worlds | |
models | |
DESTINATION share/${PROJECT_NAME}) | |
ament_package() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<sdf version="1.6"> | |
<world name="my_demo"> | |
<!-- shadows, ambient, grid, origin --> | |
<scene> | |
<shadows>false</shadows> | |
<ambient>0.3 0.3 0.3 1</ambient> | |
<background>1 1 1 1</background> | |
<grid>false</grid> | |
<origin_visual>false</origin_visual> | |
</scene> | |
<gui fullscreen='0'> | |
<camera name='user_camera'> | |
<pose>-2.0736 -2.82419 3.5577 -0 0.629796 0.656</pose> | |
<view_controller>orbit</view_controller> | |
<projection_type>perspective</projection_type> | |
</camera> | |
</gui> | |
<include> | |
<uri>model://sun</uri> | |
</include> | |
<include> | |
<uri>model://ground_plane</uri> | |
</include> | |
<model name="cafe_table"> | |
<static>true</static> | |
<include> | |
<uri>model://cafe_table</uri> | |
<pose>1.5 -0.5 0 0 0 0</pose> | |
</include> | |
</model> | |
<model name="cardboard_box_1"> | |
<static>true</static> | |
<include> | |
<uri>model://cardboard_box</uri> | |
<pose>1.5 -0.5 0.924471 0 0 0</pose> | |
</include> | |
</model> | |
<model name="cafe_table2"> | |
<static>true</static> | |
<include> | |
<uri>model://cafe_table</uri> | |
<pose>0.5 1.5 0 0 0 0</pose> | |
</include> | |
</model> | |
<model name="cardboard_box_2"> | |
<static>true</static> | |
<include> | |
<uri>model://cardboard_box</uri> | |
<pose>0.5 1.5 0.924471 0 0 0</pose> | |
</include> | |
</model> | |
</world> | |
</sdf> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
import os | |
from ament_index_python.packages import get_package_share_directory | |
from launch import LaunchDescription | |
from launch.actions import DeclareLaunchArgument | |
from launch.actions import IncludeLaunchDescription | |
from launch.launch_description_sources import PythonLaunchDescriptionSource | |
from ament_index_python.packages import get_package_prefix | |
def generate_launch_description(): | |
pkg_gazebo_ros = get_package_share_directory('gazebo_ros') | |
package_name = "my_package" | |
pkg_share_directory = get_package_share_directory('my_package') | |
# We get the whole install dir | |
# We do this to avoid having to copy or softlink manually the packages so that gazebo can find them | |
install_dir = get_package_prefix(package_name) | |
# Set the path to the WORLD model files. Is to find the models inside the models folder in my_package package | |
gazebo_models_path = os.path.join(pkg_share_directory, 'models') | |
# os.environ["GAZEBO_MODEL_PATH"] = gazebo_models_path | |
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"])) | |
# Gazebo launch | |
gazebo = IncludeLaunchDescription( | |
PythonLaunchDescriptionSource( | |
os.path.join(pkg_gazebo_ros, 'launch', 'gazebo.launch.py'), | |
) | |
) | |
return LaunchDescription([ | |
DeclareLaunchArgument( | |
'world', | |
default_value=[os.path.join( | |
pkg_share_directory, 'worlds', 'my_custom_scene.world'), ''], | |
description='SDF world file'), | |
gazebo | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment