GazeboGraspGripper.cpp
Go to the documentation of this file.
1 //
2 // Created by tom on 19/09/16.
3 //
4 
5 #include <boost/bind.hpp>
6 #include <gazebo/gazebo.hh>
7 #include <gazebo/physics/physics.hh>
8 #include <gazebo/physics/ContactManager.hh>
9 #include <gazebo/physics/Contact.hh>
10 #include <gazebo/common/common.hh>
11 #include <stdio.h>
12 
14 
16 
17 #define DEFAULT_FORCES_ANGLE_TOLERANCE 120
18 #define DEFAULT_UPDATE_RATE 5
19 #define DEFAULT_MAX_GRIP_COUNT 10
20 #define DEFAULT_RELEASE_TOLERANCE 0.005
21 #define DEFAULT_DISABLE_COLLISIONS_ON_ATTACH false
22 
23 GazeboGraspGripper::GazeboGraspGripper():
24  attached(false)
25 {
26 }
27 
29  model(o.model),
34  palmLink(o.palmLink),
36  attached(o.attached),
38 {}
39 
41  this->model.reset();
42 }
43 
44 
45 
46 bool GazeboGraspGripper::Init(physics::ModelPtr& _model,
47  const std::string& _gripperName,
48  const std::string& palmLinkName,
49  const std::vector<std::string>& fingerLinkNames,
50  bool _disableCollisionsOnAttach,
51  std::map<std::string, physics::CollisionPtr>& _collisionElems)
52 {
53  this->gripperName=_gripperName;
54  this->attached = false;
55  this->disableCollisionsOnAttach = _disableCollisionsOnAttach;
56  this->model = _model;
57  physics::PhysicsEnginePtr physics = this->model->GetWorld()->GetPhysicsEngine();
58  this->fixedJoint = physics->CreateJoint("revolute");
59 
60  this->palmLink = this->model->GetLink(palmLinkName);
61  if (!this->palmLink)
62  {
63  gzerr << "GazeboGraspGripper: Palm link "<<palmLinkName<<" not found. The gazebo grasp plugin will not work."<<std::endl;
64  return false;
65  }
66  for (std::vector<std::string>::const_iterator fingerIt=fingerLinkNames.begin();
67  fingerIt!=fingerLinkNames.end(); ++fingerIt)
68  {
69  physics::LinkPtr link = this->model->GetLink(*fingerIt);
70  //std::cout<<"Got link "<<fingerLinkElem->Get<std::string>()<<std::endl;
71  if (!link.get()){
72  gzerr << "GazeboGraspGripper ERROR: Link "<<*fingerIt<<" can't be found in gazebo for GazeboGraspGripper model plugin. Skipping."<<std::endl;
73  continue;
74  }
75  for (unsigned int j = 0; j < link->GetChildCount(); ++j)
76  {
77  physics::CollisionPtr collision = link->GetCollision(j);
78  std::string collName = collision->GetScopedName();
79  //collision->SetContactsEnabled(true);
80  std::map<std::string, physics::CollisionPtr>::iterator collIter = collisionElems.find(collName);
81  if (collIter != this->collisionElems.end()) { //this collision was already added before
82  gzwarn <<"GazeboGraspGripper: Adding Gazebo collision link element "<<collName<<" multiple times, the gazebo grasp handler may not work properly"<<std::endl;
83  continue;
84  }
85  this->collisionElems[collName] = collision;
86  _collisionElems[collName] = collision;
87  }
88  }
89  return !this->collisionElems.empty();
90 }
91 
92 
93 
94 const std::string& GazeboGraspGripper::getGripperName() const
95 {
96  return gripperName;
97 }
98 
99 bool GazeboGraspGripper::hasLink(const std::string& linkName) const
100 {
101  for (std::vector<std::string>::const_iterator it=linkNames.begin(); it!=linkNames.end(); ++it)
102  {
103  if (*it==linkName) return true;
104  }
105  return false;
106 }
107 
108 bool GazeboGraspGripper::hasCollisionLink(const std::string& linkName) const
109 {
110  return collisionElems.find(linkName) != collisionElems.end();
111 }
112 
113 
115 {
116  return attached;
117 }
118 
119 const std::string& GazeboGraspGripper::attachedObject() const
120 {
121  return attachedObjName;
122 }
123 
124 
125 
126 // #define USE_MODEL_ATTACH // this only works if the object is a model in itself, which is usually not
127 // the case. Leaving this in here anyway for documentation of what has been
128 // tried, and for and later re-evaluation.
129 bool GazeboGraspGripper::HandleAttach(const std::string& objName)
130 {
131  if (!this->palmLink) {
132  gzwarn << "GazeboGraspGripper: palm link not found, not enforcing grasp hack!\n"<<std::endl;
133  return false;
134  }
135  physics::WorldPtr world = this->model->GetWorld();
136  if (!world.get())
137  {
138  gzerr << "GazeboGraspGripper: world is NULL"<<std::endl;
139  return false;
140  }
141 #ifdef USE_MODEL_ATTACH
142  physics::ModelPtr obj = world->GetModel(objName);
143  if (!obj.get()){
144  std::cerr<<"ERROR: Object ModelPtr "<<objName<<" not found in world, can't attach it"<<std::endl;
145  return false;
146  }
147  gazebo::math::Pose diff = obj->GetLink()->GetWorldPose() - this->palmLink->GetWorldPose();
148  this->palmLink->AttachStaticModel(obj,diff);
149 #else
150  physics::CollisionPtr obj = boost::dynamic_pointer_cast<physics::Collision>(world->GetEntity(objName));
151  if (!obj.get()){
152  std::cerr<<"ERROR: Object "<<objName<<" not found in world, can't attach it"<<std::endl;
153  return false;
154  }
155  gazebo::math::Pose diff = obj->GetLink()->GetWorldPose() - this->palmLink->GetWorldPose();
156  this->fixedJoint->Load(this->palmLink,obj->GetLink(), diff);
157  this->fixedJoint->Init();
158  this->fixedJoint->SetHighStop(0, 0);
159  this->fixedJoint->SetLowStop(0, 0);
160  if (this->disableCollisionsOnAttach) {
161  // we can disable collisions of the grasped object, because when the fingers keep colliding with
162  // it, the fingers keep wobbling, which can create difficulties when moving the arm.
163  obj->GetLink()->SetCollideMode("none");
164  }
165 #endif // USE_MODEL_ATTACH
166  this->attached=true;
167  this->attachedObjName=objName;
168  return true;
169 }
170 
171 void GazeboGraspGripper::HandleDetach(const std::string& objName)
172 {
173  physics::WorldPtr world = this->model->GetWorld();
174  if (!world.get())
175  {
176  gzerr << "GazeboGraspGripper: world is NULL"<<std::endl<<std::endl;
177  return;
178  }
179 #ifdef USE_MODEL_ATTACH
180  physics::ModelPtr obj = world->GetModel(objName);
181  if (!obj.get()){
182  std::cerr<<"ERROR: Object ModelPtr "<<objName<<" not found in world, can't detach it"<<std::endl;
183  return;
184  }
185  this->palmLink->DetachStaticModel(objName);
186 #else
187  physics::CollisionPtr obj = boost::dynamic_pointer_cast<physics::Collision>(world->GetEntity(objName));
188  if (!obj.get()){
189  std::cerr<<"ERROR: Object "<<objName<<" not found in world, can't attach it"<<std::endl;
190  return;
191  }
192  else if (this->disableCollisionsOnAttach)
193  {
194  obj->GetLink()->SetCollideMode("all");
195  }
196  this->fixedJoint->Detach();
197 #endif // USE_MODEL_ATTACH
198  this->attached=false;
199  this->attachedObjName="";
200 }
const std::string & getGripperName() const
void HandleDetach(const std::string &objName)
std::map< std::string, physics::CollisionPtr > collisionElems
std::vector< std::string > linkNames
bool hasLink(const std::string &linkName) const
const std::string & attachedObject() const
Helper class for GazeboGraspFix which holds information for one arm. Attaches /detaches objects to th...
bool HandleAttach(const std::string &objName)
bool Init(physics::ModelPtr &_model, const std::string &_gripperName, const std::string &palmLinkName, const std::vector< std::string > &fingerLinkNames, bool _disableCollisionsOnAttach, std::map< std::string, physics::CollisionPtr > &_collisions)
bool hasCollisionLink(const std::string &linkName) const
int ALVAR_EXPORT diff(const std::vector< C > &v, std::vector< C > &ret)


robotican_common
Author(s):
autogenerated on Wed Jan 3 2018 03:48:33