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
26
27#include <VirtualRobot/Grasping/Grasp.h>
28#include <VirtualRobot/Grasping/GraspSet.h>
29
31
35
40
41using namespace armarx;
42using namespace memoryx;
43
44void
46{
47 usingProxy("WorkingMemory");
48 usingProxy("PriorKnowledge");
49 usingProxy("RobotStateComponent");
50
51 offeringTopic("DebugDrawerUpdates");
52}
53
54void
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
70void
74
75void
79
80StringStringDictionary
81SimpleGraspGenerator::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
94GeneratedGraspList
95SimpleGraspGenerator::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
213GeneratedGraspList
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}
#define VAROUT(x)
constexpr T c
Property< PropertyType > getProperty(const std::string &name)
The FramedPose class.
Definition FramedPose.h:281
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
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...
static bool synchronizeLocalCloneToTimestamp(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx, Ice::Long timestamp)
Synchronizes a local robot to a robot state at timestamp.
GeneratedGraspList generateGraspsByObjectName(const std::string &objectName, const Ice::Current &c=Ice::emptyCurrent) override
GeneratedGraspList generateGrasps(const std::string &objectInstanceEntityId, const Ice::Current &c=Ice::emptyCurrent) override
Calculates the framedTCPPose and framedTCPPrepose.
SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject (i.e visualization,...
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
std::string const GlobalFrame
Variable of the global coordinate system.
Definition FramedPose.h:65
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
bool Contains(const ContainerType &container, const ElementType &searchElement)
Definition algorithm.h:330
IceInternal::Handle< FramedPose > FramedPosePtr
Definition FramedPose.h:272
IceInternal::Handle< SimoxObjectWrapper > SimoxObjectWrapperPtr
VirtualRobot headers.
IceInternal::Handle< ObjectInstance > ObjectInstancePtr
IceInternal::Handle< ObjectClass > ObjectClassPtr
Definition ObjectClass.h:35
std::shared_ptr< GridFileManager > GridFileManagerPtr