24 #include <VirtualRobot/Nodes/RobotNode.h>
39 const SimoxCSpaceBasePtr& cSpacePlatformBase,
40 const MotionPlanningData& mpd,
const Ice::Current&
c)
42 TrajectoryPtr jointTrajectory = calculateJointTrajectory(cSpaceBase, mpd,
c);
43 TrajectoryPtr platformTrajectory = calculatePlatformTrajectory(cSpacePlatformBase, mpd,
c);
44 return {platformTrajectory,
45 jointTrajectory, mpd.rnsToUse, mpd.endeffector,
51 const SimoxCSpaceBasePtr& cSpacePlatformBase,
52 const MotionPlanningData& mpd,
const Ice::Current&
c)
54 VectorXf startCfg, goalCfg, startPos, goalPos;
56 Eigen::Vector3f rpyAgent;
58 SimoxCSpacePtr cSpacePlatform = SimoxCSpacePtr::dynamicCast(cSpacePlatformBase);
59 prepareJointCSpace(cSpace, mpd, &startCfg, &goalCfg, &scaledJointCSpace);
60 preparePlatformCSpace(cSpacePlatform, mpd, &startPos, &goalPos, &scaledPlatformCSpace, &rpyAgent);
61 ARMARX_INFO <<
"Starting BiRRT for Joints and Platform";
66 Path jointTrajectoryPath = finishBiRRT(rspJointHandle, scaledJointCSpace,
"Joint");
68 Path posTrajectoryPath = finishBiRRT(rspPlatformHandle, scaledPlatformCSpace,
"Platform");
71 auto transformToGlobal = [&](armarx::VectorXf & pos2D)
73 Eigen::Vector2f globalPos = Eigen::Rotation2Df(rpyAgent(2)) * Eigen::Vector2f(pos2D.at(0), pos2D.at(1));
74 pos2D.at(0) = globalPos[0] + cSpacePlatform->getAgent().agentPose->position->x;
75 pos2D.at(1) = globalPos[1] + cSpacePlatform->getAgent().agentPose->position->y;
79 for (
auto& e : posTrajectoryPath.nodes)
84 jointTrajectory = cSpace->pathToTrajectory(jointTrajectoryPath);
85 platformTrajectory = cSpacePlatform->pathToTrajectory(posTrajectoryPath);
86 return {platformTrajectory, jointTrajectory, mpd.rnsToUse, mpd.endeffector, mpd.grasp};
93 maxPostProcessingSteps = 50;
100 getProxy(mps,
"MotionPlanningServer");
101 getProxy(robotStateComponent,
"RobotStateComponent");
108 planningTasks.clear();
124 const armarx::MotionPlanningData& mpd,
125 const Ice::Current&
c)
127 TrajectoryPtr trajectory = calculateJointTrajectory(cSpaceBase, mpd,
c);
128 return TrajectoryBasePtr::dynamicCast(trajectory);
132 const armarx::MotionPlanningData& mpd,
133 const Ice::Current&
c)
135 TrajectoryPtr trajectory = calculatePlatformTrajectory(cSpacePlatformBase, mpd,
c);
136 return TrajectoryBasePtr::dynamicCast(trajectory);
139 TrajectoryPtr PlannedMotionProvider::calculateJointTrajectory(
const SimoxCSpaceBasePtr& cSpaceBase,
140 const MotionPlanningData& mpd,
const Ice::Current&
c)
146 prepareJointCSpace(cSpace, mpd, &startCfg, &goalCfg, &scaledJointCSpace);
148 Path jointTrajectoryPath = finishBiRRT(rspHandle, scaledJointCSpace,
"Joint");
149 return cSpace->pathToTrajectory(jointTrajectoryPath);
153 TrajectoryPtr PlannedMotionProvider::calculatePlatformTrajectory(
const SimoxCSpaceBasePtr& cSpacePlatformBase,
154 const MotionPlanningData& mpd,
155 const Ice::Current&
c)
160 Eigen::Vector3f rpyAgent;
161 SimoxCSpacePtr cSpacePlatform = SimoxCSpacePtr::dynamicCast(cSpacePlatformBase);
162 preparePlatformCSpace(cSpacePlatform, mpd, &startPos, &goalPos, &scaledPlatformCSpace, &rpyAgent);
164 RemoteHandle<MotionPlanningTaskControlInterfacePrx> rspHandle = startBiRRT(scaledPlatformCSpace, startPos, goalPos, 0.1);
165 Path posTrajectoryPath = finishBiRRT(rspHandle, scaledPlatformCSpace,
"Platform");
167 auto transformToGlobal = [&](armarx::VectorXf & pos2D)
169 Eigen::Vector2f globalPos = Eigen::Rotation2Df(rpyAgent(2)) * Eigen::Vector2f(pos2D.at(0), pos2D.at(1));
170 pos2D.at(0) = globalPos[0] + cSpacePlatform->getAgent().agentPose->position->x;
171 pos2D.at(1) = globalPos[1] + cSpacePlatform->getAgent().agentPose->position->y;
175 for (
auto& e : posTrajectoryPath.nodes)
177 transformToGlobal(e);
180 return cSpacePlatform->pathToTrajectory(posTrajectoryPath);
183 RemoteHandle<MotionPlanningTaskControlInterfacePrx> PlannedMotionProvider::startBiRRT(
const ScaledCSpacePtr scaledCSpace,
184 const armarx::VectorXf startPos,
185 const armarx::VectorXf goalPos,
float dcdStep,
bool doRandomShortcutPostProcessing)
188 MotionPlanningTaskBasePtr taskRRT =
new BiRRTTask {scaledCSpace, startPos, goalPos,
192 RemoteHandle<MotionPlanningTaskControlInterfacePrx> handle;
193 if (!doRandomShortcutPostProcessing)
195 handle = mps->enqueueTask(taskRRT);
199 handle = mps->enqueueTask(
201 1, dcdStep, maxPostProcessingSteps));
203 planningTasks.push_back(handle);
207 Path PlannedMotionProvider::finishBiRRT(RemoteHandle<MotionPlanningTaskControlInterfacePrx> handle,
209 const std::string roboPart)
212 handle->waitForFinishedRunning();
214 << IceUtil::Time::microSeconds(handle->getRunningTime()).toMilliSecondsDouble()
217 if (handle->getTaskStatus() == armarx::TaskStatus::ePlanningFailed)
219 throw RuntimeError(
" Motion Planning failed!");
223 << ((bool)(handle->getTaskStatus() == armarx::TaskStatus::ePlanningFailed) ?
"failed"
225 ARMARX_INFO <<
"RRTConnectTask Planning status: " << handle->getTaskStatus();
227 auto posTrajectoryPath = handle->getPath();
229 scaledCSpace->unscalePath(posTrajectoryPath);
230 return posTrajectoryPath;
233 void PlannedMotionProvider::preparePlatformCSpace(
SimoxCSpacePtr cSpacePlatform,
const MotionPlanningData& mpd,
234 VectorXf* storeStart, VectorXf* storeGoal,
237 cSpacePlatform->setStationaryObjectMargin(getProperty<float>(
"PlatformMotionSafetyMargin").
getValue());
240 Eigen::Vector3f rpy, rpyAgent;
241 VirtualRobot::MathTools::eigen4f2rpy(PosePtr::dynamicCast(mpd.globalPoseStart)->toEigen(), rpy);
242 VirtualRobot::MathTools::eigen4f2rpy(PosePtr::dynamicCast(cSpacePlatform->getAgent().agentPose)->toEigen(),
244 armarx::VectorXf startPos {mpd.globalPoseStart->position->x - cSpacePlatform->getAgent().agentPose->position->x,
245 mpd.globalPoseStart->position->y - cSpacePlatform->getAgent().agentPose->position->y,
249 Eigen::Vector2f localStartPos = Eigen::Rotation2Df(-rpyAgent(2)) * Eigen::Vector2f(startPos.at(0), startPos.at(1));
250 startPos.at(0) = localStartPos(0);
251 startPos.at(1) = localStartPos(1);
252 VirtualRobot::MathTools::eigen4f2rpy(PosePtr::dynamicCast(mpd.globalPoseGoal)->toEigen(), rpy);
253 armarx::VectorXf goalPos {mpd.globalPoseGoal->position->x - cSpacePlatform->getAgent().agentPose->position->x,
254 mpd.globalPoseGoal->position->y - cSpacePlatform->getAgent().agentPose->position->y,
257 Eigen::Vector2f localGoalPos = Eigen::Rotation2Df(-rpyAgent(2)) * Eigen::Vector2f(goalPos.at(0), goalPos.at(1));
258 goalPos.at(0) = localGoalPos(0);
259 goalPos.at(1) = localGoalPos(1);
263 ScaledCSpacePtr scaledPlatformCSpace =
new ScaledCSpace(cSpacePlatform, {0.001, 0.001, 1});
264 scaledPlatformCSpace->scaleConfig(startPos);
265 scaledPlatformCSpace->scaleConfig(goalPos);
266 *storeStart = startPos;
267 *storeGoal = goalPos;
268 *storeScaledCSpace = scaledPlatformCSpace;
269 *storeRpyAgent = rpyAgent;
272 void PlannedMotionProvider::prepareJointCSpace(
SimoxCSpacePtr cSpace,
const MotionPlanningData& mpd,
273 VectorXf* storeStart, VectorXf* storeGoal,
276 cSpace->setStationaryObjectMargin(getProperty<float>(
"JointMotionSafetyMargin").
getValue());
279 Ice::FloatSeq cSpaceScaling;
280 for (VirtualRobot::RobotNodePtr node : localRobot->getRobotNodeSet(mpd.rnsToUse)->getAllRobotNodes())
282 cSpaceScaling.push_back(node->isTranslationalJoint() ? 0.001f : 1.0f);
285 <<
VAROUT(mpd.globalPoseGoal->output());
287 armarx::VectorXf startCfg = cSpace->jointMapToVector(mpd.configStart);
288 armarx::VectorXf goalCfg = cSpace->jointMapToVector(mpd.configGoal);
291 ScaledCSpacePtr scaledJointCSpace =
new ScaledCSpace(cSpace, cSpaceScaling);
292 scaledJointCSpace->scaleConfig(startCfg);
293 scaledJointCSpace->scaleConfig(goalCfg);
294 *storeStart = startCfg;
295 *storeGoal = goalCfg;
296 *storeScaledCSpace = scaledJointCSpace;