30 kinematicUnitName = getProperty<std::string>(
"KinematicUnitName").getValue();
32 robotPoseUnitEnabled = getProperty<bool>(
"EnableRobotPoseUnit").getValue();
33 robotPoseUnitName = getProperty<std::string>(
"RobotPoseUnitName").getValue();
35 kp = getProperty<float>(
"Kp").getValue();
36 ki = getProperty<float>(
"Ki").getValue();
37 kd = getProperty<float>(
"Kd").getValue();
38 maxVelocity = getProperty<float>(
"absMaximumVelocity").getValue() / 180 *
47 kinematicUnit = getProxy<KinematicUnitInterfacePrx>(kinematicUnitName);
48 debugDrawer = getTopic<armarx::DebugDrawerInterfacePrx>(
"DebugDrawerUpdates");
54 debugDrawer->clearLayer(
"Preview");
63 assureTrajectoryController();
65 jointTrajController->setTrajectory(this->jointTraj,
c);
66 jointTraj = jointTrajController->getTrajectoryCopy();
67 jointTrajController->activateController();
71 std::string fileName = kinematicUnit->getRobotFilename();
73 debugDrawer->setRobotVisu(
"Preview",
76 fileName.substr(0, fileName.find(
"/")),
77 armarx::CollisionModel);
78 debugDrawer->updateRobotColor(
"Preview",
"previewRobot", DrawColor{0, 1, 0, 0.5});
82 &TrajectoryControllerSubUnit::previewRun,
85 "TrajectoryControllerSubUnitPreviewTask",
96 if (controllerExists())
98 if (jointTrajController->isControllerActive())
101 jointTrajController->deactivateController();
106 jointTrajController->activateController();
117 if (controllerExists())
122 debugDrawer->clearLayer(
"Preview");
125 jointTrajController->deactivateController();
126 while (jointTrajController->isControllerActive())
129 jointTrajController->deleteController();
139 if (controllerExists() && jointTrajController->isControllerActive())
141 jointTrajController->deactivateController();
142 while (jointTrajController->isControllerActive())
149 ARMARX_INFO <<
"No trajectory loaded! Cannot reset to FrameZeroPose!";
153 assureTrajectoryController();
155 jointTrajController->setTrajectory(this->jointTraj,
c);
156 if (moveToFrameZeroPose)
158 std::vector<Ice::DoubleSeq> states = jointTraj->getAllStates(0.0f, 1);
160 NameControlModeMap controlModes;
162 auto dimNames = jointTraj->getDimensionNames();
163 for (
size_t i = 0; i < dimNames.size(); i++)
165 const auto& jointName = dimNames.at(i);
166 if (std::find(usedJoints.begin(), usedJoints.end(), jointName) != usedJoints.end())
168 frameZeroPositions[jointName] = states[i][0];
169 controlModes[jointName] = ePositionControl;
173 kinematicUnit->switchControlMode(controlModes);
174 kinematicUnit->setJointAngles(frameZeroPositions);
181 const Ice::Current&
c)
185 std::unique_lock lock(dataMutex);
187 this->jointTraj = TrajectoryPtr::dynamicCast(jointTraj);
189 if (!this->jointTraj)
191 ARMARX_ERROR <<
"Error when loading TrajectoryPlayer: cannot load jointTraj !!!";
195 if (this->jointTraj->size() == 0)
197 ARMARX_ERROR <<
"Error when loading TrajectoryPlayer: trajectory is empty !!!";
202 auto startTime = this->jointTraj->begin()->getTimestamp();
203 this->jointTraj->shiftTime(-startTime);
204 bool differentJointSet = usedJoints.size() != this->jointTraj->getDimensionNames().size();
205 if (!differentJointSet)
207 for (
size_t i = 0; i < usedJoints.size(); i++)
209 if (usedJoints.at(i) != this->jointTraj->getDimensionNames().at(i))
211 differentJointSet =
true;
216 usedJoints = this->jointTraj->getDimensionNames();
219 if (usedJoints.size() != this->jointTraj->dim())
221 ARMARX_ERROR <<
"Not all trajectory dimensions are named !!! (would cause problems when "
222 "using kinematicUnit)";
226 if (!jointTrajController || differentJointSet || recreateController)
228 jointTrajController = createTrajectoryController(usedJoints,
true);
230 jointTrajController->setTrajectory(this->jointTraj,
c);
232 endTime = jointTrajController->getTrajEndTime();
233 trajEndTime = endTime;
238 const Ice::Current&
c)
240 this->basePoseTraj = TrajectoryPtr::dynamicCast(basePoseTraj);
246 std::unique_lock lock(dataMutex);
248 if (!controllerExists())
250 ARMARX_WARNING <<
"No trajectory has been loaded and therefore no controller exists.";
253 jointTrajController->setLooping(loop);
271 std::unique_lock lock(dataMutex);
273 if (!controllerExists())
275 ARMARX_WARNING <<
"No trajectory has been loaded and therefore no controller exists.";
278 return jointTrajController->getCurrentTrajTime();
286 std::unique_lock lock(dataMutex);
293 assureTrajectoryController();
295 if (jointTrajController->isControllerActive())
297 ARMARX_WARNING <<
"TrajectoryController already running. Please reset it before setting a "
307 bool b = considerConstraintsForTrajectoryOptimization;
308 considerConstraintsForTrajectoryOptimization =
false;
311 jointTraj = jointTraj->normalize(0, endTime);
314 basePoseTraj = basePoseTraj->normalize(0, endTime);
317 jointTrajController->setTrajectory(jointTraj,
c);
319 considerConstraintsForTrajectoryOptimization = b;
325 if (controllerExists() && jointTrajController->isControllerActive())
327 ARMARX_WARNING <<
"TrajectoryController already running. Please reset it before setting "
328 "whether the controller is only for preview.";
331 this->isPreview = isPreview;
335 jointTrajController = createTrajectoryController(usedJoints,
true);
336 jointTrajController->setTrajectory(jointTraj,
c);
343 const Ice::Current&
c)
347 std::unique_lock lock(dataMutex);
349 if (controllerExists())
351 ARMARX_WARNING <<
"TrajectoryController already running. Please reset it before setting "
357 (std::find(usedJoints.begin(), usedJoints.end(), jointName) == usedJoints.end()))
359 usedJoints.push_back(jointName);
363 auto it = std::find(usedJoints.begin(), usedJoints.end(), jointName);
364 if (it != usedJoints.end())
367 usedJoints.pop_back();
373 jointTrajController = createTrajectoryController(usedJoints,
true);
374 jointTrajController->setTrajectory(jointTraj,
c);
384 ARMARX_WARNING <<
"NYI : TrajectoryControllerSubUnit::enableRobotPoseUnit()";
395 if (controllerExists() && jointTrajController->isControllerActive())
397 ARMARX_WARNING <<
"TrajectoryController already running. Please reset it before setting "
398 "whether constraints should be considered.";
401 considerConstraintsForTrajectoryOptimization = consider;
405 jointTrajController = createTrajectoryController(usedJoints,
false);
406 jointTrajController->setTrajectory(jointTraj,
c);
410 NJointTrajectoryControllerPtr
411 TrajectoryControllerSubUnit::createTrajectoryController(std::vector<std::string>& jointNames,
412 bool deleteIfAlreadyExist)
414 std::unique_lock lock(controllerMutex);
416 if (controllerExists() && deleteIfAlreadyExist)
418 jointTrajController->deactivateController();
419 while (jointTrajController->isControllerActive())
423 jointTrajController->deleteController();
430 NJointTrajectoryControllerPtr trajController = jointTrajController;
431 if (!controllerExists() || deleteIfAlreadyExist)
433 recreateController =
false;
434 NJointTrajectoryControllerConfigPtr config =
new NJointTrajectoryControllerConfig();
439 config->maxVelocity = maxVelocity;
440 config->jointNames = jointNames;
441 config->considerConstraints = considerConstraintsForTrajectoryOptimization;
442 config->isPreview = isPreview;
443 controllerName = this->
getName() +
"_" +
"JointTrajectory" + IceUtil::generateUUID();
446 "NJointTrajectoryController", controllerName, config,
true,
true));
453 return trajController;
457 TrajectoryControllerSubUnit::assureTrajectoryController()
459 std::unique_lock lock(controllerMutex);
461 if (!controllerExists())
463 jointTrajController = createTrajectoryController(usedJoints,
false);
469 TrajectoryControllerSubUnit::controllerExists()
471 std::unique_lock lock(controllerMutex);
473 auto allNJointControllers =
475 NJointTrajectoryControllerPtr trajController;
476 for (
const auto&
controller : allNJointControllers)
478 trajController = NJointTrajectoryControllerPtr::dynamicCast(
controller);
483 if (trajController->getName() == controllerName)
485 jointTrajController = trajController;
493 TrajectoryControllerSubUnit::previewRun()
495 if (controllerExists())
497 if (jointTrajController->isControllerActive())
499 std::vector<Ice::DoubleSeq> states =
500 jointTraj->getAllStates(jointTrajController->getCurrentTrajTime(), 1);
503 auto dimNames = jointTraj->getDimensionNames();
504 for (
size_t i = 0; i < dimNames.size(); i++)
506 const auto& jointName = dimNames.at(i);
507 if (std::find(usedJoints.begin(), usedJoints.end(), jointName) != usedJoints.end())
509 targetPositionValues[jointName] = states[i][0];
512 debugDrawer->updateRobotConfig(
"Preview",
"previewRobot", targetPositionValues);
527 const std::set<std::string>& changedProperties)
530 if (changedProperties.count(
"Kp"))
533 kp = getProperty<float>(
"Kp").getValue();
534 recreateController =
true;
537 if (changedProperties.count(
"Kd"))
540 kd = getProperty<float>(
"Kd").getValue();
541 recreateController =
true;
544 if (changedProperties.count(
"Ki"))
547 ki = getProperty<float>(
"Ki").getValue();
548 recreateController =
true;