62 getContext<MotionControlGroupStatechartContext>();
64 NameValueMap targetJointAngles;
65 NameValueMap targetJointVelocities;
66 std::vector<std::string> jointNames;
67 std::vector<float> targetJointValues;
69 if (in.isjointNamesSet() && in.istargetJointValuesSet())
71 jointNames = in.getjointNames();
72 targetJointValues = in.gettargetJointValues();
74 else if (in.isalternativeJointMapSet())
76 auto map = in.getalternativeJointMap();
78 for (
const auto& elem : map)
80 jointNames.push_back(elem.first);
81 targetJointValues.push_back(elem.second);
86 throw LocalException(
"You must either specify the alternativeJointMap param or the "
87 "targetJointValues and jointNames params.");
90 if (jointNames.size() != targetJointValues.size())
92 throw LocalException(
"Sizes of joint name list and joint value list do not match!");
95 if (jointNames.size() == 0)
97 emitEvJointTargetReached();
98 local.setCommandedJoints(NameValueMap());
103 NameControlModeMap controlModes;
104 bool useNativeController = in.getUseNativePositionControl();
105 for (
size_t i = 0; i < jointNames.size(); i++)
107 targetJointAngles[jointNames.at(i)] = targetJointValues.at(i);
108 controlModes[jointNames.at(i)] = useNativeController ? ePositionControl : eVelocityControl;
109 ARMARX_DEBUG <<
"setting joint angle for joint '" << jointNames.at(i) <<
"' to "
110 << targetJointValues.at(i) << std::endl;
114 if (in.isjointMaxSpeedsSet() &&
115 getInput<SingleTypeVariantList>(
"jointMaxSpeeds")->getSize() == (
int)jointNames.size())
119 for (
size_t i = 0; i < jointNames.size(); i++)
121 targetJointVelocities[jointNames.at(i)] = maxJointValues->getVariant(i)->getFloat();
126 float maxVel = in.getjointMaxSpeed();
128 for (
size_t i = 0; i < jointNames.size(); i++)
130 targetJointVelocities[jointNames.at(i)] = maxVel;
136 float tolerance = getInput<float>(
"jointTargetTolerance");
137 NameValueMap jointValueMap;
139 for (
size_t i = 0; i < jointNames.size(); i++)
141 ARMARX_DEBUG <<
"creating condition (" << i <<
" of " << jointNames.size() <<
") for value "
142 << targetJointValues.at(i) <<
flush;
147 {targetJointValues.at(i), tolerance});
148 cond = cond && approx;
149 jointValueMap[jointNames.at(i)] = targetJointValues.at(i);
151 local.setCommandedJoints(jointValueMap);
152 ARMARX_DEBUG <<
"installing condition: EvJointTargetReached" << std::endl;
154 ARMARX_DEBUG <<
"condition installed: EvJointTargetReached" << std::endl;
157 if (useNativeController)
165 for (
auto& joint : jointValueMap)
171 auto currentValues = context->
getRobot()->getConfig()->getRobotNodeJointValueMap();
173 ARMARX_INFO <<
"Joint target tolerance: " << tolerance;
176 NameValueMap currentTargetJointVelocities;
178 float accelerationTime = in.getAccelerationTime();
179 float maxVel = in.getjointMaxSpeed();
180 while (!isRunningTaskStopped())
184 currentValues = context->
getRobot()->getConfig()->getRobotNodeJointValueMap();
188 for (
auto& value : jointValueMap)
190 float delta = fabs((value.second - currentValues[value.first]));
191 maxDelta = std::max(delta, maxDelta);
197 float motionDuration = maxDelta / maxVel;
199 targetJointVelocities.clear();
201 for (
auto& value : jointValueMap)
203 float delta = (value.second - currentValues[value.first]);
204 targetJointVelocities[value.first] = delta / motionDuration;
210 NameValueMap currentDeltas;
211 for (
auto& value : jointValueMap)
213 float delta = ((value.second - currentValues[value.first]));
214 currentDeltas[value.first] = delta;
215 float slowdownFactor =
216 (1 / (1 + exp(-(fabs(delta) - 0.1) / 0.04)));
217 float newVel = std::min(fabs(targetJointVelocities[value.first]),
218 fabs(delta) * slowdownFactor * 10);
219 if (newVel * delta < 0)
223 newVel *= std::min(1.0,
226 currentTargetJointVelocities[value.first] = newVel;
230 <<
"Current joint target velocities: " << currentTargetJointVelocities;
232 context->
getKinematicUnit()->setJointVelocities(currentTargetJointVelocities);
235 if (in.getSetVelocitiesToZeroAtEnd())
237 for (
auto& joint : jointValueMap)
239 targetJointVelocities[joint.first] = 0.0f;
245 if (in.getHoldPositionAfterwards())
247 currentValues = context->
getRobot()->getConfig()->getRobotNodeJointValueMap();
249 for (
auto& joint : jointValueMap)
251 controlModes[joint.first] = ePositionControl;