OnTopPredicateProvider.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 Mirko Waechter ( mirko.waechter 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
28
29
30using namespace memoryx;
31
35
36void
41
42void
44{
45 workingMemoryProxy = getProxy<WorkingMemoryInterfacePrx>("WorkingMemory");
46 objectInstancesProxy = workingMemoryProxy->getObjectInstancesSegment();
47}
48
49std::string
51{
52 return "OnTopPredicateProvider";
53}
54
55PredicateInfo
57{
58 return PredicateInfo{"ontop", 2};
59}
60
61PredicateInstanceList
63{
64 PredicateInstanceList returnPIList;
65
66 const float maxDistanceXY = 50;
67 const float minDistanceZ = 40;
68 const float maxDistanceZ = 200;
69
70 // get all currently localized objects and their positions from the working memory
71 std::map<std::string, armarx::FramedPositionPtr> objectPositions;
72 EntityIdList objectIds = objectInstancesProxy->getAllEntityIds();
73
74 for (EntityIdList::const_iterator it = objectIds.begin(); it != objectIds.end(); it++)
75 {
76 const EntityBasePtr entityBase = objectInstancesProxy->getEntityById(*it);
77 const ObjectInstancePtr object = ObjectInstancePtr::dynamicCast(entityBase);
78
79 if (object)
80 {
81 // TODO: assert that positions are in base coordinate frame
82 objectPositions[object->getName()] = object->getPosition();
83 }
84 else
85 {
86 ARMARX_WARNING << "Something went wrong when getting the object with id " << *it
87 << " from working memory";
88 }
89 }
90
91 // determine which object pairs are standing on one another
92 for (std::map<std::string, armarx::FramedPositionPtr>::iterator i = objectPositions.begin();
93 i != objectPositions.end();
94 i++)
95 {
96 Eigen::Vector3f pos1 = i->second->toEigen();
97 ARMARX_IMPORTANT << "Object " << i->first << " at position " << i->second->toEigen();
98 std::map<std::string, armarx::FramedPositionPtr>::iterator j = i;
99 j++;
100
101 for (; j != objectPositions.end(); j++)
102 {
103 Eigen::Vector3f pos2 = j->second->toEigen();
104
105 // objects have to be close to each other in the x-y-plane
106 if (sqrtf((pos1(0) - pos2(0)) * (pos1(0) - pos2(0)) +
107 (pos1(1) - pos2(1)) * (pos1(1) - pos2(1))) < maxDistanceXY)
108 {
109 // difference in z coordinate must be between min and max value
110 if (fabs(pos1(2) - pos2(2)) > minDistanceZ &&
111 fabs(pos1(2) - pos2(2)) < maxDistanceZ)
112 {
113 if (pos1(2) > pos2(2))
114 {
115 //@TODO
116 // returnPIList.push_back(PredicateInstance{"on", Ice::StringSeq{i->first, j->first}, true});
117 }
118 else
119 {
120 // returnPIList.push_back(PredicateInstance{"on", Ice::StringSeq{j->first, i->first}, true});
121 }
122 }
123 }
124 }
125 }
126
127 return returnPIList;
128}
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)
PredicateInstanceList calcPredicates(const Ice::Current &) override
std::string getDefaultName() const override
Retrieve default name of component.
PredicateInfo getPredicateInfo(const Ice::Current &c=Ice::emptyCurrent)
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
VirtualRobot headers.
IceInternal::Handle< ObjectInstance > ObjectInstancePtr