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 
34  AgentInstanceBaseList
35  AgentInstancesSegment::getAllAgentInstances(const ::Ice::Current&) const
36  {
37  AgentInstanceBaseList result;
38  auto allEntities = getAllEntities();
39  result.reserve(allEntities.size());
40 
41  for (auto&& entity : allEntities)
42  {
43  AgentInstanceBasePtr agentEntity = AgentInstanceBasePtr::dynamicCast(std::move(entity));
44 
45  if (!agentEntity)
46  {
47  ARMARX_WARNING_S << "Entity with id " << entity->getId()
48  << " is not of type AgentInstance!";
49  continue;
50  }
51 
52  result.push_back(std::move(agentEntity));
53  }
54 
55  return result;
56  }
57 
58  AgentInstanceBasePtr
59  AgentInstancesSegment::getAgentInstanceById(const std::string& id, const ::Ice::Current&) const
60  {
61  auto entity = getEntityById(id);
62  if (!entity)
63  {
64  return NULL;
65  }
66  AgentInstanceBasePtr res = AgentInstanceBasePtr::dynamicCast(entity);
67 
68  if (!res)
69  {
70  ARMARX_WARNING_S << "Entity with id " << id << " is not of type AgentInstance!";
71  }
72 
73  return res;
74  }
75 
76  AgentInstanceBasePtr
78  const ::Ice::Current&) const
79  {
80  auto entity = getEntityByName(name);
81  if (!entity)
82  {
83  return NULL;
84  }
85  AgentInstanceBasePtr res = AgentInstanceBasePtr::dynamicCast(entity);
86 
87  if (!res)
88  {
89  ARMARX_WARNING_S << "Entity with name '" << name << "' is not of type AgentEntity!";
90  }
91 
92  return res;
93  }
94 
95  armarx::FramedPoseBasePtr
96  AgentInstancesSegment::convertToWorldPose(const std::string& agentName,
97  const armarx::FramedPoseBasePtr& localPose,
98  const Ice::Current& c) const
99  {
100  if (localPose->frame == armarx::GlobalFrame)
101  {
102  return localPose;
103  }
104 
105  auto agent = getAgentInstanceByName(agentName);
106 
107  if (!agent)
108  {
109  ARMARX_ERROR_S << "no agent with name " << agentName;
110  return NULL;
111  }
112 
113  armarx::FramedPositionPtr agentPosition =
114  armarx::FramedPositionPtr::dynamicCast(agent->getPositionBase());
115  armarx::FramedOrientationPtr agentOrientation =
116  armarx::FramedOrientationPtr::dynamicCast(agent->getOrientationBase());
117  armarx::PosePtr agentPose =
118  new armarx::Pose(agentOrientation->toEigen(), agentPosition->toEigen());
119  armarx::FramedPosePtr localPoseCast = armarx::FramedPosePtr::dynamicCast(localPose);
120 
121  // auto basePrx = ic->getCommunicator()->stringToProxy(agent->getStringifiedSharedRobotInterfaceProxy());
122  // SharedRobotInterfacePrx robotPrx = SharedRobotInterfacePrx::checkedCast(basePrx);
123  auto robotPrx = agent->getSharedRobot();
124 
125  if (!robotPrx)
126  {
127  ARMARX_ERROR_S << "Could not create proxy to robot state - proxy string was : "
128  << agent->getStringifiedSharedRobotInterfaceProxy() << " for agent "
129  << agentName;
130  return NULL;
131  }
132 
133  localPoseCast->changeFrame(robotPrx, robotPrx->getRootNode()->getName());
134  Eigen::Matrix4f objectPoseGlobal = agentPose->toEigen() * localPoseCast->toEigen();
135 
136  armarx::FramedPosePtr p(new armarx::FramedPose(objectPoseGlobal, armarx::GlobalFrame, ""));
137  return p;
138  }
139 
140  armarx::FramedPoseBasePtr
141  AgentInstancesSegment::convertToLocalPose(const std::string& agentName,
142  const armarx::PoseBasePtr& worldPose,
143  const std::string& targetFrame,
144  const Ice::Current& c) const
145  {
146  auto agent = getAgentInstanceByName(agentName);
147 
148  if (!agent)
149  {
150  return NULL;
151  }
152 
153  armarx::FramedPositionPtr agentPosition =
154  armarx::FramedPositionPtr::dynamicCast(agent->getPositionBase());
155  armarx::FramedOrientationPtr agentOrientation =
156  armarx::FramedOrientationPtr::dynamicCast(agent->getOrientationBase());
157  armarx::PosePtr agentPose =
158  new armarx::Pose(agentOrientation->toEigen(), agentPosition->toEigen());
159  armarx::FramedPosePtr worldPoseCast = armarx::FramedPosePtr::dynamicCast(worldPose);
160 
161  // auto basePrx = ic->getCommunicator()->stringToProxy(agent->getStringifiedSharedRobotInterfaceProxy());
162  // SharedRobotInterfacePrx robotPrx = SharedRobotInterfacePrx::checkedCast(basePrx);
163  auto robotPrx = agent->getSharedRobot();
164 
165  if (!robotPrx)
166  {
167  ARMARX_ERROR_S << "Could not create proxy to robot state - proxy string was : "
168  << agent->getStringifiedSharedRobotInterfaceProxy();
169  return NULL;
170  }
171 
172  worldPoseCast->changeFrame(robotPrx, robotPrx->getRootNode()->getName());
173  Eigen::Matrix4f objectPoseLocal = worldPoseCast->toEigen() * agentPose->toEigen().inverse();
174 
176  objectPoseLocal, robotPrx->getRootNode()->getName(), robotPrx->getName()));
177  p->changeFrame(robotPrx, targetFrame);
178  return p;
179  }
180 
181  std::string
182  AgentInstancesSegment::addEntity(const EntityBasePtr& entity, const Ice::Current&)
183  {
184 
185  setRemoteRobotPose(entity);
186 
187  std::string result = WorkingMemoryEntitySegment::addEntity(entity);
188  return result;
189  }
190 
191  void
192  AgentInstancesSegment::updateEntity(const std::string& id,
193  const EntityBasePtr& entity,
194  const Ice::Current&)
195  {
196 
197  setRemoteRobotPose(entity);
198 
200  }
201 
202  std::string
203  AgentInstancesSegment::upsertEntity(const std::string& entityId,
204  const EntityBasePtr& newEntity,
205  const ::Ice::Current&)
206  {
207  setRemoteRobotPose(newEntity);
208  return WorkingMemoryEntitySegment::upsertEntity(entityId, newEntity);
209  }
210 
211  std::string
212  AgentInstancesSegment::upsertEntityByName(const std::string& entityName,
213  const EntityBasePtr& newEntity,
214  const ::Ice::Current&)
215  {
216  setRemoteRobotPose(newEntity);
217  return WorkingMemoryEntitySegment::upsertEntityByName(entityName, newEntity);
218  }
219 
220  void
221  AgentInstancesSegment::removeEntity(const std::string& id, const Ice::Current&)
222  {
223  ARMARX_IMPORTANT_S << "Remove agent instance " << id;
224 
226  }
227 
228  void
230  {
231  ARMARX_DEBUG_S << " removeAllEntities ";
233  }
234 
235  void
237  {
238  AgentInstancePtr agentInstance = AgentInstancePtr::dynamicCast(entity);
239 
240  if (agentInstance)
241  {
242  // ARMARX_DEBUG << "Update agent instance " << agentInstance->getName();
243  auto robotPrx = agentInstance->getSharedRobot();
244 
245  if (robotPrx)
246  {
247  ARMARX_CHECK_EXPRESSION(agentInstance->getPosition()->getFrame() ==
249  ARMARX_CHECK_EXPRESSION(agentInstance->getOrientation()->getFrame() ==
251  armarx::PosePtr pose =
252  new armarx::Pose(agentInstance->getPosition(), agentInstance->getOrientation());
253  robotPrx->setGlobalPose(pose);
254  }
255  }
256  }
257 
258 } // namespace memoryx
ARMARX_IMPORTANT_S
#define ARMARX_IMPORTANT_S
Definition: Logging.h:210
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:642
memoryx::AgentInstancesSegment::setRemoteRobotPose
void setRemoteRobotPose(EntityBasePtr entity)
Definition: AgentInstancesSegment.cpp:236
memoryx::AgentInstancesSegment::addEntity
std::string addEntity(const EntityBasePtr &entity, const Ice::Current &) override
Definition: AgentInstancesSegment.cpp:182
memoryx::WorkingMemoryEntitySegment< AgentInstance >::getEntityByName
EntityBasePtr getEntityByName(const ::std::string &name) const
Definition: WorkingMemoryEntitySegment.h:489
memoryx::AgentInstancesSegment::getAgentInstanceByName
AgentInstanceBasePtr getAgentInstanceByName(const std::string &name, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: AgentInstancesSegment.cpp:77
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
armarx::FramedPose
The FramedPose class.
Definition: FramedPose.h:280
memoryx::AgentInstancesSegment::getAgentInstanceById
AgentInstanceBasePtr getAgentInstanceById(const std::string &id, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: AgentInstancesSegment.cpp:59
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:65
memoryx::AgentInstancesSegment::upsertEntityByName
std::string upsertEntityByName(const std::string &entityName, const EntityBasePtr &newEntity, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AgentInstancesSegment.cpp:212
memoryx::WorkingMemoryEntitySegment::updateEntity
void updateEntity(const std::string &entityId, const EntityBasePtr &update, const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemoryEntitySegment.h:256
memoryx::WorkingMemoryEntitySegment::upsertEntityByName
std::string upsertEntityByName(const std::string &entityName, const EntityBasePtr &newEntity, const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemoryEntitySegment.h:238
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
memoryx::AgentInstancesSegment::updateEntity
void updateEntity(const std::string &id, const EntityBasePtr &entity, const Ice::Current &) override
Definition: AgentInstancesSegment.cpp:192
IceInternal::Handle< FramedPosition >
memoryx::WorkingMemoryEntitySegment
Definition: WorkingMemoryEntitySegment.h:45
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:96
memoryx::WorkingMemoryEntitySegment::removeAllEntities
void removeAllEntities(const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemoryEntitySegment.h:356
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:205
memoryx::WorkingMemoryEntitySegment::removeEntity
void removeEntity(const ::std::string &id, const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemoryEntitySegment.h:336
memoryx::AgentInstancesSegment::getAllAgentInstances
AgentInstanceBaseList getAllAgentInstances(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: AgentInstancesSegment.cpp:35
memoryx::AgentInstancesSegment::AgentInstancesSegment
AgentInstancesSegment(armarx::IceManagerPtr ic)
Definition: AgentInstancesSegment.cpp:27
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:216
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:172
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:213
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:141
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:203
memoryx::WorkingMemoryEntitySegment< AgentInstance >::getEntityById
EntityBasePtr getEntityById(const ::std::string &id) const
Definition: WorkingMemoryEntitySegment.h:463
memoryx::AgentInstance
Definition: AgentInstance.h:45
memoryx::AgentInstancesSegment::removeEntity
void removeEntity(const ::std::string &id, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AgentInstancesSegment.cpp:221
memoryx::AgentInstancesSegment::removeAllEntities
void removeAllEntities(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AgentInstancesSegment.cpp:229
memoryx::WorkingMemoryEntitySegment::upsertEntity
std::string upsertEntity(const std::string &entityId, const EntityBasePtr &newEntity, const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemoryEntitySegment.h:201