6#include <range/v3/algorithm/contains.hpp>
8#include <SimoxUtility/algorithm/string/string_tools.h>
18#include <RobotAPI/interface/core/NameValueMap.h>
24#include <armarx/control/skills/aron/HandParam.aron.generated.h>
25#include <armarx/control/skills/aron/ShapeHandParams.aron.generated.h>
34 removePrefixFromKeys(
const NameValueMap& map,
const std::string& prefix)
37 for (
const auto& [key, value] : map)
39 if (simox::alg::starts_with(key, prefix))
41 result[key.substr(prefix.size())] =
value;
63 if ((not params.shapeName) and (not params.jointAngles))
65 ARMARX_ERROR <<
"Exactly one of the parameters 'shapeName' and 'jointAngles' must be "
66 "set. However, neither has been set.";
70 if (params.shapeName and params.jointAngles)
72 ARMARX_ERROR <<
"Exactly one of the parameters 'shapeName' and 'jointAngles' must be "
73 "set. However, both have been set.";
83 const auto shape = params.shapeName.value();
99 ARMARX_INFO <<
"Setting shape of hand '" << handUnit->getHandName() <<
"' to '" << shape
102 handUnit->setShape(shape);
104 if (params.waitUntilFinished)
106 return waitUntilFinished(handUnit->getShapeJointValues(shape));
112 const auto jointAngles = params.jointAngles.value();
114 ARMARX_INFO <<
"Setting joint angles of hand '" << handUnit->getHandName()
115 <<
"' to: " << jointAngles;
117 handUnit->setJointAngles(jointAngles);
119 if (params.waitUntilFinished)
121 return waitUntilFinished(jointAngles);
127 armarx::skills::Skill::MainResult
128 ShapeHand::waitUntilFinished(
const NameValueMap& targetAnglesRaw)
130 if (p_.disableWaitUntilFinished)
137 auto& handUnit = srv_.getHandUnit(params.hand);
138 const auto movementTimeout = params.waitUntilFinished->movementTimeout;
140 ARMARX_INFO <<
"Waiting until target position is reached"
141 << (movementTimeout ?
" or movement times out" :
"");
143 const std::string handPrefix = params.hand == ::armarx::control::skills::arondto::Hand::Left
147 const NameValueMap targetAngles = removePrefixFromKeys(targetAnglesRaw, handPrefix);
152 ::armarx::DateTime time;
153 } pastJointAngles{.angles = handUnit->getCurrentJointValues(), .time =
DateTime::Now()};
158 bool reached =
false;
159 bool movementTimedOut =
false;
165 const auto currentAngles =
166 removePrefixFromKeys(handUnit->getCurrentJointValues(), handPrefix);
170 for (
const auto& [jointName, targetAngle] : targetAngles)
173 <<
"Joint '" << jointName <<
"' not found in current joint angles.";
174 if (std::abs(targetAngle - currentAngles.at(jointName)) > getAccuracy(jointName))
179 <<
"' is: " << std::abs(targetAngle - currentAngles.at(jointName)) <<
" > "
180 << getAccuracy(jointName) <<
"(accuracy)";
186 if (movementTimeout and (
DateTime::Now() - pastJointAngles.time).toSecondsDouble() >=
187 movementTimeout.value())
189 movementTimedOut =
true;
190 for (
const auto& [jointName, targetAngle] : targetAngles)
192 if (std::abs(currentAngles.at(jointName) -
193 pastJointAngles.angles.at(jointName)) > getAccuracy(jointName))
195 movementTimedOut =
false;
197 <<
deactivateSpam(1) <<
"Joint '" << jointName <<
"' still moving: "
198 << std::abs(currentAngles.at(jointName) -
199 pastJointAngles.angles.at(jointName))
200 <<
" > " << getAccuracy(jointName) <<
"(accuracy)"
201 <<
"in the last " << movementTimeout.value() <<
"s (movement timeout)";
205 pastJointAngles = {.angles = currentAngles, .time =
DateTime::Now()};
208 clock.waitFor(sleepTime);
215 else if (movementTimedOut)
217 ARMARX_INFO <<
"Movement timed out! Joints stopped moving. Not waiting anymore until "
218 "target postion is reached";
223 <<
"Skill aborted. Not waiting anymore until target position is reached";
231 ShapeHand::getAccuracy(
const std::string& jointName)
235 const auto accuracy = params.waitUntilFinished->accuracy;
236 const auto accuracyOverride = params.waitUntilFinished->accuracyOverride;
238 return accuracyOverride.count(jointName) > 0 ? accuracyOverride.at(jointName) : accuracy;
253 using Parameters = arondto::ShapeHandParams;
258 [inParams](Parameters& params)
260 params.hand = inParams.hand;
261 params.shapeName =
"Open";
262 params.waitUntilFinished =
263 inParams.waitUntilFinished;
286 using Parameters = arondto::ShapeHandParams;
291 [&inParams](Parameters& params)
293 params.hand = inParams.hand;
294 params.shapeName =
"Close";
295 params.waitUntilFinished =
296 params.waitUntilFinished;
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()
::armarx::skills::SkillDescription GetSkillDescription()
ShapeHand(const HandUnitServices &, Properties p)
::armarx::skills::SkillDescription GetSkillDescription()
std::optional< ProviderID > providerId
std::optional< TerminatedSkillStatusUpdate > callSubskill(const SkillID &skillId)
Call a subskill with the given ID and its default parameters.
static MainResult MakeSucceededResult(aron::data::DictPtr data=nullptr)
static MainResult MakeAbortedResult(aron::data::DictPtr data=nullptr)
bool shouldSkillTerminate() const override
Returns whether the skill should terminate as soon as possible.
SkillID getSkillId() const
Get the id of the skill.
static MainResult MakeFailedResult(aron::data::DictPtr data=nullptr)
arondto::ShapeHandParams getParameters() const
arondto::ShapeHandParams ParamType
#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_VERBOSE
The logging level for verbose information.
std::string SHAPE_HAND_SKILL_NAME
bool skillExecutionFailed(const std::optional< armarx::skills::TerminatedSkillStatusUpdate > &update)
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::HandUnitInterfacePrx & getHandUnit(const arondto::Hand hand)
A result struct for th main method of a skill.