Skip to content

Commit 10ed22b

Browse files
authored
Add interface to allow systems to declare parameters (#1431)
This adds the ISystemConfigureParameters interface, which allows systems to declare parameters. This uses a transport::parameters::ParametersRegistry and creates services for listing, getting, and setting parameters. Based on gazebosim/gz-transport#305. Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
1 parent 3830bbb commit 10ed22b

File tree

8 files changed

+100
-14
lines changed

8 files changed

+100
-14
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ set(IGN_PLUGIN_VER ${ignition-plugin1_VERSION_MAJOR})
7070

7171
#--------------------------------------
7272
# Find ignition-transport
73-
ign_find_package(ignition-transport11 REQUIRED COMPONENTS log)
73+
ign_find_package(ignition-transport11 REQUIRED COMPONENTS log parameters)
7474
set(IGN_TRANSPORT_VER ${ignition-transport11_VERSION_MAJOR})
7575

7676
#--------------------------------------

include/ignition/gazebo/System.hh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <ignition/gazebo/Export.hh>
2626
#include <ignition/gazebo/Types.hh>
2727

28+
#include <ignition/transport/parameters/Registry.hh>
29+
2830
#include <sdf/Element.hh>
2931

3032
namespace ignition
@@ -100,6 +102,20 @@ namespace ignition
100102
EventManager &_eventMgr) = 0;
101103
};
102104

