Skip to content

Commit

Permalink
make simulation use specific world file and have skybox
Browse files Browse the repository at this point in the history
the sdf file isn't hand-crafted. I ran the launch file, then saved it from gazebo, then modified the scene tag to add a sky tag, then pasted that over my file, then removed the added robot model.
  • Loading branch information
EricPedley committed Sep 14, 2024
1 parent 2edb9b7 commit ac01ddd
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 99 deletions.
3 changes: 1 addition & 2 deletions description/example_gazebo.xacro
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<!-- Note that although visualise is set to true, it won't actually visualise the depth camera in gazebo. To see the preview,
try swapping "depth" to "camera"-->
<gazebo reference="camera_link">
<sensor type="depth" name="my_camera">
<sensor type="camera" name="my_camera">
<update_rate>20</update_rate>
<visualize>true</visualize>
<camera name="cam">
Expand All @@ -99,7 +99,6 @@
</image>
<clip>
<near>0.02</near>
<far>300</far>
</clip>
<noise>
<type>gaussian</type>
Expand Down
85 changes: 85 additions & 0 deletions launch/example.launch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Author: Addison Sears-Collins
# Date: September 19, 2021
# Description: Load a world file into Gazebo.
# https://automaticaddison.com
# https://automaticaddison.com/how-to-load-a-world-file-into-gazebo-ros-2/

import os
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription
from launch.conditions import IfCondition, UnlessCondition
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import Command, LaunchConfiguration, PythonExpression
from launch_ros.actions import Node
from launch_ros.substitutions import FindPackageShare

def generate_launch_description():

# Set the path to the Gazebo ROS package
pkg_gazebo_ros = FindPackageShare(package='gazebo_ros').find('gazebo_ros')

# Set the path to this package.
pkg_share = FindPackageShare(package='two_wheeled_robot').find('two_wheeled_robot')

# Set the path to the world file
world_file_name = 'cafe.world'
world_path = os.path.join(pkg_share, 'worlds', world_file_name)

# Set the path to the SDF model files.
gazebo_models_path = os.path.join(pkg_share, 'models')
os.environ["GAZEBO_MODEL_PATH"] = gazebo_models_path

########### YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE ##############
# Launch configuration variables specific to simulation
headless = LaunchConfiguration('headless')
use_sim_time = LaunchConfiguration('use_sim_time')
use_simulator = LaunchConfiguration('use_simulator')
world = LaunchConfiguration('world')

declare_simulator_cmd = DeclareLaunchArgument(
name='headless',
default_value='False',
description='Whether to execute gzclient')

declare_use_sim_time_cmd = DeclareLaunchArgument(
name='use_sim_time',
default_value='true',
description='Use simulation (Gazebo) clock if true')

declare_use_simulator_cmd = DeclareLaunchArgument(
name='use_simulator',
default_value='True',
description='Whether to start the simulator')

declare_world_cmd = DeclareLaunchArgument(
name='world',
default_value=world_path,
description='Full path to the world model file to load')

# Specify the actions

# Start Gazebo server
start_gazebo_server_cmd = IncludeLaunchDescription(
PythonLaunchDescriptionSource(os.path.join(pkg_gazebo_ros, 'launch', 'gzserver.launch.py')),
condition=IfCondition(use_simulator),
launch_arguments={'world': world}.items())

# Start Gazebo client
start_gazebo_client_cmd = IncludeLaunchDescription(
PythonLaunchDescriptionSource(os.path.join(pkg_gazebo_ros, 'launch', 'gzclient.launch.py')),
condition=IfCondition(PythonExpression([use_simulator, ' and not ', headless])))

# Create the launch description and populate
ld = LaunchDescription()

# Declare the launch options
ld.add_action(declare_simulator_cmd)
ld.add_action(declare_use_sim_time_cmd)
ld.add_action(declare_use_simulator_cmd)
ld.add_action(declare_world_cmd)

# Add any actions
ld.add_action(start_gazebo_server_cmd)
ld.add_action(start_gazebo_client_cmd)

return ld
11 changes: 10 additions & 1 deletion launch/rsp_sim.launch.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.actions import IncludeLaunchDescription, DeclareLaunchArgument
from launch.launch_description_sources import PythonLaunchDescriptionSource

from launch_ros.substitutions import FindPackageShare

from launch_ros.actions import Node
import xacro
Expand All @@ -30,7 +31,14 @@ def generate_launch_description():
'use_sim_time': True}] # add other parameters here if required
)

pkg_share = FindPackageShare(package=pkg_name).find(pkg_name)
world_file_name = 'gz_world.sdf'
world_path = os.path.join(pkg_share, 'worlds', world_file_name)

declare_world_cmd = DeclareLaunchArgument(
name='world',
default_value=world_path,
description='Full path to the world model file to load')

gazebo = IncludeLaunchDescription(
PythonLaunchDescriptionSource([os.path.join(
Expand All @@ -50,6 +58,7 @@ def generate_launch_description():

# Run the node
return LaunchDescription([
declare_world_cmd,
gazebo,
node_robot_state_publisher,
spawn_entity
Expand Down
Loading

0 comments on commit ac01ddd

Please sign in to comment.