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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run server and client in the same process #793

Open
wants to merge 49 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
3a7d09c
Add scene manager GUI plugin that works with ign-gui's Scene3D
chapulina May 12, 2021
80161fb
cleanup
chapulina May 12, 2021
400974d
Added some-process flag
ahcorde May 5, 2021
ae902c3
Avoid some crashes
ahcorde Jun 2, 2021
57cf18f
Improved implementation
ahcorde Jun 3, 2021
343dba8
added more improvemetns
ahcorde Jun 4, 2021
999620d
uncomment code
ahcorde Jun 4, 2021
9498255
some fixes
ahcorde Jun 4, 2021
ea7107f
Updates
ahcorde Jun 8, 2021
cc4cf71
Updates
ahcorde Jun 8, 2021
5521b40
Added feedback
ahcorde Jun 9, 2021
b7251f4
added more feedback
ahcorde Jun 10, 2021
25ea384
make linters happy
ahcorde Jun 10, 2021
ce10db1
Fixed tests
ahcorde Jun 11, 2021
d0d7066
Merge branch 'main' into chapulina/6/scene_manager
chapulina Jun 19, 2021
b750115
Merge remote-tracking branch 'origin/chapulina/6/scene_manager' into …
ahcorde Jun 21, 2021
7a50843
Added feedback
ahcorde Jun 21, 2021
5f94fe2
Update to latest
chapulina Jun 21, 2021
9962dbf
Same process bool as a component and minor fixes
ahcorde Jun 22, 2021
84956cf
feedback
ahcorde Jun 23, 2021
c5b16d2
Merge branch 'main' into chapulina/6/scene_manager
chapulina Jun 24, 2021
5e92077
Merge remote-tracking branch 'origin/chapulina/6/scene_manager' into …
ahcorde Jun 24, 2021
51c8382
Use std::optional with SharedEntityComponentManager and SharedEventMa…
ahcorde Jun 24, 2021
195c1e4
tick-tock runGui and createGui
ahcorde Jun 24, 2021
47a87eb
make linters happy
ahcorde Jun 24, 2021
dc02d76
Using a worker pool to update plugins
ahcorde Jun 29, 2021
7f63aa6
make linters happy
ahcorde Jun 29, 2021
c3095a2
Merge branch 'main' into chapulina/6/scene_manager
ahcorde Jul 2, 2021
7e4a5f5
Restored CreateRenderingEntities in RenderUtils.cc
ahcorde Jul 2, 2021
fc23ff4
Merge remote-tracking branch 'origin/chapulina/6/scene_manager' into …
ahcorde Jul 2, 2021
5c0c29f
make linters happy
ahcorde Jul 2, 2021
247e827
Added feedback
ahcorde Jul 22, 2021
2cad0b5
make linters happy
ahcorde Jul 22, 2021
c741662
Merge branch 'main' into chapulina/6/scene_manager
chapulina Aug 9, 2021
fbceade
Don't make it official yet
chapulina Aug 9, 2021
f27887a
Merge branch 'chapulina/6/scene_manager' of ssh://github.com/ignition…
chapulina Aug 9, 2021
21e1b7b
alphabetize
chapulina Aug 9, 2021
725a744
Merge branch 'chapulina/6/scene_manager' into ahcorde/same_process
ahcorde Aug 10, 2021
8eeb5af
Fixed tests
ahcorde Aug 10, 2021
a01ebf0
Merge remote-tracking branch 'origin/main' into ahcorde/same_process
ahcorde Aug 11, 2021
af0c3f0
Windows compile fix
ahcorde Aug 12, 2021
e58339f
same process running on Scene3D
ahcorde Aug 12, 2021
97898e4
Merge branch 'main' into ahcorde/same_process
ahcorde Aug 13, 2021
e1e1c6f
try to fix Windows build
ahcorde Aug 13, 2021
78f940e
Merge branch 'main' into ahcorde/same_process
chapulina Aug 13, 2021
d6d1f8a
small cleanup tweaks, also fix OnPluginUpdate
chapulina Aug 14, 2021
74ed8e2
Merge branch 'main' into ahcorde/same_process
chapulina Aug 14, 2021
baf095e
Merge remote-tracking branch 'origin/main' into ahcorde/same_process
ahcorde Sep 21, 2021
449e6e4
make linters happy
ahcorde Sep 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions examples/worlds/minimal_scene.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ Missing for parity with GzScene3D:
-->
<sdf version="1.6">
<world name="minimal_scene">
<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-sensors-system"
name="ignition::gazebo::systems::Sensors">
<render_engine>ogre2</render_engine>
</plugin>

<gui fullscreen="0">

