Skip to content

Commit 053152f

Browse files
committed
Add support for simulation reset via a publicly callable API
This allows us to reset simulations without having to call into gz-transport making the code more readable from an external API. Depends on #2647 Signed-off-by: Arjo Chakravarty <[email protected]>
1 parent 2a658f9 commit 053152f

File tree

6 files changed

+63
-5
lines changed

6 files changed

+63
-5
lines changed

include/gz/sim/Server.hh

+3
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ namespace gz
338338
/// \brief Stop the server. This will stop all running simulations.
339339
public: void Stop();
340340

341+
/// \brief Reset All runners in this simulation
342+
public: void ResetAll();
343+
341344
/// \brief Private data
342345
private: std::unique_ptr<ServerPrivate> dataPtr;
343346
};

python/src/gz/sim/Server.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ void defineSimServer(pybind11::object module)
4646
.def(
4747
"is_running",
4848
pybind11::overload_cast<>(&gz::sim::Server::Running, pybind11::const_),
49-
"Get whether the server is running.");
49+
"Get whether the server is running.")
50+
.def("reset_all", &gz::sim::Server::ResetAll,
51+
"Resets all simulation runners under this server.");
5052
}
5153
} // namespace python
5254
} // namespace sim

src/Server.cc

+11
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,17 @@ bool Server::RequestRemoveEntity(const Entity _entity,
485485
return false;
486486
}
487487

488+
//////////////////////////////////////////////////
489+
void Server::ResetAll()
490+
{
491+
for (auto worldId = 0u;
492+
worldId < this->dataPtr->simRunners.size();
493+
worldId++)
494+
{
495+
this->dataPtr->simRunners[worldId]->Reset(true, false, false);
496+
}
497+
}
498+
488499
//////////////////////////////////////////////////
489500
void Server::Stop()
490501
{

src/SimulationRunner.cc

+14-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ struct MaybeGilScopedRelease
9595
#endif
9696
}
9797

98-
9998
//////////////////////////////////////////////////
10099
SimulationRunner::SimulationRunner(const sdf::World &_world,
101100
const SystemLoaderPtr &_systemLoader,
@@ -1661,3 +1660,17 @@ void SimulationRunner::CreateEntities(const sdf::World &_world)
16611660
// Store the initial state of the ECM;
16621661
this->initialEntityCompMgr.CopyFrom(this->entityCompMgr);
16631662
}
1663+
1664+
/////////////////////////////////////////////////
1665+
void SimulationRunner::Reset(const bool all,
1666+
const bool time, const bool model)
1667+
{
1668+
WorldControl control;
1669+
std::lock_guard<std::mutex> lock(this->msgBufferMutex);
1670+
control.rewind = all || time;
1671+
if (model)
1672+
{
1673+
gzwarn << "Model reset not supported" <<std::endl;
1674+
}
1675+
this->worldControls.push_back(control);
1676+
}

src/SimulationRunner.hh

+3
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ namespace gz
369369
/// \brief Set the next step to be blocking and paused.
370370
public: void SetNextStepAsBlockingPaused(const bool value);
371371

372+
/// \brief Reset the current simulation runner
373+
public: void Reset(const bool all, const bool time, const bool model);
374+
372375
/// \brief Updates the physics parameters of the simulation based on the
373376
/// Physics component of the world, if any.
374377
public: void UpdatePhysicsParams();

src/TestFixture_TEST.cc

+29-3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ TEST_F(TestFixtureTest, Callbacks)
7272
unsigned int preUpdates{0u};
7373
unsigned int updates{0u};
7474
unsigned int postUpdates{0u};
75+
unsigned int resets{0u};
76+
7577
testFixture.
7678
OnConfigure([&](const Entity &_entity,
7779
const std::shared_ptr<const sdf::Element> &_sdf,
@@ -85,20 +87,33 @@ TEST_F(TestFixtureTest, Callbacks)
8587
{
8688
this->Updates(_info, _ecm);
8789
preUpdates++;
88-
EXPECT_EQ(preUpdates, _info.iterations);
90+
if (resets == 0)
91+
{
92+
EXPECT_EQ(preUpdates, _info.iterations);
93+
}
8994
}).
9095
OnUpdate([&](const UpdateInfo &_info, EntityComponentManager &_ecm)
9196
{
9297
this->Updates(_info, _ecm);
9398
updates++;
94-
EXPECT_EQ(updates, _info.iterations);
99+
if (resets == 0)
100+
{
101+
EXPECT_EQ(updates, _info.iterations);
102+
}
95103
}).
96104
OnPostUpdate([&](const UpdateInfo &_info,
97105
const EntityComponentManager &_ecm)
98106
{
99107
this->Updates(_info, _ecm);
100108
postUpdates++;
101-
EXPECT_EQ(postUpdates, _info.iterations);
109+
if (resets == 0)
110+
{
111+
EXPECT_EQ(postUpdates, _info.iterations);
112+
}
113+
}).
114+
OnReset([&](const UpdateInfo &, EntityComponentManager &)
115+
{
116+
resets++;
102117
}).
103118
Finalize();
104119

@@ -109,8 +124,19 @@ TEST_F(TestFixtureTest, Callbacks)
109124
EXPECT_EQ(expectedIterations, preUpdates);
110125
EXPECT_EQ(expectedIterations, updates);
111126
EXPECT_EQ(expectedIterations, postUpdates);
127+
EXPECT_EQ(0u, resets);
128+
129+
testFixture.Server()->ResetAll();
130+
131+
testFixture.Server()->Run(true, expectedIterations, false);
132+
EXPECT_EQ(1u, configures);
133+
EXPECT_EQ(expectedIterations * 2 - 1, preUpdates);
134+
EXPECT_EQ(expectedIterations * 2 - 1, updates);
135+
EXPECT_EQ(expectedIterations * 2 - 1, postUpdates);
136+
EXPECT_EQ(1u, resets);
112137
}
113138

139+
114140
/////////////////////////////////////////////////
115141
TEST_F(TestFixtureTest, LoadConfig)
116142
{

0 commit comments

Comments
 (0)