-
Notifications
You must be signed in to change notification settings - Fork 44
/
WorldControl.cc
353 lines (300 loc) · 10.4 KB
/
WorldControl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright (C) 2017 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.
*
*/
#include "WorldControl.hh"
#include <string>
#include <gz/msgs/boolean.pb.h>
#include <gz/msgs/world_control.pb.h>
#include <gz/msgs/world_stats.pb.h>
#include <gz/common/Console.hh>
#include <gz/common/StringUtils.hh>
#include <gz/plugin/Register.hh>
#include <gz/transport/Node.hh>
#include "gz/gui/Application.hh"
#include "gz/gui/Helpers.hh"
#include "gz/gui/GuiEvents.hh"
#include "gz/gui/MainWindow.hh"
namespace gz::gui::plugins
{
class WorldControl::Implementation
{
/// \brief Send the world control event or call the control service.
/// \param[in] _msg Message to send.
public: void SendEventMsg(const gz::msgs::WorldControl &_msg);
/// \brief Message holding latest world statistics
public: gz::msgs::WorldStatistics msg;
/// \brief Service to send world control requests
public: std::string controlService;
/// \brief Mutex to protect msg
public: std::recursive_mutex mutex;
/// \brief Communication node
public: gz::transport::Node node;
/// \brief The multi step value
public: unsigned int multiStep = 1u;
/// \brief True for paused
public: bool pause{true};
/// \brief The paused state of the most recently received world stats msg
/// (true for paused)
public: bool lastStatsMsgPaused{true};
/// \brief Whether server communication should occur through an event (true)
/// or service (false). The service option was used by default for
/// gz-gui7 and earlier, and now uses the event by default in gz-gui8.
public: bool useEvent{true};
};
/////////////////////////////////////////////////
WorldControl::WorldControl()
: dataPtr(gz::utils::MakeUniqueImpl<Implementation>())
{
}
/////////////////////////////////////////////////
WorldControl::~WorldControl() = default;
/////////////////////////////////////////////////
void WorldControl::LoadConfig(const tinyxml2::XMLElement *_pluginElem)
{
// Default name in case user didn't define one
if (this->title.empty())
this->title = "World control";
// Create elements from configuration
if (!_pluginElem)
{
gzerr << "Null plugin element." << std::endl;
return;
}
// If no elements were set, show all buttons. We assume that the user never
// wants to hide all buttons. This happens for example when the plugin is
// inserted from the menu.
if (_pluginElem->NoChildren())
{
this->PluginItem()->setProperty("showPlay", true);
this->PluginItem()->setProperty("showStep", true);
}
// World name from window, to construct default topics and services
std::string worldName;
auto worldNames = gui::worldNames();
if (!worldNames.empty())
worldName = worldNames[0].toStdString();
// For world control requests
auto serviceElem = _pluginElem->FirstChildElement("service");
if (nullptr != serviceElem && nullptr != serviceElem->GetText())
this->dataPtr->controlService = serviceElem->GetText();
// Service specified with different world name
auto parts = common::Split(this->dataPtr->controlService, '/');
if (!worldName.empty() &&
parts.size() == 4 &&
parts[0] == "" &&
parts[1] == "world" &&
parts[2] != worldName &&
parts[3] == "control")
{
gzwarn << "Ignoring service [" << this->dataPtr->controlService
<< "], world name different from [" << worldName
<< "]. Fix or remove your <service> tag." << std::endl;
this->dataPtr->controlService = "/world/" + worldName + "/control";
}
// Service unspecified, use world name
if (this->dataPtr->controlService.empty())
{
if (worldName.empty())
{
gzerr << "Must specify a <service> for world control requests, or set "
<< "the MainWindow's [worldNames] property." << std::endl;
return;
}
this->dataPtr->controlService = "/world/" + worldName + "/control";
}
this->dataPtr->controlService = transport::TopicUtils::AsValidTopic(
this->dataPtr->controlService);
if (this->dataPtr->controlService.empty())
{
gzerr << "Failed to create valid control service for world [" << worldName
<< "]" << std::endl;
}
gzmsg << "Using world control service [" << this->dataPtr->controlService
<< "]" << std::endl;
// Play / pause buttons
if (const auto *playElem = _pluginElem->FirstChildElement("play_pause"))
{
auto has = false;
playElem->QueryBoolText(&has);
this->PluginItem()->setProperty("showPlay", has);
if (has)
{
auto startPaused = this->dataPtr->pause;
if (const auto *pausedElem =
_pluginElem->FirstChildElement("start_paused"))
{
pausedElem->QueryBoolText(&startPaused);
}
this->dataPtr->pause = startPaused;
this->dataPtr->lastStatsMsgPaused = startPaused;
if (startPaused)
emit this->paused();
else
emit this->playing();
}
}
// Step buttons
if (const auto *stepElem = _pluginElem->FirstChildElement("step"))
{
auto has = false;
stepElem->QueryBoolText(&has);
this->PluginItem()->setProperty("showStep", has);
}
// Subscribe to world stats
std::string statsTopic;
const auto *statsTopicElem = _pluginElem->FirstChildElement("stats_topic");
if (nullptr != statsTopicElem && nullptr != statsTopicElem->GetText())
statsTopic = statsTopicElem->GetText();
// Service specified with different world name
parts = common::Split(statsTopic, '/');
if (!worldName.empty() &&
parts.size() == 4 &&
parts[0] == "" &&
parts[1] == "world" &&
parts[2] != worldName &&
parts[3] == "stats")
{
gzwarn << "Ignoring topic [" << statsTopic
<< "], world name different from [" << worldName
<< "]. Fix or remove your <stats_topic> tag." << std::endl;
statsTopic = "/world/" + worldName + "/stats";
}
if (statsTopic.empty() && !worldName.empty())
{
statsTopic = "/world/" + worldName + "/stats";
}
statsTopic = transport::TopicUtils::AsValidTopic(statsTopic);
if (!statsTopic.empty())
{
// Subscribe to world_stats
if (!this->dataPtr->node.Subscribe(statsTopic,
&WorldControl::OnWorldStatsMsg, this))
{
gzerr << "Failed to subscribe to [" << statsTopic << "]" << std::endl;
}
else
{
gzmsg << "Listening to stats on [" << statsTopic << "]" << std::endl;
}
}
else
{
gzerr << "Failed to create valid topic for world [" << worldName << "]"
<< std::endl;
}
if (const auto *elem = _pluginElem->FirstChildElement("use_event"))
elem->QueryBoolText(&this->dataPtr->useEvent);
if (this->dataPtr->useEvent)
gzdbg << "Using an event to share WorldControl msgs with the server\n";
else
gzdbg << "Using a service to share WorldControl msgs with the server\n";
}
/////////////////////////////////////////////////
void WorldControl::ProcessMsg()
{
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->mutex);
// ignore the message if it's associated with a step
const auto &header = this->dataPtr->msg.header();
if (this->dataPtr->msg.stepping() ||
// (deprecated) Remove this check in Gazebo H
((header.data_size() > 0) && (header.data(0).key() == "step")))
{
return;
}
// If the pause state of the message doesn't match the pause state of this
// plugin, then play/pause must have occurred elsewhere (for example, the
// command line). If the pause state of the message matches the pause state
// of this plugin, but the pause state of the message differs from the
// previous message's pause state, this means that a pause/play request from
// this plugin has been registered by the server
if (this->dataPtr->msg.paused() &&
(!this->dataPtr->pause || !this->dataPtr->lastStatsMsgPaused))
emit this->paused();
else if (!this->dataPtr->msg.paused() &&
(this->dataPtr->pause || this->dataPtr->lastStatsMsgPaused))
emit this->playing();
this->dataPtr->pause = this->dataPtr->msg.paused();
this->dataPtr->lastStatsMsgPaused = this->dataPtr->msg.paused();
}
/////////////////////////////////////////////////
void WorldControl::OnWorldStatsMsg(const msgs::WorldStatistics &_msg)
{
std::lock_guard<std::recursive_mutex> lock(this->dataPtr->mutex);
this->dataPtr->msg.CopyFrom(_msg);
QMetaObject::invokeMethod(this, "ProcessMsg");
}
/////////////////////////////////////////////////
void WorldControl::OnPlay()
{
msgs::WorldControl msg;
msg.set_pause(false);
this->dataPtr->pause = false;
this->dataPtr->SendEventMsg(msg);
}
/////////////////////////////////////////////////
void WorldControl::OnPause()
{
msgs::WorldControl msg;
msg.set_pause(true);
this->dataPtr->pause = true;
this->dataPtr->SendEventMsg(msg);
}
/////////////////////////////////////////////////
void WorldControl::OnReset()
{
msgs::WorldControl msg;
auto *msgReset = new msgs::WorldReset();
msgReset->set_all(true);
msg.set_pause(true);
msg.set_allocated_reset(msgReset);
this->dataPtr->SendEventMsg(msg);
}
/////////////////////////////////////////////////
void WorldControl::OnStepCount(const unsigned int _steps)
{
this->dataPtr->multiStep = _steps;
}
/////////////////////////////////////////////////
void WorldControl::OnStep()
{
msgs::WorldControl msg;
msg.set_pause(this->dataPtr->pause);
msg.set_multi_step(this->dataPtr->multiStep);
this->dataPtr->SendEventMsg(msg);
}
/////////////////////////////////////////////////
void WorldControl::Implementation::SendEventMsg(const msgs::WorldControl &_msg)
{
if (this->useEvent)
{
gui::events::WorldControl event(_msg);
App()->sendEvent(App()->findChild<MainWindow *>(), &event);
}
else
{
std::function<void(const msgs::Boolean &, const bool)> cb =
[](const msgs::Boolean &/*_rep*/, const bool /*_result*/)
{
// the service CB is empty because updates are handled in
// WorldControl::ProcessMsg
};
this->node.Request(this->controlService, _msg, cb);
}
}
} // namespace gz::gui::plugins
// Register this plugin
GZ_ADD_PLUGIN(gz::gui::plugins::WorldControl,
gz::gui::Plugin)