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
24
25namespace memoryx
26{
29 AgentInstancesSegmentBase::AgentInstancesSegmentBase(),
30 ic(ic)
31 {
32 }
33
34 AgentInstanceBaseList
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
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
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
constexpr T c
The FramedPose class.
Definition FramedPose.h:281
The Pose class.
Definition Pose.h:243
armarx::FramedPoseBasePtr convertToWorldPose(const std::string &agentName, const armarx::FramedPoseBasePtr &localPose, const Ice::Current &c=Ice::emptyCurrent) const override
void removeEntity(const ::std::string &id, const ::Ice::Current &=Ice::emptyCurrent) override
AgentInstancesSegment(armarx::IceManagerPtr ic)
void removeAllEntities(const ::Ice::Current &=Ice::emptyCurrent) override
std::string upsertEntityByName(const std::string &entityName, const EntityBasePtr &newEntity, const ::Ice::Current &=Ice::emptyCurrent) override
void setRemoteRobotPose(EntityBasePtr entity)
std::string upsertEntity(const std::string &entityId, const EntityBasePtr &newEntity, const ::Ice::Current &=Ice::emptyCurrent) override
AgentInstanceBaseList getAllAgentInstances(const ::Ice::Current &=Ice::emptyCurrent) const override
AgentInstanceBasePtr getAgentInstanceByName(const std::string &name, const ::Ice::Current &=Ice::emptyCurrent) const override
std::string addEntity(const EntityBasePtr &entity, const Ice::Current &) override
AgentInstanceBasePtr getAgentInstanceById(const std::string &id, const ::Ice::Current &=Ice::emptyCurrent) const override
void updateEntity(const std::string &id, const EntityBasePtr &entity, const Ice::Current &) override
armarx::FramedPoseBasePtr convertToLocalPose(const std::string &agentName, const armarx::PoseBasePtr &worldPose, const std::string &targetFrame, const Ice::Current &c=Ice::emptyCurrent) const override
std::string upsertEntityByName(const std::string &entityName, const EntityBasePtr &newEntity, const ::Ice::Current &c=Ice::emptyCurrent) override
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...
void removeEntity(const ::std::string &id, const ::Ice::Current &c=Ice::emptyCurrent) override
std::string upsertEntity(const std::string &entityId, const EntityBasePtr &newEntity, const ::Ice::Current &c=Ice::emptyCurrent) override
void removeAllEntities(const ::Ice::Current &c=Ice::emptyCurrent) override
void updateEntity(const std::string &entityId, const EntityBasePtr &update, const ::Ice::Current &c=Ice::emptyCurrent) override
EntityBasePtr getEntityByName(const ::std::string &name) const
EntityBasePtr getEntityById(const ::std::string &id) const
EntityBaseList getAllEntities(const Ice::Current &c=Ice::emptyCurrent) const override
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_DEBUG_S
The logging level for output that is only interesting while debugging.
Definition Logging.h:205
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:216
#define ARMARX_IMPORTANT_S
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:210
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:213
std::string const GlobalFrame
Variable of the global coordinate system.
Definition FramedPose.h:65
IceInternal::Handle< Pose > PosePtr
Definition Pose.h:306
IceInternal::Handle< FramedPosition > FramedPositionPtr
Definition FramedPose.h:149
IceInternal::Handle< FramedOrientation > FramedOrientationPtr
Definition FramedPose.h:207
IceUtil::Handle< IceManager > IceManagerPtr
IceManager smart pointer.
Definition ArmarXFwd.h:39
IceInternal::Handle< FramedPose > FramedPosePtr
Definition FramedPose.h:272
VirtualRobot headers.
IceInternal::Handle< AgentInstance > AgentInstancePtr
Typedef of AgentEntityPtr as IceInternal::Handle<AgentEntity> for convenience.