Skip to content

Instantly share code, notes, and snippets.

@rfzeg
Last active September 24, 2020 11:39
Show Gist options
  • Save rfzeg/7dc632b9be4dd5703173019770e439a9 to your computer and use it in GitHub Desktop.
Save rfzeg/7dc632b9be4dd5703173019770e439a9 to your computer and use it in GitHub Desktop.
Add Gazebo environment variables to allow access to Gazebo models
0. First check what is the output of "echo $GAZEBO_MODEL_PATH"
What is the output of "echo $GAZEBO_MODEL_PATH"?
1. Run from command line to grant access to models once:
(Tip: cd to the ros package with roscd, then use pwd command to get the full path)
export GAZEBO_MODEL_PATH=~/simulation_ws/src/logistic_demo_sim/models:$GAZEBO_MODEL_PATH
2. To permanently enable Gazebo to locate gazebo model files (SDF's):
$ echo 'export GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:~/catkin_ws/src/gazebo_package/models' >> ~/.bashrc
$ source ~/.bashrc
2.1. Or open up .bashrc:
sudo gedit ~/.bashrc
2.2. and copy & paste following at the end of .bashrc file:
source /usr/share/gazebo/setup.sh
export GAZEBO_MODEL_PATH=~/gazebo_package/models:${GAZEBO_MODEL_PATH}
export GAZEBO_MODEL_PATH=~/gazebo_package/models_gazebo:${GAZEBO_MODEL_PATH}
export GAZEBO_RESOURCE_PATH=~/gazebo_package/worlds:${GAZEBO_RESOURCE_PATH}
3. Add to package.xml file:
<export>
<!-- gazebo_ros_paths_plugin automatically adds these to
GAZEBO_MODEL_PATH when you do this export inside
the package.xml file. -->
<!-- gazebo_media_path, gazebo_model_path, gazebo_plugin_path -->
<!-- The world file can use models like this:
<uri>model://my_package/models/model_name</uri>
for sdf models located inside the model directory -->
<!-- <gazebo_ros gazebo_model_path="${prefix}/.." /> -->
<!-- The world file can use models like this:
<uri>model://wohnung_no_color</uri>
requires that all sdf models are located inside the models directory -->
<gazebo_ros gazebo_model_path="${prefix}/models"/>
<!-- <gazebo_ros gazebo_model_path="${prefix}/worlds"/> -->
</export>
4. Add Gazebo environment variables to launch file:
GAZEBO_PLUGIN_PATH: plugin search path
GAZEBO_MODEL_PATH: models search path
<launch>
<env name="GAZEBO_PLUGIN_PATH" value="$(optenv GAZEBO_PLUGIN_PATH):/home/user/simulation_ws/devel/lib"/>
<env name="GAZEBO_MODEL_PATH" value="$(optenv GAZEBO_MODEL_PATH):/home/user/simulation_ws/src/gazebo_world_plugin/models"/>
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="world_name" value="$(find gazebo_world_plugin)/worlds/test.world"/>
<arg name="verbose" value="true" />
</include>
</launch>
5. Add to a bash script file that starts the project:
#!/bin/sh
echo " "
echo "configuring gazebo7 plugin paths"
echo "previous GAZEBO_PLUGIN_PATH=$GAZEBO_PLUGIN_PATH"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "script directory $SCRIPT_DIR"
MY_PLUGIN_PATH=$SCRIPT_DIR/../lib
echo "plugin path $MY_PLUGIN_PATH"
export GAZEBO_PLUGIN_PATH=$MY_PLUGIN_PATH:$GAZEBO_PLUGIN_PATH
echo "new GAZEBO_PLUGIN_PATH=$GAZEBO_PLUGIN_PATH"
6. Add two bash script files:
---
start.sh
---
#!/usr/bin/env bash
# Make sure we have correct variables.
source scripts/init.sh
---
init.sh
---
# Sets up ENV variables for Gazebo. This file should be 'source'd, not
# executed (otherwise the ENV variables will not be set in our shell.
PROJECT_HOME=$(echo "$(cd "$(dirname "$1")"; pwd)")
# Variables needed for scripts.
export GAZEBO_MODEL_PATH=$PROJECT_HOME/gazebo/models
export GAZEBO_WORLD_PATH=$PROJECT_HOME/gazebo/worlds
export GAZEBO_PLUGIN_PATH=$PROJECT_HOME/gazebo/build
6. Last ressource
Copy the model folders into the ~/.gazebo/models folder in your local computer.
########
Explanation:
<?xml version="1.0"?>
<launch>
<env name="GAZEBO_MODEL_PATH" value="$(find my_gazebo_worlds)" />
<env name="GAZEBO_RESOURCE_PATH" value="$(find my_gazebo_worlds)" />
<!-- Bring up the gazebo gui and an empty world -->
<include file="$(find gazebo_ros)/launch/empty_world.launch" >
<arg name="world_name" value="$(find my_gazebo_worlds)/worlds/myworld.world"/>
</include>
</launch>
This will setup the GAZEBO_MODEL_PATH variable and the GAZEBO_RESOURCE_PATH variable.
This means that worlds and models can be shared in a ros package.
It also means that also materials (resource path) can be shared.
#######
Spawn objects from launch file
<launch>
<!--x,y,z = 1.028, -0.11, 0.8 rpy = 0,0,0.43-->
<node name="spawn_toy_block" pkg="gazebo_ros" type="spawn_model" args="-file $(find exmpl_models)/toy_block/toy_block.sdf -sdf -model toy_block -x 0.5 -y -0.35 -z 0.8 -Y 0.43" />
</launch>
######
Final note: clear env variable:
unset GAZEBO_MODEL_PATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment