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);
159 NameValueMap frameZeroPositions;
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())
366 std::swap(*it, usedJoints.back());
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);
410NJointTrajectoryControllerPtr
411TrajectoryControllerSubUnit::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();
445 NJointTrajectoryControllerPtr::dynamicCast(robotUnit->createNJointController(
446 "NJointTrajectoryController", controllerName, config,
true,
true));
453 return trajController;
457TrajectoryControllerSubUnit::assureTrajectoryController()
459 std::unique_lock lock(controllerMutex);
461 if (!controllerExists())
463 jointTrajController = createTrajectoryController(usedJoints,
false);
469TrajectoryControllerSubUnit::controllerExists()
471 std::unique_lock lock(controllerMutex);
473 auto allNJointControllers =
474 robotUnit->getNJointControllersNotNull(robotUnit->getNJointControllerNames());
475 NJointTrajectoryControllerPtr trajController;
476 for (
const auto& controller : allNJointControllers)
478 trajController = NJointTrajectoryControllerPtr::dynamicCast(controller);
483 if (trajController->getName() == controllerName)
485 jointTrajController = trajController;
493TrajectoryControllerSubUnit::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"))
534 recreateController =
true;
537 if (changedProperties.count(
"Kd"))
541 recreateController =
true;
544 if (changedProperties.count(
"Ki"))
548 recreateController =
true;
Property< PropertyType > getProperty(const std::string &name)
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
std::string getName() const
Retrieve name of object.
IceManagerPtr getIceManager() const
Returns the IceManager.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
The periodic task executes one thread method repeatedly using the time period specified in the constr...
The RobotUnit class manages a robot and its controllers.
Ice::Double getTrajEndTime(const Ice::Current &=Ice::emptyCurrent) override
void considerConstraints(bool, const Ice::Current &=Ice::emptyCurrent) override
void onInitComponent() override
Pure virtual hook for the subclass.
bool startTrajectoryPlayer(const Ice::Current &=Ice::emptyCurrent) override
Ice::Double getCurrentTime(const Ice::Current &=Ice::emptyCurrent) override
virtual void componentPropertiesUpdated(const std::set< std::string > &changedProperties) override
Implement this function if you would like to react to changes in the properties.
void loadBasePoseTraj(const TrajectoryBasePtr &basePoseTraj, const Ice::Current &=Ice::emptyCurrent) override
void onDisconnectComponent() override
Hook for subclass.
void update(const SensorAndControl &sc, const JointAndNJointControllers &c) override
bool stopTrajectoryPlayer(const Ice::Current &=Ice::emptyCurrent) override
bool pauseTrajectoryPlayer(const Ice::Current &=Ice::emptyCurrent) override
void setup(RobotUnit *rUnit)
Ice::Double getEndTime(const Ice::Current &=Ice::emptyCurrent) override
void onConnectComponent() override
Pure virtual hook for the subclass.
bool resetTrajectoryPlayer(bool moveToFrameZeroPose, const Ice::Current &=Ice::emptyCurrent) override
void setIsPreview(bool, const Ice::Current &=Ice::emptyCurrent) override
void enableRobotPoseUnit(bool, const Ice::Current &=Ice::emptyCurrent) override
void setEndTime(Ice::Double, const Ice::Current &=Ice::emptyCurrent) override
bool setJointsInUse(const std::string &, bool, const Ice::Current &=Ice::emptyCurrent) override
void setLoopPlayback(bool loop, const Ice::Current &=Ice::emptyCurrent) override
void loadJointTraj(const TrajectoryBasePtr &jointTraj, const Ice::Current &=Ice::emptyCurrent) override
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_INFO
The normal logging level.
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
This file offers overloads of toIce() and fromIce() functions for STL container types.
detail::ControlThreadOutputBufferEntry SensorAndControl
Structure used by the RobotUnit to swap lists of Joint and NJoint controllers.