43 distanceThreshold = getProperty<float>(
"DistanceThreshold").getValue();
44 handDistanceThreshold = getProperty<float>(
"HandDistanceThreshold").getValue();
45 sceneName = getProperty<std::string>(
"PlatformGraphSceneName").getValue();
50 wm = getProxy<WorkingMemoryInterfacePrx>(
"WorkingMemory");
51 prior = getProxy<PriorKnowledgeInterfacePrx>(
"PriorKnowledge");
52 psr = getProxy<GraphNodePoseResolverInterfacePrx>(
"GraphNodePoseResolver");
54 agentInstances = wm->getAgentInstancesSegment();
55 objectInstances = wm->getObjectInstancesSegment();
56 objectClasses = prior->getObjectClassesSegment();
57 graphs = prior->getGraphSegment();
59 graphNodes = graphs->getNodesByScene(sceneName);
61 for (
const auto& classEntity : objectClasses->getAllEntities())
63 classNameParentsMap.insert({classEntity->getName(), objectClasses->getObjectClassByNameWithAllParents(classEntity->getName())->getParentClasses()});
69 return "ObjectAtPredicateProvider";
74 return {PredicateInfo{
"objectAt", 2}};
79 PredicateInstanceList result;
89 std::vector<std::pair<ObjectInstancePtr, Eigen::Matrix4f>> objects;
90 std::vector<std::pair<ObjectInstancePtr, Eigen::Matrix4f>> hands;
92 for (
const auto& objectEntity : objectInstances->getAllEntities())
94 auto objectInstance = ObjectInstancePtr::dynamicCast(objectEntity);
96 auto classes = classNameParentsMap[objectInstance->getMostProbableClass()];
102 if (!robot || robot->getName() != objectPose->agent)
104 auto agentInstance = agentInstances->getAgentInstanceByName(objectPose->agent);
105 ARMARX_CHECK_EXPRESSION(agentInstance) <<
"no agent with name '" << objectPose->agent <<
"' present while trying to change pose of " << objectInstance->getName();
106 robot = agentInstance->getSharedRobot();
110 objectPose->changeToGlobal(robot);
114 ARMARX_ERROR <<
"Could not change " << objectPose <<
" to global";
117 if (std::find(classes.cbegin(), classes.cend(),
"hand") != classes.cend())
119 hands.push_back({objectInstance, objectPose->toEigen()});
123 objects.push_back({objectInstance, objectPose->toEigen()});
128 for (
const auto& objectEntry : objects)
130 const auto&
object = objectEntry.first;
132 const auto& objectPosEigen = objectEntry.second;
133 bool tooCloseToHand =
false;
135 for (
const auto& handEntry : hands)
137 const auto& hand = handEntry.first;
138 const auto& handPosEigen = handEntry.second;
139 const float handDist = (handPosEigen.block<2, 1>(0, 3) - objectPosEigen.block<2, 1>(0, 3)).
norm();
140 ARMARX_INFO <<
"dist from object '" <<
object->getName() <<
"' to hand '" << hand->getName() <<
"': " << handDist;
141 if (handDist < handDistanceThreshold)
143 ARMARX_INFO <<
"object '" <<
object->getName() <<
"' is too close to hand '" << hand->getName() <<
"'; will not check objectAt conditions";
144 tooCloseToHand =
true;
155 std::string mostProbableClass =
object->getMostProbableClass();
156 memoryx::EntityRefBasePtr closestNodeRef;
160 for (
const auto& node : graphNodes)
162 const auto& nodeInfo = getCacheEntry(node->getId());
163 if (nodeInfo.node->isMetaEntity())
168 const auto& nodePose = nodeInfo.globalPose;
170 if (nodeInfo.originalFrame == mostProbableClass)
177 ARMARX_DEBUG << nodeInfo.node->getName() <<
"\n" << nodePose->toEigen();
181 const float nodeDist = (nodePose->toEigen().block<2, 1>(0, 3) - objectPosEigen.block<2, 1>(0, 3)).
norm();
182 ARMARX_DEBUG <<
object->getName() <<
": " << nodeInfo.node->getName() <<
" " <<
VAROUT(nodeDist);
196 if (std::find(nodeInfo.parents.cbegin(), nodeInfo.parents.cend(),
"robotlocation") == nodeInfo.parents.cend()
197 && nodeDist < minDist)
200 closestNodeRef = nodeInfo.nodeRef;
206 ARMARX_INFO <<
"closest node to '" <<
object->getName() <<
"': '" << closestNodeRef->entityName <<
"' with dist: " << minDist;
209 if (closestNodeRef && minDist <= distanceThreshold)
211 EntityRefBasePtr objectRef = objectInstances->getEntityRefById(object->getId());
212 result.push_back(PredicateInstance {predicateName, {objectRef, closestNodeRef},
true});
219 ObjectAtPredicateProvider::CachedNodeInfo ObjectAtPredicateProvider::getCacheEntry(
const std::string& nodeId)
221 auto it = nodeInfoMap.find(nodeId);
222 if (it != nodeInfoMap.end())
227 GraphNodePtr n = GraphNodePtr::dynamicCast(graphs->getEntityById(nodeId));
232 ARMARX_WARNING <<
"Could not resolve the following to a global pose:\n" << armarx::FramedPosePtr::dynamicCast(n->getPose())->toEigen() <<
"\n for node '" << n->getName() <<
"'";
235 CachedNodeInfo newCacheEntry {n, graphs->getEntityRefById(nodeId), n->getPose()->frame, n->getAllParentsAsStringList(), globalPose};
236 nodeInfoMap.insert({n->getId(), newCacheEntry});
238 return newCacheEntry;