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 
31 
32 #include <MemoryX/interface/memorytypes/MemoryEntities.h>
33 
37 
38 
39 namespace memoryx
40 {
41 
44  {
46 
47  def->topic<WorkingMemoryListenerInterface>("WorkingMemory", "WorkingMemoryName");
48  def->optional(minOrientationDistance, "MinOrientationDistance", "Min Distance an object has to rotate until the memory receives an update");
49  def->optional(minPositionDistance, "MinPositrionDistance", "Min Distance an object has to move until the memory receives an update");
50  return def;
51  }
52 
54  minOrientationDistance(0.25 * M_PI),
55  minPositionDistance(100)
56  {}
57 
58 
60  {
61  return "SimpleEpisodicMemoryWorkingMemoryConnector";
62  }
63 
64 
66  {
68  usingProxy("WorkingMemory");
69  usingProxy("RobotStateComponent");
70 
71  }
72 
73 
75  {
77  memoryPrx = getProxy<WorkingMemoryInterfacePrx>("WorkingMemory");
78  }
79 
80 
82  {
83 
84  }
85 
86 
88  {
89 
90  }
91 
92  void SimpleEpisodicMemoryWorkingMemoryConnector::reportEntityCreated(const std::string& segmentName, const memoryx::EntityBasePtr& newEntity,
93  const Ice::Current&)
94  {
95  if (segmentName != "objectInstances")
96  {
97  return;
98  }
99 
100  EntityPtr entity = EntityPtr::dynamicCast(newEntity);
101 
102  //ARMARX_DEBUG << "Entity created in segment " << segmentName << ": " << entity;
103  std::map<int, memoryx::EntityBasePtr> entitiesForSegment = entityMap[segmentName];
104  entitiesForSegment[std::stoi(entity->getId())] = entity;
105 
106  EntityAttributePtr pos = EntityAttributePtr::dynamicCast(entity->getAttribute("position"));
107  armarx::VariantPtr posValue = armarx::VariantPtr::dynamicCast(pos->getValue());
108  auto framedPos = posValue->getClass<armarx::FramedPositionBase>();
109 
110  memoryx::ObjectPoseEvent o;
111  o.objectName = entity->getName();
112  o.x = framedPos->x;
113  o.y = framedPos->y;
114  o.z = framedPos->z;
115  o.frame = framedPos->frame;
116  o.type = memoryx::ObjectPoseEventType::NEW_OBJECT_RECOGNIZED;
117  o.receivedInMs = IceUtil::Time::now().toMilliSecondsDouble();
118  m_simple_episodic_memory->registerObjectPoseEvent(o);
119  }
120 
122  const memoryx::EntityBasePtr& entityOld,
123  const memoryx::EntityBasePtr& entityNew, const Ice::Current&)
124  {
125  //ARMARX_DEBUG << "Entity changed: ";
126  //ARMARX_DEBUG << "Old: " << EntityPtr::dynamicCast(entityOld);
127  //ARMARX_DEBUG << "New: " << EntityPtr::dynamicCast(entityNew);
128  if (segmentName != "objectInstances")
129  {
130  return;
131  }
132 
133  EntityPtr prevEntity = EntityPtr::dynamicCast(entityOld);
134  EntityPtr currEntity = EntityPtr::dynamicCast(entityNew);
135 
136  EntityAttributePtr prevPos = EntityAttributePtr::dynamicCast(prevEntity->getAttribute("position"));
137  armarx::VariantPtr prevPosValue = armarx::VariantPtr::dynamicCast(prevPos->getValue());
138  auto prevFramedPos = prevPosValue->getClass<armarx::FramedPositionBase>();
139 
140  EntityAttributePtr currPos = EntityAttributePtr::dynamicCast(currEntity->getAttribute("position"));
141  armarx::VariantPtr currPosValue = armarx::VariantPtr::dynamicCast(currPos->getValue());
142  auto currFramedPos = currPosValue->getClass<armarx::FramedPositionBase>();
143 
144  Eigen::Vector3f prevPosVector(prevFramedPos->x, prevFramedPos->y, prevFramedPos->z);
145  Eigen::Vector3f currPosVector(currFramedPos->x, currFramedPos->y, currFramedPos->z);
146 
147  float distance = (currPosVector - prevPosVector).norm();
148 
149  if (distance > minPositionDistance /* && rotation */)
150  {
151  // update entity
152  std::map<int, memoryx::EntityBasePtr> entitiesForSegment = entityMap[segmentName];
153  entitiesForSegment[std::stoi(currEntity->getId())] = currEntity;
154 
155  memoryx::ObjectPoseEvent o;
156  o.objectName = currEntity->getName();
157  o.x = currPosVector(0);
158  o.y = currPosVector(1);
159  o.z = currPosVector(2);
160  o.type = memoryx::ObjectPoseEventType::OBJECT_POSE_UPDATE;
161  o.receivedInMs = IceUtil::Time::now().toMilliSecondsDouble();
162  m_simple_episodic_memory->registerObjectPoseEvent(o);
163  }
164  }
165 
166  void SimpleEpisodicMemoryWorkingMemoryConnector::reportEntityRemoved(const std::string& segmentName, const memoryx::EntityBasePtr& entity,
167  const Ice::Current&)
168  {
169  ARMARX_DEBUG << "Entity removed: id = " << entity->getId();
170  }
171 
172  void SimpleEpisodicMemoryWorkingMemoryConnector::reportSnapshotLoaded(const std::string& segmentName, const Ice::Current&)
173  {
174  ARMARX_DEBUG << "Snapshot loaded!";
175  }
176 
178  {
179  ARMARX_DEBUG << "Snapshot completely loaded!";
180  }
181 
182  void SimpleEpisodicMemoryWorkingMemoryConnector::reportMemoryCleared(const std::string& segmentName, const Ice::Current&)
183  {
184  ARMARX_DEBUG << "Scene cleared!";
185  }
186 
187 }
memoryx::SimpleEpisodicMemoryWorkingMemoryConnector::SimpleEpisodicMemoryWorkingMemoryConnector
SimpleEpisodicMemoryWorkingMemoryConnector()
Definition: SimpleEpisodicMemoryWorkingMemoryConnector.cpp:53
memoryx::SimpleEpisodicMemoryWorkingMemoryConnector::reportSnapshotLoaded
void reportSnapshotLoaded(const std::string &, const Ice::Current &) override
Definition: SimpleEpisodicMemoryWorkingMemoryConnector.cpp:172
JSONObject.h
Pose.h
GridFileManager.h
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::SimpleEpisodicMemoryWorkingMemoryConnector::reportSnapshotCompletelyLoaded
void reportSnapshotCompletelyLoaded(const Ice::Current &) override
Definition: SimpleEpisodicMemoryWorkingMemoryConnector.cpp:177
Relation.h
IceInternal::Handle
Definition: forward_declarations.h:8
memoryx::SimpleEpisodicMemoryWorkingMemoryConnector::onExitComponent
void onExitComponent() override
Definition: SimpleEpisodicMemoryWorkingMemoryConnector.cpp:87
memoryx::SimpleEpisodicMemoryWorkingMemoryConnector::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: SimpleEpisodicMemoryWorkingMemoryConnector.cpp:43
memoryx::SimpleEpisodicMemoryWorkingMemoryConnector::reportEntityUpdated
void reportEntityUpdated(const std::string &, const EntityBasePtr &, const EntityBasePtr &, const Ice::Current &) override
Definition: SimpleEpisodicMemoryWorkingMemoryConnector.cpp:121
memoryx::SimpleEpisodicMemoryWorkingMemoryConnector::onInitComponent
void onInitComponent() override
Definition: SimpleEpisodicMemoryWorkingMemoryConnector.cpp:65
memoryx::SimpleEpisodicMemoryWorkingMemoryConnector::reportEntityCreated
void reportEntityCreated(const std::string &, const EntityBasePtr &, const Ice::Current &) override
Definition: SimpleEpisodicMemoryWorkingMemoryConnector.cpp:92
M_PI
#define M_PI
Definition: MathTools.h:17
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
SimpleEpisodicMemoryWorkingMemoryConnector.h
MemoryXCoreObjectFactories.h
memoryx::SimpleEpisodicMemoryWorkingMemoryConnector::getDefaultName
std::string getDefaultName() const override
Definition: SimpleEpisodicMemoryWorkingMemoryConnector.cpp:59
memoryx::SimpleEpisodicMemoryWorkingMemoryConnector::onDisconnectComponent
void onDisconnectComponent() override
Definition: SimpleEpisodicMemoryWorkingMemoryConnector.cpp:81
memoryx::SimpleEpisodicMemoryConnector::m_simple_episodic_memory
memoryx::SimpleEpisodicMemoryInterface::ProxyType m_simple_episodic_memory
Definition: SimpleEpisodicMemoryConnector.h:41
memoryx::SimpleEpisodicMemoryWorkingMemoryConnector::reportMemoryCleared
void reportMemoryCleared(const std::string &, const Ice::Current &) override
Definition: SimpleEpisodicMemoryWorkingMemoryConnector.cpp:182
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
ObjectInstance.h
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
IceUtil::Handle
Definition: forward_declarations.h:29
MemoryXTypesObjectFactories.h
AgentInstance.h
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:88
memoryx::SimpleEpisodicMemoryConnector::m_simple_episodic_memory_proxy_name
std::string m_simple_episodic_memory_proxy_name
Definition: SimpleEpisodicMemoryConnector.h:40
armarx::ManagedIceObject::getProxy
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
Definition: ManagedIceObject.cpp:393
memoryx::SimpleEpisodicMemoryWorkingMemoryConnector::reportEntityRemoved
void reportEntityRemoved(const std::string &, const EntityBasePtr &, const Ice::Current &) override
Definition: SimpleEpisodicMemoryWorkingMemoryConnector.cpp:166
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::SimpleEpisodicMemoryWorkingMemoryConnector::onConnectComponent
void onConnectComponent() override
Definition: SimpleEpisodicMemoryWorkingMemoryConnector.cpp:74
norm
double norm(const Point &a)
Definition: point.hpp:94