Expand Down Expand Up @@ -118,9 +135,6 @@ Missing for parity with GzScene3D:
<play_pause>true</play_pause>
<step>true</step>
<start_paused>true</start_paused>
<service>/world/buoyancy/control</service>
<stats_topic>/world/buoyancy/stats</stats_topic>

</plugin>

<!-- World statistics -->
Expand All @@ -144,7 +158,6 @@ Missing for parity with GzScene3D:
<real_time>true</real_time>
<real_time_factor>true</real_time_factor>
<iterations>true</iterations>
<topic>/world/buoyancy/stats</topic>
</plugin>

<!-- Insert simple shapes -->
Expand Down
25 changes: 25 additions & 0 deletions include/ignition/gazebo/Events.hh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ namespace ignition
/// the entity, which may contain multiple <plugin> tags.
using LoadPlugins = common::EventT<void(Entity, sdf::ElementPtr),
struct LoadPluginsTag>;

/// \brief Event used to emit a render event when running in one process.
/// This is required because we have two RenderUtil classes when there
/// is a render sensor in the scene (camera, depth sensor, etc).
/// We could only have one thread updating the render scene, with this
/// signal we are able to call from the GzSceneManager the render calls
/// required by the sensor
///
/// For example:
/// \code
/// eventManager.Emit<ignition::gazebo::events::Render>();
/// \endcode
using Render = ignition::common::EventT<void(void), struct RenderTag>;

/// \brief Event used to emit render event when running in one process
using EnableSensors =
ignition::common::EventT<void(bool), struct EnableSensorsTag>;

/// \brief Event used to emit a render event when running in one process.
/// Some remove events are lost because of the rate when running in the
/// same process without sensors. This event is launched in the Simulation
/// runner when there is any new entity or entity marked to be removed
/// to remove/add entities in the renderUtil
using UpdateSystems =
ignition::common::EventT<void(void), struct UpdateSystemsTag>;
Comment on lines +75 to +85
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that the documentation for these variables needs to be updated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe is not explained properly.

When running in the same process without sensors if there is an entity that we is marked to be removed, we need to call this event otherwise we will potentially lost the step where the entity is removed, because the gui loop rate is much more slower than the physics rate loop.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense 👍 I think that part that is confusing to me is that both of these events say that they emit render events, but the variable names don't seem to have anything to do with rendering. Should the part about emitting rendering events be updated?

}
} // namespace events
} // namespace gazebo
Expand Down
12 changes: 12 additions & 0 deletions include/ignition/gazebo/Server.hh
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ namespace ignition
/// \brief Destructor
public: ~Server();

/// \brief Get the Entity Component Manager reference
/// \param[in] _worldIndex Index of the world in the simrunner
/// \return The Entity Component Manager reference
public: std::optional<std::reference_wrapper<EntityComponentManager>>
SharedEntityComponentManager(const unsigned int _worldIndex = 0) const;

/// \brief Get the Event Manager reference
/// \param[in] _worldIndex Index of the world in the simrunner
/// \return The Event Manager reference
public: std::optional<std::reference_wrapper<EventManager>>
SharedEventManager(const unsigned int _worldIndex = 0) const;

/// \brief Set the update period. The update period is the wall-clock time
/// between ECS updates.
/// Note that this is different from the simulation update rate. ECS
Expand Down
10 changes: 10 additions & 0 deletions include/ignition/gazebo/ServerConfig.hh
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ namespace ignition
/// \return The full contents of the SDF string, or empty string.
public: std::string SdfString() const;

/// \brief Set if the server and GUI should run in the same process.
/// \param[in] _sameProcessAsGUI True if the server and GUI will run in
/// the same process, False otherwise
public: void SetSameProcessAsGUI(bool _sameProcessAsGUI);

/// \brief Get if the server and GUI are running in the same process
/// \return True if the server and GUI will run in
/// the same process, False otherwise
public: bool SameProcessAsGUI() const;

/// \brief Set the update rate in Hertz. Value <=0 are ignored.
/// \param[in] _hz The desired update rate of the server in Hertz.
public: void SetUpdateRate(const double &_hz);
Expand Down
43 changes: 43 additions & 0 deletions include/ignition/gazebo/components/SameProcess.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2021 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef IGNITION_GAZEBO_COMPONENTS_SAMEPROCESS_HH_
#define IGNITION_GAZEBO_COMPONENTS_SAMEPROCESS_HH_

#include <ignition/gazebo/components/Factory.hh>
#include <ignition/gazebo/components/Component.hh>
#include <ignition/gazebo/config.hh>

namespace ignition
{
namespace gazebo
{
// Inline bracket to help doxygen filtering.
inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
namespace components
{
/// \brief A component used to hold if the server and gui are running in the
/// same process.
using SameProcess = Component<bool, class SameProcessTag>;

IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.SameProcess",
SameProcess)
}
}
}
}

