SimpleEpisodicMemoryWorkingMemoryConnector.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 MemoryX::ArmarXObjects::SimpleEpisodicMemoryWorkingMemoryConnector
17 * @author fabian.peller-konrad@kit.edu ( fabian dot peller-konrad at kit dot edu )
18 * @date 2020
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
24
26
28
31#include <MemoryX/interface/memorytypes/MemoryEntities.h>
36
37namespace memoryx
38{
39
42 {
45
46 def->topic<WorkingMemoryListenerInterface>("WorkingMemory", "WorkingMemoryName");
47 def->optional(minOrientationDistance,
48 "MinOrientationDistance",
49 "Min Distance an object has to rotate until the memory receives an update");
50 def->optional(minPositionDistance,
51 "MinPositrionDistance",
52 "Min Distance an object has to move until the memory receives an update");
53 return def;
54 }
55
57 minOrientationDistance(0.25 * M_PI), minPositionDistance(100)
58 {
59 }
60
61 std::string
63 {
64 return "SimpleEpisodicMemoryWorkingMemoryConnector";
65 }
66
67 void
74
75 void
81
82 void
86
87 void
91
92 void
94 const std::string& segmentName,
95 const memoryx::EntityBasePtr& newEntity,
96 const Ice::Current&)
97 {
98 if (segmentName != "objectInstances")
99 {
100 return;
101 }
102
103 EntityPtr entity = EntityPtr::dynamicCast(newEntity);
104
105 //ARMARX_DEBUG << "Entity created in segment " << segmentName << ": " << entity;
106 std::map<int, memoryx::EntityBasePtr> entitiesForSegment = entityMap[segmentName];
107 entitiesForSegment[std::stoi(entity->getId())] = entity;
108
109 EntityAttributePtr pos = EntityAttributePtr::dynamicCast(entity->getAttribute("position"));
110 armarx::VariantPtr posValue = armarx::VariantPtr::dynamicCast(pos->getValue());
111 auto framedPos = posValue->getClass<armarx::FramedPositionBase>();
112
113 memoryx::ObjectPoseEvent o;
114 o.objectName = entity->getName();
115 o.x = framedPos->x;
116 o.y = framedPos->y;
117 o.z = framedPos->z;
118 o.frame = framedPos->frame;
119 o.type = memoryx::ObjectPoseEventType::NEW_OBJECT_RECOGNIZED;
120 o.receivedInMs = IceUtil::Time::now().toMilliSecondsDouble();
121 m_simple_episodic_memory->registerObjectPoseEvent(o);
122 }
123
124 void
126 const std::string& segmentName,
127 const memoryx::EntityBasePtr& entityOld,
128 const memoryx::EntityBasePtr& entityNew,
129 const Ice::Current&)
130 {
131 //ARMARX_DEBUG << "Entity changed: ";
132 //ARMARX_DEBUG << "Old: " << EntityPtr::dynamicCast(entityOld);
133 //ARMARX_DEBUG << "New: " << EntityPtr::dynamicCast(entityNew);
134 if (segmentName != "objectInstances")
135 {
136 return;
137 }
138
139 EntityPtr prevEntity = EntityPtr::dynamicCast(entityOld);
140 EntityPtr currEntity = EntityPtr::dynamicCast(entityNew);
141
142 EntityAttributePtr prevPos =
143 EntityAttributePtr::dynamicCast(prevEntity->getAttribute("position"));
144 armarx::VariantPtr prevPosValue = armarx::VariantPtr::dynamicCast(prevPos->getValue());
145 auto prevFramedPos = prevPosValue->getClass<armarx::FramedPositionBase>();
146
147 EntityAttributePtr currPos =
148 EntityAttributePtr::dynamicCast(currEntity->getAttribute("position"));
149 armarx::VariantPtr currPosValue = armarx::VariantPtr::dynamicCast(currPos->getValue());
150 auto currFramedPos = currPosValue->getClass<armarx::FramedPositionBase>();
151
152 Eigen::Vector3f prevPosVector(prevFramedPos->x, prevFramedPos->y, prevFramedPos->z);
153 Eigen::Vector3f currPosVector(currFramedPos->x, currFramedPos->y, currFramedPos->z);
154
155 float distance = (currPosVector - prevPosVector).norm();
156
157 if (distance > minPositionDistance /* && rotation */)
158 {
159 // update entity
160 std::map<int, memoryx::EntityBasePtr> entitiesForSegment = entityMap[segmentName];
161 entitiesForSegment[std::stoi(currEntity->getId())] = currEntity;
162
163 memoryx::ObjectPoseEvent o;
164 o.objectName = currEntity->getName();
165 o.x = currPosVector(0);
166 o.y = currPosVector(1);
167 o.z = currPosVector(2);
168 o.type = memoryx::ObjectPoseEventType::OBJECT_POSE_UPDATE;
169 o.receivedInMs = IceUtil::Time::now().toMilliSecondsDouble();
170 m_simple_episodic_memory->registerObjectPoseEvent(o);
171 }
172 }
173
174 void
176 const std::string& segmentName,
177 const memoryx::EntityBasePtr& entity,
178 const Ice::Current&)
179 {
180 ARMARX_DEBUG << "Entity removed: id = " << entity->getId();
181 }
182
183 void
185 const Ice::Current&)
186 {
187 ARMARX_DEBUG << "Snapshot loaded!";
188 }
189
190 void
192 const Ice::Current& c)
193 {
194 ARMARX_DEBUG << "Snapshot completely loaded!";
195 }
196
197 void
199 const Ice::Current&)
200 {
201 ARMARX_DEBUG << "Scene cleared!";
202 }
203
204} // namespace memoryx
#define M_PI
Definition MathTools.h:17
constexpr T c
Default component property definition container.
Definition Component.h:70
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
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)
memoryx::SimpleEpisodicMemoryInterface::ProxyType m_simple_episodic_memory
void reportEntityUpdated(const std::string &, const EntityBasePtr &, const EntityBasePtr &, const Ice::Current &) override
void reportEntityRemoved(const std::string &, const EntityBasePtr &, const Ice::Current &) override
void reportSnapshotLoaded(const std::string &, const Ice::Current &) override
void reportEntityCreated(const std::string &, const EntityBasePtr &, const Ice::Current &) override
void reportMemoryCleared(const std::string &, const Ice::Current &) override
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
IceInternal::Handle< Variant > VariantPtr
Definition Variant.h:41
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
VirtualRobot headers.
IceInternal::Handle< EntityAttribute > EntityAttributePtr
Typedef of EntityAttributePtr as IceInternal::Handle<EntityAttribute> for convenience.
IceInternal::Handle< Entity > EntityPtr
Typedef of EntityPtr as IceInternal::Handle<Entity> for convenience.
Definition Entity.h:45
double norm(const Point &a)
Definition point.hpp:102
double distance(const Point &a, const Point &b)
Definition point.hpp:95