35 graspedPredicate(PredicateInfo{
"grasped", 3}),
36 graspedBothPredicate(PredicateInfo{
"graspedBoth", 2}),
37 handEmptyPredicate(PredicateInfo{
"handEmpty", 2})
51 wm = getProxy<WorkingMemoryInterfacePrx>(
"WorkingMemory");
52 prior = getProxy<PriorKnowledgeInterfacePrx>(
"PriorKnowledge");
53 distanceThreshold = getProperty<float>(
"DistanceThreshold").getValue();
55 agentInstances = wm->getAgentInstancesSegment();
56 objectInstances = wm->getObjectInstancesSegment();
57 objectClasses = prior->getObjectClassesSegment();
63 return "HandPredicateProvider";
69 return {graspedPredicate, graspedBothPredicate, handEmptyPredicate};
75 PredicateInstanceList result;
79 auto agents = agentInstances->getAllAgentInstances();
86 for (
const auto& agent : agents)
88 ARMARX_INFO <<
"Checking agent: " << agent->getName();
94 auto robot = agent->getSharedRobot();
95 EntityRefBasePtr agentRef = agentInstances->getEntityRefById(agent->getId());
97 std::vector<ObjectInstancePtr> hands;
98 std::vector<ObjectInfo> graspableObjects;
100 for (
const auto& entity : objectInstances->getAllEntities())
105 ->getObjectClassByNameWithAllParents(object->getMostProbableClass())
106 ->getParentClasses();
108 const bool isGraspable =
109 std::find(classes.cbegin(), classes.cend(),
"graspable") != classes.cend();
110 const bool isBothHandGraspable =
111 std::find(classes.cbegin(), classes.cend(),
"bothhandsgraspable") !=
116 graspableObjects.push_back(
117 ObjectInfo{object, ObjectInfo::ObjectType::OneHandGraspable});
119 else if (isBothHandGraspable)
121 graspableObjects.push_back(
122 ObjectInfo{object, ObjectInfo::ObjectType::TwoHandGraspable});
124 else if (object->getName() ==
"handleft3a" || object->getName() ==
"handright3a")
126 hands.push_back(
object);
130 std::vector<std::pair<EntityRefBasePtr, ObjectInfo>> graspCandidates;
131 std::vector<std::pair<EntityRefBasePtr, ObjectInfo>> bothHandsGraspCandidates;
133 for (
const auto& hand : hands)
135 EntityRefBasePtr handRef = objectInstances->getEntityRefById(hand->getId());
136 const auto handPos = hand->getPosition()->toGlobal(robot)->toEigen();
140 for (
const auto&
object : graspableObjects)
148 objPos = objPos->toGlobal(robot);
151 const float dist = (objPos->toEigen() - handPos).
norm();
154 if (dist < minDistance)
161 if (bestObject.objectInstance)
164 << bestObject.objectInstance->getName() <<
"' in hand '"
165 << hand->getName() <<
"' with distance: " << minDistance;
168 if (minDistance < distanceThreshold)
170 ARMARX_INFO <<
"grasp candidate: hand '" << handRef->entityName
171 <<
"' and object '" << bestObject.objectInstance->getName()
172 <<
"' with dist " << minDistance;
173 graspCandidates.push_back({handRef, bestObject});
175 if (minDistance < distanceThreshold * 1.5)
177 ARMARX_INFO <<
"grasp candidate for both hand graspable: hand '"
178 << handRef->entityName <<
"' and object '"
179 << bestObject.objectInstance->getName() <<
"' with dist "
181 bothHandsGraspCandidates.push_back({handRef, bestObject});
185 std::map<std::string, bool> handOccupiedMap;
186 for (
const auto& hand : hands)
188 handOccupiedMap[hand->getId()] =
false;
191 for (
auto firstIt = graspCandidates.cbegin(); firstIt != graspCandidates.cend();
194 const auto& candidate = *firstIt;
195 const auto hand = candidate.first;
196 const auto objectId = candidate.second.objectInstance->getId();
214 if (candidate.second.type != ObjectInfo::ObjectType::TwoHandGraspable)
216 result.push_back(PredicateInstance{
217 graspedPredicate.name,
218 {agentRef, hand, objectInstances->getEntityRefById(objectId)},
220 handOccupiedMap[hand->entityId] =
true;
224 for (
auto firstIt = bothHandsGraspCandidates.cbegin();
225 firstIt != bothHandsGraspCandidates.cend();
228 const auto& candidate = *firstIt;
229 const auto hand = candidate.first;
230 const auto objectId = candidate.second.objectInstance->getId();
232 if (candidate.second.type == ObjectInfo::ObjectType::TwoHandGraspable)
234 for (
auto secondIt = std::next(firstIt);
235 secondIt != bothHandsGraspCandidates.cend();
238 const auto handOther = secondIt->first;
239 const auto objectIdOther = secondIt->second.objectInstance->getId();
240 if (hand->entityId != handOther->entityId && objectId == objectIdOther)
242 result.push_back(PredicateInstance{
243 graspedBothPredicate.name,
244 {agentRef, objectInstances->getEntityRefById(objectId)},
246 handOccupiedMap[hand->entityId] =
true;
247 handOccupiedMap[handOther->entityId] =
true;
254 for (
const auto& hand : handOccupiedMap)
259 PredicateInstance{handEmptyPredicate.name,
260 {agentRef, objectInstances->getEntityRefById(hand.first)},