Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get updated GUI ECM info when a user presses 'play' #1109

Merged
merged 18 commits into from
Nov 6, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/SimulationRunner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
#include "SimulationRunner.hh"

#include <algorithm>
#include <mutex>

#include <ignition/common/Profiler.hh>
#include <ignition/msgs.hh>
#include <sdf/Root.hh>

#include "ignition/common/Profiler.hh"
#include "ignition/gazebo/components/Model.hh"
#include "ignition/gazebo/components/Name.hh"
#include "ignition/gazebo/components/Sensor.hh"
Expand Down Expand Up @@ -804,6 +806,43 @@ bool SimulationRunner::Run(const uint64_t _iterations)
void SimulationRunner::Step(const UpdateInfo &_info)
{
IGN_PROFILE("SimulationRunner::Step");

{
std::lock_guard<std::mutex> lock(this->guiServerEcmMutex);
if (this->matchGuiEcm)
{
// Update the server's ECM to match any changes that took place in the
// GUI's ECM during the most recent pause interval. We do this before
// processing WorldControl msgs to ensure that GUI ECM changes aren't
// lost (if the GUI and server modified the same entities/components
// while paused, the GUI changes will overwrite the server changes)
const std::string guiEcmService = "/changedGuiEcm";
msgs::SerializedState guiChanges;
bool result;
if (this->node->Request(guiEcmService, 500, guiChanges, result))
{
if (!result)
ignerr << "Error processing the [" << guiEcmService << "] service.\n";
else
{
igndbg << "Syncing server's ECM state with gui's ECM state.\n";
this->entityCompMgr.SetState(guiChanges);
}
}
else
{
ignerr << "Unable to receive any changes that occurred in the GUI "
<< "while paused.\n";
}
// TODO(anyone) notify server systems of changes made to the ECM,
// if there were any? Server systems are updated later in this method,
// so depending on how a server system is written/implemented, the system
// may automatically detect changes made to the ECM

this->matchGuiEcm = false;
}
}

this->currentInfo = _info;

// Publish info
Expand Down Expand Up @@ -1100,6 +1139,13 @@ bool SimulationRunner::OnWorldControl(const msgs::WorldControl &_req,
{
std::lock_guard<std::mutex> lock(this->msgBufferMutex);

{
std::lock_guard<std::mutex> ecmLock(this->guiServerEcmMutex);
// if we are going from pause to play, we need to update the server ECM
// with any changes that occurred to the GUI ECM while paused.
this->matchGuiEcm = this->Paused() && !_req.pause();
}

WorldControl control;
control.pause = _req.pause();

Expand Down
8 changes: 8 additions & 0 deletions src/SimulationRunner.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <functional>
#include <list>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <unordered_map>
Expand Down Expand Up @@ -601,6 +602,13 @@ namespace ignition
/// \brief True if Server::RunOnce triggered a blocking paused step
private: bool blockingPausedStepPending{false};

/// \brief Flag that indicates whether the server ECM needs to be updated
/// to match the GUI ECM
private: bool matchGuiEcm{false};

/// \brief Mutex used for reading/writing the gui ECM flag above
private: std::mutex guiServerEcmMutex;

friend class LevelManager;
};
}
Expand Down
21 changes: 21 additions & 0 deletions src/gui/GuiRunner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <ignition/fuel_tools/Interface.hh>
#include <ignition/gui/Application.hh>
#include <ignition/gui/MainWindow.hh>
#include <ignition/msgs.hh>
#include <ignition/transport/Node.hh>

// Include all components so they have first-class support
Expand Down Expand Up @@ -110,6 +111,26 @@ GuiRunner::GuiRunner(const std::string &_worldName)
QPointer<QTimer> timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &GuiRunner::UpdatePlugins);
timer->start(33);

// Advertise a service that shares the changed state of the GUI's ECM.
// "Changed" in this context means the changes that took place since the last
// simulation step
const std::string changedGuiEcmService = "/changedGuiEcm";
adlarkin marked this conversation as resolved.
Show resolved Hide resolved
std::function<bool(msgs::SerializedState &)> cb =
[this](msgs::SerializedState &_resp)
{
// since GUI plugins may update the GUI's ECM, make sure that no GUI
// plugins are updated while getting the ECM's changed state
std::lock_guard<std::mutex> lock(this->dataPtr->updateMutex);

_resp.CopyFrom(this->dataPtr->ecm.State());
return true;
};
if (!this->dataPtr->node.Advertise(changedGuiEcmService, cb))
{
ignerr << "failed to advertise the [" << changedGuiEcmService
<< "] service.\n";
}
}

/////////////////////////////////////////////////
Expand Down