26 #include <VirtualRobot/VirtualRobot.h>
27 #include <VirtualRobot/XML/RobotIO.h>
28 #include <VirtualRobot/RobotConfig.h>
29 #include <VirtualRobot/RobotFactory.h>
30 #include <VirtualRobot/Nodes/RobotNodeFixedFactory.h>
31 #include <VirtualRobot/Nodes/RobotNodePrismaticFactory.h>
32 #include <VirtualRobot/Nodes/RobotNodeRevoluteFactory.h>
33 #include <VirtualRobot/CollisionDetection/CollisionChecker.h>
42 #include <SimoxUtility/algorithm/string/string_tools.h>
44 #include <Eigen/Geometry>
50 using namespace Eigen;
55 std::recursive_mutex RemoteRobot::m;
70 catch (std::exception& e)
95 return _robot->hasRobotNode(robotNodeName);
130 DMES((format(
"Node: %1%") % robotNodeName));
134 DMES(
"No cache hit");
153 NameList nodes =
_robot->getRobotNodes();
154 for (std::string
const& name : nodes)
162 return _robot->hasRobotNodeSet(name);
167 std::vector<RobotNodePtr> storeNodes;
168 RobotNodeSetInfoPtr info =
_robot->getRobotNodeSet(nodeSetName);
169 return RobotNodeSet::createRobotNodeSet(
170 shared_from_this(), nodeSetName, info->names, info->rootName, info->tcpName);
176 NameList sets =
_robot->getRobotNodeSets();
178 for (std::string
const& name : sets)
192 return _robot->getScaling();
205 VirtualRobot::RobotNodePtr
RemoteRobot::createLocalNode(SharedRobotNodeInterfacePrx remoteNode, std::vector<VirtualRobot::RobotNodePtr>& allNodes, std::map< VirtualRobot::RobotNodePtr, std::vector<std::string> >& childrenMap,
RobotPtr robo)
207 std::scoped_lock cloneLock(
m);
208 static int nonameCounter = 0;
210 if (!remoteNode || !robo)
213 return VirtualRobot::RobotNodePtr();
216 VirtualRobot::RobotNodeFactoryPtr revoluteNodeFactory = VirtualRobot::RobotNodeFactory::fromName(VirtualRobot::RobotNodeRevoluteFactory::getName(), NULL);
217 VirtualRobot::RobotNodeFactoryPtr fixedNodeFactory = VirtualRobot::RobotNodeFactory::fromName(VirtualRobot::RobotNodeFixedFactory::getName(), NULL);
218 VirtualRobot::RobotNodeFactoryPtr prismaticNodeFactory = VirtualRobot::RobotNodeFactory::fromName(VirtualRobot::RobotNodePrismaticFactory::getName(), NULL);
220 Eigen::Vector3f idVec3 = Eigen::Vector3f::Zero();
221 std::string name = remoteNode->getName();
226 std::stringstream ss;
227 ss <<
"robot_node_" << nonameCounter;
232 VirtualRobot::RobotNodePtr result;
233 PosePtr lTbase = PosePtr::dynamicCast(remoteNode->getLocalTransformation());
237 float jvLo = remoteNode->getJointLimitLow();
238 float jvHi = remoteNode->getJointLimitHigh();
239 float jointOffset = 0;
241 JointType jt = remoteNode->getType();
243 SceneObject::Physics physics;
244 physics.localCoM = Vector3Ptr::dynamicCast(remoteNode->getCoM())->toEigen();
245 std::vector<float> inertia = remoteNode->getInertia();
246 for (
int i = 0; i < 3; i++)
247 for (
int j = 0; j < 3; j++)
249 physics.inertiaMatrix(i, j) = inertia.at(i * 3 + j);
251 physics.massKg = remoteNode->getMass();
257 Vector3Ptr axisBase = Vector3Ptr::dynamicCast(remoteNode->getJointTranslationDirection());
258 Eigen::Vector3f axis = axisBase->toEigen();
260 Eigen::Vector4f result4f = Eigen::Vector4f::Zero();
261 result4f.segment(0, 3) = axis;
262 PosePtr gp = PosePtr::dynamicCast(remoteNode->getGlobalPose());
263 result4f = gp->toEigen().inverse() * result4f;
264 axis = result4f.block(0, 0, 3, 1);
266 result = prismaticNodeFactory->createRobotNode(robo, name, VirtualRobot::VisualizationNodePtr(), VirtualRobot::CollisionModelPtr(),
267 jvLo, jvHi, jointOffset, localTransform, idVec3, axis, physics);
272 result = fixedNodeFactory->createRobotNode(robo, name, VirtualRobot::VisualizationNodePtr(), VirtualRobot::CollisionModelPtr(), 0,
273 0, 0, localTransform, idVec3, idVec3, physics);
278 Vector3Ptr axisBase = Vector3Ptr::dynamicCast(remoteNode->getJointRotationAxis());
279 Eigen::Vector3f axis = axisBase->toEigen();
281 Eigen::Vector4f result4f = Eigen::Vector4f::Zero();
282 result4f.segment(0, 3) = axis;
283 PosePtr gp = PosePtr::dynamicCast(remoteNode->getGlobalPose());
284 result4f = gp->toEigen().inverse() * result4f;
285 axis = result4f.block(0, 0, 3, 1);
287 result = revoluteNodeFactory->createRobotNode(robo, name, VirtualRobot::VisualizationNodePtr(), VirtualRobot::CollisionModelPtr(),
288 jvLo, jvHi, jointOffset, localTransform, axis, idVec3, physics);
294 return VirtualRobot::RobotNodePtr();
298 robo->registerRobotNode(result);
299 allNodes.push_back(result);
303 std::vector<std::string> childrenBase = remoteNode->getChildren();
304 std::vector<std::string> children;
307 for (
size_t i = 0; i < childrenBase.size(); i++)
309 if (
_robot->hasRobotNode(childrenBase[i]))
311 SharedRobotNodeInterfacePrx rnRemote =
_robot->getRobotNode(childrenBase[i]);
312 VirtualRobot::RobotNodePtr localNode =
createLocalNode(rnRemote, allNodes, childrenMap, robo);
320 children.push_back(childrenBase[i]);
324 childrenMap[result] = children;
330 std::scoped_lock cloneLock(
m);
331 std::string robotType =
getName();
332 std::string robotName =
getName();
336 SharedRobotNodeInterfacePrx root =
_robot->getRootNode();
338 std::vector<VirtualRobot::RobotNodePtr> allNodes;
339 std::map< VirtualRobot::RobotNodePtr, std::vector<std::string> > childrenMap;
341 VirtualRobot::RobotNodePtr rootLocal =
createLocalNode(root, allNodes, childrenMap, robo);
343 bool res = VirtualRobot::RobotFactory::initializeRobot(robo, allNodes, childrenMap, rootLocal);
352 std::vector<std::string> rns =
_robot->getRobotNodeSets();
354 for (
size_t i = 0; i < rns.size(); i++)
356 RobotNodeSetInfoPtr rnsInfo =
_robot->getRobotNodeSet(rns[i]);
357 RobotNodeSet::createRobotNodeSet(robo, rnsInfo->name, rnsInfo->names, rnsInfo->rootName, rnsInfo->tcpName,
true);
361 auto pose = PosePtr::dynamicCast(
_robot->getGlobalPose());
362 robo->setGlobalPose(pose->toEigen());
368 return createLocalClone(robotStatePrx->getSynchronizedRobot(),
filename, robotStatePrx->getScaling(), packages, loadMode);
375 std::scoped_lock cloneLock(
m);
387 std::shared_ptr<RemoteRobot> rob(
new RemoteRobot(sharedRobotPrx));
388 result = rob->createLocalClone();
398 Ice::StringSeq includePaths;
399 for (
const std::string& projectName : packages)
401 if (projectName.empty())
408 auto pathsString =
project.getDataDir();
409 ARMARX_DEBUG_S <<
"Data paths of ArmarX package " << projectName <<
": " << pathsString;
411 ARMARX_DEBUG_S <<
"Result: Data paths of ArmarX package " << projectName <<
": " << projectIncludePaths;
412 includePaths.insert(includePaths.end(), projectIncludePaths.begin(), projectIncludePaths.end());
422 result = RobotIO::loadRobot(
filename, loadMode);
431 if (result && scaling != 1.0f)
434 result = result->clone(result->getName(), result->getCollisionChecker(), scaling);
443 return createLocalClone(robotStatePrx, robotStatePrx->getRobotFilename(), robotStatePrx->getArmarXPackages(), loadMode);
465 PoseBasePtr globalPose;
466 RobotConfigPtr
c(
new RobotConfig(robot,
"synchronizeLocalClone"));
467 NameValueMap jv = sharedRobotPrx->getConfigAndPose(globalPose);
469 for (NameValueMap::const_iterator it = jv.begin(); it != jv.end(); it++)
472 const std::string& jointName = it->first;
473 float jointAngle = it->second;
475 if (!
c->setConfig(jointName, jointAngle))
482 auto pose = PosePtr::dynamicCast(globalPose);
483 robot->setGlobalPose(pose->toEigen());
491 RobotConfigPtr
c(
new RobotConfig(robot,
"synchronizeLocalClone"));
492 RobotStateConfig state = robotStatePrx->getRobotStateAtTimestamp(timestamp);
501 RobotConfigPtr
c(
new RobotConfig(robot,
"synchronizeLocalClone"));
502 if (state.jointMap.empty())
507 for (NameValueMap::const_iterator it = state.jointMap.begin(); it != state.jointMap.end(); it++)
510 const std::string& jointName = it->first;
511 float jointAngle = it->second;
513 if (!
c->setConfig(jointName, jointAngle))
520 auto pose = PosePtr::dynamicCast(state.globalPose);
521 robot->setGlobalPose(pose->toEigen());
537 return EndEffectorPtr();
558 return VirtualRobot::CollisionChecker::getGlobalCollisionChecker();