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"
26 
27 #include <VirtualRobot/Grasping/Grasp.h>
28 #include <VirtualRobot/Grasping/GraspSet.h>
29 
31 
35 
40 
41 using namespace armarx;
42 using namespace memoryx;
43 
44 void
46 {
47  usingProxy("WorkingMemory");
48  usingProxy("PriorKnowledge");
49  usingProxy("RobotStateComponent");
50 
51  offeringTopic("DebugDrawerUpdates");
52 }
53 
54 void
56 {
57  getProxy(rsc, "RobotStateComponent");
58  getProxy(wm, "WorkingMemory");
59  getProxy(prior, "PriorKnowledge");
60  objectInstances = wm->getObjectInstancesSegment();
61  objectClasses = prior->getObjectClassesSegment();
62 
63  fileManager =
64  memoryx::GridFileManagerPtr(new memoryx::GridFileManager(prior->getCommonStorage()));
65 
66  debugDrawerPrx = getTopic<armarx::DebugDrawerInterfacePrx>("DebugDrawerUpdates");
67  robot = RemoteRobot::createLocalCloneFromFile(rsc, VirtualRobot::RobotIO::eStructure);
68 }
69 
70 void
72 {
73 }
74 
75 void
77 {
78 }
79 
80 StringStringDictionary
81 SimpleGraspGenerator::getTcpGcpMapping()
82 {
83  StringStringDictionary mapping;
84  auto entries = Split(getProperty<std::string>("TCPtoGCPMapping"), ";", true, true);
85  for (auto& e : entries)
86  {
87  auto split = Split(e, ":", true, true);
88  ARMARX_CHECK_EXPRESSION(split.size() == 2) << e;
89  mapping[split.at(0)] = split.at(1);
90  }
91  return mapping;
92 }
93 
94 GeneratedGraspList
95 SimpleGraspGenerator::generateGrasps(const std::string& objectInstanceEntityId,
96  const Ice::Current& c)
97 {
98  GeneratedGraspList result;
99  // int counter = 0;
100 
101  ObjectInstancePtr instance =
102  ObjectInstancePtr::dynamicCast(objectInstances->getEntityById(objectInstanceEntityId));
103  ARMARX_CHECK_EXPRESSION(instance) << "no instance with id '" << objectInstanceEntityId << "'";
104 
105  ObjectClassPtr objectClass = ObjectClassPtr::dynamicCast(
106  objectClasses->getEntityByName(instance->getMostProbableClass()));
107  ARMARX_CHECK_EXPRESSION(objectClass)
108  << "no object class with name '" << instance->getMostProbableClass() << "' found ";
110  objectClass->addWrapper(new memoryx::EntityWrappers::SimoxObjectWrapper(fileManager));
111  ARMARX_CHECK_EXPRESSION(simoxWrapper);
112  VirtualRobot::ManipulationObjectPtr mo = simoxWrapper->getManipulationObject();
114 
115 
116  auto objectFramedPose = instance->getPose();
117  ARMARX_INFO << "object Pose: " << objectFramedPose->output();
118  if (instance->hasLocalizationTimestamp())
119  {
120 
122  robot, rsc, instance->getLocalizationTimestamp().toMicroSeconds());
123  ARMARX_INFO << "object has timestamp " << instance->getLocalizationTimestamp().toDateTime()
124  << " and frame " << objectFramedPose->frame << " with robot at "
125  << robot->getGlobalPose();
126  objectFramedPose->changeToGlobal(robot);
127  }
128  else
129  // dangerous - robot might have moved!
130  {
131  objectFramedPose->changeToGlobal(rsc->getSynchronizedRobot());
132  }
133  ARMARX_INFO << "global object Pose: " << objectFramedPose->output();
134 
135  ARMARX_CHECK_EXPRESSION(objectFramedPose->frame == armarx::GlobalFrame);
136  Eigen::Matrix4f globalObjectPose = objectFramedPose->toEigen();
137  if (getProperty<bool>("EnableVisualization"))
138  {
139  debugDrawerPrx->setPoseVisu("GeneratedGrasps", "ObjectPose", objectFramedPose);
140  }
141 
142  // draw the position of the object
143  // Eigen::Vector3f tmpObjectPos = localObjectPose.block<3, 1>(0, 3);
144  // armarx::Vector3Ptr objectPos = new armarx::Vector3(tmpObjectPos);
145 
146  // debugDrawerPrx->setSphereDebugLayerVisu("objectSphere", objectPos, objectColor, 50.0f);
147 
148  auto tcpGcpMapping = getTcpGcpMapping();
149  std::string graspNameInfix = getProperty<std::string>("GraspNameInfix");
150  int countGrasps = 0;
151  auto robotType = getProperty<std::string>("RobotType").getValue();
152  for (const VirtualRobot::GraspSetPtr& gs : mo->getAllGraspSets())
153  {
154  if (gs->getRobotType() != robotType)
155  {
156  continue;
157  }
158  for (const VirtualRobot::GraspPtr& g : gs->getGrasps())
159  {
160  ARMARX_INFO << "Found Grasp: " << g->getName() << " for eef: " << g->getEefName();
161  if (!Contains(g->getName(), graspNameInfix, true))
162  {
163  ARMARX_INFO << "grasp name does not contain infix " << graspNameInfix
164  << " - skipping it";
165  continue;
166  }
167  if (!robot->hasEndEffector(g->getEefName()))
168  {
169  ARMARX_INFO << "Endeffector " << g->getEefName()
170  << " does not exist in this robot - skipping grasps";
171  continue;
172  }
173  auto globalTcpPose = g->getTcpPoseGlobal(globalObjectPose);
174  FramedPosePtr framedTcpPose{new FramedPose(globalTcpPose, objectFramedPose->frame, "")};
175 
176  // calculate prepose
177  auto gcpName =
178  tcpGcpMapping.at(robot->getEndEffector(g->getEefName())->getTcp()->getName());
179  auto gcpNode = robot->getRobotNode(gcpName);
180  ARMARX_CHECK_EXPRESSION(gcpNode) << gcpName;
181  Eigen::Vector3f preposeOffsetGCPFrame = Eigen::Vector3f::Zero();
182  preposeOffsetGCPFrame(2) = -getProperty<float>("PreposeOffset").getValue();
183  Eigen::Vector3f preposeOffsetTCPFrame =
184  robot->getEndEffector(g->getEefName())
185  ->getTcp()
186  ->transformTo(gcpNode, preposeOffsetGCPFrame);
187  Eigen::Matrix4f preposeOffsetGlobal = globalTcpPose;
188  preposeOffsetGlobal.block<3, 1>(0, 3) +=
189  globalTcpPose.block<3, 3>(0, 0) * preposeOffsetTCPFrame;
190  ARMARX_INFO << " before up offset: " << preposeOffsetGlobal;
191  preposeOffsetGlobal(2, 3) += getProperty<float>("UpwardsOffset").getValue();
192  ARMARX_INFO << VAROUT(preposeOffsetGCPFrame) << "\n"
193  << VAROUT(preposeOffsetTCPFrame) << "\n"
194  << VAROUT(preposeOffsetGlobal) << VAROUT(globalTcpPose);
195  FramedPosePtr framedTcpPrepose =
196  new FramedPose(preposeOffsetGlobal, armarx::GlobalFrame, "");
197  // prepose->changeFrame(robot, gcpName);
198  result.push_back({1.f, g->getEefName(), g->getName(), framedTcpPose, framedTcpPrepose});
199 
200  countGrasps++;
201  }
202  }
203 
204  if (result.empty())
205  {
206  ARMARX_WARNING << "No grasps defined for object class '" << instance->getMostProbableClass()
207  << "'";
208  }
209  ARMARX_INFO << "Found " << result.size() << " grasps";
210  return result;
211 }
212 
213 GeneratedGraspList
215  const Ice::Current& c)
216 {
217  memoryx::ObjectInstanceBasePtr objectInstance =
218  wm->getObjectInstancesSegment()->getObjectInstanceByName(objectName);
219  std::string id = objectInstance->getId();
220  ARMARX_IMPORTANT << "objectName: " << objectName << "; instanceID: " << id;
221  return generateGrasps(id);
222 }
RemoteRobot.h
LinkedPose.h
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:36
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:512
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::Contains
bool Contains(const ContainerType &container, const ElementType &searchElement)
Definition: algorithm.h:330
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:65
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:36
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::SimpleGraspGenerator::generateGrasps
GeneratedGraspList generateGrasps(const std::string &objectInstanceEntityId, const Ice::Current &c=Ice::emptyCurrent) override
Calculates the framedTCPPose and framedTCPPrepose.
Definition: SimpleGraspGenerator.cpp:95
armarx::SimpleGraspGenerator::onConnectComponent
void onConnectComponent() override
Definition: SimpleGraspGenerator.cpp:55
StringHelpers.h
IceInternal::Handle< ObjectInstance >
armarx::SimpleGraspGenerator::onExitComponent
void onExitComponent() override
Definition: SimpleGraspGenerator.cpp:76
armarx::SimpleGraspGenerator::onInitComponent
void onInitComponent() override
Definition: SimpleGraspGenerator.cpp:45
FramedPose.h
armarx::SimpleGraspGenerator::onDisconnectComponent
void onDisconnectComponent() override
Definition: SimpleGraspGenerator.cpp:71
MemoryXCoreObjectFactories.h
memoryx::EntityWrappers::SimoxObjectWrapper
SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject (i.e visualization,...
Definition: SimoxObjectWrapper.h:46
SimoxObjectWrapper.h
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:181
memoryx::GridFileManagerPtr
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:33
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:198
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:566
memoryx::GridFileManager
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
Definition: GridFileManager.h:41
MemoryXTypesObjectFactories.h
SimpleGraspGenerator.h
armarx::SimpleGraspGenerator::generateGraspsByObjectName
GeneratedGraspList generateGraspsByObjectName(const std::string &objectName, const Ice::Current &c=Ice::emptyCurrent) override
Definition: SimpleGraspGenerator.cpp:214
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:38