-
Notifications
You must be signed in to change notification settings - Fork 13
/
WorldUtils.cc
414 lines (322 loc) · 13.4 KB
/
WorldUtils.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* Copyright (C) 2018 João Borrego
*
* 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.
*/
/*!
\file world_utils/WorldUtils.cc
\brief World Utils plugin implementation
A custom gazebo plugin that provides an interface to programatically
interact with the World object.
\author João Borrego : jsbruglie
\author Rui Figueiredo : ruipimentelfigueiredo
*/
#include "WorldUtils.hh"
namespace gazebo {
// Register this plugin with the simulator
GZ_REGISTER_WORLD_PLUGIN(WorldUtils)
WorldUtils::WorldUtils() : WorldPlugin(){
gzmsg << "[WorldUtils] Loaded world tools." << std::endl;
}
void WorldUtils::Load(physics::WorldPtr _world, sdf::ElementPtr _sdf){
// Plugin parameters
this->world = _world;
// Subscriber setup
this->node = transport::NodePtr(new transport::Node());
this->node->Init(this->world->Name());
// Setup publisher for the gazebo request topic
this->request_pub = this->node->Advertise<msgs::Request>("~/request");
// Setup publisher for the modify light request topic
this->light_pub = this->node->Advertise<msgs::Light>("~/light/modify");
// Subcribe to the request topic
this->sub = this->node->Subscribe(REQUEST_TOPIC, &WorldUtils::onRequest, this);
// Setup publisher for the response topic
this->pub = this->node->
Advertise<gap::msgs::WorldUtilsResponse>(RESPONSE_TOPIC);
// Setup regular expression used for texture replacement
this->script_reg = std::regex(REGEX_XML_SCRIPT);
// Setup regular expression used for pose replacement
this->pose_reg = std::regex(REGEX_XML_POSE);
// Connect to the world update signal
this->updateConnection = event::Events::ConnectPreRender(
std::bind(&WorldUtils::onUpdate, this));
}
/////////////////////////////////////////////////
void WorldUtils::onUpdate()
{
gap::msgs::WorldUtilsResponse msg;
bool moved = false;
std::lock_guard<std::mutex> lock(this->mutex);
// Process queue of objects with pending move
while (! this->move_queue.empty())
{
MoveObject mv_obj = this->move_queue.front();
if (mv_obj.is_light) {
physics::LightPtr light = this->world->LightByName(mv_obj.name);
if (light) {
msgs::Light msg;
msg.set_name(mv_obj.name);
gazebo::msgs::Pose *pose_msg = new gazebo::msgs::Pose();
gazebo::msgs::Set(pose_msg, mv_obj.pose);
msg.set_allocated_pose(pose_msg);
this->light_pub->Publish(msg);
/*
gzdbg << "Moving light " << light->GetName()
<< " to " << mv_obj.pose << std::endl;
*/
}
} else {
physics::ModelPtr model = this->world->ModelByName(mv_obj.name);
if (model) {
model->SetWorldPose(mv_obj.pose);
/*
gzdbg << "Moving model " << model->GetName()
<< " to " << mv_obj.pose << std::endl;
*/
}
}
this->move_queue.pop();
moved = true;
}
// TODO - report each object individually
// Report sucess in move
if (moved) {
msg.set_type(SUCCESS);
this->pub->Publish(msg);
}
}
/////////////////////////////////////////////////
void WorldUtils::onRequest(WorldUtilsRequestPtr &_msg){
/// TODO - better structure
int type;
int model_type;
std::string name;
ignition::math::Vector3d pos(0,0,0);
ignition::math::Quaterniond ori(0,0,0,0);
double mass;
std::string texture_uri;
std::string texture_name;
double radius;
double length;
ignition::math::Vector3d box_size(0,0,0);
std::string sdf_string;
type = (_msg->has_type())? (_msg->type()) : -1;
if (type == SPAWN){
for (int i = 0; i < _msg->object_size(); i++){
model_type = (_msg->object(i).has_model_type())?
(_msg->object(i).model_type()) : -1;
/// Extract parameters from message
if (_msg->object(i).has_pose()){
pos = msgs::ConvertIgn(_msg->object(i).pose().position());
ori = msgs::ConvertIgn(_msg->object(i).pose().orientation());
}
if (_msg->object(i).has_mass()){
mass = _msg->object(i).mass();
}
if (model_type == SPHERE){
name = _msg->object(i).has_name()?
_msg->object(i).name() : "plugin_sphere_" + std::to_string(this->sphere_counter++);
radius = _msg->object(i).has_radius()?
_msg->object(i).radius() : 1.0;
sdf_string = genSphere(name, mass, radius, pos, ori);
} else if (model_type == CYLINDER){
name = _msg->object(i).has_name()?
_msg->object(i).name() : "plugin_cylinder_" + std::to_string(this->cylinder_counter++);
radius = _msg->object(i).has_radius()?
_msg->object(i).radius() : 1.0;
length = _msg->object(i).has_length()?
_msg->object(i).length() : 1.0;
sdf_string = genCylinder(name, mass, radius, length, pos, ori);
} else if (model_type == BOX){
name = _msg->object(i).has_name()?
_msg->object(i).name() : "plugin_box_" + std::to_string(this->box_counter++);
if (_msg->object(i).has_box_size())
box_size = msgs::ConvertIgn(_msg->object(i).box_size());
sdf_string = genBox(name, mass, box_size, pos, ori);
} else if (model_type == CUSTOM || model_type == CUSTOM_LIGHT){
sdf_string = _msg->object(i).has_sdf()?
_msg->object(i).sdf() : "";
} else if (model_type == MODEL){
if (_msg->object(i).has_name()){
name = "model://" +_msg->object(i).name();
this->world->InsertModelFile(name);
}
}
/// If a spawn message was requested
if (!sdf_string.empty()){
std::ostringstream model_str;
if (model_type != CUSTOM && model_type != CUSTOM_LIGHT) {
/// Enclose in sdf xml tags
model_str << "<sdf version='" << SDF_VERSION << "'>"
<< sdf_string << "</sdf>";
} else {
/// Regex to modify pose string in custom model
if (_msg->object(i).has_pose()){
ignition::math::Vector3d rpy = ori.Euler();
std::ostringstream pose_xml;
pose_xml <<
"<pose>" <<
pos.X() << " " << pos.Y() << " " << pos.Z() << " " <<
rpy.X() << " " << rpy.Y() << " " << rpy.Z() <<
"</pose>";
std::string new_model_str = std::regex_replace(
sdf_string, this->pose_reg, pose_xml.str());
model_str << new_model_str;
} else {
model_str << sdf_string;
}
}
std::string new_model_str;
if (_msg->object(i).has_texture_uri() && _msg->object(i).has_texture_name()){
/// Change material script in string
texture_uri = _msg->object(i).texture_uri();
texture_name = _msg->object(i).texture_name();
std::string texture_str =
"<script><uri>" + texture_uri + "</uri>" +
"<name>" + texture_name + "</name></script>";
new_model_str = std::regex_replace(
model_str.str(), this->script_reg, texture_str);
} else {
new_model_str = model_str.str();
}
// Insert model in World
sdf::SDF objectSDF;
objectSDF.SetFromString(new_model_str);
this->world->InsertModelSDF(objectSDF);
}
}
} else if (type == MOVE) {
for (int i = 0; i < _msg->object_size(); i++)
{
model_type = (_msg->object(i).has_model_type())? (_msg->object(i).model_type()) : -1;
if (_msg->object(i).has_name() && _msg->object(i).has_pose()) {
std::string name(_msg->object(i).name());
msgs::Pose m_pose = _msg->object(i).pose();
ignition::math::Pose3d pose(msgs::ConvertIgn(m_pose));
std::lock_guard<std::mutex> lock(this->mutex);
bool is_light = (model_type == CUSTOM_LIGHT);
this->move_queue.emplace(name, is_light, pose);
}
}
} else if (type == REMOVE) {
if(_msg->object_size() > 0) {
for (int i = 0; i < _msg->object_size(); i++) {
model_type = (_msg->object(i).has_model_type())? (_msg->object(i).model_type()) : -1;
if (_msg->object(i).has_name()){
// Clear specific object(s)
clearMatching(_msg->object(i).name() , (model_type == CUSTOM_LIGHT));
} else {
// Clear everything
clearWorld();
}
}
} else {
clearWorld();
}
}
else if (type == PHYSICS) {
// Toggle world physics
bool state = (_msg->has_state())?
_msg->state() : !this->world->PhysicsEnabled();
this->world->SetPhysicsEnabled(state);
gzdbg << "[WorldUtils] Physics state "
<< this->world->PhysicsEnabled() << std::endl;
} else if (type == PAUSE) {
// Pause or unpause the world
bool state = (_msg->has_state())?
_msg->state() : !this->world->IsPaused();;
this->world->SetPaused(state);
} else if (type == STATUS) {
// Return total count of models and lights in the world
gap::msgs::WorldUtilsResponse msg;
int model_count = this->world->ModelCount();
int light_count = this->world->LightCount();
msg.set_type(INFO);
msg.set_object_count(model_count + light_count);
pub->Publish(msg,true);
}
}
/////////////////////////////////////////////////
void WorldUtils::clearWorld(){
this->world->Clear();
}
/////////////////////////////////////////////////
void WorldUtils::clearMatching(const std::string &match, const bool is_light){
std::string entity_name;
std::string match_str = match;
gazebo::msgs::Request *msg;
if (is_light){
physics::Light_V lights = this->world->Lights();
for (auto &l : lights){
entity_name = l->GetName();
if (entity_name.find(match_str) != std::string::npos){
msg = gazebo::msgs::CreateRequest("entity_delete", entity_name);
request_pub->Publish(*msg, true);
}
}
} else {
physics::Model_V models = this->world->Models();
for (auto &m : models){
entity_name = m->GetName();
if (entity_name.find(match_str) != std::string::npos){
msg = gazebo::msgs::CreateRequest("entity_delete", entity_name);
request_pub->Publish(*msg, true);
}
}
}
delete msg;
}
/////////////////////////////////////////////////
const std::string WorldUtils::genSphere(
const std::string &model_name,
const double mass,
const double radius,
const ignition::math::Vector3d position,
const ignition::math::Quaterniond orientation){
msgs::Model model;
model.set_name(model_name);
msgs::Set(model.mutable_pose(),
ignition::math::Pose3d(position, orientation));
msgs::AddSphereLink(model, mass, radius);
return msgs::ModelToSDF(model)->ToString("");
}
/////////////////////////////////////////////////
const std::string WorldUtils::genCylinder(
const std::string &model_name,
const double mass,
const double radius,
const double length,
const ignition::math::Vector3d position,
const ignition::math::Quaterniond orientation){
msgs::Model model;
model.set_name(model_name);
msgs::Set(model.mutable_pose(),
ignition::math::Pose3d(position, orientation));
msgs::AddCylinderLink(model, mass, radius, length);
return msgs::ModelToSDF(model)->ToString("");
};
/////////////////////////////////////////////////
const std::string WorldUtils::genBox(
const std::string &model_name,
const double mass,
const ignition::math::Vector3d size,
const ignition::math::Vector3d position,
const ignition::math::Quaterniond orientation){
msgs::Model model;
model.set_name(model_name);
msgs::Set(model.mutable_pose(),
ignition::math::Pose3d(position, orientation));
msgs::AddBoxLink(model, mass, size);
return msgs::ModelToSDF(model)->ToString("");
};
}