30 #include <IceUtil/UUID.h>
32 #include <SimoxUtility/algorithm/string/string_tools.h>
33 #include <VirtualRobot/IK/ConstrainedOptimizationIK.h>
34 #include <VirtualRobot/IK/constraints/CollisionCheckConstraint.h>
35 #include <VirtualRobot/IK/constraints/OrientationConstraint.h>
36 #include <VirtualRobot/IK/constraints/PoseConstraint.h>
37 #include <VirtualRobot/IK/constraints/PositionConstraint.h>
38 #include <VirtualRobot/IK/constraints/ReferenceConfigurationConstraint.h>
39 #include <VirtualRobot/MathTools.h>
40 #include <VirtualRobot/RobotConfig.h>
41 #include <VirtualRobot/RobotNodeSet.h>
57 #include <RobotComponents/interface/components/MotionPlanning/MotionPlanningServer.h>
61 static const DrawColor COLOR_POSE_LINE{0.5f, 1.0f, 0.5f, 0.5f};
62 static const DrawColor COLOR_POSE_POINT{0.0f, 1.0f, 0.0f, 0.5f};
63 static const DrawColor COLOR_CONFIG_LINE{1.0f, 0.5f, 0.5f, 0.5f};
64 static const DrawColor COLOR_CONFIG_POINT{1.0f, 0.0f, 0.0f, 0.5f};
65 static const DrawColor COLOR_ROBOT{0.0f, 0.586f, 0.508f, 1.0f};
67 static const float LINE_WIDTH = 5.f;
68 static const float SPHERE_SIZE = 6.f;
70 static const std::map<std::string, std::string> TCP_HAND_MAPPING{{
"TCP R",
"handright3a"},
71 {
"TCP L",
"handleft3a"},
72 {
"Hand L TCP",
"left_hand"},
73 {
"Hand R TCP",
"right_hand"}};
78 static std::atomic<int> i{0};
85 graspGeneratorName = getProperty<std::string>(
"GraspGeneratorName").getValue();
86 robotPlacementName = getProperty<std::string>(
"RobotPlacementName").getValue();
88 armarx::Split(getProperty<std::string>(
"RobotNodeSetNames").getValue(),
";");
89 reachabilitySpaceFilePaths =
90 armarx::Split(getProperty<std::string>(
"ReachabilitySpaceFilePaths").getValue(),
";");
92 globalDescriptionPosition =
new Vector3(0, 0, 0);
95 for (
auto& path : reachabilitySpaceFilePaths)
97 std::string packageName = std::filesystem::path{path}.begin()->string();
99 <<
"Path '" << path <<
"' could not be parsed correctly";
101 path =
project.getDataDir() +
"/" + path;
123 entityDrawer = getTopic<memoryx::EntityDrawerInterfacePrx>(
"DebugDrawerUpdates");
126 getProxy(gsm,
"GraspSelectionManager");
128 getProxy(pmp,
"PlannedMotionProvider");
131 getProxy(rsc,
"RobotStateComponent");
134 ikRobot = localRobot->clone();
142 if (getProperty<bool>(
"FilterUnnaturalGrasps").getValue())
144 getProxy(gsc,
"NaturalGraspFilter");
145 gsm->registerAsGraspSelectionCriterion(gsc);
149 Ice::FloatSeq boundsStrings =
150 Split<float>(getProperty<std::string>(
"PlanningBoundingBox").getValue(),
",");
152 planningBoundingBox.min.e0 = boundsStrings.at(0);
153 planningBoundingBox.min.e1 = boundsStrings.at(1);
154 planningBoundingBox.min.e2 = boundsStrings.at(2);
155 planningBoundingBox.max.e0 = boundsStrings.at(3);
156 planningBoundingBox.max.e1 = boundsStrings.at(4);
157 planningBoundingBox.max.e2 = boundsStrings.at(5);
201 GraspingManager::generateGrasps(
const std::string& objectInstanceEntityId)
203 setNextStepDescription(
"Generating grasps");
205 GeneratedGraspList grasps = gg->generateGrasps(objectInstanceEntityId);
206 std::sort(grasps.begin(),
208 [](
const GeneratedGrasp& l,
const GeneratedGrasp& r) { return l.score < r.score; });
218 std::pair<std::string, Ice::StringSeq>
221 float visuSlowdownFactor = getProperty<float>(
"VisualizationSlowdownFactor").getValue();
222 std::pair<std::string, Ice::StringSeq> result;
223 if (visuSlowdownFactor <= 0 || !getProperty<bool>(
"EnableVisualization"))
227 auto tcpName = localRobot->getEndEffector(grasp.eefName)->getTcp()->getName();
228 if (!TCP_HAND_MAPPING.count(tcpName))
232 auto handName = TCP_HAND_MAPPING.at(tcpName);
233 auto objClass = prior->getObjectClassesSegment()->getObjectClassByName(handName);
236 usleep(500000 * visuSlowdownFactor);
238 result.first =
"GeneratedGrasps";
239 result.second = {
"GraspCandidate" + handName +
to_string(
id),
240 "GraspCandidatePrepose" + handName +
to_string(
id)};
241 entityDrawer->setObjectVisu(
242 "GeneratedGrasps", result.second.at(0), objClass, grasp.framedPose);
243 entityDrawer->setObjectVisu(
244 "GeneratedGrasps", result.second.at(1), objClass, grasp.framedPrePose);
246 entityDrawer->updateObjectColor(
"GeneratedGrasps", result.second.at(0), color);
247 auto darkerColor = color;
248 darkerColor.r *= 0.6f;
249 darkerColor.g *= 0.6f;
250 darkerColor.b *= 0.6f;
251 entityDrawer->updateObjectColor(
"GeneratedGrasps", result.second.at(1), darkerColor);
255 ARMARX_INFO <<
"Could not find hand with name " << handName <<
" in priorknowledge";
261 GraspingManager::createCSpace()
266 if (getProperty<bool>(
"UseVoxelGridCSpace").getValue())
270 getProperty<std::string>(
"VoxelGridProviderName").getValue()),
277 cspace->addObjectsFromWorkingMemory(wm);
278 cspace->setStationaryObjectMargin(50);
279 AgentPlanningInformation agentData;
280 agentData.agentProjectNames = rsc->getArmarXPackages();
281 agentData.agentRelativeFilePath = rsc->getRobotFilename();
283 agentData.collisionSetNames = {
284 getProperty<std::string>(
"RobotCollisionNodeSet")
286 cspace->setAgent(agentData);
287 cspace->initCollisionTest();
293 GraspingManager::filterGrasps(
const GeneratedGraspList& grasps)
295 setNextStepDescription(
"Filtering grasps");
296 float visuSlowdownFactor = getProperty<float>(
"VisualizationSlowdownFactor");
300 GeneratedGraspList filteredGrasps = gsm->filterGrasps(grasps);
301 if (filteredGrasps.empty())
303 ARMARX_WARNING <<
" Step 'filter generated grasps' filtered out all grasps";
306 if (getProperty<bool>(
"EnableVisualization"))
307 for (GeneratedGrasp& grasp : filteredGrasps)
312 if (visuSlowdownFactor > 0 && getProperty<bool>(
"EnableVisualization"))
314 usleep(2000000 * visuSlowdownFactor);
317 entityDrawer->removeLayer(
"GeneratedGrasps");
319 return filteredGrasps;
322 GraspingPlacementList
323 GraspingManager::filterPlacements(
const GraspingPlacementList& placements)
326 GraspingPlacementList filteredPlacements = gsm->filterPlacements(placements);
327 if (filteredPlacements.empty())
329 ARMARX_WARNING <<
" Step 'filter generated grasps' filtered out all grasps";
332 return filteredPlacements;
335 GraspingPlacementList
336 GraspingManager::generateRobotPlacements(
const GeneratedGraspList& grasps)
340 GraspingPlacementList graspPlacements = rp->generateRobotPlacementsEx(grasps);
342 <<
"No placements for the robot platform were found.";
343 return graspPlacements;
347 GraspingManager::planMotion(
const MotionPlanningData& mpd)
350 <<
VAROUT(mpd.globalPoseGoal->output());
352 if (getProperty<bool>(
"EnableVisualization"))
354 entityDrawer->setPoseVisu(
355 layerName,
"MotionPlanningPlatformTargetPose", mpd.globalPoseGoal);
357 Eigen::Vector3f bbcenter;
358 bbcenter << (planningBoundingBox.max.e0 + planningBoundingBox.min.e0) / 2,
359 (planningBoundingBox.max.e1 + planningBoundingBox.min.e1) / 2,
360 (planningBoundingBox.max.e2 + planningBoundingBox.min.e2) / 2;
361 Eigen::Vector3f bbSize;
362 bbSize << planningBoundingBox.max.e0 - planningBoundingBox.min.e0,
363 planningBoundingBox.max.e1 - planningBoundingBox.min.e1,
364 planningBoundingBox.max.e2 - planningBoundingBox.min.e2;
368 if (getProperty<bool>(
"EnableVisualization"))
370 entityDrawer->setPoseVisu(
"Poses",
"StartPoseAgent", mpd.globalPoseStart);
373 auto robotFileName = rsc->getRobotFilename();
374 auto packageName = std::filesystem::path{robotFileName}.begin()->string();
375 auto axPackages = rsc->getArmarXPackages();
378 <<
"Could not determine package name from path '" << robotFileName <<
"', "
379 <<
"because the determined package name '" << packageName
380 <<
"' is not in the following list: " << axPackages;
382 SimoxCSpacePtr armCSpace = SimoxCSpacePtr::dynamicCast(cspace->clone());
388 AgentPlanningInformation agentData;
389 agentData.agentPose = mpd.globalPoseGoal;
390 agentData.agentProjectNames = rsc->getArmarXPackages();
391 agentData.agentRelativeFilePath = robotFileName;
393 agentData.kinemaicChainNames = {mpd.rnsToUse};
394 auto collisionSetMapping = getJointSetCollisionSetMapping();
395 std::string armCollisionSet = collisionSetMapping.at(mpd.rnsToUse);
397 auto robotCol = cspace->getAgentSceneObj()->getRobotNodeSet(
398 getProperty<std::string>(
"RobotCollisionNodeSet").
getValue());
399 auto robotColNodeNames = robotCol->getNodeNames();
400 agentData.collisionObjectNames =
401 getRobotNodeSetNodesWithoutAllwaysColliding(robotColNodeNames, armCollisionSet);
402 agentData.collisionSetNames = {armCollisionSet};
404 agentData.initialJointValues = localRobot->getConfig()->getRobotNodeJointValueMap();
405 for (
auto& pair : mpd.configStart)
407 agentData.initialJointValues[pair.first] = pair.second;
409 armCSpace->setAgent(agentData);
412 SimoxCSpacePtr cspacePlatform = SimoxCSpacePtr::dynamicCast(cspace->clone());
413 agentData.agentPose = mpd.globalPoseStart;
414 agentData.kinemaicChainNames = {
"PlatformPlanning"};
416 agentData.collisionObjectNames = robotColNodeNames;
417 agentData.collisionSetNames = {};
418 for (
auto& pair : collisionSetMapping)
420 if (std::find(agentData.collisionSetNames.begin(),
421 agentData.collisionSetNames.end(),
422 pair.second) == agentData.collisionSetNames.end())
424 agentData.collisionSetNames.push_back(pair.second);
425 agentData.collisionObjectNames = getRobotNodeSetNodesWithoutAllwaysColliding(
426 agentData.collisionObjectNames, pair.second);
431 cspacePlatform->setAgent(agentData);
443 auto planningResult = pmp->planMotion(armCSpace, cspacePlatform, mpd);
444 return planningResult;
448 GraspingManager::drawTrajectory(
const GraspingTrajectory& t,
float visuSlowdownFactor)
452 if (visuSlowdownFactor < 0)
458 TrajectoryPtr poseTrajectory = TrajectoryPtr::dynamicCast(t.poseTrajectory);
459 TrajectoryPtr configTrajectory = TrajectoryPtr::dynamicCast(t.configTrajectory);
464 auto robotId =
newId();
466 auto robotConfig = rsc->getSynchronizedRobot()->getConfig();
467 localRobot->setJointValues(robotConfig);
469 entityDrawer->setRobotVisu(
470 layerName, robotId, rsc->getRobotFilename(),
"RobotAPI", FullModel);
471 entityDrawer->updateRobotColor(layerName, robotId, COLOR_ROBOT);
472 entityDrawer->updateRobotPose(layerName, robotId,
new Pose(localRobot->getGlobalPose()));
473 entityDrawer->updateRobotConfig(
474 layerName, robotId, localRobot->getConfig()->getRobotNodeJointValueMap());
479 std::vector<PosePtr> poseData;
483 poseTrajectory->end(),
484 std::back_inserter(poseData),
487 Eigen::Matrix4f pose = Eigen::Matrix4f::Identity();
488 VirtualRobot::MathTools::rpy2eigen4f(0, 0, data.getPosition(2), pose);
489 pose(0, 3) = data.getPosition(0);
490 pose(1, 3) = data.getPosition(1);
492 return new Pose(pose);
496 for (
auto it = poseData.cbegin(); it != poseData.cend(); it += 1)
499 auto nextIt = std::next(it);
500 entityDrawer->setSphereVisu(layerName,
newId(), currPos, COLOR_POSE_POINT, SPHERE_SIZE);
501 usleep(30000 * visuSlowdownFactor);
502 if (nextIt != poseData.cend())
505 entityDrawer->setLineVisu(
506 layerName,
newId(), currPos, nextPos, LINE_WIDTH, COLOR_POSE_LINE);
508 pose.block<3, 1>(0, 3) = nextPos->toEigen();
509 entityDrawer->updateRobotPose(layerName, robotId, *nextIt);
510 usleep(1000000 * visuSlowdownFactor / poseData.size());
514 auto targetPose = poseData.back()->toEigen();
515 localRobot->setGlobalPose(targetPose);
517 std::vector<NameValueMap> configData;
519 auto jointLabels = configTrajectory->getDimensionNames();
521 configTrajectory->end(),
522 std::back_inserter(configData),
526 for (size_t i = 0; i < jointLabels.size(); ++i)
528 result.insert({jointLabels[i], data.getPosition(i)});
534 VirtualRobot::RobotNodeSetPtr rns = localRobot->getRobotNodeSet(t.rnsToUse);
536 const auto tcpName = rns->getTCP()->getName();
538 std::vector<Vector3Ptr> tcpPoseList;
541 std::back_inserter(tcpPoseList),
544 localRobot->setJointValues(config);
545 return new Vector3(localRobot->getRobotNode(tcpName)->getGlobalPose());
550 for (
auto it = tcpPoseList.cbegin(); it != tcpPoseList.cend();
551 it += stepSize, i += stepSize)
553 auto nextIt = std::next(it);
555 entityDrawer->setSphereVisu(
556 layerName,
newId(), currPose, COLOR_CONFIG_POINT, SPHERE_SIZE);
558 if (nextIt != tcpPoseList.cend())
560 auto nextPose = *nextIt;
561 entityDrawer->updateRobotConfig(layerName, robotId, configData.at(i));
562 entityDrawer->setLineVisu(
563 layerName,
newId(), currPose, nextPose, SPHERE_SIZE, COLOR_CONFIG_LINE);
564 usleep(1000000 * visuSlowdownFactor / tcpPoseList.size());
568 entityDrawer->updateRobotConfig(layerName, robotId, configData.back());
571 <<
"Unknown TCP '" << tcpName <<
"'";
572 auto handObjectClass =
573 prior->getObjectClassesSegment()->getObjectClassByName(TCP_HAND_MAPPING.at(tcpName));
576 localRobot->setJointValues(configData.back());
577 entityDrawer->setObjectVisu(layerName,
578 handObjectClass->getName(),
580 new Pose(localRobot->getRobotNode(tcpName)->getGlobalPose()));
581 entityDrawer->updateObjectColor(layerName, handObjectClass->getName(), COLOR_ROBOT);
590 GraspingManager::setDescriptionPositionForObject(
const std::string& objId)
594 auto objInstance = wm->getObjectInstancesSegment()->getObjectInstanceById(objId);
596 armarx::FramedPositionPtr::dynamicCast(objInstance->getPositionBase());
597 p->changeToGlobal(localRobot);
599 globalDescriptionPosition = p;
604 GraspingManager::setNextStepDescription(
const std::string& description)
607 if (getProperty<bool>(
"EnableVisualization"))
608 entityDrawer->setTextVisu(layerName,
610 "Step " +
to_string(step) +
": " + description,
611 globalDescriptionPosition,
612 DrawColor{0, 1, 0, 1},
617 GraspingManager::resetStepDescription()
620 globalDescriptionPosition =
new Vector3(0, 0, 0);
621 entityDrawer->removeTextVisu(layerName,
"stepDescription");
624 std::vector<MotionPlanningData>
625 GraspingManager::calculateIKs(
const GraspingPlacementList& graspPlacements)
655 std::vector<MotionPlanningData> mpdList;
658 Eigen::Vector3f robotPos = localRobot->getGlobalPose().block<3, 1>(0, 3);
669 for (
const GraspingPlacement& gp : graspPlacements)
671 NameValueMap currentConfig = rsc->getSynchronizedRobot()->getConfig();
675 auto desiredRobotPose =
676 PosePtr::dynamicCast(FramedPosePtr::dynamicCast(gp.robotPose)->toGlobal(localRobot));
677 auto desiredRobotPoseEigen = desiredRobotPose->toEigen();
683 auto desiredTCPPose =
684 FramedPosePtr::dynamicCast(gp.grasp.framedPose)->toGlobalEigen(localRobot);
685 auto desiredTCPPoseRelativeToRobotEigen = desiredRobotPoseEigen.inverse() * desiredTCPPose;
687 new FramedPose(desiredTCPPoseRelativeToRobotEigen,
688 localRobot->getRootNode()->getName(),
689 localRobot->getName())};
690 auto desiredTCPPrepose =
691 FramedPosePtr::dynamicCast(gp.grasp.framedPrePose)->toGlobalEigen(localRobot);
692 auto desiredTCPPreposeRelativeToRobotEigen =
693 desiredRobotPoseEigen.inverse() * desiredTCPPrepose;
695 new FramedPose(desiredTCPPreposeRelativeToRobotEigen,
696 localRobot->getRootNode()->getName(),
697 localRobot->getName())};
699 Ice::StringSeq potentialRNs;
700 auto tcpName = localRobot->getEndEffector(gp.grasp.eefName)->getTcp()->getName();
701 for (
const auto& rnsName : robotNodeSetNames)
704 <<
"Could not find RNS '" << rnsName <<
"' in RNS list of robot "
705 << localRobot->getName();
706 if (localRobot->getRobotNodeSet(rnsName)->getTCP()->getName() == tcpName)
708 potentialRNs.push_back(rnsName);
712 float visuSlowdownFactor = getProperty<float>(
"VisualizationSlowdownFactor").getValue();
715 if (potentialRNs.empty())
718 <<
"'; will not process the corresponding generated grasp...";
721 std::string rnsToUse;
724 if (getProperty<bool>(
"EnableVisualization"))
726 if (robotVisuId.empty())
728 robotVisuId =
newId();
729 entityDrawer->setRobotVisu(layerName,
731 rsc->getRobotFilename(),
732 simox::alg::join(rsc->getArmarXPackages(),
","),
734 entityDrawer->updateRobotColor(layerName, robotVisuId, COLOR_ROBOT);
736 entityDrawer->updateRobotColor(
737 layerName, robotVisuId, DrawColor{1.0, 1.0f, 1.0, 0.5});
738 entityDrawer->updateRobotPose(layerName, robotVisuId, desiredRobotPose);
739 entityDrawer->updateRobotConfig(
740 layerName, robotVisuId, localRobot->getConfig()->getRobotNodeJointValueMap());
743 << potentialRNs <<
"' is reachable";
745 for (
auto& rns : potentialRNs)
747 ikSolution = calculateSingleIK(
748 rns, gp.grasp.eefName, gp.robotPose, desiredTCPPoseRelativeToRobot,
false);
749 if (ikSolution.empty())
753 ikSolution = calculateSingleIK(
754 rns, gp.grasp.eefName, gp.robotPose, desiredTCPPreposeRelativeToRobot);
755 if (!ikSolution.empty())
762 ARMARX_INFO <<
"Could not find collision free IK for prepose!";
765 if (ikSolution.empty())
767 if (getProperty<bool>(
"EnableVisualization"))
769 usleep(1000000 * visuSlowdownFactor);
770 auto graspVisuId =
visualizeGrasp(gp.grasp, i, DrawColor{1.0, 0, 0, 1});
771 entityDrawer->updateRobotColor(
772 layerName, robotVisuId, DrawColor{1.0, 0.0f, 0.0, 1});
774 usleep(1000000 * visuSlowdownFactor);
775 for (
auto id : graspVisuId.second)
777 entityDrawer->removeObjectVisu(graspVisuId.first,
id);
788 if (getProperty<bool>(
"EnableVisualization"))
790 usleep(1000000 * visuSlowdownFactor);
791 entityDrawer->updateRobotColor(
792 layerName, robotVisuId, DrawColor{0.0, 1.0f, 0.0, 1});
793 entityDrawer->updateRobotPose(layerName, robotVisuId, desiredRobotPose);
794 entityDrawer->updateRobotConfig(layerName, robotVisuId, ikSolution);
795 usleep(2000000 * visuSlowdownFactor);
799 for (
auto it = currentConfig.begin(); it != currentConfig.end();)
801 if (ikSolution.find(it->first) == ikSolution.end())
803 it = currentConfig.erase(it);
811 for (
const auto& entry : ikSolution)
813 if (currentConfig.find(entry.first) == currentConfig.end())
815 ARMARX_INFO <<
"Current config: " << currentConfig;
818 <<
"calculated configuration contains a joint '" << entry.first
819 <<
"' whose current value is unknown";
837 << potentialRNs <<
"' not reachable";
839 if (getProperty<bool>(
"EnableVisualization"))
840 for (
auto id : graspVisuId.second)
842 entityDrawer->removeObjectVisu(graspVisuId.first,
id);
846 if (getProperty<bool>(
"EnableVisualization"))
848 entityDrawer->removeRobotVisu(layerName, robotVisuId);
854 GraspingManager::calculateSingleIK(
const std::string& robotNodeSetName,
855 const std::string& eef,
856 const PoseBasePtr& globalRobotPose,
857 const FramedPoseBasePtr& tcpPose,
858 bool checkCollisionFree)
861 auto ikRobot = cspace->getAgentSceneObj();
862 ikRobot->setConfig(localRobot->getConfig());
863 auto rns = ikRobot->getRobotNodeSet(robotNodeSetName);
864 ikRobot->setGlobalPose(PosePtr::dynamicCast(globalRobotPose)->
toEigen());
865 auto tcp = ikRobot->getEndEffector(eef)->getTcp();
866 Eigen::Matrix4f targetPose = FramedPosePtr::dynamicCast(tcpPose)->toGlobalEigen(ikRobot);
867 VirtualRobot::ConstrainedOptimizationIK ik(ikRobot, rns);
868 VirtualRobot::PoseConstraintPtr poseConstraint(
869 new VirtualRobot::PoseConstraint(ikRobot, rns, tcp, targetPose));
870 if (checkCollisionFree)
872 auto colSetEnv = cspace->getStationaryObjectSet();
873 VirtualRobot::CDManagerPtr cdm(
874 new VirtualRobot::CDManager(cspace->getCD().getCollisionChecker()));
875 VirtualRobot::SceneObjectSetPtr sosRns(
876 new VirtualRobot::SceneObjectSet(
"RNS", cspace->getCD().getCollisionChecker()));
877 std::string armCollisionSet = getJointSetCollisionSetMapping().at(robotNodeSetName);
878 ARMARX_INFO <<
"Using Arm Collision Set '" << armCollisionSet <<
"' and platform set '"
879 << getProperty<std::string>(
"RobotCollisionNodeSet").getValue() <<
"'";
880 sosRns->addSceneObjects(cspace->getAgentSceneObj()->getRobotNodeSet(armCollisionSet));
881 cdm->addCollisionModelPair(colSetEnv, sosRns);
883 VirtualRobot::SceneObjectSetPtr sosPlatform(
884 new VirtualRobot::SceneObjectSet(
"platform", cspace->getCD().getCollisionChecker()));
887 auto robotCol = cspace->getAgentSceneObj()->getRobotNodeSet(
888 getProperty<std::string>(
"RobotCollisionNodeSet").
getValue());
889 auto robotColNodeNames = robotCol->getNodeNames();
890 std::vector<std::string> remainingNodeNames =
891 getRobotNodeSetNodesWithoutAllwaysColliding(robotColNodeNames, armCollisionSet);
892 for (
const auto& colNodeName : remainingNodeNames)
894 sosPlatform->addSceneObject(cspace->getAgentSceneObj()->getRobotNode(colNodeName));
896 ARMARX_INFO <<
"RobotCollisionNodeSet contains the following nodes after filtering allways "
899 <<
VAROUT(remainingNodeNames);
901 cdm->addCollisionModelPair(sosPlatform, sosRns);
908 new VirtualRobot::PositionConstraint(ikRobot,
911 targetPose.block<3, 1>(0, 3),
912 VirtualRobot::IKSolver::CartesianSelection::All));
913 posConstraint->setOptimizationFunctionFactor(1);
916 new VirtualRobot::OrientationConstraint(ikRobot,
919 targetPose.block<3, 3>(0, 0),
920 VirtualRobot::IKSolver::CartesianSelection::All,
921 VirtualRobot::MathTools::deg2rad(2)));
922 oriConstraint->setOptimizationFunctionFactor(1000);
923 ik.addConstraint(posConstraint);
924 ik.addConstraint(oriConstraint);
928 NaturalIKResult natIKResult =
930 FramedPosePtr::dynamicCast(tcpPose)->toRootEigen(ikRobot),
932 new armarx::aron::data::dto::Dict());
933 if (!natIKResult.reached)
940 VirtualRobot::ReferenceConfigurationConstraintPtr natIKConstraint(
941 new VirtualRobot::ReferenceConfigurationConstraint(ikRobot, rns));
943 Eigen::VectorXf referenceConfig(natIKResult.jointValues.size());
944 for (
size_t i = 0; i < natIKResult.jointValues.size(); i++)
946 referenceConfig(i) = natIKResult.jointValues.at(i);
949 natIKConstraint->setReferenceConfiguration(referenceConfig);
950 ik.addConstraint(natIKConstraint);
953 if (!ik.initialize())
962 for (
size_t i = 0; i < rns->getSize(); ++i)
964 result[rns->getNode((
int)i)->getName()] = rns->getNode(i)->getJointValue();
974 else if (natIKResult.reached)
977 for (
size_t i = 0; i < rns->getSize(); ++i)
979 result[rns->getNode((
int)i)->getName()] = natIKResult.jointValues.at(i);
988 StringStringDictionary
989 GraspingManager::getJointSetCollisionSetMapping()
991 StringStringDictionary result;
992 auto propString = getProperty<std::string>(
"JointToLinkSetMapping").getValue();
994 for (
auto& pairStr : pairs)
998 result[pair.at(0)] = pair.at(1);
1003 GraspingTrajectoryList
1004 GraspingManager::generateGraspingTrajectoryListForGraspListInternal(
1005 const GeneratedGraspList& grasps)
1007 std::vector<MotionPlanningData> mpdList = generateIKsInternal(grasps);
1008 if (mpdList.empty())
1010 ARMARX_WARNING <<
"Step 'check reachability / solve IK' produced no valid results";
1011 return GraspingTrajectoryList{};
1017 GraspingTrajectoryList result;
1018 setNextStepDescription(
"Planning motion");
1020 for (
auto& graspingData : mpdList)
1024 GraspingTrajectory gt = planMotion(graspingData);
1026 result.push_back(gt);
1032 if (!result.empty())
1042 const Ice::Current&)
1047 resetStepDescription();
1048 entityDrawer->clearLayer(layerName);
1051 setDescriptionPositionForObject(objectInstanceEntityId);
1053 float visuSlowdownFactor = getProperty<float>(
"VisualizationSlowdownFactor");
1055 auto grasps = generateGrasps(objectInstanceEntityId);
1057 auto result = generateGraspingTrajectoryListForGraspListInternal(grasps);
1060 throw LocalException(
"Could not find any valid solution");
1063 if (getProperty<bool>(
"EnableVisualization"))
1065 drawTrajectory(result.front(), visuSlowdownFactor);
1066 sleep(2 * visuSlowdownFactor);
1067 entityDrawer->removeLayer(layerName);
1069 return result.front();
1072 GraspingTrajectoryList
1074 const Ice::Current&)
1079 resetStepDescription();
1080 entityDrawer->clearLayer(layerName);
1083 setDescriptionPositionForObject(objectInstanceEntityId);
1085 auto grasps = generateGrasps(objectInstanceEntityId);
1086 return generateGraspingTrajectoryListForGraspListInternal(grasps);
1089 GraspingTrajectoryList
1091 const Ice::Current&)
1094 return GraspingTrajectoryList{};
1098 resetStepDescription();
1099 entityDrawer->clearLayer(layerName);
1103 FramedPosePtr::dynamicCast(grasps.front().framedPose)->toGlobal(localRobot);
1104 pose->position->z += 400;
1105 globalDescriptionPosition = Vector3Ptr::dynamicCast(pose->position);
1107 auto result = generateGraspingTrajectoryListForGraspListInternal(grasps);
1110 throw LocalException(
"Could not find any valid solution");
1113 if (getProperty<bool>(
"EnableVisualization"))
1115 float visuSlowdownFactor = getProperty<float>(
"VisualizationSlowdownFactor");
1116 drawTrajectory(result.front(), visuSlowdownFactor);
1117 sleep(2 * visuSlowdownFactor);
1118 entityDrawer->removeLayer(layerName);
1125 float visuSlowdownFactor,
1126 const Ice::Current&)
1128 drawTrajectory(trajectory, visuSlowdownFactor);
1129 sleep(2 * visuSlowdownFactor);
1130 entityDrawer->removeLayer(layerName);
1133 MotionPlanningDataList
1139 resetStepDescription();
1140 entityDrawer->clearLayer(layerName);
1143 setDescriptionPositionForObject(objectInstanceEntityId);
1144 auto grasps = generateGrasps(objectInstanceEntityId);
1145 return generateIKsInternal(grasps);
1151 GeneratedGraspList grasps = gg->generateGraspsByObjectName(objectName);
1152 std::sort(grasps.begin(),
1154 [](
const GeneratedGrasp& l,
const GeneratedGrasp& r) { return l.score < r.score; });
1158 MotionPlanningDataList
1159 GraspingManager::generateIKsInternal(
const GeneratedGraspList& grasps)
1161 ikRobot->setConfig(localRobot->getConfig());
1162 auto filteredGrasps = filterGrasps(grasps);
1164 GraspingPlacementList graspPlacements;
1166 cspace = createCSpace();
1168 setNextStepDescription(
"Robot placement");
1169 const float maxDistance2D = getProperty<float>(
"MaxDistanceForDirectGrasp").getValue();
1170 GeneratedGraspList notDirectGrasps;
1171 for (
const auto& g : filteredGrasps)
1173 const Eigen::Vector3f position =
1174 FramedPosePtr::dynamicCast(g.framedPose)->getPosition()->toGlobalEigen(localRobot);
1175 const float distance2D =
1176 (position.head(2) - localRobot->getGlobalPose().block<2, 1>(0, 3)).norm();
1179 if (distance2D < maxDistance2D)
1181 GraspingPlacement pl;
1184 graspPlacements.push_back(pl);
1188 notDirectGrasps.push_back(g);
1191 if (!notDirectGrasps.empty())
1193 auto graspPlacementsNotDirect = generateRobotPlacements(notDirectGrasps);
1194 graspPlacements.insert(graspPlacements.end(),
1195 graspPlacementsNotDirect.begin(),
1196 graspPlacementsNotDirect.end());
1198 GraspingPlacementList filteredGraspPlacements = filterPlacements(graspPlacements);
1200 ARMARX_VERBOSE <<
"Step: check reachability / solve IK from current pose";
1201 setNextStepDescription(
"Calculating IK");
1202 return calculateIKs(filteredGraspPlacements);
1205 std::vector<std::string>
1206 GraspingManager::getRobotNodeSetNodesWithoutAllwaysColliding(
1207 const std::vector<std::string>& robotColNodeNames,
1208 const std::string& armCollisionSet)
1210 const auto rnsColNames =
1211 cspace->getAgentSceneObj()->getRobotNodeSet(armCollisionSet)->getNodeNames();
1212 std::set<std::string> rnsColNamesIncludingIgnore;
1213 rnsColNamesIncludingIgnore.insert(rnsColNames.begin(), rnsColNames.end());
1214 for (
const auto& rnsNodeName : rnsColNames)
1216 const auto rn = cspace->getAgentSceneObj()->getRobotNode(rnsNodeName);
1217 const auto ignored = rn->getIgnoredCollisionModels();
1218 rnsColNamesIncludingIgnore.insert(ignored.begin(), ignored.end());
1219 rnsColNamesIncludingIgnore.insert(rn->getParent()->getName());
1222 std::vector<std::string> remainingNodeNames;
1223 for (
const auto& colNodeName : robotColNodeNames)
1225 const auto it = rnsColNamesIncludingIgnore.find(colNodeName);
1226 if (it == rnsColNamesIncludingIgnore.end())
1228 remainingNodeNames.push_back(colNodeName);
1231 return remainingNodeNames;