#endif
64 changes: 58 additions & 6 deletions include/ignition/gazebo/gui/Gui.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <ignition/gui/Application.hh>

#include "ignition/gazebo/config.hh"
#include "ignition/gazebo/EntityComponentManager.hh"
#include "ignition/gazebo/EventManager.hh"
#include "ignition/gazebo/gui/Export.hh"

namespace ignition
Expand All @@ -35,20 +37,41 @@ namespace gui
/// \brief Run GUI application
/// \param[in] _argc Number of command line arguments (Used when running
/// without ign-tools. Set to 1 if using ign-tools). Note: The object
/// referenced by this variable must continue to exist for the lifetime of the
/// application.
/// referenced by this variable must continue to exist for the lifetime of
/// the application.
/// \param[in] _argv Command line arguments (Used when running without
/// ign-tools. Set to the name of the application if using ign-tools)
/// \param[in] _guiConfig The GUI configuration file. If nullptr, the default
/// configuration from IGN_HOMEDIR/.ignition/gazebo/gui.config will be used.
IGNITION_GAZEBO_GUI_VISIBLE int runGui(int &_argc, char **_argv,
IGNITION_GAZEBO_GUI_VISIBLE int IGN_DEPRECATED(6) runGui(int &_argc,
char **_argv,
const char *_guiConfig);

/// \brief Run GUI application
/// \param[in] _argc Number of command line arguments (Used when running
/// without ign-tools. Set to 1 if using ign-tools). Note: The object
/// referenced by this variable must continue to exist for the lifetime of
/// the application.
/// \param[in] _argv Command line arguments (Used when running without
/// ign-tools. Set to the name of the application if using ign-tools)
/// \param[in] _guiConfig The GUI configuration file. If nullptr, the default
/// configuration from IGN_HOMEDIR/.ignition/gazebo/gui.config will be used.
/// \param[in] _ecm Entity-component manager.
/// \param[in] _eventMgr Event manager
/// \param[in] _sameProcess Are the server and gui running in the same
/// process?
IGNITION_GAZEBO_GUI_VISIBLE int runGui(int &_argc,
char **_argv,
const char *_guiConfig,
EntityComponentManager &_ecm,
EventManager &_eventMgr,
bool _sameProcess);

/// \brief Create a Gazebo GUI application
/// \param[in] _argc Number of command line arguments (Used when running
/// without ign-tools. Set to 1 if using ign-tools). Note: The object
/// referenced by this variable must continue to exist for the lifetime of the
/// application.
/// referenced by this variable must continue to exist for the lifetime of
/// the application.
/// \param[in] _argv Command line arguments (Used when running without
/// ign-tools. Set to the name of the application if using ign-tools)
/// \param[in] _guiConfig The GUI configuration file. If nullptr, the default
Expand All @@ -61,9 +84,38 @@ namespace gui
/// \param[in] _loadPluginsFromSdf If true, plugins specified in the world
/// SDFormat file will get loaded.
IGNITION_GAZEBO_GUI_VISIBLE
std::unique_ptr<ignition::gui::Application> IGN_DEPRECATED(6) createGui(
int &_argc, char **_argv, const char *_guiConfig,
const char *_defaultGuiConfig = nullptr, bool _loadPluginsFromSdf = true);

/// \brief Create a Gazebo GUI application
/// \param[in] _argc Number of command line arguments (Used when running
/// without ign-tools. Set to 1 if using ign-tools). Note: The object
/// referenced by this variable must continue to exist for the lifetime of the
/// application.
/// \param[in] _argv Command line arguments (Used when running without
/// ign-tools. Set to the name of the application if using ign-tools)
/// \param[in] _guiConfig The GUI configuration file. If nullptr, the default
/// configuration from IGN_HOMEDIR/.ignition/gazebo/gui.config will be used.
/// \param[in] _ecm Entity-component manager.
/// \param[in] _eventMgr Event manager
/// \param[in] _sameProcess are the server and gui running in the same
/// process ?
/// \param[in] _defaultGuiConfig The default GUI configuration file. If no
/// plugins were added from a world file or from _guiConfig, this
/// configuration file will be loaded. If this argument is a nullptr or if
/// the file does not exist, the default configuration from
/// IGN_HOMEDIR/.ignition/gazebo/gui.config will be used.
/// \param[in] _loadPluginsFromSdf If true, plugins specified in the world
/// SDFormat file will get loaded.
IGNITION_GAZEBO_GUI_VISIBLE
std::unique_ptr<ignition::gui::Application> createGui(
int &_argc, char **_argv, const char *_guiConfig,
const char *_defaultGuiConfig = nullptr, bool _loadPluginsFromSdf = true);
EntityComponentManager &_ecm,
EventManager &_eventMgr,
bool _sameProcess,
const char *_defaultGuiConfig = nullptr,
bool _loadPluginsFromSdf = true);

} // namespace gui
} // namespace IGNITION_GAZEBO_VERSION_NAMESPACE
Expand Down
10 changes: 10 additions & 0 deletions include/ignition/gazebo/gui/GuiSystem.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <ignition/gazebo/config.hh>
#include <ignition/gazebo/EntityComponentManager.hh>
#include <ignition/gazebo/EventManager.hh>
#include <ignition/gazebo/gui/Export.hh>
#include <ignition/gui/Plugin.hh>

