29 #include <RobotAPI/interface/units/KinematicUnitInterface.h>
32 #include <VirtualRobot/RobotConfig.h>
37 using namespace MotionAndPlatformControlGroup;
46 timeoutEvent = setTimeoutEvent(in.getTimeoutInMs(), createEventFailure());
48 t = in.getTrajectory();
52 ARMARX_ERROR_S <<
"MoveJointPlatformTrajectory expects a trajectory with x, y, alpha as its first coordinates!";
58 if (in.isJointMaxAccelsSet())
60 maxAccs = in.getJointMaxAccels();
63 if (in.isJointMaxSpeedsSet())
72 context = getContext<MotionAndPlatformControlGroupStatechartContext>();
80 ARMARX_INFO_S <<
"trajectory:\n" << in.getTrajectory()->output();
81 if (in.getSkipFirstTimepoint() >= in.getTrajectory()->size())
85 <<
"skip first = " << in.getSkipFirstTimepoint();
92 if (in.getUseNativePositionControl())
118 while (!isRunningTaskFinished())
120 std::this_thread::sleep_for(std::chrono::milliseconds {5});
122 if (in.getSkipFirstTimepoint() >= in.getTrajectory()->size())
128 if (in.getHoldPositionAfterwards())
131 NameControlModeMap controlModes;
134 for (
size_t i = 0; i <
jointNames.size(); ++i)
137 targetJointVelocities[
jointNames.at(i)] = 0.0f;
138 controlModes[
jointNames.at(i)] = ePositionControl;
140 ARMARX_INFO_S <<
"hold position aftawards is set! (using position contol to hold position)";
146 ARMARX_INFO <<
"Joint target tolerance: " << in.getJointTargetTolerance();
148 for (
size_t i = 0; i <
jointNames.size(); ++i)
153 out.setJointAndPlatformDistancesToTargets(differences);
176 float configDeltaAng;
177 float allowedPercInaccuracy;
191 std::vector<JointData> jointData(
jointNames.size());
196 for (std::size_t i = 0; i <
jointNames.size(); ++i)
198 JointData&
data = jointData.at(i);
206 auto updateJoitnDataForNewConfig = [&]
208 for (std::size_t i = 0; i <
jointNames.size(); ++i)
210 JointData&
data = jointData.at(i);
216 <<
" move " <<
data.prevAng
217 <<
" -> " <<
data.targetAng
218 <<
" (delta = " <<
data.configDeltaAng <<
")";
222 ARMARX_INFO_S <<
"MoveJointPlatformTrajectory start axis sync (acceleration limited) PTP";
225 if (in.getSkipFirstTimepoint())
229 updateJoitnDataForNewConfig();
234 updateJoitnDataForNewConfig();
237 while (!isRunningTaskStopped())
239 auto startIter = std::chrono::high_resolution_clock::now();
247 bool dontAdvanceToNextConfig =
false;
249 float minNextPerc = std::numeric_limits<float>::infinity();
257 for (std::size_t i = 0; i <
jointNames.size(); ++i)
259 JointData&
data = jointData.at(i);
261 data.nextPercAng = -1;
263 data.nextVelForced =
false;
267 const float velSig =
sign(
data.vel);
277 const float absBrakingDistance =
data.vel *
data.vel / (2.f *
data.maxAcc);
283 (
data.deltaAngSig == velSig)
289 if (std::fabs(
data.vel) <
data.maxDeltaVel)
299 data.nextVelForced =
true;
303 assert(
data.nextVel <=
data.maxVel);
304 assert(
data.nextVel >= -
data.maxVel);
310 const float p =
data.maxDeltaVel;
312 const float nextVelOpt =
data.deltaAngSig * (-p / 2.f +
std::sqrt((p / 2.f) * (p / 2.f) -
q));
314 const float nextVelVelCapped =
std::clamp(nextVelAccCapped, -
data.maxVel,
data.maxVel);
315 data.nextVel = nextVelVelCapped;
317 assert(
data.nextVel <=
data.maxVel);
318 assert(
data.nextVel >= -
data.maxVel);
325 data.percAng > 1.f ||
331 data.nextVelForced =
true;
335 const float nextAngPredictDelta =
data.targetAng - nextAngPredict;
336 data.nextPercAng = 1 - nextAngPredictDelta /
data.configDeltaAng;
348 for (std::size_t i = 0; i <
jointNames.size(); ++i)
351 JointData&
data = jointData.at(i);
353 if (
data.nextVelForced ||
data.nextPercAng <= minNextPerc +
data.allowedPercInaccuracy)
357 targetJointVel[name] =
data.nextVel;
358 assert(std::fabs(targetJointVel[name]) <= std::fabs(
data.maxVel) + std::numeric_limits<float>::epsilon());
359 assert(std::fabs(targetJointVel[name]) <= std::fabs(
data.vel) + std::fabs(
data.maxDeltaVel) + std::numeric_limits<float>::epsilon());
361 if (
data.nextVelForced)
363 ARMARX_INFO <<
"for name " << name <<
"\t" << targetJointVel[name] <<
" (" <<
data.maxVel <<
") delta " <<
data.deltaAng;
367 ARMARX_INFO <<
"maj name " << name <<
"\t" << targetJointVel[name] <<
" (" <<
data.maxVel <<
") delta " <<
data.deltaAng <<
"\t p/np" <<
data.percAng <<
"\t/\t" <<
data.nextPercAng;
372 if (
data.percAng >= minNextPerc)
377 assert(std::fabs(targetJointVel[name]) <= std::fabs(
data.maxVel) + std::numeric_limits<float>::epsilon());
378 assert(std::fabs(targetJointVel[name]) <= std::fabs(
data.vel) + std::fabs(
data.maxDeltaVel) + std::numeric_limits<float>::epsilon());
380 ARMARX_INFO <<
"slo name " << name <<
"\t" << targetJointVel[name] <<
" (" <<
data.maxVel <<
") delta " <<
data.deltaAng <<
"\t p/np" <<
data.percAng <<
"\t/\t" <<
data.nextPercAng;
385 const float deltaAngMoved =
data.configDeltaAng -
data.deltaAng;
386 const float deltaAngMovedNext = minNextPerc *
data.configDeltaAng;
387 const float delatAngToMoveInIteratiron = deltaAngMovedNext - deltaAngMoved;
389 assert(std::fabs(newVel) <= std::fabs(
data.nextVel) + std::numeric_limits<float>::epsilon());
390 targetJointVel[name] = newVel;
391 assert(std::fabs(targetJointVel[name]) <= std::fabs(
data.maxVel) + std::numeric_limits<float>::epsilon());
392 assert(std::fabs(targetJointVel[name]) <= std::fabs(
data.vel) + std::fabs(
data.maxDeltaVel) + std::numeric_limits<float>::epsilon());
393 ARMARX_INFO <<
"var name " << name <<
"\t" << targetJointVel[name] <<
" (" <<
data.maxVel <<
") delta " <<
data.deltaAng <<
"\t p/np" <<
data.percAng <<
"\t/\t" <<
data.nextPercAng;
398 if (!dontAdvanceToNextConfig)
403 const auto tIter = std::chrono::high_resolution_clock::now() - startIter;
404 ARMARX_INFO_S <<
deactivateSpam() <<
"Controler iteration took " << std::chrono::duration_cast<std::chrono::milliseconds>(tIter).count() <<
" ms";
407 ARMARX_WARNING_S <<
"iteration took long! (" << std::chrono::duration_cast<std::chrono::milliseconds>(tIter).count() <<
" ms. Period = " <<
period.count() <<
" ms)";
411 std::this_thread::sleep_until(
period + startIter);
418 ARMARX_INFO_S <<
"MoveJointPlatformTrajectory end success axis sync (acceleration limited) PTP";
421 ARMARX_INFO_S <<
"MoveJointPlatformTrajectory exit failure axis sync (acceleration limited) PTP";
429 for (std::size_t i = 0; i <
jointNames.size(); ++i)
438 if (isRunningTaskStopped())
442 currentCfgIt->writePositionsToNameValueMap(jointTargetMap);
448 if (isRunningTaskStopped())
455 float maxTranslationDelta =
457 std::fabs(
getValue(0) - jointTargetMap.at(
"x")),
458 std::fabs(
getValue(1) - jointTargetMap.at(
"y"))
461 for (std::size_t i = 2; i <
jointNames.size(); ++i)
476 std::this_thread::sleep_for(
period);
480 ARMARX_INFO_S <<
"MoveJointPlatformTrajectory exit native PTP";
502 return (val < 0) ? -1 : 1;