/
Gripper.cc
399 lines (323 loc) · 11.3 KB
/
Gripper.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
/*
* Copyright (C) 2012 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 <functional>
#include <map>
#include <mutex>
#include <vector>
#include <ignition/math/Helpers.hh>
#include <ignition/math/Pose3.hh>
#include "gazebo/msgs/msgs.hh"
#include "gazebo/common/Events.hh"
#include "gazebo/transport/Node.hh"
#include "gazebo/transport/Subscriber.hh"
#include "gazebo/physics/ContactManager.hh"
#include "gazebo/physics/World.hh"
#include "gazebo/physics/Joint.hh"
#include "gazebo/physics/Link.hh"
#include "gazebo/physics/Collision.hh"
#include "gazebo/physics/Contact.hh"
#include "gazebo/physics/Model.hh"
#include "gazebo/physics/PhysicsEngine.hh"
#include "gazebo/physics/Gripper.hh"
using namespace gazebo;
using namespace physics;
/// \internal
/// \brief Private data class for Gripper
class gazebo::physics::GripperPrivate
{
/// \brief Callback used when the gripper contacts an object.
/// \param[in] _msg Message that contains contact information.
public: void OnContacts(ConstContactsPtr &_msg);
/// \brief Update the gripper.
public: void OnUpdate();
/// \brief Attach an object to the gripper.
public: void HandleAttach();
/// \brief Detach an object from the gripper.
public: void HandleDetach();
/// \brief A reset function.
public: void ResetDiffs();
/// \brief Model that contains this gripper.
public: physics::ModelPtr model;
/// \brief Pointer to the world.
public: physics::WorldPtr world;
/// \brief A fixed joint to connect the gripper to a grasped object.
public: physics::JointPtr fixedJoint;
/// \brief The base link for the gripper.
public: physics::LinkPtr palmLink;
/// \brief All our connections.
public: std::vector<event::ConnectionPtr> connections;
/// \brief The collisions for the links in the gripper.
public: std::map<std::string, physics::CollisionPtr> collisions;
/// \brief The current contacts.
public: std::vector<msgs::Contact> contacts;
/// \brief Mutex used to protect reading/writing the contact message.
public: std::mutex mutexContacts;
/// \brief True if the gripper has an object.
public: bool attached;
/// \brief Previous difference between the palm link and grasped
/// object.
public: ignition::math::Pose3d prevDiff;
/// \brief Used to determine when to create the fixed joint.
public: std::vector<double> diffs;
/// \brief Current index into the diff array.
public: int diffIndex;
/// \brief Rate at which to update the gripper.
public: common::Time updateRate;
/// \brief Previous time when the gripper was updated.
public: common::Time prevUpdateTime;
/// \brief Number of iterations the gripper was contacting the same
/// object.
public: int posCount;
/// \brief Number of iterations the gripper was not contacting the same
/// object.
public: int zeroCount;
/// \brief Minimum number of links touching.
public: unsigned int minContactCount;
/// \brief Steps touching before engaging fixed joint
public: int attachSteps;
/// \brief Steps not touching before disengaging fixed joint
public: int detachSteps;
/// \brief Name of the gripper.
public: std::string name;
/// \brief Node for communication.
public: transport::NodePtr node;
/// \brief Subscription to contact messages from the physics engine.
public: transport::SubscriberPtr contactSub;
};
/////////////////////////////////////////////////
Gripper::Gripper(ModelPtr _model) : dataPtr(new GripperPrivate)
{
this->dataPtr->model = _model;
this->dataPtr->world = this->dataPtr->model->GetWorld();
this->dataPtr->diffs.resize(10);
this->dataPtr->diffIndex = 0;
this->dataPtr->ResetDiffs();
this->dataPtr->attached = false;
this->dataPtr->updateRate = common::Time(0, common::Time::SecToNano(0.75));
this->dataPtr->node = transport::NodePtr(new transport::Node());
}
/////////////////////////////////////////////////
Gripper::~Gripper()
{
if (this->dataPtr->world && this->dataPtr->world->Running())
{
physics::ContactManager *mgr =
this->dataPtr->world->Physics()->GetContactManager();
mgr->RemoveFilter(this->Name());
}
this->dataPtr->model.reset();
this->dataPtr->world.reset();
this->dataPtr->connections.clear();
}
/////////////////////////////////////////////////
void Gripper::Load(sdf::ElementPtr _sdf)
{
this->dataPtr->node->Init(this->dataPtr->world->Name());
this->dataPtr->name = _sdf->Get<std::string>("name");
this->dataPtr->fixedJoint =
this->dataPtr->world->Physics()->CreateJoint("fixed",
this->dataPtr->model);
this->dataPtr->fixedJoint->SetName(
this->dataPtr->model->GetName() + "__gripper_fixed_joint__");
sdf::ElementPtr graspCheck = _sdf->GetElement("grasp_check");
this->dataPtr->minContactCount =
graspCheck->Get<unsigned int>("min_contact_count");
this->dataPtr->attachSteps = graspCheck->Get<int>("attach_steps");
this->dataPtr->detachSteps = graspCheck->Get<int>("detach_steps");
sdf::ElementPtr palmLinkElem = _sdf->GetElement("palm_link");
this->dataPtr->palmLink =
this->dataPtr->model->GetLink(palmLinkElem->Get<std::string>());
if (!this->dataPtr->palmLink)
{
gzerr << "palm link [" << palmLinkElem->Get<std::string>()
<< "] not found!\n";
}
sdf::ElementPtr gripperLinkElem = _sdf->GetElement("gripper_link");
while (gripperLinkElem)
{
physics::LinkPtr gripperLink
= this->dataPtr->model->GetLink(gripperLinkElem->Get<std::string>());
for (unsigned int j = 0; j < gripperLink->GetChildCount(); ++j)
{
physics::CollisionPtr collision = gripperLink->GetCollision(j);
std::map<std::string, physics::CollisionPtr>::iterator collIter
= this->dataPtr->collisions.find(collision->GetScopedName());
if (collIter != this->dataPtr->collisions.end())
continue;
this->dataPtr->collisions[collision->GetScopedName()] = collision;
}
gripperLinkElem = gripperLinkElem->GetNextElement("gripper_link");
}
if (!this->dataPtr->collisions.empty())
{
// request the contact manager to publish messages to a custom topic for
// this sensor
physics::ContactManager *mgr =
this->dataPtr->world->Physics()->GetContactManager();
std::string topic = mgr->CreateFilter(this->Name(),
this->dataPtr->collisions);
if (!this->dataPtr->contactSub)
{
this->dataPtr->contactSub = this->dataPtr->node->Subscribe(topic,
&GripperPrivate::OnContacts, this->dataPtr.get());
}
}
this->dataPtr->connections.push_back(event::Events::ConnectWorldUpdateEnd(
std::bind(&GripperPrivate::OnUpdate, this->dataPtr.get())));
}
/////////////////////////////////////////////////
void Gripper::Init()
{
this->dataPtr->prevUpdateTime = common::Time::GetWallTime();
this->dataPtr->zeroCount = 0;
this->dataPtr->posCount = 0;
this->dataPtr->attached = false;
}
/////////////////////////////////////////////////
std::string Gripper::Name() const
{
return this->dataPtr->name;
}
/////////////////////////////////////////////////
bool Gripper::IsAttached() const
{
return this->dataPtr->attached;
}
/////////////////////////////////////////////////
void GripperPrivate::OnUpdate()
{
if (common::Time::GetWallTime() - this->prevUpdateTime < this->updateRate)
{
return;
}
// @todo: should package the decision into a function
if (this->contacts.size() >= this->minContactCount)
{
this->posCount++;
this->zeroCount = 0;
}
else
{
this->zeroCount++;
this->posCount = std::max(0, this->posCount-1);
}
if (this->posCount > this->attachSteps && !this->attached)
{
this->HandleAttach();
}
else if (this->zeroCount > this->detachSteps && this->attached)
{
this->HandleDetach();
}
{
std::lock_guard<std::mutex> lock(this->mutexContacts);
this->contacts.clear();
}
this->prevUpdateTime = common::Time::GetWallTime();
}
/////////////////////////////////////////////////
void GripperPrivate::HandleAttach()
{
if (!this->palmLink)
{
gzwarn << "palm link not found, not enforcing grasp hack!\n";
return;
}
std::map<std::string, physics::CollisionPtr> cc;
std::map<std::string, int> contactCounts;
std::map<std::string, int>::iterator iter;
// This function is only called from the OnUpdate function so
// the call to contacts.clear() is not going to happen in
// parallel with the reads in the following code, no mutex
// needed.
for (unsigned int i = 0; i < this->contacts.size(); ++i)
{
std::string name1 = this->contacts[i].collision1();
std::string name2 = this->contacts[i].collision2();
if (this->collisions.find(name1) == this->collisions.end())
{
cc[name1] = boost::dynamic_pointer_cast<Collision>(
this->world->EntityByName(name1));
contactCounts[name1] += 1;
}
if (this->collisions.find(name2) == this->collisions.end())
{
cc[name2] = boost::dynamic_pointer_cast<Collision>(
this->world->EntityByName(name2));
contactCounts[name2] += 1;
}
}
iter = contactCounts.begin();
while (iter != contactCounts.end())
{
if (iter->second < 2)
{
contactCounts.erase(iter++);
}
else
{
if (!this->attached && cc[iter->first])
{
ignition::math::Pose3d diff =
cc[iter->first]->GetLink()->WorldPose() - this->palmLink->WorldPose();
double dd = (diff - this->prevDiff).Pos().SquaredLength();
this->prevDiff = diff;
this->diffs[this->diffIndex] = dd;
double var = ignition::math::variance<double>(this->diffs);
double max = ignition::math::max<double>(this->diffs);
if (var < 1e-5 && max < 1e-5)
{
this->attached = true;
this->fixedJoint->Load(this->palmLink,
cc[iter->first]->GetLink(), ignition::math::Pose3d());
this->fixedJoint->Init();
}
this->diffIndex = (this->diffIndex+1) % 10;
}
++iter;
}
}
}
/////////////////////////////////////////////////
void GripperPrivate::HandleDetach()
{
this->attached = false;
this->fixedJoint->Detach();
}
/////////////////////////////////////////////////
void GripperPrivate::OnContacts(ConstContactsPtr &_msg)
{
for (int i = 0; i < _msg->contact_size(); ++i)
{
CollisionPtr collision1 = boost::dynamic_pointer_cast<Collision>(
this->world->EntityByName(_msg->contact(i).collision1()));
CollisionPtr collision2 = boost::dynamic_pointer_cast<Collision>(
this->world->EntityByName(_msg->contact(i).collision2()));
if ((collision1 && !collision1->IsStatic()) &&
(collision2 && !collision2->IsStatic()))
{
std::lock_guard<std::mutex> lock(this->mutexContacts);
this->contacts.push_back(_msg->contact(i));
}
}
}
/////////////////////////////////////////////////
void GripperPrivate::ResetDiffs()
{
for (unsigned int i = 0; i < 10; ++i)
this->diffs[i] = ignition::math::MAX_D;
}