36 #include <RobotComponents/interface/components/MotionPlanning/MotionPlanningServer.h>
37 #include <IceUtil/UUID.h>
40 #include <VirtualRobot/IK/ConstrainedOptimizationIK.h>
41 #include <VirtualRobot/IK/constraints/CollisionCheckConstraint.h>
42 #include <VirtualRobot/IK/constraints/OrientationConstraint.h>
43 #include <VirtualRobot/IK/constraints/PoseConstraint.h>
44 #include <VirtualRobot/IK/constraints/PositionConstraint.h>
45 #include <VirtualRobot/IK/constraints/ReferenceConfigurationConstraint.h>
47 #include <VirtualRobot/RobotConfig.h>
52 #include <SimoxUtility/algorithm/string/string_tools.h>
56 static const DrawColor COLOR_POSE_LINE
58 0.5f, 1.0f, 0.5f, 0.5f
60 static const DrawColor COLOR_POSE_POINT
62 0.0f, 1.0f, 0.0f, 0.5f
64 static const DrawColor COLOR_CONFIG_LINE
66 1.0f, 0.5f, 0.5f, 0.5f
68 static const DrawColor COLOR_CONFIG_POINT
70 1.0f, 0.0f, 0.0f, 0.5f
72 static const DrawColor COLOR_ROBOT
74 0.0f, 0.586f, 0.508f, 1.0f
77 static const float LINE_WIDTH = 5.f;
78 static const float SPHERE_SIZE = 6.f;
80 static const std::map<std::string, std::string> TCP_HAND_MAPPING
82 {
"TCP R",
"handright3a"},
83 {
"TCP L",
"handleft3a"},
84 {
"Hand L TCP",
"left_hand"},
85 {
"Hand R TCP",
"right_hand"}
92 static std::atomic<int> i {0};
98 graspGeneratorName = getProperty<std::string>(
"GraspGeneratorName").getValue();
99 robotPlacementName = getProperty<std::string>(
"RobotPlacementName").getValue();
100 robotNodeSetNames =
armarx::Split(getProperty<std::string>(
"RobotNodeSetNames").getValue(),
";");
101 reachabilitySpaceFilePaths =
armarx::Split(getProperty<std::string>(
"ReachabilitySpaceFilePaths").getValue(),
";");
103 globalDescriptionPosition =
new Vector3(0, 0, 0);
106 for (
auto& path : reachabilitySpaceFilePaths)
108 std::string packageName = std::filesystem::path {path} .begin()->string();
111 path =
project.getDataDir() +
"/" + path;
132 entityDrawer = getTopic<memoryx::EntityDrawerInterfacePrx>(
"DebugDrawerUpdates");
135 getProxy(gsm,
"GraspSelectionManager");
137 getProxy(pmp,
"PlannedMotionProvider");
140 getProxy(rsc,
"RobotStateComponent");
142 ikRobot = localRobot->clone();
150 if (getProperty<bool>(
"FilterUnnaturalGrasps").getValue())
152 getProxy(gsc,
"NaturalGraspFilter");
153 gsm->registerAsGraspSelectionCriterion(gsc);
157 Ice::FloatSeq boundsStrings = Split<float>(getProperty<std::string>(
"PlanningBoundingBox").getValue(),
",");
159 planningBoundingBox.min.e0 = boundsStrings.at(0);
160 planningBoundingBox.min.e1 = boundsStrings.at(1);
161 planningBoundingBox.min.e2 = boundsStrings.at(2);
162 planningBoundingBox.max.e0 = boundsStrings.at(3);
163 planningBoundingBox.max.e1 = boundsStrings.at(4);
164 planningBoundingBox.max.e2 = boundsStrings.at(5);
206 GeneratedGraspList GraspingManager::generateGrasps(
const std::string& objectInstanceEntityId)
208 setNextStepDescription(
"Generating grasps");
210 GeneratedGraspList grasps = gg->generateGrasps(objectInstanceEntityId);
211 std::sort(grasps.begin(), grasps.end(), [](
const GeneratedGrasp & l,
const GeneratedGrasp & r)
213 return l.score < r.score;
226 float visuSlowdownFactor = getProperty<float>(
"VisualizationSlowdownFactor").getValue();
227 std::pair<std::string, Ice::StringSeq> result;
228 if (visuSlowdownFactor <= 0 || !getProperty<bool>(
"EnableVisualization"))
232 auto tcpName = localRobot->getEndEffector(grasp.eefName)->getTcp()->getName();
233 if (!TCP_HAND_MAPPING.count(tcpName))
237 auto handName = TCP_HAND_MAPPING.at(tcpName);
238 auto objClass = prior->getObjectClassesSegment()->getObjectClassByName(handName);
241 usleep(500000 * visuSlowdownFactor);
243 result.first =
"GeneratedGrasps";
244 result.second = {
"GraspCandidate" + handName +
to_string(
id),
"GraspCandidatePrepose" + handName +
to_string(
id)};
245 entityDrawer->setObjectVisu(
"GeneratedGrasps", result.second.at(0), objClass, grasp.framedPose);
246 entityDrawer->setObjectVisu(
"GeneratedGrasps", result.second.at(1), objClass, grasp.framedPrePose);
248 entityDrawer->updateObjectColor(
"GeneratedGrasps", result.second.at(0), color);
249 auto darkerColor = color;
250 darkerColor.r *= 0.6f;
251 darkerColor.g *= 0.6f;
252 darkerColor.b *= 0.6f;
253 entityDrawer->updateObjectColor(
"GeneratedGrasps", result.second.at(1), darkerColor);
258 ARMARX_INFO <<
"Could not find hand with name " << handName <<
" in priorknowledge";
268 if (getProperty<bool>(
"UseVoxelGridCSpace").getValue())
270 cspace =
new VoxelGridCSpace(getProxy<visionx::VoxelGridProviderInterfacePrx>(getProperty<std::string>(
"VoxelGridProviderName").getValue()), cs);
276 cspace->addObjectsFromWorkingMemory(wm);
277 cspace->setStationaryObjectMargin(50);
278 AgentPlanningInformation agentData;
279 agentData.agentProjectNames = rsc->getArmarXPackages();
280 agentData.agentRelativeFilePath = rsc->getRobotFilename();
282 agentData.collisionSetNames = {getProperty<std::string>(
"RobotCollisionNodeSet").getValue()};
283 cspace->setAgent(agentData);
284 cspace->initCollisionTest();
289 GeneratedGraspList GraspingManager::filterGrasps(
const GeneratedGraspList& grasps)
291 setNextStepDescription(
"Filtering grasps");
292 float visuSlowdownFactor = getProperty<float>(
"VisualizationSlowdownFactor");
296 GeneratedGraspList filteredGrasps = gsm->filterGrasps(grasps);
297 if (filteredGrasps.empty())
299 ARMARX_WARNING <<
" Step 'filter generated grasps' filtered out all grasps";
302 if (getProperty<bool>(
"EnableVisualization"))
303 for (GeneratedGrasp& grasp : filteredGrasps)
308 if (visuSlowdownFactor > 0 && getProperty<bool>(
"EnableVisualization"))
310 usleep(2000000 * visuSlowdownFactor);
313 entityDrawer->removeLayer(
"GeneratedGrasps");
315 return filteredGrasps;
318 GraspingPlacementList GraspingManager::filterPlacements(
const GraspingPlacementList& placements)
321 GraspingPlacementList filteredPlacements = gsm->filterPlacements(placements);
322 if (filteredPlacements.empty())
324 ARMARX_WARNING <<
" Step 'filter generated grasps' filtered out all grasps";
327 return filteredPlacements;
331 GraspingPlacementList GraspingManager::generateRobotPlacements(
const GeneratedGraspList& grasps)
335 GraspingPlacementList graspPlacements = rp->generateRobotPlacementsEx(grasps);
337 return graspPlacements;
340 GraspingTrajectory GraspingManager::planMotion(
const MotionPlanningData& mpd)
344 if (getProperty<bool>(
"EnableVisualization"))
346 entityDrawer->setPoseVisu(layerName,
"MotionPlanningPlatformTargetPose", mpd.globalPoseGoal);
348 Eigen::Vector3f bbcenter;
349 bbcenter << (planningBoundingBox.max.e0 + planningBoundingBox.min.e0) / 2,
350 (planningBoundingBox.max.e1 + planningBoundingBox.min.e1) / 2,
351 (planningBoundingBox.max.e2 + planningBoundingBox.min.e2) / 2;
352 Eigen::Vector3f bbSize;
353 bbSize << planningBoundingBox.max.e0 - planningBoundingBox.min.e0,
354 planningBoundingBox.max.e1 - planningBoundingBox.min.e1,
355 planningBoundingBox.max.e2 - planningBoundingBox.min.e2;
359 if (getProperty<bool>(
"EnableVisualization"))
361 entityDrawer->setPoseVisu(
"Poses",
"StartPoseAgent", mpd.globalPoseStart);
364 auto robotFileName = rsc->getRobotFilename();
365 auto packageName = std::filesystem::path {robotFileName} .begin()->string();
366 auto axPackages = rsc->getArmarXPackages();
367 ARMARX_CHECK_EXPRESSION(std::find(axPackages.cbegin(), axPackages.cend(), packageName) != axPackages.cend()) <<
"Could not determine package name from path '" << robotFileName <<
"', "
368 <<
"because the determined package name '" << packageName <<
"' is not in the following list: " << axPackages;
370 SimoxCSpacePtr armCSpace = SimoxCSpacePtr::dynamicCast(cspace->clone());
376 AgentPlanningInformation agentData;
377 agentData.agentPose = mpd.globalPoseGoal;
378 agentData.agentProjectNames = rsc->getArmarXPackages();
379 agentData.agentRelativeFilePath = robotFileName;
381 agentData.kinemaicChainNames = {mpd.rnsToUse};
382 auto collisionSetMapping = getJointSetCollisionSetMapping();
383 std::string armCollisionSet = collisionSetMapping.at(mpd.rnsToUse);
385 auto robotCol = cspace->getAgentSceneObj()->getRobotNodeSet(getProperty<std::string>(
"RobotCollisionNodeSet").
getValue());
386 auto robotColNodeNames = robotCol->getNodeNames();
387 agentData.collisionObjectNames = getRobotNodeSetNodesWithoutAllwaysColliding(robotColNodeNames, armCollisionSet);
388 agentData.collisionSetNames = {armCollisionSet};
390 agentData.initialJointValues = localRobot->getConfig()->getRobotNodeJointValueMap();
391 for (
auto& pair : mpd.configStart)
393 agentData.initialJointValues[pair.first] = pair.second;
395 armCSpace->setAgent(agentData);
398 SimoxCSpacePtr cspacePlatform = SimoxCSpacePtr::dynamicCast(cspace->clone());
399 agentData.agentPose = mpd.globalPoseStart;
400 agentData.kinemaicChainNames = {
"PlatformPlanning"};
402 agentData.collisionObjectNames = robotColNodeNames;
403 agentData.collisionSetNames = {};
404 for (
auto& pair : collisionSetMapping)
406 if (std::find(agentData.collisionSetNames.begin(), agentData.collisionSetNames.end(), pair.second) == agentData.collisionSetNames.end())
408 agentData.collisionSetNames.push_back(pair.second);
409 agentData.collisionObjectNames = getRobotNodeSetNodesWithoutAllwaysColliding(agentData.collisionObjectNames, pair.second);
414 cspacePlatform->setAgent(agentData);
426 auto planningResult = pmp->planMotion(armCSpace, cspacePlatform, mpd);
427 return planningResult;
432 void GraspingManager::drawTrajectory(
const GraspingTrajectory& t,
float visuSlowdownFactor)
436 if (visuSlowdownFactor < 0)
442 TrajectoryPtr poseTrajectory = TrajectoryPtr::dynamicCast(t.poseTrajectory);
443 TrajectoryPtr configTrajectory = TrajectoryPtr::dynamicCast(t.configTrajectory);
448 auto robotId =
newId();
450 auto robotConfig = rsc->getSynchronizedRobot()->getConfig();
451 localRobot->setJointValues(robotConfig);
453 entityDrawer->setRobotVisu(layerName, robotId, rsc->getRobotFilename(),
"RobotAPI", FullModel);
454 entityDrawer->updateRobotColor(layerName, robotId, COLOR_ROBOT);
455 entityDrawer->updateRobotPose(layerName, robotId,
new Pose(localRobot->getGlobalPose()));
456 entityDrawer->updateRobotConfig(layerName, robotId, localRobot->getConfig()->getRobotNodeJointValueMap());
461 std::vector<PosePtr> poseData;
466 Eigen::Matrix4f pose = Eigen::Matrix4f::Identity();
467 VirtualRobot::MathTools::rpy2eigen4f(0, 0, data.getPosition(2), pose);
468 pose(0, 3) = data.getPosition(0);
469 pose(1, 3) = data.getPosition(1);
471 return new Pose(pose);
475 for (
auto it = poseData.cbegin(); it != poseData.cend(); it += 1)
478 auto nextIt = std::next(it);
479 entityDrawer->setSphereVisu(layerName,
newId(), currPos, COLOR_POSE_POINT, SPHERE_SIZE);
480 usleep(30000 * visuSlowdownFactor);
481 if (nextIt != poseData.cend())
484 entityDrawer->setLineVisu(layerName,
newId(), currPos, nextPos, LINE_WIDTH, COLOR_POSE_LINE);
486 pose.block<3, 1>(0, 3) = nextPos->toEigen();
487 entityDrawer->updateRobotPose(layerName, robotId, *nextIt);
488 usleep(1000000 * visuSlowdownFactor / poseData.size());
492 auto targetPose = poseData.back()->toEigen();
493 localRobot->setGlobalPose(targetPose);
495 std::vector<NameValueMap> configData;
497 auto jointLabels = configTrajectory->getDimensionNames();
501 for (size_t i = 0; i < jointLabels.size(); ++i)
503 result.insert({jointLabels[i], data.getPosition(i)});
510 VirtualRobot::RobotNodeSetPtr rns = localRobot->getRobotNodeSet(t.rnsToUse);
512 const auto tcpName = rns->getTCP()->getName();
514 std::vector<Vector3Ptr> tcpPoseList;
517 localRobot->setJointValues(config);
518 return new Vector3(localRobot->getRobotNode(tcpName)->getGlobalPose());
523 for (
auto it = tcpPoseList.cbegin(); it != tcpPoseList.cend(); it += stepSize, i += stepSize)
525 auto nextIt = std::next(it);
527 entityDrawer->setSphereVisu(layerName,
newId(), currPose, COLOR_CONFIG_POINT, SPHERE_SIZE);
529 if (nextIt != tcpPoseList.cend())
531 auto nextPose = *nextIt;
532 entityDrawer->updateRobotConfig(layerName, robotId, configData.at(i));
533 entityDrawer->setLineVisu(layerName,
newId(), currPose, nextPose, SPHERE_SIZE, COLOR_CONFIG_LINE);
534 usleep(1000000 * visuSlowdownFactor / tcpPoseList.size());
538 entityDrawer->updateRobotConfig(layerName, robotId, configData.back());
540 ARMARX_CHECK_EXPRESSION(TCP_HAND_MAPPING.find(tcpName) != TCP_HAND_MAPPING.end()) <<
"Unknown TCP '" << tcpName <<
"'";
541 auto handObjectClass = prior->getObjectClassesSegment()->getObjectClassByName(TCP_HAND_MAPPING.at(tcpName));
544 localRobot->setJointValues(configData.back());
545 entityDrawer->setObjectVisu(layerName, handObjectClass->getName(), handObjectClass,
new Pose(localRobot->getRobotNode(tcpName)->getGlobalPose()));
546 entityDrawer->updateObjectColor(layerName, handObjectClass->getName(), COLOR_ROBOT);
554 void GraspingManager::setDescriptionPositionForObject(
const std::string& objId)
558 auto objInstance = wm->getObjectInstancesSegment()->getObjectInstanceById(objId);
559 FramedPositionPtr p = armarx::FramedPositionPtr::dynamicCast(objInstance->getPositionBase());
560 p->changeToGlobal(localRobot);
562 globalDescriptionPosition = p;
566 void GraspingManager::setNextStepDescription(
const std::string& description)
569 if (getProperty<bool>(
"EnableVisualization"))
570 entityDrawer->setTextVisu(layerName,
"stepDescription",
"Step " +
to_string(step) +
": " + description, globalDescriptionPosition, DrawColor {0, 1, 0, 1}, 15);
573 void GraspingManager::resetStepDescription()
576 globalDescriptionPosition =
new Vector3(0, 0, 0);
577 entityDrawer->removeTextVisu(layerName,
"stepDescription");
582 std::vector<MotionPlanningData> GraspingManager::calculateIKs(
const GraspingPlacementList& graspPlacements)
612 std::vector<MotionPlanningData>mpdList;
615 Eigen::Vector3f robotPos = localRobot->getGlobalPose().block<3, 1>(0, 3);
626 for (
const GraspingPlacement& gp : graspPlacements)
628 NameValueMap currentConfig = rsc->getSynchronizedRobot()->getConfig();
632 auto desiredRobotPose = PosePtr::dynamicCast(FramedPosePtr::dynamicCast(gp.robotPose)->toGlobal(localRobot));
633 auto desiredRobotPoseEigen = desiredRobotPose->toEigen();
639 auto desiredTCPPose = FramedPosePtr::dynamicCast(gp.grasp.framedPose)->toGlobalEigen(localRobot);
640 auto desiredTCPPoseRelativeToRobotEigen = desiredRobotPoseEigen.inverse() * desiredTCPPose;
641 FramedPosePtr desiredTCPPoseRelativeToRobot {
new FramedPose(desiredTCPPoseRelativeToRobotEigen, localRobot->getRootNode()->getName(), localRobot->getName())};
642 auto desiredTCPPrepose = FramedPosePtr::dynamicCast(gp.grasp.framedPrePose)->toGlobalEigen(localRobot);
643 auto desiredTCPPreposeRelativeToRobotEigen = desiredRobotPoseEigen.inverse() * desiredTCPPrepose;
644 FramedPosePtr desiredTCPPreposeRelativeToRobot {
new FramedPose(desiredTCPPreposeRelativeToRobotEigen, localRobot->getRootNode()->getName(), localRobot->getName())};
646 Ice::StringSeq potentialRNs;
647 auto tcpName = localRobot->getEndEffector(gp.grasp.eefName)->getTcp()->getName();
648 for (
const auto& rnsName : robotNodeSetNames)
650 ARMARX_CHECK_EXPRESSION(localRobot->hasRobotNodeSet(rnsName)) <<
"Could not find RNS '" << rnsName <<
"' in RNS list of robot " << localRobot->getName();
651 if (localRobot->getRobotNodeSet(rnsName)->getTCP()->getName() == tcpName)
653 potentialRNs.push_back(rnsName);
657 float visuSlowdownFactor = getProperty<float>(
"VisualizationSlowdownFactor").getValue();
660 if (potentialRNs.empty())
662 ARMARX_WARNING <<
"Could not find RNS with tcp '" << tcpName <<
"'; will not process the corresponding generated grasp...";
665 std::string rnsToUse;
668 if (getProperty<bool>(
"EnableVisualization"))
670 if (robotVisuId.empty())
672 robotVisuId =
newId();
673 entityDrawer->setRobotVisu(layerName, robotVisuId, rsc->getRobotFilename(), simox::alg::join(rsc->getArmarXPackages(),
","), FullModel);
674 entityDrawer->updateRobotColor(layerName, robotVisuId, COLOR_ROBOT);
676 entityDrawer->updateRobotColor(layerName, robotVisuId, DrawColor {1.0, 1.0f, 1.0, 0.5});
677 entityDrawer->updateRobotPose(layerName, robotVisuId, desiredRobotPose);
678 entityDrawer->updateRobotConfig(layerName, robotVisuId, localRobot->getConfig()->getRobotNodeJointValueMap());
680 ARMARX_VERBOSE <<
"Pose " <<
VAROUT(*desiredTCPPoseRelativeToRobot) <<
" with RNS '" << potentialRNs <<
"' is reachable";
682 for (
auto& rns : potentialRNs)
684 ikSolution = calculateSingleIK(rns, gp.grasp.eefName, gp.robotPose, desiredTCPPoseRelativeToRobot,
false);
685 if (ikSolution.empty())
689 ikSolution = calculateSingleIK(rns, gp.grasp.eefName, gp.robotPose, desiredTCPPreposeRelativeToRobot);
690 if (!ikSolution.empty())
697 ARMARX_INFO <<
"Could not find collision free IK for prepose!";
700 if (ikSolution.empty())
702 if (getProperty<bool>(
"EnableVisualization"))
704 usleep(1000000 * visuSlowdownFactor);
705 auto graspVisuId =
visualizeGrasp(gp.grasp, i, DrawColor {1.0, 0, 0, 1});
706 entityDrawer->updateRobotColor(layerName, robotVisuId, DrawColor {1.0, 0.0f, 0.0, 1});
708 usleep(1000000 * visuSlowdownFactor);
709 for (
auto id : graspVisuId.second)
711 entityDrawer->removeObjectVisu(graspVisuId.first,
id);
722 if (getProperty<bool>(
"EnableVisualization"))
724 usleep(1000000 * visuSlowdownFactor);
725 entityDrawer->updateRobotColor(layerName, robotVisuId, DrawColor {0.0, 1.0f, 0.0, 1});
726 entityDrawer->updateRobotPose(layerName, robotVisuId, desiredRobotPose);
727 entityDrawer->updateRobotConfig(layerName, robotVisuId, ikSolution);
728 usleep(2000000 * visuSlowdownFactor);
732 for (
auto it = currentConfig.begin(); it != currentConfig.end();)
734 if (ikSolution.find(it->first) == ikSolution.end())
736 it = currentConfig.erase(it);
744 for (
const auto& entry : ikSolution)
746 if (currentConfig.find(entry.first) == currentConfig.end())
748 ARMARX_INFO <<
"Current config: " << currentConfig;
751 "calculated configuration contains a joint '" << entry.first <<
"' whose current value is unknown";
759 desiredRobotPose, currentConfig, ikSolution, rnsToUse, gp.grasp.eefName,
766 ARMARX_VERBOSE <<
"Pose " <<
VAROUT(*desiredTCPPoseRelativeToRobot) <<
" with RNS '" << potentialRNs <<
"' not reachable";
768 if (getProperty<bool>(
"EnableVisualization"))
769 for (
auto id : graspVisuId.second)
771 entityDrawer->removeObjectVisu(graspVisuId.first,
id);
775 if (getProperty<bool>(
"EnableVisualization"))
777 entityDrawer->removeRobotVisu(layerName, robotVisuId);
782 NameValueMap GraspingManager::calculateSingleIK(
const std::string& robotNodeSetName,
const std::string& eef,
const PoseBasePtr& globalRobotPose,
const FramedPoseBasePtr& tcpPose,
bool checkCollisionFree)
785 auto ikRobot = cspace->getAgentSceneObj();
786 ikRobot->setConfig(localRobot->getConfig());
787 auto rns = ikRobot->getRobotNodeSet(robotNodeSetName);
788 ikRobot->setGlobalPose(PosePtr::dynamicCast(globalRobotPose)->
toEigen());
789 auto tcp = ikRobot->getEndEffector(eef)->getTcp();
790 Eigen::Matrix4f targetPose = FramedPosePtr::dynamicCast(tcpPose)->toGlobalEigen(ikRobot);
791 VirtualRobot::ConstrainedOptimizationIK ik(ikRobot, rns);
792 VirtualRobot::PoseConstraintPtr poseConstraint(
new VirtualRobot::PoseConstraint(
793 ikRobot, rns, tcp, targetPose
795 if (checkCollisionFree)
797 auto colSetEnv = cspace->getStationaryObjectSet();
798 VirtualRobot::CDManagerPtr cdm(
new VirtualRobot::CDManager(cspace->getCD().getCollisionChecker()));
799 VirtualRobot::SceneObjectSetPtr sosRns(
new VirtualRobot::SceneObjectSet(
"RNS", cspace->getCD().getCollisionChecker()));
800 std::string armCollisionSet = getJointSetCollisionSetMapping().at(robotNodeSetName);
801 ARMARX_INFO <<
"Using Arm Collision Set '" << armCollisionSet <<
"' and platform set '" << getProperty<std::string>(
"RobotCollisionNodeSet").getValue() <<
"'";
802 sosRns->addSceneObjects(cspace->getAgentSceneObj()->getRobotNodeSet(armCollisionSet));
803 cdm->addCollisionModelPair(colSetEnv, sosRns);
805 VirtualRobot::SceneObjectSetPtr sosPlatform(
new VirtualRobot::SceneObjectSet(
"platform", cspace->getCD().getCollisionChecker()));
808 auto robotCol = cspace->getAgentSceneObj()->getRobotNodeSet(getProperty<std::string>(
"RobotCollisionNodeSet").
getValue());
809 auto robotColNodeNames = robotCol->getNodeNames();
810 std::vector<std::string> remainingNodeNames = getRobotNodeSetNodesWithoutAllwaysColliding(robotColNodeNames, armCollisionSet);
811 for (
const auto& colNodeName : remainingNodeNames)
813 sosPlatform->addSceneObject(cspace->getAgentSceneObj()->getRobotNode(colNodeName));
815 ARMARX_INFO <<
"RobotCollisionNodeSet contains the following nodes after filtering allways colliding nodes:" << std::endl <<
VAROUT(remainingNodeNames);
817 cdm->addCollisionModelPair(sosPlatform, sosRns);
823 targetPose.block<3, 1>(0, 3), VirtualRobot::IKSolver::CartesianSelection::All));
824 posConstraint->setOptimizationFunctionFactor(1);
827 targetPose.block<3, 3>(0, 0), VirtualRobot::IKSolver::CartesianSelection::All, VirtualRobot::MathTools::deg2rad(2)));
828 oriConstraint->setOptimizationFunctionFactor(1000);
829 ik.addConstraint(posConstraint);
830 ik.addConstraint(oriConstraint);
834 NaturalIKResult natIKResult =
getNaturalIK()->solveIK(eef, FramedPosePtr::dynamicCast(tcpPose)->toRootEigen(ikRobot),
true,
new armarx::aron::data::dto::Dict());
835 if (!natIKResult.reached)
842 VirtualRobot::ReferenceConfigurationConstraintPtr natIKConstraint(
new VirtualRobot::ReferenceConfigurationConstraint(ikRobot, rns));
844 Eigen::VectorXf referenceConfig(natIKResult.jointValues.size());
845 for (
size_t i = 0; i < natIKResult.jointValues.size(); i++)
847 referenceConfig(i) = natIKResult.jointValues.at(i);
850 natIKConstraint->setReferenceConfiguration(referenceConfig);
851 ik.addConstraint(natIKConstraint);
855 if (!ik.initialize())
864 for (
size_t i = 0; i < rns->getSize(); ++i)
866 result[rns->getNode((
int)i)->getName()] = rns->getNode(i)->getJointValue();
877 else if (natIKResult.reached)
880 for (
size_t i = 0; i < rns->getSize(); ++i)
882 result[rns->getNode((
int)i)->getName()] = natIKResult.jointValues.at(i);
893 StringStringDictionary GraspingManager::getJointSetCollisionSetMapping()
895 StringStringDictionary result;
896 auto propString = getProperty<std::string>(
"JointToLinkSetMapping").getValue();
898 for (
auto& pairStr : pairs)
902 result[pair.at(0)] = pair.at(1);
907 GraspingTrajectoryList GraspingManager::generateGraspingTrajectoryListForGraspListInternal(
const GeneratedGraspList& grasps)
909 std::vector<MotionPlanningData> mpdList = generateIKsInternal(grasps);
912 ARMARX_WARNING <<
"Step 'check reachability / solve IK' produced no valid results";
913 return GraspingTrajectoryList{};
919 GraspingTrajectoryList result;
920 setNextStepDescription(
"Planning motion");
922 for (
auto& graspingData : mpdList)
926 GraspingTrajectory gt = planMotion(graspingData);
928 result.push_back(gt);
948 resetStepDescription();
949 entityDrawer->clearLayer(layerName);
952 setDescriptionPositionForObject(objectInstanceEntityId);
954 float visuSlowdownFactor = getProperty<float>(
"VisualizationSlowdownFactor");
956 auto grasps = generateGrasps(objectInstanceEntityId);
958 auto result = generateGraspingTrajectoryListForGraspListInternal(grasps);
961 throw LocalException(
"Could not find any valid solution");
964 if (getProperty<bool>(
"EnableVisualization"))
966 drawTrajectory(result.front(), visuSlowdownFactor);
967 sleep(2 * visuSlowdownFactor);
968 entityDrawer->removeLayer(layerName);
970 return result.front();
978 resetStepDescription();
979 entityDrawer->clearLayer(layerName);
982 setDescriptionPositionForObject(objectInstanceEntityId);
984 auto grasps = generateGrasps(objectInstanceEntityId);
985 return generateGraspingTrajectoryListForGraspListInternal(grasps);
990 if (grasps.empty())
return GraspingTrajectoryList{};
994 resetStepDescription();
995 entityDrawer->clearLayer(layerName);
998 FramedPosePtr pose = FramedPosePtr::dynamicCast(grasps.front().framedPose)->toGlobal(localRobot);
999 pose->position->z += 400;
1000 globalDescriptionPosition = Vector3Ptr::dynamicCast(pose->position);
1002 auto result = generateGraspingTrajectoryListForGraspListInternal(grasps);
1005 throw LocalException(
"Could not find any valid solution");
1008 if (getProperty<bool>(
"EnableVisualization"))
1010 float visuSlowdownFactor = getProperty<float>(
"VisualizationSlowdownFactor");
1011 drawTrajectory(result.front(), visuSlowdownFactor);
1012 sleep(2 * visuSlowdownFactor);
1013 entityDrawer->removeLayer(layerName);
1020 drawTrajectory(trajectory, visuSlowdownFactor);
1021 sleep(2 * visuSlowdownFactor);
1022 entityDrawer->removeLayer(layerName);
1030 resetStepDescription();
1031 entityDrawer->clearLayer(layerName);
1034 setDescriptionPositionForObject(objectInstanceEntityId);
1035 auto grasps = generateGrasps(objectInstanceEntityId);
1036 return generateIKsInternal(grasps);
1041 GeneratedGraspList grasps = gg->generateGraspsByObjectName(objectName);
1042 std::sort(grasps.begin(), grasps.end(), [](
const GeneratedGrasp & l,
const GeneratedGrasp & r)
1044 return l.score < r.score;
1049 MotionPlanningDataList GraspingManager::generateIKsInternal(
const GeneratedGraspList& grasps)
1051 ikRobot->setConfig(localRobot->getConfig());
1052 auto filteredGrasps = filterGrasps(grasps);
1054 GraspingPlacementList graspPlacements;
1056 cspace = createCSpace();
1058 setNextStepDescription(
"Robot placement");
1059 const float maxDistance2D = getProperty<float>(
"MaxDistanceForDirectGrasp").getValue();
1060 GeneratedGraspList notDirectGrasps;
1061 for (
const auto& g : filteredGrasps)
1063 const Eigen::Vector3f position = FramedPosePtr::dynamicCast(g.framedPose)->getPosition()->toGlobalEigen(localRobot);
1064 const float distance2D = (position.head(2) - localRobot->getGlobalPose().block<2, 1>(0, 3)).norm();
1067 if (distance2D < maxDistance2D)
1069 GraspingPlacement pl;
1072 graspPlacements.push_back(pl);
1076 notDirectGrasps.push_back(g);
1079 if (!notDirectGrasps.empty())
1081 auto graspPlacementsNotDirect = generateRobotPlacements(notDirectGrasps);
1082 graspPlacements.insert(graspPlacements.end(), graspPlacementsNotDirect.begin(), graspPlacementsNotDirect.end());
1084 GraspingPlacementList filteredGraspPlacements = filterPlacements(graspPlacements);
1086 ARMARX_VERBOSE <<
"Step: check reachability / solve IK from current pose";
1087 setNextStepDescription(
"Calculating IK");
1088 return calculateIKs(filteredGraspPlacements);
1091 std::vector<std::string> GraspingManager::getRobotNodeSetNodesWithoutAllwaysColliding(
const std::vector<std::string>& robotColNodeNames,
const std::string& armCollisionSet)
1093 const auto rnsColNames = cspace->getAgentSceneObj()->getRobotNodeSet(armCollisionSet)->getNodeNames();
1094 std::set<std::string> rnsColNamesIncludingIgnore;
1095 rnsColNamesIncludingIgnore.insert(rnsColNames.begin(), rnsColNames.end());
1096 for (
const auto& rnsNodeName : rnsColNames)
1098 const auto rn = cspace->getAgentSceneObj()->getRobotNode(rnsNodeName);
1099 const auto ignored = rn->getIgnoredCollisionModels();
1100 rnsColNamesIncludingIgnore.insert(ignored.begin(), ignored.end());
1101 rnsColNamesIncludingIgnore.insert(rn->getParent()->getName());
1104 std::vector<std::string> remainingNodeNames;
1105 for (
const auto& colNodeName : robotColNodeNames)
1107 const auto it = rnsColNamesIncludingIgnore.find(colNodeName);
1108 if (it == rnsColNamesIncludingIgnore.end())
1110 remainingNodeNames.push_back(colNodeName);
1113 return remainingNodeNames;