-
Notifications
You must be signed in to change notification settings - Fork 261
/
ign.cc
300 lines (250 loc) · 8.7 KB
/
ign.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
/*
* Copyright (C) 2019 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 <tinyxml2.h>
#include <cstring>
#include <ignition/common/Console.hh>
#include <ignition/common/SignalHandler.hh>
#include <ignition/gui/Application.hh>
#include <ignition/gui/MainWindow.hh>
#include "ignition/gazebo/config.hh"
#include "ignition/gazebo/gui/GuiRunner.hh"
#include "ignition/gazebo/gui/TmpIface.hh"
#include "ignition/gazebo/Server.hh"
#include "ignition/gazebo/ServerConfig.hh"
#include "ign.hh"
//////////////////////////////////////////////////
extern "C" IGNITION_GAZEBO_VISIBLE char *ignitionGazeboVersion()
{
return strdup(IGNITION_GAZEBO_VERSION_FULL);
}
//////////////////////////////////////////////////
extern "C" IGNITION_GAZEBO_VISIBLE char *gazeboVersionHeader()
{
return strdup(IGNITION_GAZEBO_VERSION_HEADER);
}
//////////////////////////////////////////////////
extern "C" IGNITION_GAZEBO_VISIBLE void cmdVerbosity(
const char *_verbosity)
{
ignition::common::Console::SetVerbosity(std::atoi(_verbosity));
}
//////////////////////////////////////////////////
extern "C" IGNITION_GAZEBO_VISIBLE const char *worldInstallDir()
{
return IGN_GAZEBO_WORLD_INSTALL_DIR;
}
//////////////////////////////////////////////////
extern "C" IGNITION_GAZEBO_VISIBLE int runServer(const char *_sdfString,
int _iterations, int _run, float _hz, int _levels, const char *_networkRole,
int _networkSecondaries, int _record, const char *_recordPath,
const char *_playback, const char *_file)
{
ignition::gazebo::ServerConfig serverConfig;
if (_recordPath != nullptr && std::strlen(_recordPath) > 0)
{
ignLogInit(_recordPath, "server_console.log");
}
else
{
ignLogInit(serverConfig.LogRecordPath(), "server_console.log");
}
ignmsg << "Ignition Gazebo Server v" << IGNITION_GAZEBO_VERSION_FULL
<< std::endl;
// Set the SDF string to user
if (_sdfString != nullptr && std::strlen(_sdfString) > 0)
{
if (!serverConfig.SetSdfString(_sdfString))
{
ignerr << "Failed to set SDF string [" << _sdfString << "]" << std::endl;
return -1;
}
}
serverConfig.SetSdfFile(_file);
// Set the update rate.
if (_hz > 0.0)
serverConfig.SetUpdateRate(_hz);
// Set whether levels should be used.
if (_levels > 0)
{
ignmsg << "Using the level system\n";
serverConfig.SetUseLevels(true);
}
if (_networkRole && std::strlen(_networkRole) > 0)
{
ignmsg << "Using the distributed simulation and levels systems\n";
serverConfig.SetNetworkRole(_networkRole);
serverConfig.SetNetworkSecondaries(_networkSecondaries);
serverConfig.SetUseLevels(true);
}
if ((_recordPath != nullptr && std::strlen(_recordPath) > 0) || _record > 0)
{
if (_playback != nullptr && std::strlen(_playback) > 0)
{
ignerr << "Both record and playback are specified. Only specify one.\n";
return -1;
}
serverConfig.SetUseLogRecord(true);
if (_recordPath != nullptr && std::strlen(_recordPath) > 0)
{
serverConfig.SetLogRecordPath(_recordPath);
}
else
{
ignmsg << "Recording states to default path\n";
}
}
if (_playback != nullptr && std::strlen(_playback) > 0)
{
if (_sdfString != nullptr && std::strlen(_sdfString) > 0)
{
ignerr << "Both an SDF file and playback flag are specified. "
<< "Only specify one.\n";
return -1;
}
else
{
ignmsg << "Playing back states" << _playback << std::endl;
serverConfig.SetLogPlaybackPath(_playback);
}
}
// Create the Gazebo server
ignition::gazebo::Server server(serverConfig);
// Run the server
server.Run(true, _iterations, _run == 0);
igndbg << "Shutting down server" << std::endl;
return 0;
}
//////////////////////////////////////////////////
extern "C" IGNITION_GAZEBO_VISIBLE int runGui()
{
ignition::common::SignalHandler sigHandler;
bool sigKilled = false;
sigHandler.AddCallback([&](const int /*_sig*/)
{
sigKilled = true;
});
ignmsg << "Ignition Gazebo GUI v" << IGNITION_GAZEBO_VERSION_FULL
<< std::endl;
// Temporary transport interface
auto tmp = std::make_unique<ignition::gazebo::TmpIface>();
int argc = 0;
char **argv = nullptr;
// Initialize Qt app
ignition::gui::Application app(argc, argv);
app.AddPluginPath(IGN_GAZEBO_GUI_PLUGIN_INSTALL_DIR);
// add import path so we can load custom modules
app.Engine()->addImportPath(IGN_GAZEBO_GUI_PLUGIN_INSTALL_DIR);
// Load configuration file
auto configPath = ignition::common::joinPaths(
IGNITION_GAZEBO_GUI_CONFIG_PATH, "gui.config");
if (!app.LoadConfig(configPath))
{
return -1;
}
// Customize window
auto win = app.findChild<ignition::gui::MainWindow *>()->QuickWindow();
win->setProperty("title", "Gazebo");
// Let QML files use TmpIface' functions and properties
auto context = new QQmlContext(app.Engine()->rootContext());
context->setContextProperty("TmpIface", tmp.get());
// Instantiate GazeboDrawer.qml file into a component
QQmlComponent component(app.Engine(), ":/Gazebo/GazeboDrawer.qml");
auto gzDrawerItem = qobject_cast<QQuickItem *>(component.create(context));
if (gzDrawerItem)
{
// C++ ownership
QQmlEngine::setObjectOwnership(gzDrawerItem, QQmlEngine::CppOwnership);
// Add to main window
auto parentDrawerItem = win->findChild<QQuickItem *>("sideDrawer");
gzDrawerItem->setParentItem(parentDrawerItem);
gzDrawerItem->setParent(app.Engine());
}
else
{
ignerr << "Failed to instantiate custom drawer, drawer will be empty"
<< std::endl;
}
// Get list of worlds
ignition::transport::Node node;
bool executed{false};
bool result{false};
unsigned int timeout{5000};
std::string service{"/gazebo/worlds"};
ignition::msgs::StringMsg_V worldsMsg;
// This loop is here to allow the server time to download resources.
// \todo(nkoenig) Async resource download. Search for "Async resource
// download in `src/Server.cc` for corresponding todo item. This todo is
// resolved when this while loop can be removed.
while (!sigKilled && !executed)
{
igndbg << "Requesting list of world names. The server may be busy "
<< "downloading resources. Please be patient." << std::endl;
executed = node.Request(service, timeout, worldsMsg, result);
}
// Only print error message if a sigkill was not received.
if (!sigKilled)
{
if (!executed)
ignerr << "Timed out when getting world names." << std::endl;
else if (!result)
ignerr << "Failed to get world names." << std::endl;
}
if (!executed || !result)
return -1;
// TODO(anyone) Parallelize this if multiple worlds becomes an important use
// case.
std::vector<ignition::gazebo::GuiRunner *> runners;
for (int w = 0; w < worldsMsg.data_size(); ++w)
{
const auto &worldName = worldsMsg.data(w);
// Request GUI info for each world
result = false;
service = std::string("/world/" + worldName + "/gui/info");
igndbg << "Requesting GUI from [" << service << "]..." << std::endl;
// Request and block
ignition::msgs::GUI res;
executed = node.Request(service, timeout, res, result);
if (!executed)
ignerr << "Service call timed out for [" << service << "]" << std::endl;
else if (!result)
ignerr << "Service call failed for [" << service << "]" << std::endl;
for (int p = 0; p < res.plugin_size(); ++p)
{
const auto &plugin = res.plugin(p);
const auto &fileName = plugin.filename();
std::string pluginStr = "<plugin filename='" + fileName + "'>" +
plugin.innerxml() + "</plugin>";
tinyxml2::XMLDocument pluginDoc;
pluginDoc.Parse(pluginStr.c_str());
ignition::gui::App()->LoadPlugin(fileName,
pluginDoc.FirstChildElement("plugin"));
}
// GUI runner
auto runner = new ignition::gazebo::GuiRunner(worldName);
runner->connect(&app, &ignition::gui::Application::PluginAdded, runner,
&ignition::gazebo::GuiRunner::OnPluginAdded);
runners.push_back(runner);
}
// Run main window.
// This blocks until the window is closed or we receive a SIGINT
app.exec();
for (auto runner : runners)
delete runner;
runners.clear();
igndbg << "Shutting down GUI" << std::endl;
return 0;
}