28 #include <VirtualRobot/RobotConfig.h>
32 #include <RobotAPI/interface/units/KinematicUnitInterface.h>
36 using namespace MotionAndPlatformControlGroup;
39 MoveJointPlatformTrajectory::SubClassRegistry
48 timeoutEvent = setTimeoutEvent(in.getTimeoutInMs(), createEventFailure());
50 t = in.getTrajectory();
54 ARMARX_ERROR_S <<
"MoveJointPlatformTrajectory expects a trajectory with x, y, alpha as "
55 "its first coordinates!";
61 if (in.isJointMaxAccelsSet())
63 maxAccs = in.getJointMaxAccels();
66 if (in.isJointMaxSpeedsSet())
74 context = getContext<MotionAndPlatformControlGroupStatechartContext>();
83 ARMARX_INFO_S <<
"trajectory:\n" << in.getTrajectory()->output();
84 if (in.getSkipFirstTimepoint() >= in.getTrajectory()->size())
88 <<
"skip first = " << in.getSkipFirstTimepoint();
95 if (in.getUseNativePositionControl())
125 while (!isRunningTaskFinished())
127 std::this_thread::sleep_for(std::chrono::milliseconds{5});
129 if (in.getSkipFirstTimepoint() >= in.getTrajectory()->size())
135 if (in.getHoldPositionAfterwards())
138 NameControlModeMap controlModes;
141 for (
size_t i = 0; i <
jointNames.size(); ++i)
145 targetJointVelocities[
jointNames.at(i)] = 0.0f;
146 controlModes[
jointNames.at(i)] = ePositionControl;
148 ARMARX_INFO_S <<
"hold position aftawards is set! (using position contol to hold position)";
154 ARMARX_INFO <<
"Joint target tolerance: " << in.getJointTargetTolerance();
156 for (
size_t i = 0; i <
jointNames.size(); ++i)
161 out.setJointAndPlatformDistancesToTargets(differences);
184 float configDeltaAng;
185 float allowedPercInaccuracy;
200 std::vector<JointData> jointData(
jointNames.size());
205 for (std::size_t i = 0; i <
jointNames.size(); ++i)
207 JointData&
data = jointData.at(i);
215 auto updateJoitnDataForNewConfig = [&]
217 for (std::size_t i = 0; i <
jointNames.size(); ++i)
219 JointData&
data = jointData.at(i);
225 <<
data.targetAng <<
" (delta = " <<
data.configDeltaAng <<
")";
229 ARMARX_INFO_S <<
"MoveJointPlatformTrajectory start axis sync (acceleration limited) PTP";
232 if (in.getSkipFirstTimepoint())
236 updateJoitnDataForNewConfig();
241 updateJoitnDataForNewConfig();
244 while (!isRunningTaskStopped())
246 auto startIter = std::chrono::high_resolution_clock::now();
254 bool dontAdvanceToNextConfig =
false;
256 float minNextPerc = std::numeric_limits<float>::infinity();
264 for (std::size_t i = 0; i <
jointNames.size(); ++i)
266 JointData&
data = jointData.at(i);
268 data.nextPercAng = -1;
270 data.nextVelForced =
false;
274 const float velSig =
sign(
data.vel);
284 const float absBrakingDistance =
data.vel *
data.vel / (2.f *
data.maxAcc);
288 if (((
data.deltaAngSig ==
290 && (std::fabs(
data.deltaAng) <=
297 if (std::fabs(
data.vel) <
data.maxDeltaVel)
307 data.nextVelForced =
true;
310 assert(
data.nextVel <=
data.maxVel);
311 assert(
data.nextVel >= -
data.maxVel);
317 const float p =
data.maxDeltaVel;
318 const float q =
data.deltaAngSig *
320 const float nextVelOpt =
321 data.deltaAngSig * (-p / 2.f +
std::sqrt((p / 2.f) * (p / 2.f) -
q));
324 const float nextVelVelCapped =
326 data.nextVel = nextVelVelCapped;
328 assert(
data.nextVel <=
data.maxVel);
329 assert(
data.nextVel >= -
data.maxVel);
330 const float nextAngPredict =
335 1 -
data.deltaAng /
data.configDeltaAng;
337 if (
data.percAng > 1.f ||
343 data.nextVelForced =
true;
347 const float nextAngPredictDelta =
data.targetAng - nextAngPredict;
349 1 - nextAngPredictDelta /
data.configDeltaAng;
361 for (std::size_t i = 0; i <
jointNames.size(); ++i)
364 JointData&
data = jointData.at(i);
366 if (
data.nextVelForced ||
367 data.nextPercAng <= minNextPerc +
data.allowedPercInaccuracy)
371 targetJointVel[name] =
data.nextVel;
372 assert(std::fabs(targetJointVel[name]) <=
373 std::fabs(
data.maxVel) + std::numeric_limits<float>::epsilon());
374 assert(std::fabs(targetJointVel[name]) <=
375 std::fabs(
data.vel) + std::fabs(
data.maxDeltaVel) +
376 std::numeric_limits<float>::epsilon());
378 if (
data.nextVelForced)
380 ARMARX_INFO <<
"for name " << name <<
"\t" << targetJointVel[name] <<
" ("
381 <<
data.maxVel <<
") delta " <<
data.deltaAng;
385 ARMARX_INFO <<
"maj name " << name <<
"\t" << targetJointVel[name] <<
" ("
386 <<
data.maxVel <<
") delta " <<
data.deltaAng <<
"\t p/np"
387 <<
data.percAng <<
"\t/\t" <<
data.nextPercAng;
392 if (
data.percAng >= minNextPerc)
396 targetJointVel[name] =
398 assert(std::fabs(targetJointVel[name]) <=
399 std::fabs(
data.maxVel) + std::numeric_limits<float>::epsilon());
400 assert(std::fabs(targetJointVel[name]) <=
401 std::fabs(
data.vel) + std::fabs(
data.maxDeltaVel) +
402 std::numeric_limits<float>::epsilon());
404 ARMARX_INFO <<
"slo name " << name <<
"\t" << targetJointVel[name] <<
" ("
405 <<
data.maxVel <<
") delta " <<
data.deltaAng <<
"\t p/np"
406 <<
data.percAng <<
"\t/\t" <<
data.nextPercAng;
411 const float deltaAngMoved =
data.configDeltaAng -
data.deltaAng;
412 const float deltaAngMovedNext = minNextPerc *
data.configDeltaAng;
413 const float delatAngToMoveInIteratiron = deltaAngMovedNext - deltaAngMoved;
415 assert(std::fabs(newVel) <=
416 std::fabs(
data.nextVel) + std::numeric_limits<float>::epsilon());
417 targetJointVel[name] = newVel;
418 assert(std::fabs(targetJointVel[name]) <=
419 std::fabs(
data.maxVel) + std::numeric_limits<float>::epsilon());
420 assert(std::fabs(targetJointVel[name]) <=
421 std::fabs(
data.vel) + std::fabs(
data.maxDeltaVel) +
422 std::numeric_limits<float>::epsilon());
423 ARMARX_INFO <<
"var name " << name <<
"\t" << targetJointVel[name] <<
" ("
424 <<
data.maxVel <<
") delta " <<
data.deltaAng <<
"\t p/np"
425 <<
data.percAng <<
"\t/\t" <<
data.nextPercAng;
430 if (!dontAdvanceToNextConfig)
435 const auto tIter = std::chrono::high_resolution_clock::now() - startIter;
437 << std::chrono::duration_cast<std::chrono::milliseconds>(tIter).count()
442 <<
"iteration took long! ("
443 << std::chrono::duration_cast<std::chrono::milliseconds>(tIter).count()
444 <<
" ms. Period = " <<
period.count() <<
" ms)";
448 std::this_thread::sleep_until(
period + startIter);
456 <<
"MoveJointPlatformTrajectory end success axis sync (acceleration limited) PTP";
460 <<
"MoveJointPlatformTrajectory exit failure axis sync (acceleration limited) PTP";
469 for (std::size_t i = 0; i <
jointNames.size(); ++i)
478 if (isRunningTaskStopped())
482 currentCfgIt->writePositionsToNameValueMap(jointTargetMap);
488 if (isRunningTaskStopped())
495 float maxTranslationDelta =
std::max(std::fabs(
getValue(0) - jointTargetMap.at(
"x")),
496 std::fabs(
getValue(1) - jointTargetMap.at(
"y")));
498 for (std::size_t i = 2; i <
jointNames.size(); ++i)
509 std::this_thread::sleep_for(
period);
513 ARMARX_INFO_S <<
"MoveJointPlatformTrajectory exit native PTP";
532 return (val < 0) ? -1 : 1;