Tutorials/1.9/Creating ROS plugins for Gazebo

In this tutorial we'll walk through creating a very basic Gazebo plugin that is ROS-aware.


 * 1) Create a ROS Package

Create a new ROS package in your catkin workspace:

cd ~/catkin_ws catkin_create-pkg gazebo_tutorials gazebo_ros roscpp


 * 1) Create the Plugin

Create a very simple plugin as described here and save the file as gazebo_tutorials/src/simple_world_plugin.cpp:


 * 1) include 
 * 2) include 

namespace gazebo { class WorldPluginTutorial : public WorldPlugin { public: WorldPluginTutorial : WorldPlugin { }

void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf) {   // Make sure the ROS node for Gazebo has already been initialized if (!ros::isInitialized) {     ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized, unable to load plugin. "        << "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");      return;    }

ROS_INFO("Hello World!"); }

}; GZ_REGISTER_WORLD_PLUGIN(WorldPluginTutorial) }


 * 1) Update CMakeLists.txt

Open gazebo_tutorials/CMakeLists.txt and replace it with the following:

cmake_minimum_required(VERSION 2.8.3) project(gazebo_tutorials)

find_package(catkin REQUIRED COMPONENTS  roscpp   gazebo_ros )
 * 1) Load catkin and all dependencies required for this package

find_package(gazebo REQUIRED)
 * 1) Depend on system install of Gazebo

link_directories(${GAZEBO_LIBRARY_DIRS}) include_directories(${Boost_INCLUDE_DIR} ${catkin_INCLUDE_DIRS} ${GAZEBO_INCLUDE_DIRS})

add_library(${PROJECT_NAME} src/simple_world_plugin.cpp) target_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES} ${GAZEBO_LIBRARIES})

catkin_package( DEPENDS     roscpp     gazebo_ros )


 * 1) Update package.xml

Update gazebo_tutorials/package.xml by adding the following line within the   tags (or add the    tags also).




 * 1) Compiling the Plugin

Build the plugin by going to the base of your work space and running catkin:

cd ~/catkin_ws catkin_make


 * 1) Creating a World file

Save the following file as gazebo_tutorials/worlds/hello.world:

 <sdf version="1.4"> model://ground_plane

model://sun

<plugin name="gazebo_tutorials" filename="libgazebo_tutorials.so"/>


 * 1) Create a Launch File

Create the following launch file gazebo_tutorials/launch/hello.launch:

<include file="$(find gazebo_ros)/launch/empty_world.launch"> <arg name="world_name" value="$(find gazebo_tutorials)/worlds/hello.world"/>


 * 1) Run the Plugin

roslaunch gazebo_tutorials hello.launch

An empty Gazebo should open and in the terminal you should see it print out something like: INFO ros.gazebo_tutorials: Hello World!


 * 1) Starting from a Template

A template is available to help you quickly get a Gazebo-ROS plugin working:

[gazebo_ros_template.cpp](https://github.com/ros-simulation/gazebo_ros_pkgs/blob/hydro-devel/gazebo_plugins/src/gazebo_ros_template.cpp)


 * 1) Adding Functionality

To make your plugin do something useful with Gazebo and ROS, we suggest you read the ROS-agnostic tutorials on Plugins as well as the user manual.


 * 1) ROS Node Note

All gazebo-ros plugins should check if the ROS node has already been initialized in their Load</tt> function, as discussed in this [issue](http://answers.gazebosim.org/question/1493/rosinit-needed-for-ros-gazebo-plugin/). The initialization of the ROS node is performed automatically when you run rosrun gazebo_ros gazebo or use the generic empty.world launch file. The gazebo_ros/src/gazebo_ros_api_plugin.cpp should be the only place in Gazebo that calls ros::init.


 * 1) Next Steps

For miscellaneous Gazebo-ROS tricks see Advanced ROS integration