Go to the documentation of this file.00001 #include <boost/bind.hpp>
00002 #include <gazebo/gazebo.hh>
00003 #include <gazebo/physics/physics.hh>
00004 #include <gazebo/physics/ContactManager.hh>
00005 #include <gazebo/physics/Contact.hh>
00006 #include <gazebo/common/common.hh>
00007 #include <stdio.h>
00008
00009 #include <gazebo_grasp_plugin/GazeboGraspGripper.h>
00010 #include <gazebo_version_helpers/GazeboVersionHelpers.h>
00011
00012 using gazebo::GazeboGraspGripper;
00013
00014 #define DEFAULT_FORCES_ANGLE_TOLERANCE 120
00015 #define DEFAULT_UPDATE_RATE 5
00016 #define DEFAULT_MAX_GRIP_COUNT 10
00017 #define DEFAULT_RELEASE_TOLERANCE 0.005
00018 #define DEFAULT_DISABLE_COLLISIONS_ON_ATTACH false
00019
00021 GazeboGraspGripper::GazeboGraspGripper():
00022 attached(false)
00023 {
00024 }
00025
00027 GazeboGraspGripper::GazeboGraspGripper(const GazeboGraspGripper &o):
00028 model(o.model),
00029 gripperName(o.gripperName),
00030 linkNames(o.linkNames),
00031 collisionElems(o.collisionElems),
00032 fixedJoint(o.fixedJoint),
00033 palmLink(o.palmLink),
00034 disableCollisionsOnAttach(o.disableCollisionsOnAttach),
00035 attached(o.attached),
00036 attachedObjName(o.attachedObjName)
00037 {}
00038
00040 GazeboGraspGripper::~GazeboGraspGripper()
00041 {
00042 this->model.reset();
00043 }
00044
00046 bool GazeboGraspGripper::Init(physics::ModelPtr &_model,
00047 const std::string &_gripperName,
00048 const std::string &palmLinkName,
00049 const std::vector<std::string> &fingerLinkNames,
00050 bool _disableCollisionsOnAttach,
00051 std::map<std::string, physics::CollisionPtr> &_collisionElems)
00052 {
00053 this->gripperName = _gripperName;
00054 this->attached = false;
00055 this->disableCollisionsOnAttach = _disableCollisionsOnAttach;
00056 this->model = _model;
00057 physics::PhysicsEnginePtr physics =
00058 gazebo::GetPhysics(this->model->GetWorld());
00059 this->fixedJoint = physics->CreateJoint("revolute");
00060
00061 this->palmLink = this->model->GetLink(palmLinkName);
00062 if (!this->palmLink)
00063 {
00064 gzerr << "GazeboGraspGripper: Palm link " << palmLinkName <<
00065 " not found. The gazebo grasp plugin will not work." << std::endl;
00066 return false;
00067 }
00068 for (std::vector<std::string>::const_iterator fingerIt =
00069 fingerLinkNames.begin();
00070 fingerIt != fingerLinkNames.end(); ++fingerIt)
00071 {
00072 physics::LinkPtr link = this->model->GetLink(*fingerIt);
00073
00074 if (!link.get())
00075 {
00076 gzerr << "GazeboGraspGripper ERROR: Link " << *fingerIt <<
00077 " can't be found in gazebo for GazeboGraspGripper model plugin. Skipping." <<
00078 std::endl;
00079 continue;
00080 }
00081 for (unsigned int j = 0; j < link->GetChildCount(); ++j)
00082 {
00083 physics::CollisionPtr collision = link->GetCollision(j);
00084 std::string collName = collision->GetScopedName();
00085
00086 std::map<std::string, physics::CollisionPtr>::iterator collIter =
00087 collisionElems.find(collName);
00088 if (collIter !=
00089 this->collisionElems.end())
00090 {
00091 gzwarn << "GazeboGraspGripper: Adding Gazebo collision link element " <<
00092 collName << " multiple times, the gazebo grasp handler may not work properly" <<
00093 std::endl;
00094 continue;
00095 }
00096 this->collisionElems[collName] = collision;
00097 _collisionElems[collName] = collision;
00098 }
00099 }
00100 return !this->collisionElems.empty();
00101 }
00102
00104 const std::string &GazeboGraspGripper::getGripperName() const
00105 {
00106 return gripperName;
00107 }
00108
00110 bool GazeboGraspGripper::hasLink(const std::string &linkName) const
00111 {
00112 for (std::vector<std::string>::const_iterator it = linkNames.begin();
00113 it != linkNames.end(); ++it)
00114 {
00115 if (*it == linkName) return true;
00116 }
00117 return false;
00118 }
00119
00121 bool GazeboGraspGripper::hasCollisionLink(const std::string &linkName) const
00122 {
00123 return collisionElems.find(linkName) != collisionElems.end();
00124 }
00125
00126
00128 bool GazeboGraspGripper::isObjectAttached() const
00129 {
00130 return attached;
00131 }
00132
00134 const std::string &GazeboGraspGripper::attachedObject() const
00135 {
00136 return attachedObjName;
00137 }
00138
00139
00140
00142
00143
00144
00145 bool GazeboGraspGripper::HandleAttach(const std::string &objName)
00146 {
00147 if (!this->palmLink)
00148 {
00149 gzwarn << "GazeboGraspGripper: palm link not found, not enforcing grasp hack!\n"
00150 << std::endl;
00151 return false;
00152 }
00153 physics::WorldPtr world = this->model->GetWorld();
00154 if (!world.get())
00155 {
00156 gzerr << "GazeboGraspGripper: world is NULL" << std::endl;
00157 return false;
00158 }
00159 #ifdef USE_MODEL_ATTACH
00160 physics::ModelPtr obj = world->GetModel(objName);
00161 if (!obj.get())
00162 {
00163 std::cerr << "ERROR: Object ModelPtr " << objName <<
00164 " not found in world, can't attach it" << std::endl;
00165 return false;
00166 }
00167 gazebo::math::Pose diff = obj->GetLink()->GetWorldPose() -
00168 this->palmLink->GetWorldPose();
00169 this->palmLink->AttachStaticModel(obj, diff);
00170 #else
00171 physics::CollisionPtr obj =
00172 boost::dynamic_pointer_cast<physics::Collision>(gazebo::GetEntityByName(world, objName));
00173 if (!obj.get())
00174 {
00175 std::cerr << "ERROR: Object " << objName <<
00176 " not found in world, can't attach it" << std::endl;
00177 return false;
00178 }
00179 gazebo::GzPose3 diff = gazebo::GetWorldPose(obj->GetLink()) -
00180 gazebo::GetWorldPose(this->palmLink);
00181 this->fixedJoint->Load(this->palmLink, obj->GetLink(), diff);
00182 this->fixedJoint->Init();
00183 #if GAZEBO_MAJOR_VERSION >= 8
00184 this->fixedJoint->SetUpperLimit(0, 0);
00185 this->fixedJoint->SetLowerLimit(0, 0);
00186 #else
00187 this->fixedJoint->SetHighStop(0, 0);
00188 this->fixedJoint->SetLowStop(0, 0);
00189 #endif
00190
00191 if (this->disableCollisionsOnAttach)
00192 {
00193
00194
00195 obj->GetLink()->SetCollideMode("none");
00196 }
00197 #endif // USE_MODEL_ATTACH
00198 this->attached = true;
00199 this->attachedObjName = objName;
00200 return true;
00201 }
00202
00204 void GazeboGraspGripper::HandleDetach(const std::string &objName)
00205 {
00206 physics::WorldPtr world = this->model->GetWorld();
00207 if (!world.get())
00208 {
00209 gzerr << "GazeboGraspGripper: world is NULL" << std::endl << std::endl;
00210 return;
00211 }
00212 #ifdef USE_MODEL_ATTACH
00213 physics::ModelPtr obj = world->GetModel(objName);
00214 if (!obj.get())
00215 {
00216 std::cerr << "ERROR: Object ModelPtr " << objName <<
00217 " not found in world, can't detach it" << std::endl;
00218 return;
00219 }
00220 this->palmLink->DetachStaticModel(objName);
00221 #else
00222 physics::CollisionPtr obj = boost::dynamic_pointer_cast<physics::Collision>
00223 (gazebo::GetEntityByName(world, objName));
00224 if (!obj.get())
00225 {
00226 std::cerr << "ERROR: Object " << objName <<
00227 " not found in world, can't attach it" << std::endl;
00228 return;
00229 }
00230 else if (this->disableCollisionsOnAttach)
00231 {
00232 obj->GetLink()->SetCollideMode("all");
00233 }
00234
00235
00236 #if 0
00237 if (obj && obj->GetLink())
00238 {
00239 auto linVel = GetWorldVelocity(obj->GetLink());
00240 gzmsg << "PRE-DETACH Velocity for link " << obj->GetLink()->GetName()
00241 << " (collision name " << objName << "): " << linVel
00242 << ", absolute val " << GetLength(linVel) << std::endl;
00243 }
00244 #endif
00245
00246
00247 this->fixedJoint->Detach();
00248
00249
00250 #if 0
00251 if (obj && obj->GetLink())
00252 {
00253 auto linVel = GetWorldVelocity(obj->GetLink());
00254 gzmsg << "POST-DETACH Velocity for link " << obj->GetLink()->GetName()
00255 << " (collision name " << objName << "): " << linVel
00256 << ", absolute val " << GetLength(linVel) << std::endl;
00257
00258 }
00259 #endif
00260
00261
00262 #endif // USE_MODEL_ATTACH
00263 this->attached = false;
00264 this->attachedObjName = "";
00265 }