SharedRobotServants.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package
19 * @author
20 * @date
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#include "SharedRobotServants.h"
25
26#include <Eigen/Geometry>
27
28#include <Ice/ObjectAdapter.h>
29
30#include <VirtualRobot/Nodes/RobotNode.h>
31#include <VirtualRobot/Nodes/RobotNodePrismatic.h>
32#include <VirtualRobot/Nodes/RobotNodeRevolute.h>
33#include <VirtualRobot/Robot.h>
34#include <VirtualRobot/RobotConfig.h>
35#include <VirtualRobot/RobotNodeSet.h>
36#include <VirtualRobot/VirtualRobot.h>
37
40
41using namespace VirtualRobot;
42using namespace Eigen;
43using namespace Ice;
44
45
46#undef VERBOSE
47
48//#define VERBOSE
49
50
51namespace armarx
52{
53
54 using NodeCache = std::map<std::string, SharedRobotNodeInterfacePrx>;
55
56 ///////////////////////////////
57 // SharedObjectBase
58 ///////////////////////////////
59
61 {
62 this->_referenceCount = 0;
63#ifdef VERBOSE
64 ARMARX_LOG_S << "construct " << this << flush;
65#endif
66 }
67
68 void
69 SharedObjectBase::ref(const Current& current)
70 {
71 std::unique_lock lock(this->_counterMutex);
72
73 _referenceCount++;
74
75#ifdef VERBOSE
76 ARMARX_LOG_S << "ref: " << _referenceCount << " " << this << flush;
77#endif
78 }
79
80 void
81 SharedObjectBase::unref(const Current& current)
82 {
83 std::unique_lock lock(this->_counterMutex);
84
85#ifdef VERBOSE
86 ARMARX_LOG_S << "unref: " << _referenceCount << " " << this << flush;
87#endif
88 _referenceCount--;
89
90 if (_referenceCount == 0)
91 {
92 this->destroy(current);
93 }
94 }
95
96 void
97 SharedObjectBase::destroy(const Current& current)
98 {
99 try
100 {
101 current.adapter->remove(current.id);
102#ifdef VERBOSE
103 ARMARX_ERROR_S << "[SharedObject] destroy "
104 << " " << this << flush;
105#endif
106 }
107 catch (const NotRegisteredException& e)
108 {
109 // ARMARX_INFO_S << "destroy failed with: " << e.what();
110 throw ObjectNotExistException(__FILE__, __LINE__);
111 };
112 }
113
114 ///////////////////////////////
115 // SharedRobotNodeServant
116 ///////////////////////////////
117
119 {
120#ifdef VERBOSE
121 ARMARX_LOG_S << "[SharedRobotNodeServant] construct "
122 << " " << this << flush;
123#endif
124 }
125
127 {
128#ifdef VERBOSE
129 ARMARX_FATAL_S << "[SharedRobotNodeServant] destruct "
130 << " " << this << flush;
131#endif
132 }
133
134 float
135 SharedRobotNodeServant::getJointValue(const Current& current) const
136 {
137 ReadLockPtr lock = _node->getRobot()->getReadLock();
138 return _node->getJointValue();
139 }
140
141 std::string
142 SharedRobotNodeServant::getName(const Current& current) const
143 {
144 ReadLockPtr lock = _node->getRobot()->getReadLock();
145 return _node->getName();
146 }
147
148 PoseBasePtr
150 {
151 ReadLockPtr lock = _node->getRobot()->getReadLock();
152 return new Pose(_node->getLocalTransformation());
153 }
154
155 FramedPoseBasePtr
156 SharedRobotNodeServant::getGlobalPose(const Current& current) const
157 {
158 ReadLockPtr lock = _node->getRobot()->getReadLock();
159 return new FramedPose(_node->getGlobalPose(), GlobalFrame, "");
160 }
161
162 FramedPoseBasePtr
163 SharedRobotNodeServant::getPoseInRootFrame(const Current& current) const
164 {
165 ReadLockPtr lock = _node->getRobot()->getReadLock();
166 return new FramedPose(_node->getPoseInRootFrame(),
167 _node->getRobot()->getRootNode()->getName(),
168 _node->getRobot()->getName());
169 }
170
171 JointType
172 SharedRobotNodeServant::getType(const Current& current) const
173 {
174 ReadLockPtr lock = _node->getRobot()->getReadLock();
175
176 if (_node->isRotationalJoint())
177 {
178 return eRevolute;
179 }
180 else if (_node->isTranslationalJoint())
181 {
182 return ePrismatic;
183 }
184 else
185 {
186 return eFixed;
187 }
188 }
189
190 Vector3BasePtr
192 {
193 ReadLockPtr lock = _node->getRobot()->getReadLock();
194 RobotNodePrismatic* prismatic = dynamic_cast<RobotNodePrismatic*>(_node.get());
195
196 if (prismatic)
197 {
198 return new Vector3(prismatic->getJointTranslationDirection());
199 }
200 else
201 {
202 return new Vector3;
203 }
204 }
205
206 Vector3BasePtr
208 {
209 ReadLockPtr lock = _node->getRobot()->getReadLock();
210 RobotNodeRevolute* revolute = dynamic_cast<RobotNodeRevolute*>(_node.get());
211
212 if (revolute)
213 {
214 return new Vector3(revolute->getJointRotationAxis());
215 }
216 else
217 {
218 return new Vector3;
219 }
220 }
221
222 bool
223 SharedRobotNodeServant::hasChild(const std::string& name,
224 bool recursive,
225 const Current& current) const
226 {
227 ReadLockPtr lock = _node->getRobot()->getReadLock();
228 //return _node->hasChild(name,recursive);
229 return false;
230 }
231
232 std::string
233 SharedRobotNodeServant::getParent(const Current& current) const
234 {
235 ReadLockPtr lock = _node->getRobot()->getReadLock();
236 SceneObjectPtr parent = _node->getParent();
237
238 if (!parent)
239 {
240 throw UserException("This RobotNode has no parent.");
241 }
242
243 return parent->getName();
244 }
245
246 NameList
247 SharedRobotNodeServant::getChildren(const Current& current) const
248 {
249 ReadLockPtr lock = _node->getRobot()->getReadLock();
250 std::vector<SceneObjectPtr> children = _node->getChildren();
251 NameList names;
252 for (SceneObjectPtr const& node : children)
253 {
254 names.push_back(node->getName());
255 }
256 return names;
257 }
258
259 NameList
261 const Ice::Current& current) const
262 {
263 ReadLockPtr lock = _node->getRobot()->getReadLock();
264 std::vector<RobotNodePtr> parents =
265 _node->getAllParents(_node->getRobot()->getRobotNodeSet(name));
266 NameList names;
267 for (RobotNodePtr const& node : parents)
268 {
269 names.push_back(node->getName());
270 }
271 return names;
272 }
273
274 float
276 {
277 ReadLockPtr lock = _node->getRobot()->getReadLock();
278 return _node->getJointValueOffset();
279 }
280
281 float
282 SharedRobotNodeServant::getJointLimitHigh(const Current& current) const
283 {
284 ReadLockPtr lock = _node->getRobot()->getReadLock();
285 return _node->getJointLimitHigh();
286 }
287
288 float
289 SharedRobotNodeServant::getJointLimitLow(const Current& current) const
290 {
291 ReadLockPtr lock = _node->getRobot()->getReadLock();
292 return _node->getJointLimitLow();
293 }
294
295 Vector3BasePtr
296 SharedRobotNodeServant::getCoM(const Current& current) const
297 {
298 ReadLockPtr lock = _node->getRobot()->getReadLock();
299 return new Vector3(_node->getCoMLocal());
300 }
301
302 FloatSeq
303 SharedRobotNodeServant::getInertia(const Current& current) const
304 {
305 ReadLockPtr lock = _node->getRobot()->getReadLock();
306 FloatSeq result;
307
308 for (int i = 0; i < 3; i++)
309 for (int j = 0; j < 3; j++)
310 {
311 result.push_back(_node->getInertiaMatrix()(i, j));
312 }
313
314 return result;
315 }
316
317 float
318 SharedRobotNodeServant::getMass(const Current& current) const
319 {
320 ReadLockPtr lock = _node->getRobot()->getReadLock();
321 return _node->getMass();
322 }
323
324 ///////////////////////////////
325 // SharedRobotServant
326 ///////////////////////////////
327
330 RobotStateListenerInterfacePrx robotStateListenerPrx) :
331 _robot(robot),
334 {
335#ifdef VERBOSE
336 ARMARX_WARNING_S << "construct " << this << flush;
337#endif
338 }
339
341 {
342#ifdef VERBOSE
343 ARMARX_WARNING_S << "destruct " << this << flush;
344#endif
345 std::unique_lock cloneLock(m);
346
347 for (auto value : this->_cachedNodes)
348 {
349 try
350 {
351 value.second->unref();
352 }
353 catch (...)
354 {
355 }
356 }
357 }
358
359 void
364
365 SharedRobotNodeInterfacePrx
366 SharedRobotServant::getRobotNode(const std::string& name, const Current& current)
367 {
368 // ARMARX_LOG_S << "Looking for node: " << name << flush;
369 assert(_robot);
370 std::unique_lock cloneLock(m);
371 SharedRobotNodeInterfacePrx prx;
372
373 if (this->_cachedNodes.find(name) == this->_cachedNodes.end())
374 {
375 RobotNodePtr robotNode = _robot->getRobotNode(name);
376
377 if (!robotNode)
378 {
379 ARMARX_WARNING_S << "RobotNode \"" + name + "\" not defined.";
380 throw UserException("RobotNode \"" + name + "\" not defined.");
381 }
382
383 SharedRobotNodeInterfacePtr servant =
384 new SharedRobotNodeServant(_robot->getRobotNode(name));
385 //servant->ref();
386 prx = SharedRobotNodeInterfacePrx::uncheckedCast(current.adapter->addWithUUID(servant));
387 prx->ref();
388 // return prx;
389 this->_cachedNodes[name] = prx;
390 }
391
392 return this->_cachedNodes[name];
393 }
394
395 SharedRobotNodeInterfacePrx
396 SharedRobotServant::getRootNode(const Current& current)
397 {
398 assert(_robot);
399 std::unique_lock cloneLock(m);
400 std::string name = _robot->getRootNode() /*,current*/->getName();
401 return this->getRobotNode(name, current);
402 }
403
404 bool
405 SharedRobotServant::hasRobotNode(const std::string& name, const Current& current)
406 {
407 return _robot->hasRobotNode(name);
408 }
409
410 NameList
411 SharedRobotServant::getRobotNodes(const Current& current)
412 {
413 std::vector<RobotNodePtr> robotNodes = _robot->getRobotNodes();
414 NameList names;
415 for (RobotNodePtr const& node : robotNodes)
416 {
417 names.push_back(node->getName());
418 }
419 return names;
420 }
421
422 RobotNodeSetInfoPtr
423 SharedRobotServant::getRobotNodeSet(const std::string& name, const Current& current)
424 {
425 RobotNodeSetPtr robotNodeSet = _robot->getRobotNodeSet(name);
426
427 if (!robotNodeSet)
428 {
429 throw UserException("RobotNodeSet \"" + name + "\" not defined");
430 }
431
432 std::vector<RobotNodePtr> robotNodes = robotNodeSet->getAllRobotNodes();
433 NameList names;
434 for (RobotNodePtr const& node : robotNodes)
435 {
436 names.push_back(node->getName());
437 }
438
439 RobotNodeSetInfoPtr info = new RobotNodeSetInfo;
440 info->names = names;
441 info->name = name;
442 info->tcpName = robotNodeSet->getTCP()->getName();
443 info->rootName = robotNodeSet->getKinematicRoot()->getName();
444
445 return info;
446 }
447
448 NameList
450 {
451 std::vector<RobotNodeSetPtr> robotNodeSets = _robot->getRobotNodeSets();
452 NameList names;
453 for (RobotNodeSetPtr const& set : robotNodeSets)
454 {
455 names.push_back(set->getName());
456 }
457
458 return names;
459 }
460
461 bool
462 SharedRobotServant::hasRobotNodeSet(const std::string& name, const Current& current)
463 {
464 return _robot->hasRobotNodeSet(name);
465 }
466
467 std::string
468 SharedRobotServant::getName(const Current& current)
469 {
470 //ARMARX_VERBOSE_S << "SharedRobotServant::getname:" << _robot->getName();
471 return _robot->getName();
472 }
473
474 std::string
475 SharedRobotServant::getType(const Current& current)
476 {
477 return _robot->getType();
478 }
479
480 void
481 SharedRobotServant::setTimestamp(const IceUtil::Time& updateTime)
482 {
483 updateTimestamp = updateTime;
484 }
485
486 PoseBasePtr
487 SharedRobotServant::getGlobalPose(const Current& current)
488 {
489 ReadLockPtr lock = _robot->getReadLock();
490 return new Pose(_robot->getGlobalPose());
491 }
492
493 NameValueMap
494 SharedRobotServant::getConfig(const Current& current)
495 {
496 if (!_robot)
497 {
498 ARMARX_WARNING_S << "no robot set";
499 return NameValueMap();
500 }
501
502 ReadLockPtr lock = _robot->getReadLock();
503 std::map<std::string, float> values = _robot->getConfig()->getRobotNodeJointValueMap();
504 NameValueMap result(values.begin(), values.end());
505 return result;
506 }
507
508 NameValueMap
509 SharedRobotServant::getConfigAndPose(PoseBasePtr& globalPose, const Current& current)
510 {
511 globalPose = getGlobalPose(current);
512 return getConfig(current);
513 }
514
515 TimestampBasePtr
517 {
519 }
520
523 {
524 return robotStateComponent;
525 }
526
527 void
528 SharedRobotServant::setGlobalPose(const armarx::PoseBasePtr& pose, const Current&)
529 {
530 WriteLockPtr lock = _robot->getWriteLock();
531 Eigen::Matrix4f newPose = PosePtr::dynamicCast(pose)->toEigen();
533 {
534 Eigen::Matrix4f oldPose = _robot->getGlobalPose();
535 robotStateListenerPrx->reportGlobalRobotRootPose(
536 new FramedPose(newPose, GlobalFrame, ""),
537 TimeUtil::GetTime().toMicroSeconds(),
538 !oldPose.isApprox(newPose));
539 }
540 _robot->setGlobalPose(newPose, true);
541 }
542
543 float
545 {
546 return _robot->getScaling();
547 }
548
551 {
552 return this->_robot;
553 }
554
555} // namespace armarx
The FramedPose class.
Definition FramedPose.h:281
The Pose class.
Definition Pose.h:243
void ref(const Ice::Current &c=Ice::emptyCurrent) override
void unref(const Ice::Current &c=Ice::emptyCurrent) override
void destroy(const Ice::Current &c=Ice::emptyCurrent) override
The SharedRobotNodeServant class is a remote representation of a Simox VirtualRobot::Robot.
FramedPoseBasePtr getGlobalPose(const Ice::Current &current=Ice::emptyCurrent) const override
VirtualRobot::RobotNodePtr _node
float getMass(const Ice::Current &current=Ice::emptyCurrent) const override
JointType getType(const Ice::Current &current=Ice::emptyCurrent) const override
std::string getName(const Ice::Current &current=Ice::emptyCurrent) const override
float getJointValueOffest(const Ice::Current &current=Ice::emptyCurrent) const override
SharedRobotNodeServant(VirtualRobot::RobotNodePtr node)
Vector3BasePtr getCoM(const Ice::Current &current=Ice::emptyCurrent) const override
NameList getChildren(const Ice::Current &current=Ice::emptyCurrent) const override
float getJointLimitLow(const Ice::Current &current=Ice::emptyCurrent) const override
Ice::FloatSeq getInertia(const Ice::Current &current=Ice::emptyCurrent) const override
PoseBasePtr getLocalTransformation(const Ice::Current &current=Ice::emptyCurrent) const override
Vector3BasePtr getJointRotationAxis(const Ice::Current &current=Ice::emptyCurrent) const override
NameList getAllParents(const std::string &name, const Ice::Current &current=Ice::emptyCurrent) const override
float getJointLimitHigh(const Ice::Current &current=Ice::emptyCurrent) const override
FramedPoseBasePtr getPoseInRootFrame(const Ice::Current &current=Ice::emptyCurrent) const override
Vector3BasePtr getJointTranslationDirection(const Ice::Current &current=Ice::emptyCurrent) const override
bool hasChild(const std::string &name, bool recursive, const Ice::Current &current=Ice::emptyCurrent) const override
float getJointValue(const Ice::Current &current=Ice::emptyCurrent) const override
std::string getParent(const Ice::Current &current=Ice::emptyCurrent) const override
SharedRobotNodeInterfacePrx getRootNode(const Ice::Current &current=Ice::emptyCurrent) override
TimestampBasePtr getTimestamp(const Ice::Current &=Ice::emptyCurrent) const override
std::string getType(const Ice::Current &current=Ice::emptyCurrent) override
RobotStateComponentInterfacePrx robotStateComponent
float getScaling(const Ice::Current &=Ice::emptyCurrent) override
PoseBasePtr getGlobalPose(const Ice::Current &current=Ice::emptyCurrent) override
std::string getName(const Ice::Current &current=Ice::emptyCurrent) override
void setGlobalPose(const PoseBasePtr &pose, const Ice::Current &current=Ice::emptyCurrent) override
RobotStateComponentInterfacePrx getRobotStateComponent(const Ice::Current &) const override
SharedRobotNodeInterfacePrx getRobotNode(const std::string &name, const Ice::Current &current=Ice::emptyCurrent) override
bool hasRobotNodeSet(const std::string &name, const Ice::Current &current=Ice::emptyCurrent) override
std::map< std::string, SharedRobotNodeInterfacePrx > _cachedNodes
NameValueMap getConfig(const Ice::Current &current=Ice::emptyCurrent) override
SharedRobotServant(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStateComponent, RobotStateListenerInterfacePrx robotStateListenerPrx)
bool hasRobotNode(const std::string &name, const Ice::Current &current=Ice::emptyCurrent) override
NameList getRobotNodeSets(const Ice::Current &current=Ice::emptyCurrent) override
NameList getRobotNodes(const Ice::Current &current=Ice::emptyCurrent) override
NameValueMap getConfigAndPose(PoseBasePtr &globalPose, const Ice::Current &current=Ice::emptyCurrent) override
VirtualRobot::RobotPtr _robot
RobotNodeSetInfoPtr getRobotNodeSet(const std::string &name, const Ice::Current &current=Ice::emptyCurrent) override
void setRobotStateComponent(RobotStateComponentInterfacePrx robotStateComponent)
RobotStateListenerInterfacePrx robotStateListenerPrx
void setTimestamp(const IceUtil::Time &updateTime)
VirtualRobot::RobotPtr getRobot() const
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
Implements a Variant type for timestamps.
The Vector3 class.
Definition Pose.h:113
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:216
#define ARMARX_FATAL_S
The logging level for unexpected behaviour, that will lead to a seriously malfunctioning program and ...
Definition Logging.h:219
#define ARMARX_LOG_S
This macro creates a new temporary instance which can then be used to log data using the << operator.
Definition Logging.h:145
#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
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::map< std::string, SharedRobotNodeInterfacePrx > NodeCache
const LogSender::manipulator flush
Definition LogSender.h:251
::IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface > RobotStateComponentInterfacePrx