29 #include <VirtualRobot/CollisionDetection/CollisionChecker.h>
30 #include <VirtualRobot/IK/DifferentialIK.h>
31 #include <VirtualRobot/IK/IKSolver.h>
32 #include <VirtualRobot/Nodes/ForceTorqueSensor.h>
33 #include <VirtualRobot/Nodes/RobotNode.h>
34 #include <VirtualRobot/Obstacle.h>
35 #include <VirtualRobot/Robot.h>
36 #include <VirtualRobot/RobotFactory.h>
37 #include <VirtualRobot/RobotNodeSet.h>
38 #include <VirtualRobot/Visualization/VisualizationFactory.h>
62 float maxRealTimeSimSpeed,
64 std::string floorTexture)
93 if (up(1) == 0 && up(2) == 0)
99 else if (up(0) == 0 && up(2) == 0)
107 w, h, d, VirtualRobot::VisualizationFactory::Color::Gray());
108 std::string name(
"Floor");
112 gp(2, 3) = -sizeSmall * 0.5f;
116 groundObject->setSimulationType(VirtualRobot::SceneObject::Physics::eStatic);
123 const std::map<std::string, float>& angles,
124 const std::map<std::string, float>& velocities)
128 ARMARX_DEBUG_S <<
"actuateRobotJoints: first:" << angles.begin()->first
129 <<
", ang: " << angles.begin()->second <<
", vel:" << velocities.begin()->second;
136 kinRobot->setJointValues(angles);
140 for (
auto& entry : angles)
148 const std::map<std::string, float>& angles)
151 if (angles.size() == 0)
155 ARMARX_DEBUG_S <<
"actuateRobotJointsPos: first:" << angles.begin()->first
156 <<
", ang: " << angles.begin()->second;
164 kinRobot->setJointValues(angles);
168 for (
auto& entry : angles)
176 const std::map<std::string, float>& velocities)
179 ARMARX_DEBUG_S <<
"actuateRobotJointsVel: first: vel:" << velocities.begin()->second;
191 std::map<std::string, float> res = velocities;
198 const std::map<std::string, float>& torques)
201 ARMARX_DEBUG_S <<
"actuateRobotJointsTorque: first:" << torques.begin()->first <<
","
202 << torques.begin()->second;
211 ARMARX_DEBUG <<
"setRobotPose:" << globalPose(0, 3) <<
"," << globalPose(1, 3) <<
", "
221 kinRobot->setGlobalPose(globalPose);
226 const std::string& robotNodeName,
227 const Eigen::Vector3f& force)
235 const std::string& robotNodeName,
236 const Eigen::Vector3f& torque)
274 o->setGlobalPose(globalPose);
289 return kinRobot->getMass();
292 std::map<std::string, float>
296 std::map<std::string, float> res;
302 return std::map<std::string, float>();
305 std::vector<RobotNodePtr> rn = kinRob->getRobotNodes();
311 res[i->getName()] = i->getJointValue();
332 if (!kinRob->hasRobotNode(nodeName))
338 float res = kinRob->getRobotNode(nodeName)->getJointValue();
343 std::map<std::string, float>
347 std::map<std::string, float> res;
359 ARMARX_DEBUG_S <<
"getRobotJointVelocities:" << res.begin()->second;
375 if (!kinRobot->hasRobotNode(nodeName))
392 std::map<std::string, float>
395 return std::map<std::string, float>();
416 if (!kinRobot->hasRobotNode(nodeName))
422 return kinRobot->getRobotNode(nodeName)->getJointLimitLo();
437 if (!kinRobot->hasRobotNode(nodeName))
443 return kinRobot->getRobotNode(nodeName)->getJointLimitHi();
459 return kinRobot->getGlobalPose();
475 if (!kinRobot->hasRobotNode(nodeName))
481 return kinRobot->getRobotNode(nodeName)->getMaxTorque();
486 const std::string& nodeName,
499 if (!kinRobot->hasRobotNode(nodeName))
505 kinRobot->getRobotNode(nodeName)->setMaxTorque(maxTorque);
521 if (!kinRobot->hasRobotNode(robotNodeName))
527 return kinRobot->getRobotNode(robotNodeName)->getGlobalPose();
561 RobotNodePtr rn = kinRobot->getRobotNode(nodeName);
569 RobotNodeSetPtr rns =
570 RobotNodeSet::createRobotNodeSet(kinRobot,
"All_Nodes", kinRobot->getRobotNodes());
571 DifferentialIKPtr j(
new DifferentialIK(rns));
575 std::vector<std::string> nodeNames = rns->getNodeNames();
576 Eigen::VectorXf joint_vel(nodeNames.size());
578 for (std::vector<std::string>::iterator it = nodeNames.begin(); it != nodeNames.end();
584 Eigen::Vector3f omega;
585 omega = jac * joint_vel;
592 const std::string& robotNodeName,
593 const Eigen::Vector3f& vel)
612 const std::string& robotNodeName,
613 const Eigen::Vector3f& vel)
634 const std::string& robotNodeName,
635 const Eigen::Vector3f& vel)
649 newPose.setIdentity();
650 newPose.block<3, 1>(0, 3) = vel;
651 auto globalPose = kinRobot->getGlobalPose();
652 globalPose(0, 3) = 0;
653 globalPose(1, 3) = 0;
654 globalPose(2, 3) = 0;
662 const std::string& robotNodeName,
663 const Eigen::Vector3f& vel)
677 newPose.setIdentity();
678 newPose.block<3, 1>(0, 3) = vel;
680 globalPose(0, 3) = 0;
681 globalPose(1, 3) = 0;
682 globalPose(2, 3) = 0;
692 std::map<std::string, KinematicRobotInfo>::iterator it =
kinematicRobots.begin();
695 if (it->first == robotName)
717 return (kinRobot->hasRobotNode(robotNodeName));
734 std::map<std::string, RobotPtr>
737 std::map<std::string, RobotPtr> result;
742 result[elem.first] = (info.
robot);
751 std::vector<VirtualRobot::SceneObjectPtr>::const_iterator it;
755 if ((*it)->getName() == objectName)
778 return o->getGlobalPose();
783 const std::string& robotNodeName,
784 const std::string& objectName)
787 ARMARX_INFO_S <<
"Object released, robot " << robotName <<
", node:" << robotNodeName
788 <<
", object:" << objectName;
796 if (!kinRobot->hasRobotNode(robotNodeName))
809 RobotNodePtr rn = kinRobot->getRobotNode(objectName);
816 bool res = RobotFactory::detach(kinRobot, rn);
822 const std::string& robotNodeName,
823 const std::string& objectName,
827 ARMARX_INFO_S <<
"Object grasped, robot " << robotName <<
", node:" << robotNodeName
828 <<
", object:" << objectName;
833 if (ao.robotName == robotName && ao.robotNodeName == robotNodeName &&
834 ao.objectName == objectName)
847 if (!kinRobot->hasRobotNode(robotNodeName))
853 RobotNodePtr rn = kinRobot->getRobotNode(robotNodeName);
863 storeLocalTransform = rn->toLocalCoordinateSystem(o->getGlobalPose());
864 bool res = RobotFactory::attach(kinRobot, o, rn, storeLocalTransform);
913 ARMARX_ERROR_S <<
"More than robot with identical name is currently not supported!";
926 RobotPtr r = robot->clone(robot->getName());
929 r->setThreadsafe(
false);
930 r->setUpdateVisualization(
false);
931 r->setPropagatingJointValuesEnabled(
false);
942 const std::string& robotName,
943 const std::string& frameName)
954 RobotNodePtr rn = kinRobot->getRobotNode(frameName);
971 std::map<std::string, armarx::PoseBasePtr>& objMap)
1002 auto robotName =
data.first;
1010 std::vector<RobotNodePtr> rn = kinRobot->getRobotNodes();
1018 std::string n = i->getName();
1027 .insert(std::make_pair(
1028 i, std::make_pair(timestamp, i->getJointValue())))
1035 (timestamp - filterIt->second.first).toSecondsDouble();
1036 filterIt->second.second = i->getJointValue();
1037 filterIt->second.first = timestamp;
1053 Eigen::Vector3f linearVelocity;
1055 Eigen::Vector3f pos = kinRobot->getGlobalPose().block<3, 1>(0, 3);
1059 .insert(std::make_pair(kinRobot, std::make_pair(timestamp, pos)))
1062 linearVelocity = (pos - filterIt->second.second) /
1063 (timestamp - filterIt->second.first).toSecondsDouble();
1064 filterIt->second.second = pos;
1065 filterIt->second.first = timestamp;
1071 Eigen::Vector3f angularVelocity;
1072 angularVelocity.setZero();
1081 .insert(std::make_pair(
1082 kinRobot, std::make_pair(timestamp, kinRobot->getGlobalPose())))
1086 double deltaTimestamp = (timestamp - filterVelIt->second.first).toSecondsDouble();
1101 Rot_prev = filterVelIt->second.second.block(0, 0, 3, 3);
1102 Rot_new = kinRobot->getGlobalPose().block(0, 0, 3, 3);
1103 A = Rot_new * Rot_prev.transpose();
1104 theta = acos((
A.trace() - 1) / 2.);
1112 Omega_mat = theta * (
A -
A.transpose()) / (2 * deltaTimestamp * sin(theta));
1117 if (deltaTimestamp > 0.1)
1131 Eigen::Vector3f& linearVelocity,
1132 Eigen::Vector3f& angularVelocity)
1142 std::vector<RobotNodePtr> rn = kinRobot->getRobotNodes();
1157 std::string n = i->getName();
1214 bool objectFound =
false;
1218 if (
s.name == oEngine->getName())
1230 <<
" not in synchronized list";
1239 VirtualRobot::SceneObject::Physics::SimulationType simType)
1249 clonedObj->setUpdateVisualization(
false);
1254 std::vector<std::string>
1257 std::vector<std::string>
names;
1260 std::map<std::string, KinematicRobotInfo>::iterator it =
kinematicRobots.begin();
1263 names.push_back(it->first);
1272 SceneObject::Physics::SimulationType simType)
1286 const std::string& robotNodeName,
1287 SceneObject::Physics::SimulationType simType)
1296 RobotNodePtr rn = r->getRobotNode(robotNodeName);
1299 ARMARX_WARNING_S <<
"Could not find robot node with name " << robotNodeName;
1305 std::vector<std::string>
1308 std::vector<std::string>
names;
1314 names.push_back(o->getName());
1333 std::vector<SceneObjectPtr>::iterator it =
1378 auto startTime = IceUtil::Time::now();
1398 auto duration = (IceUtil::Time::now() - startTime).toMilliSecondsDouble();
1400 if (duration < minStepSizeMs)
1402 ARMARX_DEBUG <<
"Sim calculation took " << duration <<
" - Sleeping now for "
1404 usleep(1000 * (minStepSizeMs - duration));
1420 std::map<std::string, float> targetPos;
1422 ARMARX_DEBUG <<
"Applying velocities for static robot " << kinRob->getName();
1425 if (!kinRob->hasRobotNode(nv.first))
1430 double change = (nv.second * deltaInSeconds);
1434 double newAngle = kinRob->getRobotNode(nv.first)->getJointValue() + change;
1435 targetPos[nv.first] = (
float)newAngle;
1437 if (targetPos.size() > 0)
1440 kinRob->setJointValues(targetPos);
1447 m3 = Eigen::AngleAxisf(ri.
velRotTarget(0) * deltaInSeconds, Eigen::Vector3f::UnitX()) *
1448 Eigen::AngleAxisf(ri.
velRotTarget(1) * deltaInSeconds, Eigen::Vector3f::UnitY()) *
1449 Eigen::AngleAxisf(ri.
velRotTarget(2) * deltaInSeconds, Eigen::Vector3f::UnitZ());
1450 gp.block(0, 0, 3, 3) *= m3;
1470 if (!kinRob->hasRobotNode(o.objectName))
1476 Eigen::Matrix4f pose = kinRob->getRobotNode(o.objectName)->getGlobalPose();
1480 if (kino->getName() == o.objectName)
1482 kino->setGlobalPose(pose);
1495 std::vector<SimDynamics::DynamicsEngine::DynamicsContactInfo>
1499 return std::vector<SimDynamics::DynamicsEngine::DynamicsContactInfo>();
1502 std::vector<SceneObjectPtr>
1514 std::string textureFile = floorTexture;
1519 Eigen::Vector3f pos;
1521 Eigen::Vector3f up = Eigen::Vector3f::UnitZ();
1534 std::map<std::string, armarx::PoseBasePtr>& objMap)
1536 if (!currentObjEngine)
1541 PosePtr p(
new Pose(currentObjEngine->getGlobalPose()));
1542 objMap[currentObjEngine->getName()] = p;
1543 std::vector<SceneObjectPtr> childrenE = currentObjEngine->getChildren();
1545 for (
const auto& i : childrenE)
1581 auto newEndTime = IceUtil::Time::now();
1582 auto frameTime = newEndTime -
lastTime;
1585 return (
float)(frameTime).toMilliSecondsDouble();
1598 const std::string& robotNodeName1,
1599 const std::string& robotNodeName2)
1606 throw LocalException(
"Wrong robot name. '") << robotName <<
"'";
1609 if (!robot->hasRobotNode(robotNodeName1))
1611 throw LocalException(
"Wrong robot node name. '") << robotNodeName1 <<
"'";
1613 if (!robot->hasRobotNode(robotNodeName2))
1615 throw LocalException(
"Wrong robot node name. '") << robotNodeName2 <<
"'";
1619 Eigen::Vector3f p1, p2;
1620 auto model1 = robot->getRobotNode(robotNodeName1)->getCollisionModel();
1621 auto model2 = robot->getRobotNode(robotNodeName2)->getCollisionModel();
1622 float d = robot->getCollisionChecker()->calculateDistance(model1, model2, p1, p2);
1633 const std::string& robotNodeName,
1634 const std::string& worldObjectName)
1641 throw LocalException(
"Wrong robot name. '") << robotName <<
"'";
1644 if (!robot->hasRobotNode(robotNodeName))
1646 throw LocalException(
"Wrong robot node name. '") << robotNodeName <<
"'";
1651 throw LocalException(
"No object with name '") << worldObjectName <<
"'";
1653 Eigen::Vector3f p1, p2;
1655 float d = robot->getCollisionChecker()->calculateDistance(
1656 robot->getRobotNode(robotNodeName)->getCollisionModel(),
so->getCollisionModel(), p1, p2);