GraspablePredicateProvider.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package ArmarX::
17* @author Valerij Wittenbeck ( valerij.wittenbeck at kit dot edu)
18* @date 2015
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
24
25#include <SimoxUtility/algorithm/string/string_tools.h>
26#include <VirtualRobot/Grasping/Grasp.h>
27#include <VirtualRobot/Grasping/GraspSet.h>
28
30
32
37
38using namespace memoryx;
39
41 leftColor(armarx::DrawColor{0.f, 0.f, 1.f, 0.25f}),
42 rightColor(armarx::DrawColor{0.f, 1.f, 0.f, 0.25f}),
43 leftGraspable(PredicateInfo{"leftgraspable", 1}),
44 rightGraspable(PredicateInfo{"rightgraspable", 1})
45{
46}
47
48std::vector<Box>
49GraspablePredicateProvider::stringToBoxes(const std::string& boxesString)
50{
51 std::vector<Box> result;
52 std::vector<std::string> boxTuples = simox::alg::split(boxesString, ";");
53
54 for (const auto& boxTupleString : boxTuples)
55 {
56 std::vector<std::string> boxTuple = simox::alg::split(boxTupleString, " ");
57 ARMARX_CHECK_EXPRESSION(boxTuple.size() == 6);
58
59 result.push_back(Box{std::stof(boxTuple[0]),
60 std::stof(boxTuple[1]),
61 std::stof(boxTuple[2]),
62 std::stof(boxTuple[3]),
63 std::stof(boxTuple[4]),
64 std::stof(boxTuple[5])});
65 }
66
67 return result;
68}
69
70void
72{
73 boxesL = stringToBoxes(getProperty<std::string>("BoundingBoxesL").getValue());
74 boxesR = stringToBoxes(getProperty<std::string>("BoundingBoxesR").getValue());
75
76 usingProxy("WorkingMemory");
77 usingProxy("PriorKnowledge");
78
79 debugDrawerTopicName = getProperty<std::string>("DebugDrawerTopicName").getValue();
80
81 if (!debugDrawerTopicName.empty())
82 {
83 offeringTopic(debugDrawerTopicName);
84 }
85}
86
87void
89{
90 wm = getProxy<WorkingMemoryInterfacePrx>("WorkingMemory");
91 objectInstances = wm->getObjectInstancesSegment();
92 prior = getProxy<PriorKnowledgeInterfacePrx>("PriorKnowledge");
93 objectClasses = prior->getObjectClassesSegment();
94 fileManager =
95 memoryx::GridFileManagerPtr(new memoryx::GridFileManager(prior->getCommonStorage()));
96
97 if (!debugDrawerTopicName.empty())
98 {
100 getTopic<armarx::DebugDrawerInterfacePrx>(debugDrawerTopicName);
101
102 for (size_t i = 0; i < boxesL.size(); ++i)
103 {
104 boxesL[i].drawTo(debugDrawer,
106 leftGraspable.name + "_box_" + std::to_string(i),
107 leftColor);
108 }
109
110 for (size_t i = 0; i < boxesR.size(); ++i)
111 {
112 boxesR[i].drawTo(debugDrawer,
114 rightGraspable.name + "_box_" + std::to_string(i),
115 rightColor);
116 }
117 }
118}
119
120std::string
122{
123 return "GraspablePredicateProvider";
124}
125
126PredicateInfoList
128{
129 return {leftGraspable, rightGraspable};
130}
131
132void
133GraspablePredicateProvider::addPredicateIfInside(PredicateInstanceList& result,
134 const std::string& predicateName,
135 const ObjectInstancePtr& obj,
136 const std::vector<Box>& boxes)
137{
138 bool inside = false;
139 auto pos = obj->getPosition();
140
141 if (pos->getFrame() != armarx::GlobalFrame && !pos->getFrame().empty())
142 {
143 pos = armarx::FramedPosePtr::dynamicCast(
144 wm->getAgentInstancesSegment()->convertToWorldPose(pos->agent, obj->getPose()))
145 ->getPosition();
146 }
147
148 ARMARX_DEBUG << "Obj: " << obj->getName() << " pos: " << *pos;
149
150 for (const auto& box : boxes)
151 {
152 inside |= box.inside(pos);
153 }
154
155 if (inside)
156 {
157 ARMARX_DEBUG << obj->getName() << " " << predicateName;
158 result.push_back(PredicateInstance{
159 predicateName, {objectInstances->getEntityRefById(obj->getId())}, true});
160 }
161}
162
163PredicateInstanceList
165{
166 PredicateInstanceList result;
167
168 for (const auto& entity : objectInstances->getAllEntities())
169 {
170 ObjectInstancePtr object = ObjectInstancePtr::dynamicCast(entity);
171 const std::string className = object->getMostProbableClass();
172
173 auto graspabilityDescriptorIt = std::find_if(graspabilityDescriptors.cbegin(),
174 graspabilityDescriptors.cend(),
175 [&](const GraspabilityDescriptor& gd)
176 { return gd.className == className; });
177
178 if (graspabilityDescriptorIt == graspabilityDescriptors.cend())
179 {
180 auto parents =
181 objectClasses->getObjectClassByNameWithAllParents(className)->getParentClasses();
182
183 bool hasGraspableSuperclass =
184 std::find(parents.cbegin(), parents.cend(), "graspable") != parents.cend();
185 hasGraspableSuperclass |=
186 std::find(parents.cbegin(), parents.cend(), "bothhandsgraspable") != parents.cend();
187 GraspabilityDescriptor gd{className, hasGraspableSuperclass, false, false};
188
189 ObjectClassPtr objectClass =
190 ObjectClassPtr::dynamicCast(objectClasses->getEntityByName(className));
191 memoryx::EntityWrappers::SimoxObjectWrapperPtr simoxWrapper = objectClass->addWrapper(
193 VirtualRobot::ManipulationObjectPtr mo = simoxWrapper->getManipulationObject();
194
195 gd.hasLeftGrasp = false;
196 gd.hasRightGrasp = false;
197 if (mo)
198 {
199 auto hasGrasp = [](VirtualRobot::GraspSetPtr set)
200 {
201 if (!set)
202 {
203 return false;
204 }
205
206 for (size_t i = 0; i < set->getSize(); ++i)
207 {
208 if (armarx::Contains(set->getGrasp(i)->getName(), "Grasp", true))
209 {
210 return true;
211 }
212 }
213 return false;
214 };
215 gd.hasLeftGrasp = hasGrasp(mo->getGraspSet("TCP L"));
216 gd.hasRightGrasp = hasGrasp(mo->getGraspSet("TCP R"));
217 }
218
219 graspabilityDescriptors.push_back(gd);
220 graspabilityDescriptorIt = std::prev(graspabilityDescriptors.cend());
221
222 ARMARX_INFO << "new graspability descriptor: [className: '" << className
223 << "'; child of graspable/bothhandsgraspable: " << gd.hasGraspableSuperclass
224 << "; has left grasp: " << gd.hasLeftGrasp
225 << "; has right grasp: " << gd.hasRightGrasp << "]";
226 }
227
228 const auto& gd = *graspabilityDescriptorIt;
229 ARMARX_DEBUG << "checking " << className << "/" << gd.className << " "
230 << gd.hasGraspableSuperclass << " " << gd.hasLeftGrasp << " "
231 << gd.hasRightGrasp;
232
233 if (!gd.hasGraspableSuperclass)
234 {
235 continue;
236 }
237
238 if (gd.hasLeftGrasp)
239 {
240 addPredicateIfInside(result, leftGraspable.name, object, boxesL);
241 }
242
243 if (gd.hasRightGrasp)
244 {
245 addPredicateIfInside(result, rightGraspable.name, object, boxesR);
246 }
247 }
248
249 return result;
250}
Property< PropertyType > getProperty(const std::string &name)
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)
SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject (i.e visualization,...
PredicateInfoList getPredicateInfos(const Ice::Current &=Ice::emptyCurrent) override
std::string getDefaultName() const override
Retrieve default name of component.
PredicateInstanceList calcPredicates(const Ice::Current &=Ice::emptyCurrent) override
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_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
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.
bool Contains(const ContainerType &container, const ElementType &searchElement)
Definition algorithm.h:330
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface > DebugDrawerInterfacePrx
IceInternal::Handle< SimoxObjectWrapper > SimoxObjectWrapperPtr
VirtualRobot headers.
IceInternal::Handle< ObjectInstance > ObjectInstancePtr
IceInternal::Handle< ObjectClass > ObjectClassPtr
Definition ObjectClass.h:35
std::shared_ptr< GridFileManager > GridFileManagerPtr