Skip to content

Commit

Permalink
Port: Automatically load a subset of world plugins (#281) (#537)
Browse files Browse the repository at this point in the history
Feature to allow loading of a default set of system plugins from a file. This behavior will trigger when a world sdf file is loaded with no plugins defined.  In this case, the simulator will load the plugins from a series of locations including environment variable, the users home folder, and finally in the installation directory.

This should allow users to not have to specify the same set of plugins in every world sdf file.

Signed-off-by: Michael Carroll <[email protected]>
Co-authored-by: Louise Poubel <[email protected]>
  • Loading branch information
mjcarroll and chapulina authored Jan 13, 2021
1 parent 08252fd commit e9e9f19
Show file tree
Hide file tree
Showing 32 changed files with 1,414 additions and 302 deletions.
12 changes: 0 additions & 12 deletions examples/worlds/default.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@
<max_step_size>0.001</max_step_size>
<real_time_factor>1.0</real_time_factor>
</physics>
<plugin
filename="ignition-gazebo-physics-system"
name="ignition::gazebo::systems::Physics">
</plugin>
<plugin
filename="ignition-gazebo-user-commands-system"
name="ignition::gazebo::systems::UserCommands">
</plugin>
<plugin
filename="ignition-gazebo-scene-broadcaster-system"
name="ignition::gazebo::systems::SceneBroadcaster">
</plugin>

<light type="directional" name="sun">
<cast_shadows>true</cast_shadows>
Expand Down
17 changes: 0 additions & 17 deletions examples/worlds/shapes.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@ Try moving a model:
-->
<sdf version="1.6">
<world name="shapes">
<physics name="1ms" type="ignored">
<max_step_size>0.001</max_step_size>
<real_time_factor>1.0</real_time_factor>
</physics>
<plugin
filename="ignition-gazebo-physics-system"
name="ignition::gazebo::systems::Physics">
</plugin>
<plugin
filename="ignition-gazebo-user-commands-system"
name="ignition::gazebo::systems::UserCommands">
</plugin>
<plugin
filename="ignition-gazebo-scene-broadcaster-system"
name="ignition::gazebo::systems::SceneBroadcaster">
</plugin>

<scene>
<ambient>1.0 1.0 1.0</ambient>
<background>0.8 0.8 0.8</background>
Expand Down
2 changes: 2 additions & 0 deletions include/ignition/gazebo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ign_install_all_headers()

add_subdirectory(components)

install (FILES server.config playback_server.config DESTINATION ${IGN_DATA_INSTALL_DIR})
57 changes: 57 additions & 0 deletions include/ignition/gazebo/ServerConfig.hh
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,24 @@ namespace ignition
/// \param[in] _info Information about the plugin to load.
public: void AddPlugin(const PluginInfo &_info);

/// \brief Add multiple plugins to the simulation
/// \param[in] _info List of Information about the plugin to load.
public: void AddPlugins(const std::list<PluginInfo> &_plugins);

/// \brief Generate PluginInfo for Log recording based on the
/// internal state of this ServerConfig object:
/// \sa UseLogRecord
/// \sa LogRecordPath
/// \sa LogRecordResources
/// \sa LogRecordCompressPath
/// \sa LogRecordTopics
public: PluginInfo LogRecordPlugin() const;

/// \brief Generate PluginInfo for Log playback based on the
/// internal state of this ServerConfig object:
/// \sa LogPlaybackPath
public: PluginInfo LogPlaybackPlugin() const;

/// \brief Get all the plugins that should be loaded.
/// \return A list of all the plugins specified via
/// AddPlugin(const PluginInfo &).
Expand All @@ -370,6 +388,45 @@ namespace ignition
/// \brief Private data pointer
private: std::unique_ptr<ServerConfigPrivate> dataPtr;
};

/// \brief Parse plugins from XML configuration file.
/// \param[in] _fname Absolute path to the configuration file to parse.
/// \return A list of all of the plugins found in the configuration file
std::list<ServerConfig::PluginInfo>
IGNITION_GAZEBO_VISIBLE
parsePluginsFromFile(const std::string &_fname);

/// \brief Parse plugins from XML configuration string.
/// \param[in] _str XML configuration content to parse
/// \return A list of all of the plugins found in the configuration string.
std::list<ServerConfig::PluginInfo>
IGNITION_GAZEBO_VISIBLE
parsePluginsFromString(const std::string &_str);

/// \brief Load plugin information, following ordering.
///
/// This method is used when no plugins are found in an SDF
/// file to load either a default or custom set of plugins.
///
/// The following order is used to resolve:
/// 1. Config file located at IGN_GAZEBO_SERVER_CONFIG_PATH environment
/// variable.
/// * If IGN_GAZEBO_SERVER_CONFIG_PATH is set but empty, no plugins
/// are loaded.
/// 2. File at ${IGN_HOMEDIR}/.ignition/gazebo/server.config
/// 3. File at ${IGN_DATA_INSTALL_DIR}/server.config
///
/// If any of the above files exist but are empty, resolution
/// stops and the plugin list will be empty.
///
//
/// \param[in] _isPlayback Is the server in playback mode. If so, fallback
/// to playback_server.config.
//
/// \return A list of plugins to load, based on above ordering
std::list<ServerConfig::PluginInfo>
IGNITION_GAZEBO_VISIBLE
loadPluginInfo(bool _isPlayback = false);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions include/ignition/gazebo/Util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ namespace ignition
/// `<include>`
const std::string kSdfPathEnv{"SDF_PATH"};