105+
/// \class ISystemConfigureParameters ISystem.hh ignition/gazebo/System.hh
106+
/// \brief Interface for a system that declares parameters.
107+
///
108+
/// ISystemConfigureParameters::ConfigureParameters is called after
109+
/// ISystemConfigure::Configure.
110+
class ISystemConfigureParameters {
111+
/// \brief Configure the parameters of the system.
112+
/// \param[in] _registry The parameter registry.
113+
public: virtual void ConfigureParameters(
114+
ignition::transport::parameters::ParametersRegistry &
115+
_registry,
116+
EntityComponentManager &_ecm) = 0;
117+
};
118+
103119
/// \class ISystemPreUpdate ISystem.hh ignition/gazebo/System.hh
104120
/// \brief Interface for a system that uses the PreUpdate phase
105121
class ISystemPreUpdate {

src/SimulationRunner.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ SimulationRunner::SimulationRunner(const sdf::World *_world,
6161
// Keep world name
6262
this->worldName = _world->Name();
6363

64+
this->parametersRegistry = std::make_unique<
65+
ignition::transport::parameters::ParametersRegistry>(
66+
std::string{"world/"} + this->worldName);
67+
6468
// Get the physics profile
6569
// TODO(luca): remove duplicated logic in SdfEntityCreator and LevelManager
6670
auto physics = _world->PhysicsByIndex(0);
@@ -132,8 +136,9 @@ SimulationRunner::SimulationRunner(const sdf::World *_world,
132136
this->node = std::make_unique<transport::Node>(opts);
133137

134138
// Create the system manager
135-
this->systemMgr = std::make_unique<SystemManager>(_systemLoader,
136-
&this->entityCompMgr, &this->eventMgr, validNs);
139+
this->systemMgr = std::make_unique<SystemManager>(
140+
_systemLoader, &this->entityCompMgr, &this->eventMgr, validNs,
141+
this->parametersRegistry.get());
137142

138143
this->pauseConn = this->eventMgr.Connect<events::Pause>(
139144
std::bind(&SimulationRunner::SetPaused, this, std::placeholders::_1));

src/SimulationRunner.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,11 @@ namespace ignition
396396
/// Note: must be before EntityComponentManager
397397
private: EventManager eventMgr;
398398

399+
/// \brief Manager all parameters
400+
private: std::unique_ptr<
401+
ignition::transport::parameters::ParametersRegistry
402+
> parametersRegistry;
403+
399404
/// \brief Manager of all components.
400405
private: EntityComponentManager entityCompMgr;
401406

src/SystemInternal.hh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ namespace ignition
4545
: systemPlugin(std::move(_systemPlugin)),
4646
system(systemPlugin->QueryInterface<System>()),
4747
configure(systemPlugin->QueryInterface<ISystemConfigure>()),
48+
configureParameters(
49+
systemPlugin->QueryInterface<ISystemConfigureParameters>()),
4850
preupdate(systemPlugin->QueryInterface<ISystemPreUpdate>()),
4951
update(systemPlugin->QueryInterface<ISystemUpdate>()),
5052
postupdate(systemPlugin->QueryInterface<ISystemPostUpdate>()),
@@ -60,6 +62,8 @@ namespace ignition
6062
: systemShared(_system),
6163
system(_system.get()),
6264
configure(dynamic_cast<ISystemConfigure *>(_system.get())),
65+
configureParameters(
66+
dynamic_cast<ISystemConfigureParameters *>(_system.get())),
6367
preupdate(dynamic_cast<ISystemPreUpdate *>(_system.get())),
6468
update(dynamic_cast<ISystemUpdate *>(_system.get())),
6569
postupdate(dynamic_cast<ISystemPostUpdate *>(_system.get())),
@@ -83,6 +87,11 @@ namespace ignition
8387
/// Will be nullptr if the System doesn't implement this interface.
8488
public: ISystemConfigure *configure = nullptr;
8589

90+
/// \brief Access this system via the ISystemConfigureParameters
91+
/// interface.
92+
/// Will be nullptr if the System doesn't implement this interface.
93+
public: ISystemConfigureParameters *configureParameters = nullptr;
94+
8695
/// \brief Access this system via the ISystemPreUpdate interface
8796
/// Will be nullptr if the System doesn't implement this interface.
8897
public: ISystemPreUpdate *preupdate = nullptr;

src/SystemManager.cc

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ using namespace ignition;
2828
using namespace gazebo;
2929

3030
//////////////////////////////////////////////////
31-
SystemManager::SystemManager(const SystemLoaderPtr &_systemLoader,
32-
EntityComponentManager *_entityCompMgr,
33-
EventManager *_eventMgr,
34-
const std::string &_namespace)
31+
SystemManager::SystemManager(
32+
const SystemLoaderPtr &_systemLoader,
33+
EntityComponentManager *_entityCompMgr,
34+
EventManager *_eventMgr,
35+
const std::string &_namespace,
36+
ignition::transport::parameters::ParametersRegistry *_parametersRegistry)
3537
: systemLoader(_systemLoader),
3638
entityCompMgr(_entityCompMgr),
37-
eventMgr(_eventMgr)
39+
eventMgr(_eventMgr),
40+
parametersRegistry(_parametersRegistry)
3841
{
3942
transport::NodeOptions opts;
4043
opts.SetNameSpace(_namespace);
@@ -102,6 +105,9 @@ size_t SystemManager::ActivatePendingSystems()
102105
if (system.configure)
103106
this->systemsConfigure.push_back(system.configure);
104107

108+
if (system.configureParameters)
109+
this->systemsConfigureParameters.push_back(system.configureParameters);
110+
105111
if (system.preupdate)
106112
this->systemsPreupdate.push_back(system.preupdate);
107113

@@ -169,6 +175,16 @@ void SystemManager::AddSystemImpl(
169175
*this->eventMgr);
170176
}
171177

178+
// Configure the system parameters, if necessary
179+
if (
180+
_system.configureParameters && this->entityCompMgr &&
181+
this->parametersRegistry)
182+
{
183+
_system.configureParameters->ConfigureParameters(
184+
*this->parametersRegistry,
185+
*this->entityCompMgr);
186+
}
187+
172188
// Update callbacks will be handled later, add to queue
173189
std::lock_guard<std::mutex> lock(this->pendingSystemsMutex);
174190
this->pendingSystems.push_back(_system);
@@ -180,6 +196,13 @@ const std::vector<ISystemConfigure *>& SystemManager::SystemsConfigure()
180196
return this->systemsConfigure;
181197
}
182198

199+
//////////////////////////////////////////////////
200+
const std::vector<ISystemConfigureParameters *>&
201+
SystemManager::SystemsConfigureParameters()
202+
{
203+
return this->systemsConfigureParameters;
204+
}
205+
183206
//////////////////////////////////////////////////
184207
const std::vector<ISystemPreUpdate *>& SystemManager::SystemsPreUpdate()
185208
{

src/SystemManager.hh

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ namespace ignition
5252
/// \param[in] _eventMgr Pointer to the event manager to be used when
5353
/// configuring new systems
5454
/// \param[in] _namespace Namespace to use for the transport node
55-
public: explicit SystemManager(const SystemLoaderPtr &_systemLoader,
56-
EntityComponentManager *_entityCompMgr = nullptr,
57-
EventManager *_eventMgr = nullptr,
58-
const std::string &_namespace = std::string());
55+
public: explicit SystemManager(
56+
const SystemLoaderPtr &_systemLoader,
57+
EntityComponentManager *_entityCompMgr = nullptr,
58+
EventManager *_eventMgr = nullptr,
59+
const std::string &_namespace = std::string(),
60+
ignition::transport::parameters::ParametersRegistry *
61+
_parametersRegistry = nullptr);
5962

6063
/// \brief Load system plugin for a given entity.
6164
/// \param[in] _entity Entity
@@ -99,6 +102,12 @@ namespace ignition
99102
/// \return Vector of systems's configure interfaces.
100103
public: const std::vector<ISystemConfigure *>& SystemsConfigure();
101104

105+
/// \brief Get an vector of all active systems implementing
106+
/// "ConfigureParameters"
107+
/// \return Vector of systems's configure interfaces.
108+
public: const std::vector<ISystemConfigureParameters *>&
109+
SystemsConfigureParameters();
110+
102111
/// \brief Get an vector of all active systems implementing "PreUpdate"
103112
/// \return Vector of systems's pre-update interfaces.
104113
public: const std::vector<ISystemPreUpdate *>& SystemsPreUpdate();
@@ -162,6 +171,10 @@ namespace ignition
162171
/// \brief Systems implementing Configure
163172
private: std::vector<ISystemConfigure *> systemsConfigure;
164173

174+
/// \brief Systems implementing ConfigureParameters
175+
private: std::vector<ISystemConfigureParameters *>
176+
systemsConfigureParameters;
177+
165178
/// \brief Systems implementing PreUpdate
166179
private: std::vector<ISystemPreUpdate *> systemsPreupdate;
167180

@@ -191,6 +204,10 @@ namespace ignition
191204

192205
/// \brief Node for communication.
193206
private: std::unique_ptr<transport::Node> node{nullptr};
207+
208+
/// \brief Pointer to associated parameters registry
209+
private: ignition::transport::parameters::ParametersRegistry *
210+
parametersRegistry;
194211
};
195212
}
196213
} // namespace gazebo

