Skip to content

Commit 56a74bf

Browse files
Nate Koenigchapulina
authored andcommitted
Fix GuiRunner initial state and entity spawn timing issue
Signed-off-by: Nate Koenig <[email protected]> Signed-off-by: Louise Poubel <[email protected]>
1 parent d6fbc26 commit 56a74bf

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/gui/GuiRunner.cc

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class ignition::gazebo::GuiRunner::Implementation
3939
/// \brief Update the plugins.
4040
public: void UpdatePlugins();
4141

42+
/// \brief Process new state information.
43+
/// \param[in] _msg Message containing new state.
44+
public: void ProcessState(const msgs::SerializedStepMap &_msg);
45+
4246
/// \brief Entity-component manager.
4347
public: gazebo::EntityComponentManager ecm;
4448

@@ -59,6 +63,9 @@ class ignition::gazebo::GuiRunner::Implementation
5963

6064
/// \brief The plugin update thread..
6165
public: std::thread updateThread;
66+
67+
/// \brief True if the initial state has been received and processed.
68+
public: bool receivedInitialState{false};
6269
};
6370

6471
/////////////////////////////////////////////////
@@ -146,6 +153,10 @@ void GuiRunner::RequestState()
146153
ignition::msgs::StringMsg req;
147154
req.set_data(reqSrv);
148155

156+
// Subscribe to periodic updates.
157+
this->dataPtr->node.Subscribe(this->dataPtr->stateTopic,
158+
&GuiRunner::OnState, this);
159+
149160
// send async state request
150161
this->dataPtr->node.Request(this->dataPtr->stateTopic + "_async", req);
151162
}
@@ -160,37 +171,41 @@ void GuiRunner::OnPluginAdded(const QString &)
160171
/////////////////////////////////////////////////
161172
void GuiRunner::OnStateAsyncService(const msgs::SerializedStepMap &_res)
162173
{
163-
this->OnState(_res);
174+
this->dataPtr->ProcessState(_res);
175+
this->dataPtr->receivedInitialState = true;
164176

165177
// todo(anyone) store reqSrv string in a member variable and use it here
166178
// and in RequestState()
167179
std::string id = std::to_string(gui::App()->applicationPid());
168180
std::string reqSrv =
169181
this->dataPtr->node.Options().NameSpace() + "/" + id + "/state_async";
170182
this->dataPtr->node.UnadvertiseSrv(reqSrv);
171-
172-
// Only subscribe to periodic updates after receiving initial state
173-
if (this->dataPtr->node.SubscribedTopics().empty())
174-
{
175-
this->dataPtr->node.Subscribe(this->dataPtr->stateTopic,
176-
&GuiRunner::OnState, this);
177-
}
178183
}
179184

180185
/////////////////////////////////////////////////
181186
void GuiRunner::OnState(const msgs::SerializedStepMap &_msg)
182187
{
183-
IGN_PROFILE_THREAD_NAME("GuiRunner::OnState");
188+
// Only process state updates after initial state has been received.
189+
if (!this->dataPtr->receivedInitialState)
190+
return;
191+
this->dataPtr->ProcessState(_msg);
192+
}
193+
194+
/////////////////////////////////////////////////
195+
void GuiRunner::Implementation::ProcessState(
196+
const msgs::SerializedStepMap &_msg)
197+
{
198+
IGN_PROFILE_THREAD_NAME("GuiRunner::ProcessState");
184199
IGN_PROFILE("GuiRunner::Update");
185200

186-
std::lock_guard<std::mutex> lock(this->dataPtr->updateMutex);
187-
this->dataPtr->ecm.SetState(_msg.state());
201+
std::lock_guard<std::mutex> lock(this->updateMutex);
202+
this->ecm.SetState(_msg.state());
188203

189204
// Update all plugins
190-
this->dataPtr->updateInfo = convert<UpdateInfo>(_msg.stats());
191-
this->dataPtr->UpdatePlugins();
192-
this->dataPtr->ecm.ClearNewlyCreatedEntities();
193-
this->dataPtr->ecm.ProcessRemoveEntityRequests();
205+
this->updateInfo = convert<UpdateInfo>(_msg.stats());
206+
this->UpdatePlugins();
207+
this->ecm.ClearNewlyCreatedEntities();
208+
this->ecm.ProcessRemoveEntityRequests();
194209
}
195210

196211
/////////////////////////////////////////////////

0 commit comments

Comments
 (0)