/
VelocityControl.cc
384 lines (332 loc) · 11.9 KB
/
VelocityControl.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
/*
* Copyright (C) 2020 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 <map>
#include <mutex>
#include <string>
#include <vector>
#include <unordered_map>
#include <ignition/common/Profiler.hh>
#include <ignition/math/Vector3.hh>
#include <ignition/plugin/Register.hh>
#include <ignition/transport/Node.hh>
#include "ignition/gazebo/components/AngularVelocityCmd.hh"
#include "ignition/gazebo/components/LinearVelocityCmd.hh"
#include "ignition/gazebo/Model.hh"
#include "ignition/gazebo/Util.hh"
#include "VelocityControl.hh"
using namespace ignition;
using namespace gazebo;
using namespace systems;
class ignition::gazebo::systems::VelocityControlPrivate
{
/// \brief Callback for model velocity subscription
/// \param[in] _msg Velocity message
public: void OnCmdVel(const ignition::msgs::Twist &_msg);
/// \brief Callback for link velocity subscription
/// \param[in] _msg Velocity message
public: void OnLinkCmdVel(const ignition::msgs::Twist &_msg,
const ignition::transport::MessageInfo &_info);
/// \brief Update the linear and angular velocities.
/// \param[in] _info System update information.
/// \param[in] _ecm The EntityComponentManager of the given simulation
/// instance.
public: void UpdateVelocity(const ignition::gazebo::UpdateInfo &_info,
const ignition::gazebo::EntityComponentManager &_ecm);
/// \brief Update link velocity.
/// \param[in] _info System update information.
/// \param[in] _ecm The EntityComponentManager of the given simulation
/// instance.
public: void UpdateLinkVelocity(const ignition::gazebo::UpdateInfo &_info,
const ignition::gazebo::EntityComponentManager &_ecm);
/// \brief Ignition communication node.
public: transport::Node node;
/// \brief Model interface
public: Model model{kNullEntity};
/// \brief Angular velocity of a model
public: math::Vector3d angularVelocity{0, 0, 0};
/// \brief Linear velocity of a model
public: math::Vector3d linearVelocity{0, 0, 0};
/// \brief Last target velocity requested.
public: msgs::Twist targetVel;
/// \brief A mutex to protect the model velocity command.
public: std::mutex mutex;
/// \brief Link names
public: std::vector<std::string> linkNames;
/// \brief Link entities in a model
public: std::unordered_map<std::string, Entity> links;
/// \brief Angular velocities of links
public: std::unordered_map<std::string, math::Vector3d> angularVelocities;
/// \brief Linear velocities of links
public: std::unordered_map<std::string, math::Vector3d> linearVelocities;
/// \brief All link velocites
public: std::unordered_map<std::string, msgs::Twist> linkVels;
};
//////////////////////////////////////////////////
VelocityControl::VelocityControl()
: dataPtr(std::make_unique<VelocityControlPrivate>())
{
}
//////////////////////////////////////////////////
void VelocityControl::Configure(const Entity &_entity,
const std::shared_ptr<const sdf::Element> &_sdf,
EntityComponentManager &_ecm,
EventManager &/*_eventMgr*/)
{
this->dataPtr->model = Model(_entity);
if (!this->dataPtr->model.Valid(_ecm))
{
ignerr << "VelocityControl plugin should be attached to a model entity. "
<< "Failed to initialize." << std::endl;
return;
}
if (_sdf->HasElement("initial_linear"))
{
this->dataPtr->linearVelocity = _sdf->Get<math::Vector3d>("initial_linear");
msgs::Set(this->dataPtr->targetVel.mutable_linear(),
this->dataPtr->linearVelocity);
ignmsg << "Linear velocity initialized to ["
<< this->dataPtr->linearVelocity << "]" << std::endl;
}
if (_sdf->HasElement("initial_angular"))
{
this->dataPtr->angularVelocity =
_sdf->Get<math::Vector3d>("initial_angular");
msgs::Set(this->dataPtr->targetVel.mutable_angular(),
this->dataPtr->angularVelocity);
ignmsg << "Angular velocity initialized to ["
<< this->dataPtr->angularVelocity << "]" << std::endl;
}
// Subscribe to model commands
std::vector<std::string> modelTopics;
if (_sdf->HasElement("topic"))
{
modelTopics.push_back(_sdf->Get<std::string>("topic"));
}
modelTopics.push_back(
"/model/" + this->dataPtr->model.Name(_ecm) + "/cmd_vel");
auto modelTopic = validTopic(modelTopics);
this->dataPtr->node.Subscribe(
modelTopic, &VelocityControlPrivate::OnCmdVel, this->dataPtr.get());
ignmsg << "VelocityControl subscribing to twist messages on ["
<< modelTopic << "]"
<< std::endl;
// Ugly, but needed because the sdf::Element::GetElement is not a const
// function and _sdf is a const shared pointer to a const sdf::Element.
auto ptr = const_cast<sdf::Element *>(_sdf.get());
if (!ptr->HasElement("link_name"))
return;
sdf::ElementPtr sdfElem = ptr->GetElement("link_name");
while (sdfElem)
{
this->dataPtr->linkNames.push_back(sdfElem->Get<std::string>());
sdfElem = sdfElem->GetNextElement("link_name");
}
// Subscribe to link commands
for (const auto &linkName : this->dataPtr->linkNames)
{
std::string linkTopic{"/model/" + this->dataPtr->model.Name(_ecm) +
"/link/" + linkName + "/cmd_vel"};
linkTopic = transport::TopicUtils::AsValidTopic(linkTopic);
this->dataPtr->node.Subscribe(
linkTopic, &VelocityControlPrivate::OnLinkCmdVel, this->dataPtr.get());
ignmsg << "VelocityControl subscribing to twist messages on ["
<< linkTopic << "]"
<< std::endl;
}
}
//////////////////////////////////////////////////
void VelocityControl::PreUpdate(const ignition::gazebo::UpdateInfo &_info,
ignition::gazebo::EntityComponentManager &_ecm)
{
IGN_PROFILE("VelocityControl::PreUpdate");
// \TODO(anyone) Support rewind
if (_info.dt < std::chrono::steady_clock::duration::zero())
{
ignwarn << "Detected jump back in time ["
<< std::chrono::duration_cast<std::chrono::seconds>(_info.dt).count()
<< "s]. System may not work properly." << std::endl;
}
// Nothing left to do if paused.
if (_info.paused)
return;
// update angular velocity of model
auto modelAngularVel =
_ecm.Component<components::AngularVelocityCmd>(
this->dataPtr->model.Entity());
if (modelAngularVel == nullptr)
{
_ecm.CreateComponent(
this->dataPtr->model.Entity(),
components::AngularVelocityCmd({this->dataPtr->angularVelocity}));
}
else
{
*modelAngularVel =
components::AngularVelocityCmd({this->dataPtr->angularVelocity});
}
// update linear velocity of model
auto modelLinearVel =
_ecm.Component<components::LinearVelocityCmd>(
this->dataPtr->model.Entity());
if (modelLinearVel == nullptr)
{
_ecm.CreateComponent(
this->dataPtr->model.Entity(),
components::LinearVelocityCmd({this->dataPtr->linearVelocity}));
}
else
{
*modelLinearVel =
components::LinearVelocityCmd({this->dataPtr->linearVelocity});
}
// If there are links, create link components
// If the link hasn't been identified yet, look for it
auto modelName = this->dataPtr->model.Name(_ecm);
if (this->dataPtr->linkNames.empty())
return;
// find all the link entity ids
if (this->dataPtr->links.size() != this->dataPtr->linkNames.size())
{
for (const auto &linkName : this->dataPtr->linkNames)
{
if (this->dataPtr->links.find(linkName) == this->dataPtr->links.end())
{
Entity link = this->dataPtr->model.LinkByName(_ecm, linkName);
if (link != kNullEntity)
this->dataPtr->links.insert({linkName, link});
else
{
ignwarn << "Failed to find link [" << linkName
<< "] for model [" << modelName << "]" << std::endl;
}
}
}
}
// update link velocities
for (const auto& [linkName, angularVel] : this->dataPtr->angularVelocities)
{
auto it = this->dataPtr->links.find(linkName);
if (it != this->dataPtr->links.end())
{
auto linkAngularVelComp =
_ecm.Component<components::AngularVelocityCmd>(it->second);
if (!linkAngularVelComp)
{
_ecm.CreateComponent(it->second,
components::AngularVelocityCmd({angularVel}));
}
else
{
*linkAngularVelComp = components::AngularVelocityCmd(angularVel);
}
}
else
{
ignwarn << "No link found for angular velocity cmd ["
<< linkName << "]" << std::endl;
}
}
for (const auto& [linkName, linearVel] : this->dataPtr->linearVelocities)
{
auto it = this->dataPtr->links.find(linkName);
if (it != this->dataPtr->links.end())
{
auto linkLinearVelComp =
_ecm.Component<components::LinearVelocityCmd>(it->second);
if (!linkLinearVelComp)
{
_ecm.CreateComponent(it->second,
components::LinearVelocityCmd({linearVel}));
}
else
{
*linkLinearVelComp = components::LinearVelocityCmd(linearVel);
}
}
else
{
ignwarn << "No link found for linear velocity cmd ["
<< linkName << "]" << std::endl;
}
}
}
//////////////////////////////////////////////////
void VelocityControl::PostUpdate(const UpdateInfo &_info,
const EntityComponentManager &_ecm)
{
IGN_PROFILE("VelocityControl::PostUpdate");
// Nothing left to do if paused.
if (_info.paused)
return;
// update model velocities
this->dataPtr->UpdateVelocity(_info, _ecm);
// update link velocities
this->dataPtr->UpdateLinkVelocity(_info, _ecm);
}
//////////////////////////////////////////////////
void VelocityControlPrivate::UpdateVelocity(
const ignition::gazebo::UpdateInfo &/*_info*/,
const ignition::gazebo::EntityComponentManager &/*_ecm*/)
{
IGN_PROFILE("VeocityControl::UpdateVelocity");
std::lock_guard<std::mutex> lock(this->mutex);
this->linearVelocity = msgs::Convert(this->targetVel.linear());
this->angularVelocity = msgs::Convert(this->targetVel.angular());
}
//////////////////////////////////////////////////
void VelocityControlPrivate::UpdateLinkVelocity(
const ignition::gazebo::UpdateInfo &/*_info*/,
const ignition::gazebo::EntityComponentManager &/*_ecm*/)
{
IGN_PROFILE("VelocityControl::UpdateLinkVelocity");
std::lock_guard<std::mutex> lock(this->mutex);
for (const auto& [linkName, msg] : this->linkVels)
{
auto linearVel = msgs::Convert(msg.linear());
auto angularVel = msgs::Convert(msg.angular());
this->linearVelocities[linkName] = linearVel;
this->angularVelocities[linkName] = angularVel;
}
this->linkVels.clear();
}
//////////////////////////////////////////////////
void VelocityControlPrivate::OnCmdVel(const msgs::Twist &_msg)
{
std::lock_guard<std::mutex> lock(this->mutex);
this->targetVel = _msg;
}
//////////////////////////////////////////////////
void VelocityControlPrivate::OnLinkCmdVel(const msgs::Twist &_msg,
const transport::MessageInfo &_info)
{
std::lock_guard<std::mutex> lock(this->mutex);
for (const auto &linkName : this->linkNames)
{
if (_info.Topic().find("/" + linkName + "/cmd_vel") != std::string::npos)
{
this->linkVels.insert({linkName, _msg});
break;
}
}
}
IGNITION_ADD_PLUGIN(VelocityControl,
ignition::gazebo::System,
VelocityControl::ISystemConfigure,
VelocityControl::ISystemPreUpdate,
VelocityControl::ISystemPostUpdate)
IGNITION_ADD_PLUGIN_ALIAS(VelocityControl,
"ignition::gazebo::systems::VelocityControl")