forked from gazebosim/gz-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplyLinkWrench.cc
363 lines (301 loc) · 10.3 KB
/
ApplyLinkWrench.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
/*
* Copyright (C) 2022 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 <gz/msgs/entity_wrench.pb.h>
#include <mutex>
#include <string>
#include <queue>
#include <vector>
#include <gz/common/Profiler.hh>
#include <gz/math/Helpers.hh>
#include <gz/math/Vector3.hh>
#include <gz/msgs/Utility.hh>
#include <gz/plugin/Register.hh>
#include <gz/transport/Node.hh>
#include "gz/sim/components/Link.hh"
#include "gz/sim/components/World.hh"
#include "gz/sim/Link.hh"
#include "gz/sim/Model.hh"
#include "gz/sim/World.hh"
#include "gz/sim/Util.hh"
#include "ApplyLinkWrench.hh"
using namespace gz;
using namespace gz::sim;
using namespace systems;
class ignition::gazebo::systems::ApplyLinkWrenchPrivate
{
/// \brief Callback for wrench subscription
/// \param[in] _msg Wrench message
public: void OnWrench(const msgs::EntityWrench &_msg);
/// \brief Callback for persistent wrench subscription
/// \param[in] _msg Wrench message
public: void OnWrenchPersistent(const msgs::EntityWrench &_msg);
/// \brief Callback for clearing persistent wrenches
/// \param[in] _msg Entity message
public: void OnWrenchClear(const msgs::Entity &_msg);
/// \brief True if a console message should be printed whenever an
/// instantaneous wrench is applied, a persistent wrench is cleared, etc.
public: bool verbose{true};
/// \brief Queue of incoming instantaneous wrenches
public: std::queue<msgs::EntityWrench> newWrenches;
/// \brief All persistent wrenches
public: std::vector<msgs::EntityWrench> persistentWrenches;
/// \brief Entities whose wrenches should be cleared
public: std::queue<msgs::Entity> clearWrenches;
/// \brief Communication node.
public: transport::Node node;
/// \brief A mutex to protect wrenches
public: std::mutex mutex;
};
/// \brief Extract wrench information from a message.
/// \param[in] _ecm Entity component manager
/// \param[in] _msg Entity message. If it's a link, that link is returned. If
/// it's a model, its canonical link is returned.
/// \param[out] Force to apply.
/// \param[out] Torque to apply.
/// \return Target link entity.
Link decomposeMessage(const EntityComponentManager &_ecm,
const msgs::EntityWrench &_msg, math::Vector3d &_force,
math::Vector3d &_torque)
{
if (_msg.wrench().has_force_offset())
{
ignwarn << "Force offset currently not supported, it will be ignored."
<< std::endl;
}
if (_msg.wrench().has_force())
{
_force = msgs::Convert(_msg.wrench().force());
}
if (_msg.wrench().has_torque())
{
_torque = msgs::Convert(_msg.wrench().torque());
}
auto entity = entityFromMsg(_ecm, _msg.entity());
if (entity == kNullEntity)
{
return Link();
}
Link link(entity);
if (link.Valid(_ecm))
{
return link;
}
Model model(entity);
if (model.Valid(_ecm))
{
return Link(model.CanonicalLink(_ecm));
}
ignerr << "Wrench can only be applied to a link or a model. Entity ["
<< entity << "] isn't either of them." << std::endl;
return Link();
}
//////////////////////////////////////////////////
ApplyLinkWrench::ApplyLinkWrench()
: dataPtr(std::make_unique<ApplyLinkWrenchPrivate>())
{
}
//////////////////////////////////////////////////
void ApplyLinkWrench::Configure(const Entity &_entity,
const std::shared_ptr<const sdf::Element> &_sdf,
EntityComponentManager &_ecm,
EventManager &/*_eventMgr*/)
{
auto world = World(_entity);
if (!world.Valid(_ecm))
{
ignerr << "ApplyLinkWrench system should be attached to a world."
<< std::endl;
return;
}
this->dataPtr->verbose = _sdf->Get<bool>("verbose", true).first;
// Initial wrenches
for (auto elem = _sdf->FindElement("persistent");
elem != nullptr;
elem = elem->GetNextElement("persistent"))
{
msgs::EntityWrench msg;
if (!elem->HasElement("entity_name") || !elem->HasElement("entity_type"))
{
ignerr << "Skipping <persistent> element missing entity name or type."
<< std::endl;
continue;
}
msg.mutable_entity()->set_name(elem->Get<std::string>("entity_name"));
auto typeStr = elem->FindElement("entity_type")->Get<std::string>();
if (typeStr == "link")
{
msg.mutable_entity()->set_type(msgs::Entity::LINK);
}
else if (typeStr == "model")
{
msg.mutable_entity()->set_type(msgs::Entity::MODEL);
}
else
{
ignerr << "Skipping <persistent> element, entity type [" << typeStr
<< "] not supported." << std::endl;
continue;
}
if (elem->HasElement("force"))
{
msgs::Set(msg.mutable_wrench()->mutable_force(),
elem->FindElement("force")->Get<math::Vector3d>());
}
if (elem->HasElement("torque"))
{
msgs::Set(msg.mutable_wrench()->mutable_torque(),
elem->FindElement("torque")->Get<math::Vector3d>());
}
this->dataPtr->OnWrenchPersistent(msg);
}
// Topic to apply wrench for one time step
// TODO(chapulina) Use AsValidTopic when merging forward
std::string topic{"/world/" + world.Name(_ecm).value() + "/wrench"};
if (_sdf->HasElement("topic"))
topic = _sdf->Get<std::string>("topic");
this->dataPtr->node.Subscribe(topic, &ApplyLinkWrenchPrivate::OnWrench,
this->dataPtr.get());
ignmsg << "Listening to instantaneous wrench commands in [" << topic << "]"
<< std::endl;
// Topic to apply wrench continuously
topic = "/world/" + world.Name(_ecm).value() + "/wrench/persistent";
if (_sdf->HasElement("topic_persistent"))
topic = _sdf->Get<std::string>("topic_persistent");
this->dataPtr->node.Subscribe(topic,
&ApplyLinkWrenchPrivate::OnWrenchPersistent, this->dataPtr.get());
ignmsg << "Listening to persistent wrench commands in [" << topic << "]"
<< std::endl;
// Topic to clear persistent wrenches
topic = "/world/" + world.Name(_ecm).value() + "/wrench/clear";
if (_sdf->HasElement("topic_clear"))
topic = _sdf->Get<std::string>("topic_clear");
this->dataPtr->node.Subscribe(topic,
&ApplyLinkWrenchPrivate::OnWrenchClear, this->dataPtr.get());
ignmsg << "Listening to wrench clear commands in [" << topic << "]"
<< std::endl;
}
//////////////////////////////////////////////////
void ApplyLinkWrench::PreUpdate(const UpdateInfo &_info,
EntityComponentManager &_ecm)
{
IGN_PROFILE("ApplyLinkWrench::PreUpdate");
std::lock_guard<std::mutex> lock(this->dataPtr->mutex);
// Clear persistent wrenches
while (!this->dataPtr->clearWrenches.empty())
{
auto clearMsg = this->dataPtr->clearWrenches.front();
auto clearEntity = entityFromMsg(_ecm, clearMsg);
for (auto msgIt = this->dataPtr->persistentWrenches.begin();
msgIt != this->dataPtr->persistentWrenches.end(); msgIt++)
{
auto persistentEntity = entityFromMsg(_ecm, msgIt->entity());
if (persistentEntity == clearEntity)
{
this->dataPtr->persistentWrenches.erase(msgIt--);
if (this->dataPtr->verbose)
{
igndbg << "Clearing persistent wrench for entity [" << clearEntity
<< "]" << std::endl;
}
}
}
this->dataPtr->clearWrenches.pop();
}
// Only apply wrenches when not paused
if (_info.paused)
return;
// Apply instantaneous wrenches
while (!this->dataPtr->newWrenches.empty())
{
auto msg = this->dataPtr->newWrenches.front();
math::Vector3d force;
math::Vector3d torque;
auto link = decomposeMessage(_ecm, msg, force, torque);
if (!link.Valid(_ecm))
{
ignerr << "Entity not found." << std::endl
<< msg.DebugString() << std::endl;
this->dataPtr->newWrenches.pop();
continue;
}
link.AddWorldWrench(_ecm, force, torque);
if (this->dataPtr->verbose)
{
igndbg << "Applying wrench [" << force << " " << torque << "] to entity ["
<< link.Entity() << "] for 1 time step." << std::endl;
}
this->dataPtr->newWrenches.pop();
}
// Apply persistent wrenches at every time step
for (auto msg : this->dataPtr->persistentWrenches)
{
math::Vector3d force;
math::Vector3d torque;
auto link = decomposeMessage(_ecm, msg, force, torque);
if (!link.Valid(_ecm))
{
// Not an error, persistent wrenches can be applied preemptively before
// an entity is inserted
continue;
}
link.AddWorldWrench(_ecm, force, torque);
}
}
//////////////////////////////////////////////////
void ApplyLinkWrenchPrivate::OnWrench(const msgs::EntityWrench &_msg)
{
std::lock_guard<std::mutex> lock(this->mutex);
if (!_msg.has_entity() || !_msg.has_wrench())
{
ignerr << "Missing entity or wrench in message: " << std::endl
<< _msg.DebugString() << std::endl;
return;
}
this->newWrenches.push(_msg);
}
//////////////////////////////////////////////////
void ApplyLinkWrenchPrivate::OnWrenchPersistent(const msgs::EntityWrench &_msg)
{
std::lock_guard<std::mutex> lock(this->mutex);
if (!_msg.has_entity() || !_msg.has_wrench())
{
ignerr << "Missing entity or wrench in message: " << std::endl
<< _msg.DebugString() << std::endl;
return;
}
if (this->verbose)
{
igndbg << "Queueing persistent wrench:" << std::endl
<< _msg.DebugString() << std::endl;
}
this->persistentWrenches.push_back(_msg);
}
//////////////////////////////////////////////////
void ApplyLinkWrenchPrivate::OnWrenchClear(const msgs::Entity &_msg)
{
std::lock_guard<std::mutex> lock(this->mutex);
this->clearWrenches.push(_msg);
}
IGNITION_ADD_PLUGIN(ApplyLinkWrench,
System,
ApplyLinkWrench::ISystemConfigure,
ApplyLinkWrench::ISystemPreUpdate)
IGNITION_ADD_PLUGIN_ALIAS(ApplyLinkWrench,
"gz::sim::systems::ApplyLinkWrench")
// TODO(CH3): Deprecated, remove on version 8
IGNITION_ADD_PLUGIN_ALIAS(ApplyLinkWrench,
"ignition::gazebo::systems::ApplyLinkWrench")