13 std::map<std::string, float>
14 MoveJointsToPosition::filterJointTargetValues(
16 const std::vector<std::string>& disabledJoints)
const
18 std::map<std::string, float> filtered;
19 for (
const auto& [jointName,
v] : jointsTargetValues)
21 if (std::find(disabledJoints.begin(), disabledJoints.end(), jointName) !=
25 <<
" because it was explictly disabled in skill params.";
28 filtered[jointName] =
v;
36 auto filtered = this->filterJointTargetValues(in.parameters.jointsTargetValues,
37 in.parameters.disabledJoints);
39 auto jointNames = simox::alg::get_keys(filtered);
42 return {::armarx::skills::TerminatedSkillStatus::Succeeded};
48 auto filtered = this->filterJointTargetValues(in.parameters.jointsTargetValues,
49 in.parameters.disabledJoints);
51 auto jointNames = simox::alg::get_keys(filtered);
63 if (not checkJointsTargetValuesValid(in.parameters.jointsTargetValues, virtualRobot))
74 catch (
const armarx::KinematicUnitUnavailable& e)
76 ARMARX_IMPORTANT <<
"Failed to request joints. Terminating. Joints unavailable:"
84 switchControlMode(filtered, ePositionControl);
86 if (in.parameters.inSimulation)
90 moveJointsSimulation(filtered,
91 in.parameters.simulationJointSpeed,
92 in.parameters.simulationJointSpeedOverride,
100 in.parameters.accuracy,
101 in.parameters.accuracyOverride,
102 in.parameters.timeoutMS,
103 in.parameters.timeoutOverrideMS,
110 MoveJointsToPosition::checkJointsTargetValuesValid(
const NameValueMap& jointsTargetValues,
113 for (
const auto& [jointName, targetValue] : jointsTargetValues)
115 if (not virtualRobot->hasRobotNode(jointName))
117 ARMARX_ERROR <<
"Robot has no joint '" << jointName <<
"'";
120 const VirtualRobot::RobotNodePtr robotNode = virtualRobot->getRobotNode(jointName);
121 if (not robotNode->checkJointLimits(targetValue))
123 ARMARX_ERROR <<
"Target value '" << targetValue <<
"' for joint '" << jointName
124 <<
"' out of limits (" << robotNode->getJointLimitLo() <<
" - "
125 << robotNode->getJointLimitHi() <<
")";
133 MoveJointsToPosition::switchControlMode(
const NameValueMap& jointsTargetValues,
134 const ControlMode controlMode)
136 armarx::NameControlModeMap modeMap;
137 for (
const auto& [jointName, _] : jointsTargetValues)
139 modeMap[jointName] = controlMode;
145 MoveJointsToPosition::moveJoints(
const NameValueMap& jointsTargetValues,
146 const float accuracyDefault,
148 const float timeoutDefault,
155 ARMARX_INFO <<
"Waiting until target values are reached";
160 auto do_if_terminate_and_not_reached = [&]()
162 ARMARX_INFO <<
"Skill got aborted, stopping movement";
165 <<
"Robot state synchronization failed";
168 switchControlMode(jointsTargetValues, eVelocityControl);
171 for (
auto& [jointName, _] : jointsTargetValues)
173 targetJointVelocities[jointName] = 0;
175 srv_.
kinematicUnit->setJointVelocities(targetJointVelocities);
182 bool reachedOrTimedOut =
false;
186 for (
const auto& [jointName, _] : jointsTargetValues)
188 if (timeoutOverride.find(jointName) == timeoutOverride.end())
190 timeoutOverride[jointName] = timeoutDefault;
195 std::map<std::string, bool> timeoutedJoints;
196 for (
const auto& [jointName, _] : jointsTargetValues)
198 timeoutedJoints[jointName] =
false;
205 std::map<std::string, armarx::DateTime> lastMotion;
206 for (
const auto& [jointName, _] : jointsTargetValues)
211 while (not reachedOrTimedOut)
216 <<
"Robot state synchronization failed";
218 reachedOrTimedOut =
true;
219 for (
const auto& [jointName, targetValue] : jointsTargetValues)
221 if (not timeoutedJoints[jointName])
224 const float currentValue =
225 virtualRobot->getRobotNode(jointName)->getJointValue();
226 const float accuracy = accuracyOverride.count(jointName) > 0
227 ? accuracyOverride.at(jointName)
231 <<
"' is: " <<
std::abs(targetValue - currentValue);
234 if (
std::abs(currentValue - pastJointAngles[jointName]) > accuracy)
237 pastJointAngles[jointName] = currentValue;
242 if ((timeoutOverride[jointName] >= 0) &&
243 ((
DateTime::Now() - lastMotion[jointName]).toMilliSecondsDouble() >=
244 timeoutOverride[jointName]))
246 timeoutedJoints[jointName] =
true;
247 ARMARX_INFO <<
"Joint " << jointName <<
" has reached its timeout.";
250 if (
std::abs(targetValue - currentValue) > accuracy)
254 <<
"' is higer than accuracy: " <<
std::abs(targetValue - currentValue)
255 <<
" > " << accuracy <<
"(accuracy)";
256 reachedOrTimedOut =
false;
262 clock.waitFor(sleepTime);
267 MoveJointsToPosition::moveJointsSimulation(
const NameValueMap& jointsTargetValues,
268 const float speedDefault,
274 <<
"Robot state synchronization failed";
278 double maxDuration = 0;
280 for (
const auto& [jointName, targetValue] : jointsTargetValues)
283 speedOverride.count(jointName) > 0 ? speedOverride.at(jointName) : speedDefault;
285 const float currentValue = virtualRobot->getRobotNode(jointName)->getJointValue();
286 const double duration =
std::abs(targetValue - currentValue) / speed;
289 track[0] = currentValue;
290 track[duration] = targetValue;
292 maxDuration =
std::max(maxDuration, duration);
295 ARMARX_INFO <<
"Simulating trajectory (duration: " << maxDuration <<
"s)";
303 while (t < maxDuration)
309 for (
const auto& [name, _] : jointsTargetValues)
311 const float value = std::get<float>(trajectory[name].at(t));
312 nextValues[name] =
value;
317 clock.waitFor(sleepTime);