30 #include <VirtualRobot/math/Helpers.h>
31 #include <VirtualRobot/MathTools.h>
38 : _ik(ik), _robotUnit(robotUnit), _controllerName(controllerName), _config(config), _arm(arm)
40 _runCfg = _config->runCfg;
41 _velocityBaseSettings.
basePosVel = _runCfg.maxTcpPosVel;
42 _velocityBaseSettings.
baseOriVel = _runCfg.maxTcpOriVel;
44 _userNullspaceTargets = std::vector<float>(arm.
rns->getSize(), std::nanf(
""));
45 _naturalNullspaceTargets = std::vector<float>(arm.
rns->getSize(), std::nanf(
""));
47 VirtualRobot::RobotNodeSetPtr rns = arm.
rns;
48 for (
size_t i = 0; i < rns->getSize(); i++)
50 VirtualRobot::RobotNodePtr rn = rns->getNode(i);
55 _rnsToElb.emplace_back(rn);
62 _defaultWaypointConfigs[
"default"] = wpc;
64 _ftOffset.force = Eigen::Vector3f::Zero();
65 _ftOffset.torque = Eigen::Vector3f::Zero();
70 NJointCartesianNaturalPositionControllerConfigPtr cfg =
new NJointCartesianNaturalPositionControllerConfig();
72 cfg->elbowNode = elbowNode;
78 NJointControllerInterfacePrx ctrl = _robotUnit->getNJointController(_controllerName);
81 _controllerCreated =
false;
82 _controller = NJointCartesianNaturalPositionControllerInterfacePrx::checkedCast(ctrl);
83 _controller->setConfig(_runCfg);
87 ctrl = _robotUnit->createNJointController(
"NJointCartesianNaturalPositionController", _controllerName, _config);
88 _controller = NJointCartesianNaturalPositionControllerInterfacePrx::checkedCast(ctrl);
89 _controllerCreated =
true;
91 if (_activateControllerOnInit)
93 _controller->activateController();
100 _tcpTarget = tcpTarget;
107 ARMARX_ERROR <<
"could not solve natural IK for target: " << tcpTarget;
110 _elbTarget = _arm.
elbow->getPoseInRootFrame();
112 if (_setJointNullspaceFromNaturalIK)
114 for (
size_t i = 0; i < _rnsToElb.size(); i++)
116 _naturalNullspaceTargets.at(i) = ikResult.
jointValues(i);
119 updateNullspaceTargets();
127 _userNullspaceTargets = nullspaceTargets;
128 updateNullspaceTargets();
167 _ftOffset = _controller->getAverageFTValue();
168 _controller->setFTOffset(_ftOffset);
177 _controller->setFTLimit(force, torque);
182 _controller->clearFTLimit();
188 FTSensorValue ft = _controller->getCurrentFTValue();
191 ft.force = ft.force - _ftOffset.force;
192 ft.torque = ft.torque - _ftOffset.torque;
198 FTSensorValue ft = _controller->getAverageFTValue();
201 ft.force = ft.force - _ftOffset.force;
202 ft.torque = ft.torque - _ftOffset.torque;
209 _controller->stopMovement();
210 _controller->clearFTLimit();
211 _controller->setNullspaceControlEnabled(
true);
217 void CartesianNaturalPositionControllerProxy::updateDynamicKp()
224 _dynamicKp.
calculate(error, KpElb, KpJla);
226 _runCfg.elbowKp = KpElb;
227 _runCfg.jointLimitAvoidanceKp = KpJla;
228 _controller->setConfig(_runCfg);
234 void CartesianNaturalPositionControllerProxy::updateNullspaceTargets()
236 std::vector<float> nsTargets = _userNullspaceTargets;
237 for (
size_t i = 0; i < _naturalNullspaceTargets.size(); i++)
239 if (std::isnan(nsTargets.at(i)))
241 nsTargets.at(i) = _naturalNullspaceTargets.at(i);
244 _controller->setNullspaceTarget(nsTargets);
249 float f = std::exp(-0.5f * (error * error) / (
sigmaMM *
sigmaMM));
251 KpJla = (1 - f) *
maxKp;
256 _controller->setConfig(runCfg);
257 this->_runCfg = runCfg;
267 if (_waypoints.size() == 0)
269 _waypointChanged =
true;
271 _waypoints.emplace_back(waypoint);
282 w.
config = _defaultWaypointConfigs[
"default"];
293 w.
config = _defaultWaypointConfigs[
"default"];
304 _currentWaypointIndex = 0;
309 _defaultWaypointConfigs[
"default"] = config;
314 std::stringstream ss;
316 ss << std::fixed <<
"Waypoint: " << (_currentWaypointIndex + 1) <<
"/" << _waypoints.size() <<
" distance: " <<
getTcpPositionError() <<
" mm " << VirtualRobot::MathTools::rad2deg(
getTcpOrientationError()) <<
" deg";
322 size_t len = rns->getSize();
323 std::vector<Eigen::Vector3f> positions;
324 for (
size_t i = 0; i < len; i++)
326 positions.push_back(rns->getNode(i)->getPositionInRootFrame());
328 positions.push_back(rns->getTCP()->getPositionInRootFrame());
330 std::vector<float> dists;
331 for (
size_t i = 0; i < len; i++)
333 dists.push_back((positions.at(i) - positions.at(i + 1)).norm());
336 std::vector<float> result(len, 0);
338 for (
int i = len - 1; i >= 0; i--)
341 result.at(i) = maxPosVel / dist;
348 std::vector<float> result(vec.size(), 0);
349 for (
size_t i = 0; i < vec.size(); i++)
351 result.at(i) = vec.at(i) * scale;
358 _activateControllerOnInit =
value;
365 float scale = referencePosVel /
v.basePosVel;
366 _runCfg.maxTcpPosVel =
v.basePosVel *
v.scaleTcpPosVel * scale;
367 _runCfg.maxTcpOriVel =
v.baseOriVel *
v.scaleTcpOriVel * scale;
368 _runCfg.maxElbPosVel =
v.basePosVel *
v.scaleElbPosVel * scale;
369 _runCfg.maxJointVelocity =
ScaleVec(
v.baseJointVelocity,
v.scaleJointVelocities * scale);
370 _runCfg.maxNullspaceVelocity =
ScaleVec(
v.baseJointVelocity,
v.scaleNullspaceVelocities * scale);
374 _runCfg.jointLimitAvoidanceKp = k.
baseKpJla;
375 _runCfg.nullspaceTargetKp = k.
baseKpNs;
380 _controller->setConfig(_runCfg);
388 _kpBaseSettings.
baseKpJla = _runCfg.jointLimitAvoidanceKp;
389 _kpBaseSettings.
baseKpNs = _runCfg.nullspaceTargetKp;
397 if (_controllerCreated)
400 _controller->deactivateAndDeleteController();
405 _controller->deactivateController();
407 _controllerCreated =
false;
412 if (_waypoints.size() == 0)
420 _currentWaypointIndex++;
421 _waypointChanged =
true;
423 if (_waypointChanged)
425 _waypointChanged =
false;
427 return onWaypointChanged();
432 bool CartesianNaturalPositionControllerProxy::onWaypointChanged()
436 _userNullspaceTargets = w.targets.userNullspaceTargets;
437 ARMARX_IMPORTANT <<
"Waypoint target position: " << math::Helpers::GetPosition(w.targets.tcpTarget).transpose();
438 return setTarget(w.targets.tcpTarget, w.targets.setOri, w.targets.minElbowHeight);
444 return _waypoints.at(_currentWaypointIndex);
449 return _waypoints.size() == 0 || _currentWaypointIndex == _waypoints.size() - 1;
459 _dynamicKp = dynamicKp;
476 this->config = config;
482 ARMARX_CHECK(this->targets.userNullspaceTargets.size() == userNullspaceTargets.size());
483 this->targets.userNullspaceTargets = userNullspaceTargets;
524 : jointValues(rns->getJointValues()), rns(rns)
531 rns->setJointValues(jointValues);