15 std::map<std::string, float>
16 MoveJointsToPosition::filterJointTargetValues(
18 const std::vector<std::string>& disabledJoints)
const
20 std::map<std::string, float> filtered;
21 for (
const auto& [jointName,
v] : jointsTargetValues)
23 if (std::find(disabledJoints.begin(), disabledJoints.end(), jointName) !=
27 <<
" because it was explictly disabled in skill params.";
30 filtered[jointName] =
v;
38 auto filtered = this->filterJointTargetValues(in.parameters.jointsTargetValues,
39 in.parameters.disabledJoints);
41 auto jointNames = simox::alg::get_keys(filtered);
44 return {::armarx::skills::TerminatedSkillStatus::Succeeded};
50 auto filtered = this->filterJointTargetValues(in.parameters.jointsTargetValues,
51 in.parameters.disabledJoints);
53 auto jointNames = simox::alg::get_keys(filtered);
65 if (not checkJointsTargetValuesValid(in.parameters.jointsTargetValues, virtualRobot))
76 catch (
const armarx::KinematicUnitUnavailable& e)
78 ARMARX_IMPORTANT <<
"Failed to request joints. Terminating. Joints unavailable:"
86 switchControlMode(filtered, ePositionControl);
88 if (in.parameters.inSimulation)
92 moveJointsSimulation(filtered,
93 in.parameters.simulationJointSpeed,
94 in.parameters.simulationJointSpeedOverride,
102 in.parameters.accuracy,
103 in.parameters.accuracyOverride,
104 in.parameters.timeoutMS,
105 in.parameters.timeoutOverrideMS,
112 MoveJointsToPosition::checkJointsTargetValuesValid(
const NameValueMap& jointsTargetValues,
115 for (
const auto& [jointName, targetValue] : jointsTargetValues)
117 if (not virtualRobot->hasRobotNode(jointName))
119 ARMARX_ERROR <<
"Robot has no joint '" << jointName <<
"'";
122 const VirtualRobot::RobotNodePtr robotNode = virtualRobot->getRobotNode(jointName);
123 if (not robotNode->checkJointLimits(targetValue))
125 ARMARX_ERROR <<
"Target value '" << targetValue <<
"' for joint '" << jointName
126 <<
"' out of limits (" << robotNode->getJointLimitLo() <<
" - "
127 << robotNode->getJointLimitHi() <<
")";
135 MoveJointsToPosition::switchControlMode(
const NameValueMap& jointsTargetValues,
136 const ControlMode controlMode)
138 armarx::NameControlModeMap modeMap;
139 for (
const auto& [jointName, _] : jointsTargetValues)
141 modeMap[jointName] = controlMode;
147 MoveJointsToPosition::moveJoints(
const NameValueMap& jointsTargetValues,
148 const float accuracyDefault,
150 const float timeoutDefault,
157 ARMARX_INFO <<
"Waiting until target values are reached";
162 auto do_if_terminate_and_not_reached = [&]()
164 ARMARX_INFO <<
"Skill got aborted, stopping movement";
167 <<
"Robot state synchronization failed";
170 switchControlMode(jointsTargetValues, eVelocityControl);
173 for (
auto& [jointName, _] : jointsTargetValues)
175 targetJointVelocities[jointName] = 0;
177 srv_.
kinematicUnit->setJointVelocities(targetJointVelocities);
184 bool reachedOrTimedOut =
false;
188 for (
const auto& [jointName, _] : jointsTargetValues)
190 if (timeoutOverride.find(jointName) == timeoutOverride.end())
192 timeoutOverride[jointName] = timeoutDefault;
197 std::map<std::string, bool> timeoutedJoints;
198 for (
const auto& [jointName, _] : jointsTargetValues)
200 timeoutedJoints[jointName] =
false;
207 std::map<std::string, armarx::DateTime> lastMotion;
208 for (
const auto& [jointName, _] : jointsTargetValues)
213 while (not reachedOrTimedOut)
218 <<
"Robot state synchronization failed";
220 reachedOrTimedOut =
true;
221 for (
const auto& [jointName, targetValue] : jointsTargetValues)
223 if (not timeoutedJoints[jointName])
226 const float currentValue =
227 virtualRobot->getRobotNode(jointName)->getJointValue();
228 const float accuracy = accuracyOverride.count(jointName) > 0
229 ? accuracyOverride.at(jointName)
233 <<
"' is: " <<
std::abs(targetValue - currentValue);
236 if (
std::abs(currentValue - pastJointAngles[jointName]) > accuracy)
239 pastJointAngles[jointName] = currentValue;
244 if ((timeoutOverride[jointName] >= 0) &&
245 ((
DateTime::Now() - lastMotion[jointName]).toMilliSecondsDouble() >=
246 timeoutOverride[jointName]))
248 timeoutedJoints[jointName] =
true;
249 ARMARX_INFO <<
"Joint " << jointName <<
" has reached its timeout.";
252 if (
std::abs(targetValue - currentValue) > accuracy)
256 <<
"' is higer than accuracy: " <<
std::abs(targetValue - currentValue)
257 <<
" > " << accuracy <<
"(accuracy)";
258 reachedOrTimedOut =
false;
264 clock.waitFor(sleepTime);
269 MoveJointsToPosition::moveJointsSimulation(
const NameValueMap& jointsTargetValues,
270 const float speedDefault,
276 <<
"Robot state synchronization failed";
280 double maxDuration = 0;
282 for (
const auto& [jointName, targetValue] : jointsTargetValues)
285 speedOverride.count(jointName) > 0 ? speedOverride.at(jointName) : speedDefault;
287 const float currentValue = virtualRobot->getRobotNode(jointName)->getJointValue();
288 const double duration =
std::abs(targetValue - currentValue) / speed;
291 track[0] = currentValue;
292 track[duration] = targetValue;
294 maxDuration =
std::max(maxDuration, duration);
297 ARMARX_INFO <<
"Simulating trajectory (duration: " << maxDuration <<
"s)";
305 while (t < maxDuration)
311 for (
const auto& [name, _] : jointsTargetValues)
313 const float value = std::get<float>(trajectory[name].at(t));
314 nextValues[name] =
value;
319 clock.waitFor(sleepTime);