/// \breif Environment variable holding server config paths.
const std::string kServerConfigPathEnv{"IGN_GAZEBO_SERVER_CONFIG_PATH"};

/// \brief Environment variable holding paths to custom rendering engine
/// plugins.
const std::string kRenderPluginPathEnv{"IGN_GAZEBO_RENDER_ENGINE_PATH"};
Expand Down
1 change: 1 addition & 0 deletions include/ignition/gazebo/config.hh.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#define IGNITION_GAZEBO_GUI_CONFIG_PATH "${CMAKE_INSTALL_PREFIX}/${IGN_DATA_INSTALL_DIR}/gui"
#define IGNITION_GAZEBO_SYSTEM_CONFIG_PATH "${CMAKE_INSTALL_PREFIX}/${IGN_DATA_INSTALL_DIR}/systems"
#define IGNITION_GAZEBO_SERVER_CONFIG_PATH "${CMAKE_INSTALL_PREFIX}/${IGN_DATA_INSTALL_DIR}"
#define IGN_GAZEBO_PLUGIN_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${IGN_LIB_INSTALL_DIR}/ign-${IGN_DESIGNATION}-${PROJECT_VERSION_MAJOR}/plugins"
#define IGN_GAZEBO_GUI_PLUGIN_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${IGN_LIB_INSTALL_DIR}/ign-${IGN_DESIGNATION}-${PROJECT_VERSION_MAJOR}/plugins/gui"
#define IGN_GAZEBO_WORLD_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${IGN_DATA_INSTALL_DIR}/worlds"
Expand Down
14 changes: 14 additions & 0 deletions include/ignition/gazebo/playback_server.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<server_config>
<plugins>
<plugin entity_name="*"
entity_type="world"
filename="ignition-gazebo-user-commands-system"
name="ignition::gazebo::systems::UserCommands">
</plugin>
<plugin entity_name="*"
entity_type="world"
filename="ignition-gazebo-scene-broadcaster-system"
name="ignition::gazebo::systems::SceneBroadcaster">
</plugin>
</plugins>
</server_config>
19 changes: 19 additions & 0 deletions include/ignition/gazebo/server.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<server_config>
<plugins>
<plugin entity_name="*"
entity_type="world"
filename="ignition-gazebo-physics-system"
name="ignition::gazebo::systems::Physics">
</plugin>
<plugin entity_name="*"
entity_type="world"
filename="ignition-gazebo-user-commands-system"
name="ignition::gazebo::systems::UserCommands">
</plugin>
<plugin entity_name="*"
entity_type="world"
filename="ignition-gazebo-scene-broadcaster-system"
name="ignition::gazebo::systems::SceneBroadcaster">
</plugin>
</plugins>
</server_config>
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ set (gtest_sources
SdfEntityCreator_TEST.cc
SdfGenerator_TEST.cc
Server_TEST.cc
ServerConfig_TEST.cc
SimulationRunner_TEST.cc
System_TEST.cc
SystemLoader_TEST.cc
Expand Down
45 changes: 3 additions & 42 deletions src/Server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,53 +59,14 @@ std::string findFuelResourceSdf(const std::string &_path)
/// \brief This struct provides access to the default world.
struct DefaultWorld
{
/// \brief Get the default plugins as a string.
/// \return An SDF string that contains the default plugins.
public: static std::string &DefaultPlugins(const ServerConfig &_config)
{
std::vector<std::string> pluginsV = {
{
std::string("<plugin filename='ignition-gazebo") +
IGNITION_GAZEBO_MAJOR_VERSION_STR + "-scene-broadcaster-system' "
"name='ignition::gazebo::systems::SceneBroadcaster'></plugin>"
},
{
std::string("<plugin filename='ignition-gazebo") +
IGNITION_GAZEBO_MAJOR_VERSION_STR + "-user-commands-system' " +
"name='ignition::gazebo::systems::UserCommands'></plugin>"
}
};

// The set of default gazebo plugins.
if (_config.LogPlaybackPath().empty())
{
pluginsV.push_back(std::string("<plugin filename='ignition-gazebo") +
IGNITION_GAZEBO_MAJOR_VERSION_STR + "-physics-system' "
"name='ignition::gazebo::systems::Physics'></plugin>");
}

// Playback plugin
else
{
pluginsV.push_back(std::string("<plugin filename='ignition-gazebo") +
IGNITION_GAZEBO_MAJOR_VERSION_STR + "-log-system' "
"name='ignition::gazebo::systems::LogPlayback'><path>" +
_config.LogPlaybackPath() + "</path></plugin>");
}

static std::string plugins = std::accumulate(pluginsV.begin(),
pluginsV.end(), std::string(""));
return plugins;
}

/// \brief Get the default world as a string.
/// Plugins will be loaded from the server.config file.
/// \return An SDF string that contains the default world.
public: static std::string &World(const ServerConfig &_config)
public: static std::string &World()
{
static std::string world = std::string("<?xml version='1.0'?>"
"<sdf version='1.6'>"
"<world name='default'>") +
DefaultPlugins(_config) +
"</world>"
"</sdf>";

Expand Down Expand Up @@ -211,7 +172,7 @@ Server::Server(const ServerConfig &_config)
ignmsg << "Loading default world.\n";
// Load an empty world.
/// \todo(nkoenig) Add a "AddWorld" function to sdf::Root.
errors = this->dataPtr->sdfRoot.LoadSdfString(DefaultWorld::World(_config));
errors = this->dataPtr->sdfRoot.LoadSdfString(DefaultWorld::World());
}

if (!errors.empty())
Expand Down
Loading

0 comments on commit e9e9f19

Please sign in to comment.