35 graspedPredicate(PredicateInfo {
"grasped", 3}),
36 graspedBothPredicate(PredicateInfo {
"graspedBoth", 2}),
37 handEmptyPredicate(PredicateInfo {
"handEmpty", 2})
50 wm = getProxy<WorkingMemoryInterfacePrx>(
"WorkingMemory");
51 prior = getProxy<PriorKnowledgeInterfacePrx>(
"PriorKnowledge");
52 distanceThreshold = getProperty<float>(
"DistanceThreshold").getValue();
54 agentInstances = wm->getAgentInstancesSegment();
55 objectInstances = wm->getObjectInstancesSegment();
56 objectClasses = prior->getObjectClassesSegment();
61 return "HandPredicateProvider";
66 return {graspedPredicate, graspedBothPredicate, handEmptyPredicate};
71 PredicateInstanceList result;
75 auto agents = agentInstances->getAllAgentInstances();
82 for (
const auto& agent : agents)
84 ARMARX_INFO <<
"Checking agent: " << agent->getName();
90 auto robot = agent->getSharedRobot();
91 EntityRefBasePtr agentRef = agentInstances->getEntityRefById(agent->getId());
93 std::vector<ObjectInstancePtr> hands;
94 std::vector<ObjectInfo> graspableObjects;
96 for (
const auto& entity : objectInstances->getAllEntities())
99 auto classes = objectClasses->getObjectClassByNameWithAllParents(object->getMostProbableClass())->getParentClasses();
101 const bool isGraspable = std::find(classes.cbegin(), classes.cend(),
"graspable") != classes.cend();
102 const bool isBothHandGraspable = std::find(classes.cbegin(), classes.cend(),
"bothhandsgraspable") != classes.cend();
106 graspableObjects.push_back(ObjectInfo {object, ObjectInfo::ObjectType::OneHandGraspable});
108 else if (isBothHandGraspable)
110 graspableObjects.push_back(ObjectInfo {object, ObjectInfo::ObjectType::TwoHandGraspable});
112 else if (object->getName() ==
"handleft3a" || object->getName() ==
"handright3a")
114 hands.push_back(
object);
118 std::vector<std::pair<EntityRefBasePtr, ObjectInfo>> graspCandidates;
119 std::vector<std::pair<EntityRefBasePtr, ObjectInfo>> bothHandsGraspCandidates;
121 for (
const auto& hand : hands)
123 EntityRefBasePtr handRef = objectInstances->getEntityRefById(hand->getId());
124 const auto handPos = hand->getPosition()->toGlobal(robot)->toEigen();
128 for (
const auto&
object : graspableObjects)
136 objPos = objPos->toGlobal(robot);
139 const float dist = (objPos->toEigen() - handPos).
norm();
142 if (dist < minDistance)
149 if (bestObject.objectInstance)
151 ARMARX_INFO <<
"Closest object for grasped: '" << bestObject.objectInstance->getName() <<
"' in hand '" << hand->getName() <<
"' with distance: " << minDistance;
154 if (minDistance < distanceThreshold)
156 ARMARX_INFO <<
"grasp candidate: hand '" << handRef->entityName <<
"' and object '" << bestObject.objectInstance->getName() <<
"' with dist " << minDistance;
157 graspCandidates.push_back({handRef, bestObject});
159 if (minDistance < distanceThreshold * 1.5)
161 ARMARX_INFO <<
"grasp candidate for both hand graspable: hand '" << handRef->entityName <<
"' and object '" << bestObject.objectInstance->getName() <<
"' with dist " << minDistance;
162 bothHandsGraspCandidates.push_back({handRef, bestObject});
167 std::map<std::string, bool> handOccupiedMap;
168 for (
const auto& hand : hands)
170 handOccupiedMap[hand->getId()] =
false;
173 for (
auto firstIt = graspCandidates.cbegin(); firstIt != graspCandidates.cend(); ++firstIt)
175 const auto& candidate = *firstIt;
176 const auto hand = candidate.first;
177 const auto objectId = candidate.second.objectInstance->getId();
195 if (candidate.second.type != ObjectInfo::ObjectType::TwoHandGraspable)
197 result.push_back(PredicateInstance {graspedPredicate.name, {agentRef, hand, objectInstances->getEntityRefById(objectId)},
true});
198 handOccupiedMap[hand->entityId] =
true;
202 for (
auto firstIt = bothHandsGraspCandidates.cbegin(); firstIt != bothHandsGraspCandidates.cend(); ++firstIt)
204 const auto& candidate = *firstIt;
205 const auto hand = candidate.first;
206 const auto objectId = candidate.second.objectInstance->getId();
208 if (candidate.second.type == ObjectInfo::ObjectType::TwoHandGraspable)
210 for (
auto secondIt = std::next(firstIt); secondIt != bothHandsGraspCandidates.cend(); ++secondIt)
212 const auto handOther = secondIt->first;
213 const auto objectIdOther = secondIt->second.objectInstance->getId();
214 if (hand->entityId != handOther->entityId && objectId == objectIdOther)
216 result.push_back(PredicateInstance {graspedBothPredicate.name, {agentRef, objectInstances->getEntityRefById(objectId)},
true});
217 handOccupiedMap[hand->entityId] =
true;
218 handOccupiedMap[handOther->entityId] =
true;
225 for (
const auto& hand : handOccupiedMap)
229 result.push_back(PredicateInstance {handEmptyPredicate.name, {agentRef, objectInstances->getEntityRefById(hand.first)},
true});