ObjectAtPredicateProvider.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 2014
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
24 
29 
30 using namespace memoryx;
31 
33 {
34 }
35 
36 
38 {
39  usingProxy("WorkingMemory");
40  usingProxy("PriorKnowledge");
41  usingProxy("GraphNodePoseResolver");
42 
43  distanceThreshold = getProperty<float>("DistanceThreshold").getValue();
44  handDistanceThreshold = getProperty<float>("HandDistanceThreshold").getValue();
45  sceneName = getProperty<std::string>("PlatformGraphSceneName").getValue();
46 }
47 
49 {
50  wm = getProxy<WorkingMemoryInterfacePrx>("WorkingMemory");
51  prior = getProxy<PriorKnowledgeInterfacePrx>("PriorKnowledge");
52  psr = getProxy<GraphNodePoseResolverInterfacePrx>("GraphNodePoseResolver");
53 
54  agentInstances = wm->getAgentInstancesSegment();
55  objectInstances = wm->getObjectInstancesSegment();
56  objectClasses = prior->getObjectClassesSegment();
57  graphs = prior->getGraphSegment();
58 
59  graphNodes = graphs->getNodesByScene(sceneName);
60 
61  for (const auto& classEntity : objectClasses->getAllEntities())
62  {
63  classNameParentsMap.insert({classEntity->getName(), objectClasses->getObjectClassByNameWithAllParents(classEntity->getName())->getParentClasses()});
64  }
65 }
66 
68 {
69  return "ObjectAtPredicateProvider";
70 }
71 
72 PredicateInfoList ObjectAtPredicateProvider::getPredicateInfos(const Ice::Current&)
73 {
74  return {PredicateInfo{"objectAt", 2}};
75 }
76 
77 PredicateInstanceList ObjectAtPredicateProvider::calcPredicates(const Ice::Current&)
78 {
79  PredicateInstanceList result;
80  const std::string predicateName = getPredicateInfos().at(0).name;
81 
82  // float minx = -700;
83  // float maxx = 700;
84  // float miny = 400;
85  // float maxy = 1200;
86 
88 
89  std::vector<std::pair<ObjectInstancePtr, Eigen::Matrix4f>> objects;
90  std::vector<std::pair<ObjectInstancePtr, Eigen::Matrix4f>> hands;
91 
92  for (const auto& objectEntity : objectInstances->getAllEntities())
93  {
94  auto objectInstance = ObjectInstancePtr::dynamicCast(objectEntity);
95  ARMARX_CHECK_EXPRESSION(objectInstance);
96  auto classes = classNameParentsMap[objectInstance->getMostProbableClass()];
97 
98  armarx::FramedPosePtr objectPose = objectInstance->getPose();
99  ARMARX_CHECK_EXPRESSION(objectPose);
100  if (objectPose->frame != armarx::GlobalFrame && !objectPose->frame.empty())
101  {
102  if (!robot || robot->getName() != objectPose->agent)
103  {
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();
107  }
108  if (robot)
109  {
110  objectPose->changeToGlobal(robot);
111  }
112  else
113  {
114  ARMARX_ERROR << "Could not change " << objectPose << " to global";
115  }
116  }
117  if (std::find(classes.cbegin(), classes.cend(), "hand") != classes.cend())
118  {
119  hands.push_back({objectInstance, objectPose->toEigen()});
120  }
121  else
122  {
123  objects.push_back({objectInstance, objectPose->toEigen()});
124  }
125  }
126 
127  // auto objClassSeg = prior->getObjectClassesSegment();
128  for (const auto& objectEntry : objects)
129  {
130  const auto& object = objectEntry.first;
131  ARMARX_CHECK_EXPRESSION(object);
132  const auto& objectPosEigen = objectEntry.second;
133  bool tooCloseToHand = false;
134 
135  for (const auto& handEntry : hands)
136  {
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)
142  {
143  ARMARX_INFO << "object '" << object->getName() << "' is too close to hand '" << hand->getName() << "'; will not check objectAt conditions";
144  tooCloseToHand = true;
145  break;
146  }
147  }
148 
149  if (tooCloseToHand)
150  {
151  continue;
152  }
153  ARMARX_CHECK_EXPRESSION(object);
154  ARMARX_DEBUG << object->getName() << objectPosEigen;
155  std::string mostProbableClass = object->getMostProbableClass();
156  memoryx::EntityRefBasePtr closestNodeRef;
157  float minDist = std::numeric_limits<float>::max();
158 
159  //@TODO just call gnpr to get closest node?
160  for (const auto& node : graphNodes)
161  {
162  const auto& nodeInfo = getCacheEntry(node->getId());
163  if (nodeInfo.node->isMetaEntity())
164  {
165  continue;
166  }
167 
168  const auto& nodePose = nodeInfo.globalPose;
169 
170  if (nodeInfo.originalFrame == mostProbableClass)
171  {
172  continue;
173  }
174  ARMARX_CHECK_EXPRESSION(nodeInfo.node);
175  ARMARX_CHECK_EXPRESSION(nodePose);
176 
177  ARMARX_DEBUG << nodeInfo.node->getName() << "\n" << nodePose->toEigen();
178  // if(!robot || robot->getName() != nodePos->agent)
179  // nodePos->changeFrame();
180  // Eigen::Matrix4f objPosRelativeToNode = nodePose->toEigen().inverse() * objectPosEigen;
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);
183  // this allows objects to be at robot locations
184  // if (
185  // ((objPosRelativeToNode(0, 3) >= minx && objPosRelativeToNode(0, 3) <= maxx
186  // && objPosRelativeToNode(1, 3) >= miny && objPosRelativeToNode(1, 3) <= maxy)
187  // || std::find(nodeInfo.parents.cbegin(), nodeInfo.parents.cend(), "robotlocation") == nodeInfo.parents.cend()
188  // )
189  // && nodeDist < minDist)
190  // {
191  // minDist = nodeDist;
192  // closestNodeRef = nodeInfo.nodeRef;
193  // }
194 
195  //this doesn't allow objects to be at robot locations
196  if (std::find(nodeInfo.parents.cbegin(), nodeInfo.parents.cend(), "robotlocation") == nodeInfo.parents.cend()
197  && nodeDist < minDist)
198  {
199  minDist = nodeDist;
200  closestNodeRef = nodeInfo.nodeRef;
201  }
202  }
203 
204  if (closestNodeRef)
205  {
206  ARMARX_INFO << "closest node to '" << object->getName() << "': '" << closestNodeRef->entityName << "' with dist: " << minDist;
207  }
208 
209  if (closestNodeRef && minDist <= distanceThreshold)
210  {
211  EntityRefBasePtr objectRef = objectInstances->getEntityRefById(object->getId());
212  result.push_back(PredicateInstance {predicateName, {objectRef, closestNodeRef}, true});
213  }
214  }
215 
216  return result;
217 }
218 
219 ObjectAtPredicateProvider::CachedNodeInfo ObjectAtPredicateProvider::getCacheEntry(const std::string& nodeId)
220 {
221  auto it = nodeInfoMap.find(nodeId);
222  if (it != nodeInfoMap.end())
223  {
224  return it->second;
225  }
226 
227  GraphNodePtr n = GraphNodePtr::dynamicCast(graphs->getEntityById(nodeId));
228 
229  armarx::FramedPosePtr globalPose = armarx::FramedPosePtr::dynamicCast(psr->resolveToGlobalPose(n));
230  if (!globalPose)
231  {
232  ARMARX_WARNING << "Could not resolve the following to a global pose:\n" << armarx::FramedPosePtr::dynamicCast(n->getPose())->toEigen() << "\n for node '" << n->getName() << "'";
233  }
234 
235  CachedNodeInfo newCacheEntry {n, graphs->getEntityRefById(nodeId), n->getPose()->frame, n->getAllParentsAsStringList(), globalPose};
236  nodeInfoMap.insert({n->getId(), newCacheEntry});
237 
238  return newCacheEntry;
239 }
240 
241 
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
memoryx::ObjectAtPredicateProvider::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: ObjectAtPredicateProvider.cpp:67
IceInternal::Handle< FramedPose >
FramedPose.h
memoryx::ObjectAtPredicateProvider::calcPredicates
PredicateInstanceList calcPredicates(const Ice::Current &=Ice::emptyCurrent) override
Definition: ObjectAtPredicateProvider.cpp:77
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
memoryx::ObjectAtPredicateProvider::getPredicateInfos
PredicateInfoList getPredicateInfos(const Ice::Current &=Ice::emptyCurrent) override
Definition: ObjectAtPredicateProvider.cpp:72
MemoryXCoreObjectFactories.h
memoryx::ObjectAtPredicateProvider::onConnectWorldStateUpdater
void onConnectWorldStateUpdater() override
Definition: ObjectAtPredicateProvider.cpp:48
max
T max(T t1, T t2)
Definition: gdiam.h:48
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
memoryx::ObjectAtPredicateProvider::ObjectAtPredicateProvider
ObjectAtPredicateProvider()
Definition: ObjectAtPredicateProvider.cpp:32
ObjectInstance.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
IceInternal::ProxyHandle< ::IceProxy::armarx::SharedRobotInterface >
MemoryXTypesObjectFactories.h
ObjectAtPredicateProvider.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:151
memoryx::ObjectAtPredicateProvider::onInitWorldStateUpdater
void onInitWorldStateUpdater() override
Definition: ObjectAtPredicateProvider.cpp:37
norm
double norm(const Point &a)
Definition: point.hpp:94