27 #include <RobotAPI/interface/components/TrajectoryPlayerInterface.h>
30 #include <MotionPlanning/Planner/BiRrt.h>
34 #include <MotionPlanning/PostProcessing/ShortcutProcessor.h>
36 #include <MotionPlanning/CSpace/CSpacePath.h>
38 #include <VirtualRobot/TimeOptimalTrajectory/TimeOptimalTrajectory.h>
39 #include <VirtualRobot/RobotNodeSet.h>
47 using namespace MotionControlGroup;
69 std::set<std::string> jointNameSet;
70 if (!in.isSingleGoalConfigSet() && !in.isGoalConfigSet())
72 ARMARX_WARNING <<
"Either GoalConfig or SingleGoalConfig input parameter must be set!";
77 if (in.isSingleGoalConfigSet())
81 std::vector<StringValueMapPtr> goalConfig;
82 if (in.isGoalConfigSet())
84 goalConfig = in.getGoalConfig();
90 goalConfig.push_back(mapPtr);
94 for (
auto goalConfigMapPtr : goalConfig)
97 auto goalConfigMap = goalConfigMapPtr->toStdMap<
float>();
98 for (
auto pair : goalConfigMap)
100 if (jointNameSet.count(pair.first) != 0)
102 ARMARX_ERROR <<
"joint " << pair.first <<
" is used twice!";
106 jointNameSet.insert(pair.first);
111 float timeStep = in.getTimestep();
112 std::vector<TrajectoryPtr> solutions;
115 auto robot = context->
getRobotPool()->getRobot(in.getSafetyMargin());
127 if (!in.isCollisionSetNamesSet() && !in.isCollisionSetPairsSet())
129 throw LocalException() <<
"You need to either specify CollisionSetNames or CollisionSetPairs!";
132 VirtualRobot::CDManagerPtr cdm(
new VirtualRobot::CDManager(robot->getCollisionChecker()));
135 auto rnsList = in.isCollisionSetNamesSet() ? in.getCollisionSetNames() : Ice::StringSeq();
136 for (
size_t i = 0; i < rnsList.size(); ++i)
138 auto rns1 = robot->getRobotNodeSet(rnsList.at(i));
140 cdm->addCollisionModel(rns1);
152 auto rnsPairList = in.getCollisionSetPairs();
153 for (
auto& subList : rnsPairList)
156 auto getRns = [&](
const std::string & name)
158 VirtualRobot::RobotNodeSetPtr rns;
159 if (robot->hasRobotNodeSet(name))
161 rns = robot->getRobotNodeSet(name);
165 if (robot->hasRobotNode(name))
167 auto node = robot->getRobotNode(name);
168 rns = VirtualRobot::RobotNodeSet::createRobotNodeSet(robot, node->getName(), {node});
176 auto rns1 = getRns(subList->getVariant(0)->getString());
177 auto rns2 = getRns(subList->getVariant(1)->getString());
179 cdm->addCollisionModelPair(rns1, rns2);
182 for (
auto goalConfigMapPtr : goalConfig)
184 if (isRunningTaskStopped())
188 auto goalConfigMap = goalConfigMapPtr->toStdMap<
float>();
189 Ice::StringSeq jointNames =
getMapKeys(goalConfigMap);
192 auto rns = VirtualRobot::RobotNodeSet::createRobotNodeSet(robot,
"tempset", jointNames, robot->getRootNode()->getName());
195 Saba::CSpaceSampledPtr cspace(
new Saba::CSpaceSampled(robot, cdm, rns, 1000000));
197 Saba::BiRrt rrt(cspace, Saba::Rrt::eExtend);
198 rrt.setPlanningTimeout(in.getTimeoutMs());
201 Eigen::VectorXf goalConfig(rns->getSize());
202 for (std::size_t i = 0; i < rns->getSize(); ++i)
204 goalConfig(i) = goalConfigMap.at(rns->getNode(i)->getName());
206 rrt.setStart(rns->getJointValuesEigen());
207 rrt.setGoal(goalConfig);
209 auto planningOk = rrt.plan();
211 Saba::CSpacePathPtr solutionOptimized;
215 auto solution = rrt.getSolution();
219 Saba::ShortcutProcessorPtr postProcessing(
new Saba::ShortcutProcessor(solution, cspace,
false));
220 solutionOptimized = postProcessing->shortenSolutionRandom(in.getOptimizationSteps(), in.getOptimizationMaxStepSize());
226 emitPlanningFailed();
233 auto waypoints = solutionOptimized->getPoints();
234 ARMARX_INFO <<
"Waypoint Count: " << waypoints.size();
236 LimitlessStateSeq limitlessStates;
237 for (std::size_t i = 0; i < rns->getSize(); ++i)
239 auto node = rns->getNode(i);
240 limitlessStates.push_back(LimitlessState {node->isLimitless(), node->getJointLimitLow(), node->getJointLimitHigh()});
242 waypointTraj->setDimensionNames(jointNames);
243 waypointTraj->setLimitless(limitlessStates);
245 for (
auto waypoint : waypoints)
247 for (std::size_t i = 0; i < rns->getSize(); ++i)
249 waypointTraj->setPositionEntry(t, i, waypoint(i));
255 Eigen::VectorXd maxVelocities = Eigen::VectorXd::Constant(rns->getSize(), (
double)in.getMaxVelocity());
256 Eigen::VectorXd maxAcceleration = Eigen::VectorXd::Constant(rns->getSize(), (
double)in.getMaxAcceleration());
257 if (in.isJointScaleSet())
259 auto scalingMap = in.getJointScale();
260 for (
size_t i = 0; i < rns->getSize(); ++i)
262 if (scalingMap.at(rns->getNode(i)->getName()) > 0)
264 auto scale = scalingMap.at(rns->getNode(i)->getName());
265 maxVelocities(i) *= scale;
266 maxAcceleration(i) *= scale;
270 auto newTraj = waypointTraj->calculateTimeOptimalTrajectory(maxVelocities, maxAcceleration, in.getMaxDeviation(), IceUtil::Time::secondsDouble(timeStep));
272 solutions.push_back(newTraj);
275 Ice::StringSeq jointNames;
277 for (
auto& sol : solutions)
279 size_t dims = sol->dim();
280 Ice::DoubleSeq timestamps = sol->getTimestamps();
282 for (
size_t d = 0; d < dims; ++d)
284 combinedTraj->addPositionsToDimension(fullTrajDim, sol->getDimensionData(d), timestamps);
285 jointNames.push_back(sol->getDimensionName(d));
289 combinedTraj->setDimensionNames(jointNames);
290 double duration = combinedTraj->getTimeLength();
291 auto rns = VirtualRobot::RobotNodeSet::createRobotNodeSet(robot,
"tempset", jointNames, robot->getRootNode()->getName());
292 LimitlessStateSeq limitlessStates;
293 for (std::size_t i = 0; i < rns->getSize(); ++i)
295 auto node = rns->getNode(i);
296 limitlessStates.push_back(LimitlessState {node->isLimitless(), node->getJointLimitLow(), node->getJointLimitHigh()});
298 combinedTraj->setLimitless(limitlessStates);
305 if (in.getUseTrajectoryPlayerComponent())
307 TrajectoryPlayerInterfacePrx trajPlayer = context->
getProxy<TrajectoryPlayerInterfacePrx>(in.getTrajectoryPlayerName());
308 trajPlayer->resetTrajectoryPlayer(
false);
309 trajPlayer->considerConstraints(
false);
310 trajPlayer->setLoopPlayback(
false);
311 trajPlayer->loadJointTraj(combinedTraj);
312 trajPlayer->startTrajectoryPlayer();
313 auto endTime = trajPlayer->getEndTime();
314 double currentTime = 0;
319 currentTime = trajPlayer->getCurrentTime();
321 while (currentTime < endTime && !isRunningTaskStopped());
322 if (!isRunningTaskStopped() && currentTime >= endTime)
324 if (in.getWaitTimeAfterExecution() > 0)
326 ARMARX_INFO <<
"Waiting for " << in.getWaitTimeAfterExecution();
329 trajPlayer->pauseTrajectoryPlayer();
332 else if (isRunningTaskStopped())
334 trajPlayer->pauseTrajectoryPlayer();
342 NameControlModeMap modes;
344 for (
auto& jointName : jointNames)
346 modes[jointName] = eVelocityControl;
350 while (!isRunningTaskStopped())
355 Eigen::VectorXf velocityTargets = ctrl.
update(timeStep, rns->getJointValuesEigen());
357 for (
auto& jointName : jointNames)
359 targetVelocities[jointName] = velocityTargets(i);
373 for (
auto& jointName : jointNames)
375 targetVelocities[jointName] = 0;
381 for (
auto goalConfigMapPtr : goalConfig)
383 auto goalConfigMap = goalConfigMapPtr->toStdMap<
float>();
385 for (
auto& pair : goalConfigMap)
387 errors[pair.first] = robot->getRobotNode(pair.first)->getJointValue();
399 if (in.getUseTrajectoryPlayerComponent())
402 TrajectoryPlayerInterfacePrx trajPlayer = context->
getProxy<TrajectoryPlayerInterfacePrx>(in.getTrajectoryPlayerName());
403 trajPlayer->stopTrajectoryPlayer();