Expand Down Expand Up @@ -53,6 +54,15 @@ namespace gazebo
/// and write entities and their components.
public: virtual void Update(const UpdateInfo &/*_info*/,
EntityComponentManager &/*_ecm*/){}

/// \brief When running in the same process GUI and server we need to
/// configure the gui system plugins with the event manager to listen
/// ecm updates and set if we are running in the same process
/// \param[in] _ecm Mutable event manager
/// \param[in] _sameProcess are the server and gui running in the same
/// process ?
public: virtual void Configure(
EventManager &/*_event*/, bool /*sameProcess*/){}
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
};
}
}
Expand Down
9 changes: 7 additions & 2 deletions include/ignition/gazebo/rendering/RenderUtil.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
#include <ignition/gazebo/rendering/Export.hh>
#include <ignition/gazebo/System.hh>

#include "ignition/gazebo/rendering/SceneManager.hh"
#include "ignition/gazebo/EventManager.hh"
#include "ignition/gazebo/rendering/MarkerManager.hh"
#include "ignition/gazebo/rendering/SceneManager.hh"


namespace ignition
Expand All @@ -44,7 +45,7 @@ inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
class IGNITION_GAZEBO_RENDERING_VISIBLE RenderUtil
{
/// \brief Constructor
public: explicit RenderUtil();
public: RenderUtil();

/// \brief Destructor
public: ~RenderUtil();
Expand All @@ -62,6 +63,10 @@ inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
/// \brief Main update function. Must be called in the rendering thread.
public: void Update();

/// brief Set the event Manager
/// \param[in] _eventMgr Reference to the event Manager
public: void SetEventManager(EventManager &_eventMgr);

/// \brief Get a pointer to the scene
/// \return Pointer to the scene
public: rendering::ScenePtr Scene() const;
Expand Down
5 changes: 5 additions & 0 deletions src/LevelManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "ignition/gazebo/components/Pose.hh"
#include "ignition/gazebo/components/RenderEngineGuiPlugin.hh"
#include "ignition/gazebo/components/RenderEngineServerPlugin.hh"
#include "ignition/gazebo/components/SameProcess.hh"
#include "ignition/gazebo/components/Scene.hh"
#include "ignition/gazebo/components/Wind.hh"
#include "ignition/gazebo/components/World.hh"
Expand Down Expand Up @@ -151,6 +152,10 @@ void LevelManager::ReadLevelPerformerInfo()
components::RenderEngineGuiPlugin(
this->runner->serverConfig.RenderEngineGui()));

this->runner->entityCompMgr.CreateComponent(this->worldEntity,
components::SameProcess(
this->runner->serverConfig.SameProcessAsGUI()));

auto worldElem = this->runner->sdfWorld->Element();

// Create Wind
Expand Down
25 changes: 25 additions & 0 deletions src/Server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include <numeric>
#include <optional>

#include <ignition/common/SystemPaths.hh>
#include <ignition/fuel_tools/Interface.hh>
Expand Down Expand Up @@ -206,6 +207,30 @@ Server::Server(const ServerConfig &_config)
/////////////////////////////////////////////////
Server::~Server() = default;

/////////////////////////////////////////////////
std::optional<std::reference_wrapper<EntityComponentManager>>
Server::SharedEntityComponentManager(const unsigned int _worldIndex) const
{
if (this->dataPtr->simRunners.size() > _worldIndex)
{
return std::reference_wrapper<EntityComponentManager>(
this->dataPtr->simRunners[_worldIndex]->EntityCompMgr());
}
return std::nullopt;
}

/////////////////////////////////////////////////
std::optional<std::reference_wrapper<EventManager>>
Server::SharedEventManager(const unsigned int _worldIndex) const
{
if (this->dataPtr->simRunners.size() > _worldIndex)
{
return std::reference_wrapper<EventManager>(
this->dataPtr->simRunners[_worldIndex]->EventMgr());
}
return std::nullopt;
}

/////////////////////////////////////////////////
bool Server::Run(const bool _blocking, const uint64_t _iterations,
const bool _paused)
Expand Down
Loading