Skip to content

Commit

Permalink
Support downloading simulation assets in the background
Browse files Browse the repository at this point in the history
Signed-off-by: Nate Koenig <[email protected]>
  • Loading branch information
nkoenig committed Nov 11, 2024
1 parent c298007 commit 34e7c86
Show file tree
Hide file tree
Showing 13 changed files with 405 additions and 121 deletions.
14 changes: 14 additions & 0 deletions include/gz/sim/ServerConfig.hh
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,20 @@ namespace gz
public: const std::chrono::time_point<std::chrono::system_clock> &
Timestamp() const;

/// \brief Set whether asset download should block the start of
/// simulation.
/// The default value is true.
/// \param[in] _set True to wait while assets download. False will
/// download assets in a background thread.
public: void SetWaitForAssets(bool _set);

/// \brief Get whether asset download should block the start of
/// simulation.
/// The default value is true.
/// \return True if simulation should wait while assets download. False
/// indicates assets should be downloaded in a separate thread.
public: bool WaitForAssets() const;

/// \brief Get the type of source
/// \return The source type.
public: SourceType Source() const;
Expand Down
1 change: 0 additions & 1 deletion src/SdfEntityCreator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ void SdfEntityCreator::CreateEntities(const sdf::World *_world,
const sdf::Model *model = _world->ModelByIndex(modelIndex);
if (levelEntityNames.empty() ||
levelEntityNames.find(model->Name()) != levelEntityNames.end())

{
Entity modelEntity = this->CreateEntities(model);

Expand Down
59 changes: 39 additions & 20 deletions src/Server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ Server::Server(const ServerConfig &_config)
config.SetCacheLocation(_config.ResourceCache());
this->dataPtr->fuelClient = std::make_unique<fuel_tools::FuelClient>(config);

// Turn off downloads so that we can do an initial parsing of the SDF
// file. This will let us get the world names, which in turn allows the
// GUI to not-block while simulation assets download.
this->dataPtr->enableDownload = false;

// Configure SDF to fetch assets from Gazebo Fuel.
sdf::setFindCallback(std::bind(&ServerPrivate::FetchResource,
this->dataPtr.get(), std::placeholders::_1));
Expand All @@ -77,37 +82,51 @@ Server::Server(const ServerConfig &_config)

addResourcePaths();

// Loads the SDF root object based on values in a ServerConfig object.
sdf::Errors errors = this->dataPtr->LoadSdfRootHelper(_config);
sdf::Root sdfRoot;

if (!errors.empty())
// Loads the SDF root object based on values in a ServerConfig object.
// Ignore the sdf::Errors returned by this function. The errors will be
// displayed later in the downloadThread.
ServerConfig cfg = _config;
cfg.SetBehaviorOnSdfErrors(
ServerConfig::SdfErrorBehavior::CONTINUE_LOADING);
sdf::Errors errors = this->dataPtr->LoadSdfRootHelper(
cfg, sdfRoot);

// Remove all the models, lights, and actors from the primary sdfRoot object
// so that they can be downloaded and added to simulation in the background.
for (uint64_t i = 0; i < sdfRoot.WorldCount(); ++i)
{
for (auto &err : errors)
gzerr << err << "\n";
if (_config.BehaviorOnSdfErrors() ==
ServerConfig::SdfErrorBehavior::EXIT_IMMEDIATELY)
{
return;
}
sdfRoot.WorldByIndex(i)->ClearModels();
sdfRoot.WorldByIndex(i)->ClearActors();
sdfRoot.WorldByIndex(i)->ClearLights();
}

// Add record plugin
if (_config.UseLogRecord())
{
this->dataPtr->AddRecordPlugin(_config);
}
// This will create the simulation runners.
this->dataPtr->CreateSimulationRunners(sdfRoot);

// Storing the sdf root. The ServerPrivate.hh header file mentions
// that other classes may keep pointers to child nodes of the root,
// so we need to keep this object around.
// However, everything seems to work fine without storing this.
// \todo(nkoenig): Look into removing the sdfRoot member variable.
this->dataPtr->sdfRoot = sdfRoot;

// Establish publishers and subscribers. Setup transport before
// downloading simulation assets so that the GUI is not blocked during
// download.
this->dataPtr->SetupTransport();

this->dataPtr->CreateEntities();
// Download the simulation assets. This function will block if
// _config.WaitForAssets() is true;
this->dataPtr->DownloadAssets(_config);

// Set the desired update period, this will override the desired RTF given in
// the world file which was parsed by CreateEntities.
// the world file which was parsed by LoadSdfRootHelper.
if (_config.UpdatePeriod())
{
this->SetUpdatePeriod(_config.UpdatePeriod().value());
}

// Establish publishers and subscribers.
this->dataPtr->SetupTransport();
}

/////////////////////////////////////////////////
Expand Down
19 changes: 18 additions & 1 deletion src/ServerConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,10 @@ class gz::sim::ServerConfigPrivate
seed(_cfg->seed),
logRecordTopics(_cfg->logRecordTopics),
isHeadlessRendering(_cfg->isHeadlessRendering),
sdfRoot(_cfg->sdfRoot),
source(_cfg->source),
behaviorOnSdfErrors(_cfg->behaviorOnSdfErrors){ }
behaviorOnSdfErrors(_cfg->behaviorOnSdfErrors),
waitForAssets(_cfg->waitForAssets) { }

// \brief The SDF file that the server should load
public: std::string sdfFile = "";
Expand Down Expand Up @@ -297,6 +299,9 @@ class gz::sim::ServerConfigPrivate
/// \brief Server loading behavior in presence of SDF errors.
public: ServerConfig::SdfErrorBehavior behaviorOnSdfErrors{
ServerConfig::SdfErrorBehavior::EXIT_IMMEDIATELY};

/// \brief True to block while simulation assets download.
public: bool waitForAssets = true;
};

//////////////////////////////////////////////////
Expand Down Expand Up @@ -737,6 +742,18 @@ ServerConfig::Timestamp() const
return this->dataPtr->timestamp;
}

/////////////////////////////////////////////////
void ServerConfig::SetWaitForAssets(bool _set)
{
this->dataPtr->waitForAssets = _set;
}

/////////////////////////////////////////////////
bool ServerConfig::WaitForAssets() const
{
return this->dataPtr->waitForAssets;
}

/////////////////////////////////////////////////
void ServerConfig::AddLogRecordTopic(const std::string &_topic)
{
Expand Down
Loading

0 comments on commit 34e7c86

Please sign in to comment.