SimpleGraspGenerator.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2015-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package RobotComponents::SimpleGraspGenerator
19  * @author Valerij Wittenbeck (valerij dot wittenbeck at student dot kit dot edu)
20  * @date 2016
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "SimpleGraspGenerator.h"
31 #include <VirtualRobot/Grasping/GraspSet.h>
35 
36 using namespace armarx;
37 using namespace memoryx;
38 
40 {
41  usingProxy("WorkingMemory");
42  usingProxy("PriorKnowledge");
43  usingProxy("RobotStateComponent");
44 
45  offeringTopic("DebugDrawerUpdates");
46 }
47 
49 {
50  getProxy(rsc, "RobotStateComponent");
51  getProxy(wm, "WorkingMemory");
52  getProxy(prior, "PriorKnowledge");
53  objectInstances = wm->getObjectInstancesSegment();
54  objectClasses = prior->getObjectClassesSegment();
55 
56  fileManager = memoryx::GridFileManagerPtr(new memoryx::GridFileManager(prior->getCommonStorage()));
57 
58  debugDrawerPrx = getTopic<armarx::DebugDrawerInterfacePrx>("DebugDrawerUpdates");
59  robot = RemoteRobot::createLocalCloneFromFile(rsc, VirtualRobot::RobotIO::eStructure);
60 
61 }
62 
64 {
65 }
66 
68 {
69 }
70 
71 StringStringDictionary SimpleGraspGenerator::getTcpGcpMapping()
72 {
73  StringStringDictionary mapping;
74  auto entries = Split(getProperty<std::string>("TCPtoGCPMapping"), ";", true, true);
75  for (auto& e : entries)
76  {
77  auto split = Split(e, ":", true, true);
78  ARMARX_CHECK_EXPRESSION(split.size() == 2) << e;
79  mapping[split.at(0)] = split.at(1);
80  }
81  return mapping;
82 }
83 
84 GeneratedGraspList SimpleGraspGenerator::generateGrasps(const std::string& objectInstanceEntityId, const Ice::Current& c)
85 {
86  GeneratedGraspList result;
87  // int counter = 0;
88 
89  ObjectInstancePtr instance = ObjectInstancePtr::dynamicCast(objectInstances->getEntityById(objectInstanceEntityId));
90  ARMARX_CHECK_EXPRESSION(instance) << "no instance with id '" << objectInstanceEntityId << "'";
91 
92  ObjectClassPtr objectClass = ObjectClassPtr::dynamicCast(objectClasses->getEntityByName(instance->getMostProbableClass()));
93  ARMARX_CHECK_EXPRESSION(objectClass) << "no object class with name '" << instance->getMostProbableClass() << "' found ";
94  memoryx::EntityWrappers::SimoxObjectWrapperPtr simoxWrapper = objectClass->addWrapper(new memoryx::EntityWrappers::SimoxObjectWrapper(fileManager));
95  ARMARX_CHECK_EXPRESSION(simoxWrapper);
96  VirtualRobot::ManipulationObjectPtr mo = simoxWrapper->getManipulationObject();
98 
99 
100  auto objectFramedPose = instance->getPose();
101  ARMARX_INFO << "object Pose: " << objectFramedPose->output();
102  if (instance->hasLocalizationTimestamp())
103  {
104 
105  RemoteRobot::synchronizeLocalCloneToTimestamp(robot, rsc, instance->getLocalizationTimestamp().toMicroSeconds());
106  ARMARX_INFO << "object has timestamp " << instance->getLocalizationTimestamp().toDateTime() << " and frame " << objectFramedPose->frame << " with robot at " << robot->getGlobalPose();
107  objectFramedPose->changeToGlobal(robot);
108  }
109  else
110  // dangerous - robot might have moved!
111  {
112  objectFramedPose->changeToGlobal(rsc->getSynchronizedRobot());
113  }
114  ARMARX_INFO << "global object Pose: " << objectFramedPose->output();
115 
116  ARMARX_CHECK_EXPRESSION(objectFramedPose->frame == armarx::GlobalFrame);
117  Eigen::Matrix4f globalObjectPose = objectFramedPose->toEigen();
118  if (getProperty<bool>("EnableVisualization"))
119  {
120  debugDrawerPrx->setPoseVisu("GeneratedGrasps", "ObjectPose", objectFramedPose);
121  }
122 
123  // draw the position of the object
124  // Eigen::Vector3f tmpObjectPos = localObjectPose.block<3, 1>(0, 3);
125  // armarx::Vector3Ptr objectPos = new armarx::Vector3(tmpObjectPos);
126 
127  // debugDrawerPrx->setSphereDebugLayerVisu("objectSphere", objectPos, objectColor, 50.0f);
128 
129  auto tcpGcpMapping = getTcpGcpMapping();
130  std::string graspNameInfix = getProperty<std::string>("GraspNameInfix");
131  int countGrasps = 0;
132  auto robotType = getProperty<std::string>("RobotType").getValue();
133  for (const VirtualRobot::GraspSetPtr& gs : mo->getAllGraspSets())
134  {
135  if (gs->getRobotType() != robotType)
136  {
137  continue;
138  }
139  for (const VirtualRobot::GraspPtr& g : gs->getGrasps())
140  {
141  ARMARX_INFO << "Found Grasp: " << g->getName() << " for eef: " << g->getEefName();
142  if (!Contains(g->getName(), graspNameInfix, true))
143  {
144  ARMARX_INFO << "grasp name does not contain infix " << graspNameInfix << " - skipping it";
145  continue;
146  }
147  if (!robot->hasEndEffector(g->getEefName()))
148  {
149  ARMARX_INFO << "Endeffector " << g->getEefName() << " does not exist in this robot - skipping grasps";
150  continue;
151  }
152  auto globalTcpPose = g->getTcpPoseGlobal(globalObjectPose);
153  FramedPosePtr framedTcpPose {new FramedPose(globalTcpPose, objectFramedPose->frame, "")};
154 
155  // calculate prepose
156  auto gcpName = tcpGcpMapping.at(robot->getEndEffector(g->getEefName())->getTcp()->getName());
157  auto gcpNode = robot->getRobotNode(gcpName);
158  ARMARX_CHECK_EXPRESSION(gcpNode) << gcpName;
159  Eigen::Vector3f preposeOffsetGCPFrame = Eigen::Vector3f::Zero();
160  preposeOffsetGCPFrame(2) = -getProperty<float>("PreposeOffset").getValue();
161  Eigen::Vector3f preposeOffsetTCPFrame = robot->getEndEffector(g->getEefName())->getTcp()->transformTo(gcpNode, preposeOffsetGCPFrame);
162  Eigen::Matrix4f preposeOffsetGlobal = globalTcpPose;
163  preposeOffsetGlobal.block<3, 1>(0, 3) += globalTcpPose.block<3, 3>(0, 0) * preposeOffsetTCPFrame;
164  ARMARX_INFO << " before up offset: " << preposeOffsetGlobal;
165  preposeOffsetGlobal(2, 3) += getProperty<float>("UpwardsOffset").getValue();
166  ARMARX_INFO << VAROUT(preposeOffsetGCPFrame) << "\n" << VAROUT(preposeOffsetTCPFrame) << "\n" << VAROUT(preposeOffsetGlobal) << VAROUT(globalTcpPose);
167  FramedPosePtr framedTcpPrepose = new FramedPose(preposeOffsetGlobal, armarx::GlobalFrame, "");
168  // prepose->changeFrame(robot, gcpName);
169  result.push_back({1.f, g->getEefName(), g->getName(), framedTcpPose, framedTcpPrepose});
170 
171  countGrasps++;
172  }
173  }
174 
175  if (result.empty())
176  {
177  ARMARX_WARNING << "No grasps defined for object class '" << instance->getMostProbableClass() << "'";
178  }
179  ARMARX_INFO << "Found " << result.size() << " grasps";
180  return result;
181 }
182 
183 GeneratedGraspList SimpleGraspGenerator::generateGraspsByObjectName(const std::string& objectName, const Ice::Current& c)
184 {
185  memoryx::ObjectInstanceBasePtr objectInstance = wm->getObjectInstancesSegment()->getObjectInstanceByName(objectName);
186  std::string id = objectInstance->getId();
187  ARMARX_IMPORTANT << "objectName: " << objectName << "; instanceID: " << id;
188  return generateGrasps(id);
189 }
RemoteRobot.h
LinkedPose.h
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:37
armarx::RemoteRobot::createLocalCloneFromFile
static VirtualRobot::RobotPtr createLocalCloneFromFile(RobotStateComponentInterfacePrx robotStatePrx, VirtualRobot::RobotIO::RobotDescription loadMode=VirtualRobot::RobotIO::eFull)
This is a convenience function for createLocalClone, which automatically gets the filename from the R...
Definition: RemoteRobot.cpp:441
armarx::Contains
bool Contains(const ContainerType &container, const ElementType &searchElement)
Definition: algorithm.h:295
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:35
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::SimpleGraspGenerator::generateGrasps
GeneratedGraspList generateGrasps(const std::string &objectInstanceEntityId, const Ice::Current &c=Ice::emptyCurrent) override
Calculates the framedTCPPose and framedTCPPrepose.
Definition: SimpleGraspGenerator.cpp:84
armarx::SimpleGraspGenerator::onConnectComponent
void onConnectComponent() override
Definition: SimpleGraspGenerator.cpp:48
StringHelpers.h
IceInternal::Handle< ObjectInstance >
armarx::SimpleGraspGenerator::onExitComponent
void onExitComponent() override
Definition: SimpleGraspGenerator.cpp:67
armarx::SimpleGraspGenerator::onInitComponent
void onInitComponent() override
Definition: SimpleGraspGenerator.cpp:39
FramedPose.h
armarx::SimpleGraspGenerator::onDisconnectComponent
void onDisconnectComponent() override
Definition: SimpleGraspGenerator.cpp:63
MemoryXCoreObjectFactories.h
memoryx::EntityWrappers::SimoxObjectWrapper
SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject (i.e visualization,...
Definition: SimoxObjectWrapper.h:46
SimoxObjectWrapper.h
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
ObjectInstance.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
memoryx::GridFileManagerPtr
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:32
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::RemoteRobot::synchronizeLocalCloneToTimestamp
static bool synchronizeLocalCloneToTimestamp(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx, Ice::Long timestamp)
Synchronizes a local robot to a robot state at timestamp.
Definition: RemoteRobot.cpp:487
memoryx::GridFileManager
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
Definition: GridFileManager.h:42
MemoryXTypesObjectFactories.h
SimpleGraspGenerator.h
armarx::SimpleGraspGenerator::generateGraspsByObjectName
GeneratedGraspList generateGraspsByObjectName(const std::string &objectName, const Ice::Current &c=Ice::emptyCurrent) override
Definition: SimpleGraspGenerator.cpp:183
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36