-
Notifications
You must be signed in to change notification settings - Fork 270
/
Contact.cc
302 lines (253 loc) · 9.32 KB
/
Contact.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
/*
* 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 "Contact.hh"
#include <gz/msgs/contact.pb.h>
#include <gz/msgs/contacts.pb.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include <gz/common/Profiler.hh>
#include <gz/plugin/Register.hh>
#include <sdf/Element.hh>
#include <gz/transport/Node.hh>
#include "gz/sim/Conversions.hh"
#include "gz/sim/EntityComponentManager.hh"
#include "gz/sim/Util.hh"
#include "gz/sim/components/Collision.hh"
#include "gz/sim/components/ContactSensor.hh"
#include "gz/sim/components/ContactSensorData.hh"
#include "gz/sim/components/Link.hh"
#include "gz/sim/components/Name.hh"
#include "gz/sim/components/ParentEntity.hh"
using namespace gz;
using namespace sim;
using namespace systems;
class ContactSensor
{
/// \brief Load the Contact sensor from an sdf element
/// \param[in] _sdf SDF element describing the Contact sensor
/// \param[in] _topic string with topic name
/// \param[in] _collisionEntities A list of entities that act as contact
/// sensors
public: void Load(const sdf::ElementPtr &_sdf, const std::string &_topic,
const std::vector<Entity> &_collisionEntities);
/// \brief Add contacts to the list to be published
/// \param[in] _stamp Time stamp of the sensor measurement
/// \param[in] _contacts A contact message to be added to the list
public: void AddContacts(const std::chrono::steady_clock::duration &_stamp,
const msgs::Contacts &_contacts);
/// \brief Publish sensor data over gz transport
public: void Publish();
/// \brief Topic to publish data to
public: std::string topic;
/// \brief Message to publish
public: msgs::Contacts contactsMsg;
/// \brief Gazebo transport node
public: transport::Node node;
/// \brief Gazebo transport publisher
public: transport::Node::Publisher pub;
/// \brief Entities for which this sensor publishes data
public: std::vector<Entity> collisionEntities;
};
class gz::sim::systems::ContactPrivate
{
/// \brief Create sensors that correspond to entities in the simulation
/// \param[in] _ecm Mutable reference to ECM.
public: void CreateSensors(EntityComponentManager &_ecm);
/// \brief Update and publish sensor data
/// \param[in] _ecm Immutable reference to ECM.
public: void UpdateSensors(const UpdateInfo &_info,
const EntityComponentManager &_ecm);
/// \brief Remove sensors if their entities have been removed from
/// simulation.
/// \param[in] _ecm Immutable reference to ECM.
public: void RemoveSensors(const EntityComponentManager &_ecm);
/// \brief A map of Contact entity to its Contact sensor.
public: std::unordered_map<Entity,
std::unique_ptr<ContactSensor>> entitySensorMap;
};
//////////////////////////////////////////////////
void ContactSensor::Load(const sdf::ElementPtr &_sdf, const std::string &_topic,
const std::vector<Entity> &_collisionEntities)
{
this->collisionEntities = _collisionEntities;
auto contactElem = _sdf->GetElement("contact");
auto tmpTopic =
contactElem->Get<std::string>("topic", "__default_topic__").first;
if (tmpTopic == "__default_topic__")
{
// use default topic for sensor
this->topic = _topic;
}
else
{
this->topic = tmpTopic;
}
gzmsg << "Contact system publishing on " << this->topic << std::endl;
this->pub = this->node.Advertise<msgs::Contacts>(this->topic);
}
//////////////////////////////////////////////////
void ContactSensor::AddContacts(
const std::chrono::steady_clock::duration &_stamp,
const msgs::Contacts &_contacts)
{
auto stamp = convert<msgs::Time>(_stamp);
for (const auto &contact : _contacts.contact())
{
auto *newContact = this->contactsMsg.add_contact();
newContact->CopyFrom(contact);
newContact->mutable_header()->mutable_stamp()->CopyFrom(stamp);
}
this->contactsMsg.mutable_header()->mutable_stamp()->CopyFrom(stamp);
}
//////////////////////////////////////////////////
void ContactSensor::Publish()
{
// Only publish if there are contacts
if (this->contactsMsg.contact_size() > 0)
{
this->pub.Publish(this->contactsMsg);
this->contactsMsg.Clear();
}
}
//////////////////////////////////////////////////
void ContactPrivate::CreateSensors(EntityComponentManager &_ecm)
{
GZ_PROFILE("ContactPrivate::CreateSensors");
_ecm.EachNew<components::ContactSensor>(
[&](const Entity &_entity,
const components::ContactSensor *_contact) -> bool
{
// Check if the parent entity is a link
auto *parentEntity = _ecm.Component<components::ParentEntity>(_entity);
if (nullptr == parentEntity)
return true;
auto *linkComp = _ecm.Component<components::Link>(parentEntity->Data());
if (nullptr == linkComp)
{
// Contact sensors should only be attached to links
return true;
}
auto collisionElem =
_contact->Data()->GetElement("contact")->GetElement("collision");
std::vector<Entity> collisionEntities;
// Get all the collision elements
for (; collisionElem;
collisionElem = collisionElem->GetNextElement("collision"))
{
auto collisionName = collisionElem->Get<std::string>();
// Get collision entity that matches the name given by the sensor's
// configuration.
auto childEntities = _ecm.ChildrenByComponents(
parentEntity->Data(), components::Collision(),
components::Name(collisionName));
if (!childEntities.empty())
{
// We assume that if childEntities is not empty, it only has one
// element.
collisionEntities.push_back(childEntities.front());
// Create component to be filled by physics.
_ecm.CreateComponent(childEntities.front(),
components::ContactSensorData());
}
}
std::string defaultTopic = scopedName(_entity, _ecm, "/") + "/contact";
auto sensor = std::make_unique<ContactSensor>();
sensor->Load(_contact->Data(), defaultTopic, collisionEntities);
this->entitySensorMap.insert(
std::make_pair(_entity, std::move(sensor)));
return true;
});
}
//////////////////////////////////////////////////
void ContactPrivate::UpdateSensors(const UpdateInfo &_info,
const EntityComponentManager &_ecm)
{
GZ_PROFILE("ContactPrivate::UpdateSensors");
for (const auto &item : this->entitySensorMap)
{
for (const Entity &entity : item.second->collisionEntities)
{
auto contacts = _ecm.Component<components::ContactSensorData>(entity);
// We will assume that the ContactData component will have been created if
// this entity is in the collisionEntities list
if (contacts->Data().contact_size() > 0)
{
item.second->AddContacts(_info.simTime, contacts->Data());
}
}
}
}
//////////////////////////////////////////////////
void ContactPrivate::RemoveSensors(
const EntityComponentManager &_ecm)
{
GZ_PROFILE("ContactPrivate::RemoveSensors");
_ecm.EachRemoved<components::ContactSensor>(
[&](const Entity &_entity,
const components::ContactSensor *)->bool
{
auto sensorId = this->entitySensorMap.find(_entity);
if (sensorId == this->entitySensorMap.end())
{
gzerr << "Internal error, missing Contact sensor for entity ["
<< _entity << "]" << std::endl;
return true;
}
this->entitySensorMap.erase(sensorId);
return true;
});
}
//////////////////////////////////////////////////
Contact::Contact() : System(), dataPtr(std::make_unique<ContactPrivate>())
{
}
//////////////////////////////////////////////////
void Contact::PreUpdate(const UpdateInfo &, EntityComponentManager &_ecm)
{
GZ_PROFILE("Contact::PreUpdate");
this->dataPtr->CreateSensors(_ecm);
}
//////////////////////////////////////////////////
void Contact::PostUpdate(const UpdateInfo &_info,
const EntityComponentManager &_ecm)
{
GZ_PROFILE("Contact::PostUpdate");
// \TODO(anyone) Support rewind
if (_info.dt < std::chrono::steady_clock::duration::zero())
{
gzwarn << "Detected jump back in time ["
<< std::chrono::duration<double>(_info.dt).count()
<< "s]. System may not work properly." << std::endl;
}
if (!_info.paused)
{
this->dataPtr->UpdateSensors(_info, _ecm);
for (auto &it : this->dataPtr->entitySensorMap)
{
// Publish sensor data
it.second->Publish();
}
}
this->dataPtr->RemoveSensors(_ecm);
}
GZ_ADD_PLUGIN(Contact, System,
Contact::ISystemPreUpdate,
Contact::ISystemPostUpdate
)
GZ_ADD_PLUGIN_ALIAS(Contact, "gz::sim::systems::Contact")