simple one liner tutorial gazebo plugin is not working

asked 2020-04-03 12:47:22 -0600

updated 2020-04-03 16:18:07 -0600

I feel really stupid. I'm just trying to go through some simple online tutorials to learn how to write plugins for Gazebo. I've got ROS kinetic and Gazebo 7.0.0 on Ubuntu.

First I did the Hello World (world plugin) tutorial and it works fine:

#include <gazebo/common/Plugin.hh>
#include <ros/ros.h>

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

  void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
  {

    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)
}

But then I tried to move on to the Factory (world plugin) tutorial and it failed. The Factory plugin just inserts models into the world. I have minimized the code to just the bare minimum while troubleshooting and it's got like one line different than the Hello World plugin above.

#include <gazebo/common/Plugin.hh>
#include <gazebo/physics/physics.hh>
#include <ros/ros.h>

namespace gazebo
{
class Factory : public WorldPlugin
{
public:
  Factory() : WorldPlugin()
  {
  }

  void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
  {

    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("Factory World!");

    // the coke_can model was dropped in manually earlier
    // so it should be found in ~/.gazebo/models folder which is already in gazebo model path
    _world->InsertModelFile("model://coke_can");

    ROS_INFO("Goodbye World!");

  }

};
GZ_REGISTER_WORLD_PLUGIN(Factory)
}

When I launch gazebo with the world file that includes this factory plugin, it does not give me any errors. It just waits for Service set_physics_properties to be advertised and gives up after a while.

Here is my world file:

<?xml version="1.0" ?>
<sdf version="1.4">
  <world name="default">
    <include>
      <uri>model://ground_plane</uri>
    </include>

    <include>
      <uri>model://sun</uri>
    </include>

    <plugin name="gazebo_plugin" filename="libfactory_plugin.so"/>
  </world>
</sdf>

Here is my command line showing no errors:

$ rosrun gazebo_ros gazebo ~/catkin_ws/src/my_plugins/worlds/factory.world 
[ INFO] [1585935624.280625641]: Finished loading Gazebo ROS API Plugin.
[ INFO] [1585935624.281090520]: waitForService: Service [/gazebo/set_physics_properties] has not been advertised, waiting...
$

Here is my CMakeLists.txt file

cmake_minimum_required(VERSION 2.8.3)
project(my_plugins)

# Check for c++11 / c++0x support
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
    set(CMAKE_CXX_FLAGS "-std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
    set(CMAKE_CXX_FLAGS "-std=c++0x")
else()
    message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

find_package(
  catkin REQUIRED COMPONENTS
  gazebo_ros
  roscpp
  gazebo REQUIRED
)

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

catkin_package(DEPENDS roscpp gazebo_ros)

# hello world plugin
add_library(hello_world_plugin src/hello_world_plugin.cpp)
target_link_libraries(hello_world_plugin ${catkin_LIBRARIES} ${GAZEBO_LIBRARIES})

# factory plugin
add_library(factory_plugin src/factory_plugin.cpp)
target_link_libraries(factory_plugin ${catkin_LIBRARIES} ${GAZEBO_LIBRARIES})

So what am I doing wrong? Thanks.

edit retag flag offensive close merge delete