17 std::map<std::string, float>
18 MoveJointsToPosition::filterJointTargetValues(
19 const NameValueMap& jointsTargetValues,
20 const std::vector<std::string>& disabledJoints)
const
22 std::map<std::string, float> filtered;
23 for (
const auto& [jointName, v] : jointsTargetValues)
25 if (std::find(disabledJoints.begin(), disabledJoints.end(), jointName) !=
29 <<
" because it was explictly disabled in skill params.";
32 filtered[jointName] = v;
40 auto filtered = this->filterJointTargetValues(in.parameters.jointsTargetValues,
41 in.parameters.disabledJoints);
43 auto jointNames = simox::alg::get_keys(filtered);
49 ::armarx::skills::Skill::MainResult
52 auto filtered = this->filterJointTargetValues(in.parameters.jointsTargetValues,
53 in.parameters.disabledJoints);
55 auto jointNames = simox::alg::get_keys(filtered);
59 srv_.virtualRobotReader.getRobot(srv_.robotName);
62 ARMARX_ERROR <<
"Could not get robot '" << srv_.robotName <<
"'";
67 if (not checkJointsTargetValuesValid(in.parameters.jointsTargetValues, virtualRobot))
76 srv_.kinematicUnit->requestJoints(jointNames);
78 catch (
const armarx::KinematicUnitUnavailable& e)
80 ARMARX_IMPORTANT <<
"Failed to request joints. Terminating. Joints unavailable:"
88 switchControlMode(filtered, ePositionControl);
90 if (in.parameters.inSimulation)
94 moveJointsSimulation(filtered,
95 in.parameters.simulationJointSpeed,
96 in.parameters.simulationJointSpeedOverride,
104 in.parameters.accuracy,
105 in.parameters.accuracyOverride,
106 in.parameters.timeoutMS,
107 in.parameters.timeoutOverrideMS,
114 MoveJointsToPosition::checkJointsTargetValuesValid(
const NameValueMap& jointsTargetValues,
117 for (
const auto& [jointName, targetValue] : jointsTargetValues)
119 if (not virtualRobot->hasRobotNode(jointName))
121 ARMARX_ERROR <<
"Robot has no joint '" << jointName <<
"'";
124 const VirtualRobot::RobotNodePtr robotNode = virtualRobot->getRobotNode(jointName);
125 if (not robotNode->checkJointLimits(targetValue))
127 ARMARX_ERROR <<
"Target value '" << targetValue <<
"' for joint '" << jointName
128 <<
"' out of limits (" << robotNode->getJointLimitLo() <<
" - "
129 << robotNode->getJointLimitHi() <<
")";
137 MoveJointsToPosition::switchControlMode(
const NameValueMap& jointsTargetValues,
138 const ControlMode controlMode)
140 armarx::NameControlModeMap modeMap;
141 for (
const auto& [jointName, _] : jointsTargetValues)
143 modeMap[jointName] = controlMode;
145 srv_.kinematicUnit->switchControlMode(modeMap);
149 MoveJointsToPosition::moveJoints(
const NameValueMap& jointsTargetValues,
150 const float accuracyDefault,
151 const NameValueMap& accuracyOverride,
152 const float timeoutDefault,
153 NameValueMap timeoutOverride,
156 srv_.kinematicUnit->setJointAngles(jointsTargetValues);
159 ARMARX_INFO <<
"Waiting until target values are reached";
164 auto do_if_terminate_and_not_reached = [&]()
166 ARMARX_INFO <<
"Skill got aborted, stopping movement";
167 ARMARX_CHECK(srv_.virtualRobotReader.synchronizeRobotJoints(*virtualRobot,
169 <<
"Robot state synchronization failed";
172 switchControlMode(jointsTargetValues, eVelocityControl);
175 for (
auto& [jointName, _] : jointsTargetValues)
177 targetJointVelocities[jointName] = 0;
179 srv_.kinematicUnit->setJointVelocities(targetJointVelocities);
186 bool reachedOrTimedOut =
false;
190 for (
const auto& [jointName, _] : jointsTargetValues)
192 if (timeoutOverride.find(jointName) == timeoutOverride.end())
194 timeoutOverride[jointName] = timeoutDefault;
199 std::map<std::string, bool> timeoutedJoints;
200 for (
const auto& [jointName, _] : jointsTargetValues)
202 timeoutedJoints[jointName] =
false;
206 NameValueMap pastJointAngles = srv_.kinematicUnit->getJointAngles();
209 std::map<std::string, armarx::DateTime> lastMotion;
210 for (
const auto& [jointName, _] : jointsTargetValues)
215 while (not reachedOrTimedOut)
218 ARMARX_CHECK(srv_.virtualRobotReader.synchronizeRobotJoints(*virtualRobot,
220 <<
"Robot state synchronization failed";
222 reachedOrTimedOut =
true;
223 for (
const auto& [jointName, targetValue] : jointsTargetValues)
225 if (not timeoutedJoints[jointName])
228 const float currentValue =
229 virtualRobot->getRobotNode(jointName)->getJointValue();
230 const float accuracy = accuracyOverride.count(jointName) > 0
231 ? accuracyOverride.at(jointName)
235 <<
"' is: " << std::abs(targetValue - currentValue);
238 if (std::abs(currentValue - pastJointAngles[jointName]) > accuracy)
241 pastJointAngles[jointName] = currentValue;
246 if ((timeoutOverride[jointName] >= 0) &&
247 ((
DateTime::Now() - lastMotion[jointName]).toMilliSecondsDouble() >=
248 timeoutOverride[jointName]))
250 timeoutedJoints[jointName] =
true;
251 ARMARX_INFO <<
"Joint " << jointName <<
" has reached its timeout.";
254 if (std::abs(targetValue - currentValue) > accuracy)
258 <<
"' is higer than accuracy: " << std::abs(targetValue - currentValue)
259 <<
" > " << accuracy <<
"(accuracy)";
260 reachedOrTimedOut =
false;
266 clock.waitFor(sleepTime);
271 MoveJointsToPosition::moveJointsSimulation(
const NameValueMap& jointsTargetValues,
272 const float speedDefault,
273 const NameValueMap& speedOverride,
278 <<
"Robot state synchronization failed";
281 armarx::trajectory::Trajectory trajectory;
282 double maxDuration = 0;
284 for (
const auto& [jointName, targetValue] : jointsTargetValues)
287 speedOverride.count(jointName) > 0 ? speedOverride.at(jointName) : speedDefault;
289 const float currentValue = virtualRobot->getRobotNode(jointName)->getJointValue();
290 const double duration = std::abs(targetValue - currentValue) / speed;
295 track[0] = currentValue;
296 track[duration] = targetValue;
298 maxDuration = std::max(maxDuration, duration);
301 ARMARX_INFO <<
"Simulating trajectory (duration: " << maxDuration <<
"s)";
309 while (t < maxDuration)
315 for (
const auto& [name, _] : jointsTargetValues)
317 const float value = std::get<float>(trajectory[name].at(t));
318 nextValues[name] =
value;
321 srv_.kinematicUnit->setJointAngles(nextValues);
323 clock.waitFor(sleepTime);
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
::armarx::skills::SkillDescription GetSkillDescription()
MoveJointsToPosition(const Services &)
arondto::MoveJointsToPositionParams ParamType
static MainResult MakeSucceededResult(aron::data::DictPtr data=nullptr)
virtual MainResult main()
Override this method with the actual implementation.
void throwIfSkillShouldTerminate(const std::string &abortedMessage="") const
virtual ExitResult exit()
Override this method with the actual implementation.
static MainResult MakeFailedResult(aron::data::DictPtr data=nullptr)
VariantTrack & addTrack(const TrackID &id)
Add track with the given ID (and no update function).
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
#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.
std::shared_ptr< class Robot > RobotPtr
armarx::core::time::Duration Duration
Track< VariantValue > VariantTrack
A track with value type TValue.
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< Value > value()
This file is part of ArmarX.
std::chrono::system_clock Clock
::armarx::KinematicUnitInterfacePrx kinematicUnit
A result struct for skill exit function.