src/SystemManager_TEST.cc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ using namespace ignition::gazebo;
3131
/////////////////////////////////////////////////
3232
class SystemWithConfigure:
3333
public System,
34-
public ISystemConfigure
34+
public ISystemConfigure,
35+
public ISystemConfigureParameters
3536
{
3637
// Documentation inherited
3738
public: void Configure(
@@ -40,7 +41,12 @@ class SystemWithConfigure:
4041
EntityComponentManager &,
4142
EventManager &) override { configured++; };
4243

44+
public: void ConfigureParameters(
45+
ignition::transport::parameters::ParametersRegistry &,
46+
EntityComponentManager &) override { configuredParameters++; }
47+
4348
public: int configured = 0;
49+
public: int configuredParameters = 0;
4450
};
4551

4652
/////////////////////////////////////////////////
@@ -99,6 +105,7 @@ TEST(SystemManager, AddSystemNoEcm)
99105

100106
// SystemManager without an ECM/EventmManager will mean no config occurs
101107
EXPECT_EQ(0, configSystem->configured);
108+
EXPECT_EQ(0, configSystem->configuredParameters);
102109

103110
EXPECT_EQ(0u, systemMgr.ActiveCount());
104111
EXPECT_EQ(1u, systemMgr.PendingCount());
@@ -150,7 +157,10 @@ TEST(SystemManager, AddSystemEcm)
150157
auto ecm = EntityComponentManager();
151158
auto eventManager = EventManager();
152159

153-
SystemManager systemMgr(loader, &ecm, &eventManager);
160+
auto paramRegistry = std::make_unique<
161+
ignition::transport::parameters::ParametersRegistry>("SystemManager_TEST");
162+
SystemManager systemMgr(
163+
loader, &ecm, &eventManager, std::string(), paramRegistry.get());
154164

155165
EXPECT_EQ(0u, systemMgr.ActiveCount());
156166
EXPECT_EQ(0u, systemMgr.PendingCount());
@@ -165,6 +175,7 @@ TEST(SystemManager, AddSystemEcm)
165175

166176
// Configure called during AddSystem
167177
EXPECT_EQ(1, configSystem->configured);
178+
EXPECT_EQ(1, configSystem->configuredParameters);
168179

169180
EXPECT_EQ(0u, systemMgr.ActiveCount());
170181
EXPECT_EQ(1u, systemMgr.PendingCount());

0 commit comments

Comments
 (0)