AgentInstancesSegment.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::WorkingMemory
17 * @author Thomas von der Heyde (tvh242 at hotmail dot com)
18 * @date 2014
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "AgentInstancesSegment.h"
24 
25 namespace memoryx
26 {
29  AgentInstancesSegmentBase::AgentInstancesSegmentBase(),
30  ic(ic)
31  {}
32 
33  AgentInstanceBaseList AgentInstancesSegment::getAllAgentInstances(const ::Ice::Current&) const
34  {
35  AgentInstanceBaseList result;
36  auto allEntities = getAllEntities();
37  result.reserve(allEntities.size());
38 
39  for (auto&& entity : allEntities)
40  {
41  AgentInstanceBasePtr agentEntity = AgentInstanceBasePtr::dynamicCast(std::move(entity));
42 
43  if (!agentEntity)
44  {
45  ARMARX_WARNING_S << "Entity with id " << entity->getId() << " is not of type AgentInstance!";
46  continue;
47  }
48 
49  result.push_back(std::move(agentEntity));
50  }
51 
52  return result;
53  }
54 
55  AgentInstanceBasePtr AgentInstancesSegment::getAgentInstanceById(const std::string& id, const::Ice::Current&) const
56  {
57  auto entity = getEntityById(id);
58  if (!entity)
59  {
60  return NULL;
61  }
62  AgentInstanceBasePtr res = AgentInstanceBasePtr::dynamicCast(entity);
63 
64  if (!res)
65  {
66  ARMARX_WARNING_S << "Entity with id " << id << " is not of type AgentInstance!";
67  }
68 
69  return res;
70  }
71 
72  AgentInstanceBasePtr AgentInstancesSegment::getAgentInstanceByName(const std::string& name, const::Ice::Current&) const
73  {
74  auto entity = getEntityByName(name);
75  if (!entity)
76  {
77  return NULL;
78  }
79  AgentInstanceBasePtr res = AgentInstanceBasePtr::dynamicCast(entity);
80 
81  if (!res)
82  {
83  ARMARX_WARNING_S << "Entity with name '" << name << "' is not of type AgentEntity!";
84  }
85 
86  return res;
87  }
88 
89  armarx::FramedPoseBasePtr AgentInstancesSegment::convertToWorldPose(const std::string& agentName, const armarx::FramedPoseBasePtr& localPose, const Ice::Current& c) const
90  {
91  if (localPose->frame == armarx::GlobalFrame)
92  {
93  return localPose;
94  }
95 
96  auto agent = getAgentInstanceByName(agentName);
97 
98  if (!agent)
99  {
100  ARMARX_ERROR_S << "no agent with name " << agentName;
101  return NULL;
102  }
103 
104  armarx::FramedPositionPtr agentPosition = armarx::FramedPositionPtr::dynamicCast(agent->getPositionBase());
105  armarx::FramedOrientationPtr agentOrientation = armarx::FramedOrientationPtr::dynamicCast(agent->getOrientationBase());
106  armarx::PosePtr agentPose = new armarx::Pose(agentOrientation->toEigen(), agentPosition->toEigen());
107  armarx::FramedPosePtr localPoseCast = armarx::FramedPosePtr::dynamicCast(localPose);
108 
109  // auto basePrx = ic->getCommunicator()->stringToProxy(agent->getStringifiedSharedRobotInterfaceProxy());
110  // SharedRobotInterfacePrx robotPrx = SharedRobotInterfacePrx::checkedCast(basePrx);
111  auto robotPrx = agent->getSharedRobot();
112 
113  if (!robotPrx)
114  {
115  ARMARX_ERROR_S << "Could not create proxy to robot state - proxy string was : " << agent->getStringifiedSharedRobotInterfaceProxy() << " for agent " << agentName;
116  return NULL;
117  }
118 
119  localPoseCast->changeFrame(robotPrx, robotPrx->getRootNode()->getName());
120  Eigen::Matrix4f objectPoseGlobal = agentPose->toEigen() * localPoseCast->toEigen();
121 
122  armarx::FramedPosePtr p(new armarx::FramedPose(objectPoseGlobal, armarx::GlobalFrame, ""));
123  return p;
124  }
125 
126  armarx::FramedPoseBasePtr AgentInstancesSegment::convertToLocalPose(const std::string& agentName, const armarx::PoseBasePtr& worldPose, const std::string& targetFrame, const Ice::Current& c) const
127  {
128  auto agent = getAgentInstanceByName(agentName);
129 
130  if (!agent)
131  {
132  return NULL;
133  }
134 
135  armarx::FramedPositionPtr agentPosition = armarx::FramedPositionPtr::dynamicCast(agent->getPositionBase());
136  armarx::FramedOrientationPtr agentOrientation = armarx::FramedOrientationPtr::dynamicCast(agent->getOrientationBase());
137  armarx::PosePtr agentPose = new armarx::Pose(agentOrientation->toEigen(), agentPosition->toEigen());
138  armarx::FramedPosePtr worldPoseCast = armarx::FramedPosePtr::dynamicCast(worldPose);
139 
140  // auto basePrx = ic->getCommunicator()->stringToProxy(agent->getStringifiedSharedRobotInterfaceProxy());
141  // SharedRobotInterfacePrx robotPrx = SharedRobotInterfacePrx::checkedCast(basePrx);
142  auto robotPrx = agent->getSharedRobot();
143 
144  if (!robotPrx)
145  {
146  ARMARX_ERROR_S << "Could not create proxy to robot state - proxy string was : " << agent->getStringifiedSharedRobotInterfaceProxy();
147  return NULL;
148  }
149 
150  worldPoseCast->changeFrame(robotPrx, robotPrx->getRootNode()->getName());
151  Eigen::Matrix4f objectPoseLocal = worldPoseCast->toEigen() * agentPose->toEigen().inverse();
152 
153  armarx::FramedPosePtr p(new armarx::FramedPose(objectPoseLocal, robotPrx->getRootNode()->getName(), robotPrx->getName()));
154  p->changeFrame(robotPrx, targetFrame);
155  return p;
156  }
157 
158  std::string AgentInstancesSegment::addEntity(const EntityBasePtr& entity, const Ice::Current&)
159  {
160 
161  setRemoteRobotPose(entity);
162 
163  std::string result = WorkingMemoryEntitySegment::addEntity(entity);
164  return result;
165  }
166 
167  void AgentInstancesSegment::updateEntity(const std::string& id, const EntityBasePtr& entity, const Ice::Current&)
168  {
169 
170  setRemoteRobotPose(entity);
171 
173  }
174 
175  std::string AgentInstancesSegment::upsertEntity(const std::string& entityId, const EntityBasePtr& newEntity, const ::Ice::Current&)
176  {
177  setRemoteRobotPose(newEntity);
178  return WorkingMemoryEntitySegment::upsertEntity(entityId, newEntity);
179  }
180 
181  std::string AgentInstancesSegment::upsertEntityByName(const std::string& entityName, const EntityBasePtr& newEntity, const ::Ice::Current&)
182  {
183  setRemoteRobotPose(newEntity);
184  return WorkingMemoryEntitySegment::upsertEntityByName(entityName, newEntity);
185  }
186 
187  void AgentInstancesSegment::removeEntity(const std::string& id, const Ice::Current&)
188  {
189  ARMARX_IMPORTANT_S << "Remove agent instance " << id;
190 
192 
193  }
194 
196  {
197  ARMARX_DEBUG_S << " removeAllEntities ";
199 
200  }
201 
202  void AgentInstancesSegment::setRemoteRobotPose(EntityBasePtr entity)
203  {
204  AgentInstancePtr agentInstance = AgentInstancePtr::dynamicCast(entity);
205 
206  if (agentInstance)
207  {
208  // ARMARX_DEBUG << "Update agent instance " << agentInstance->getName();
209  auto robotPrx = agentInstance->getSharedRobot();
210 
211  if (robotPrx)
212  {
213  ARMARX_CHECK_EXPRESSION(agentInstance->getPosition()->getFrame() == armarx::GlobalFrame);
214  ARMARX_CHECK_EXPRESSION(agentInstance->getOrientation()->getFrame() == armarx::GlobalFrame);
215  armarx::PosePtr pose = new armarx::Pose(agentInstance->getPosition(), agentInstance->getOrientation());
216  robotPrx->setGlobalPose(pose);
217  }
218  }
219  }
220 
221 }
ARMARX_IMPORTANT_S
#define ARMARX_IMPORTANT_S
Definition: Logging.h:203
cyberglove_with_calib_22dof.ic
ic
Definition: cyberglove_with_calib_22dof.py:22
memoryx::WorkingMemoryEntitySegment< AgentInstance >::getAllEntities
EntityBaseList getAllEntities(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: WorkingMemoryEntitySegment.h:585
memoryx::AgentInstancesSegment::setRemoteRobotPose
void setRemoteRobotPose(EntityBasePtr entity)
Definition: AgentInstancesSegment.cpp:202
memoryx::AgentInstancesSegment::addEntity
std::string addEntity(const EntityBasePtr &entity, const Ice::Current &) override
Definition: AgentInstancesSegment.cpp:158
memoryx::WorkingMemoryEntitySegment< AgentInstance >::getEntityByName
EntityBasePtr getEntityByName(const ::std::string &name) const
Definition: WorkingMemoryEntitySegment.h:451
memoryx::AgentInstancesSegment::getAgentInstanceByName
AgentInstanceBasePtr getAgentInstanceByName(const std::string &name, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: AgentInstancesSegment.cpp:72
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
armarx::FramedPose
The FramedPose class.
Definition: FramedPose.h:258
memoryx::AgentInstancesSegment::getAgentInstanceById
AgentInstanceBasePtr getAgentInstanceById(const std::string &id, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: AgentInstancesSegment.cpp:55
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
memoryx::AgentInstancesSegment::upsertEntityByName
std::string upsertEntityByName(const std::string &entityName, const EntityBasePtr &newEntity, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AgentInstancesSegment.cpp:181
memoryx::WorkingMemoryEntitySegment::updateEntity
void updateEntity(const std::string &entityId, const EntityBasePtr &update, const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemoryEntitySegment.h:246
memoryx::WorkingMemoryEntitySegment::upsertEntityByName
std::string upsertEntityByName(const std::string &entityName, const EntityBasePtr &newEntity, const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemoryEntitySegment.h:229
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::AgentInstancesSegment::updateEntity
void updateEntity(const std::string &id, const EntityBasePtr &entity, const Ice::Current &) override
Definition: AgentInstancesSegment.cpp:167
IceInternal::Handle< FramedPosition >
memoryx::WorkingMemoryEntitySegment
Definition: WorkingMemoryEntitySegment.h:44
memoryx::AgentInstancesSegment::convertToWorldPose
armarx::FramedPoseBasePtr convertToWorldPose(const std::string &agentName, const armarx::FramedPoseBasePtr &localPose, const Ice::Current &c=Ice::emptyCurrent) const override
Definition: AgentInstancesSegment.cpp:89
memoryx::WorkingMemoryEntitySegment::removeAllEntities
void removeAllEntities(const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemoryEntitySegment.h:331
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:198
memoryx::WorkingMemoryEntitySegment::removeEntity
void removeEntity(const ::std::string &id, const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemoryEntitySegment.h:312
memoryx::AgentInstancesSegment::getAllAgentInstances
AgentInstanceBaseList getAllAgentInstances(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: AgentInstancesSegment.cpp:33
memoryx::AgentInstancesSegment::AgentInstancesSegment
AgentInstancesSegment(armarx::IceManagerPtr ic)
Definition: AgentInstancesSegment.cpp:27
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
AgentInstancesSegment.h
memoryx::WorkingMemoryEntitySegment::addEntity
std::string addEntity(const EntityBasePtr &entity, const ::Ice::Current &c=Ice::emptyCurrent) override
addEntity addes an entity to this segment. The registered fusion methods are applied in order to fuse...
Definition: WorkingMemoryEntitySegment.h:164
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
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
memoryx::AgentInstancesSegment::convertToLocalPose
armarx::FramedPoseBasePtr convertToLocalPose(const std::string &agentName, const armarx::PoseBasePtr &worldPose, const std::string &targetFrame, const Ice::Current &c=Ice::emptyCurrent) const override
Definition: AgentInstancesSegment.cpp:126
IceUtil::Handle< IceManager >
memoryx::AgentInstancesSegment::upsertEntity
std::string upsertEntity(const std::string &entityId, const EntityBasePtr &newEntity, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AgentInstancesSegment.cpp:175
memoryx::WorkingMemoryEntitySegment< AgentInstance >::getEntityById
EntityBasePtr getEntityById(const ::std::string &id) const
Definition: WorkingMemoryEntitySegment.h:427
memoryx::AgentInstance
Definition: AgentInstance.h:44
memoryx::AgentInstancesSegment::removeEntity
void removeEntity(const ::std::string &id, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AgentInstancesSegment.cpp:187
memoryx::AgentInstancesSegment::removeAllEntities
void removeAllEntities(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AgentInstancesSegment.cpp:195
memoryx::WorkingMemoryEntitySegment::upsertEntity
std::string upsertEntity(const std::string &entityId, const EntityBasePtr &newEntity, const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemoryEntitySegment.h:194