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)
132 auto& handUnit = srv_.getHandUnit(params.hand);
133 const auto movementTimeout = params.waitUntilFinished->movementTimeout;
135 ARMARX_INFO <<
"Waiting until target position is reached"
136 << (movementTimeout ?
" or movement times out" :
"");
138 const std::string handPrefix = params.hand == ::armarx::control::skills::arondto::Hand::Left
142 const NameValueMap targetAngles = removePrefixFromKeys(targetAnglesRaw, handPrefix);
147 ::armarx::DateTime time;
148 } pastJointAngles{handUnit->getCurrentJointValues(),
DateTime::Now()};
153 bool reached =
false;
154 bool movementTimedOut =
false;
160 const auto currentAngles =
161 removePrefixFromKeys(handUnit->getCurrentJointValues(), handPrefix);
165 for (
const auto& [jointName, targetAngle] : targetAngles)
168 <<
"Joint '" << jointName <<
"' not found in current joint angles.";
169 if (std::abs(targetAngle - currentAngles.at(jointName)) > getAccuracy(jointName))
174 <<
"' is: " << std::abs(targetAngle - currentAngles.at(jointName)) <<
" > "
175 << getAccuracy(jointName) <<
"(accuracy)";
181 if (movementTimeout and (
DateTime::Now() - pastJointAngles.time).toSecondsDouble() >=
182 movementTimeout.value())
184 movementTimedOut =
true;
185 for (
const auto& [jointName, targetAngle] : targetAngles)
187 if (std::abs(currentAngles.at(jointName) -
188 pastJointAngles.angles.at(jointName)) > getAccuracy(jointName))
190 movementTimedOut =
false;
192 <<
deactivateSpam(1) <<
"Joint '" << jointName <<
"' still moving: "
193 << std::abs(currentAngles.at(jointName) -
194 pastJointAngles.angles.at(jointName))
195 <<
" > " << getAccuracy(jointName) <<
"(accuracy)"
196 <<
"in the last " << movementTimeout.value() <<
"s (movement timeout)";
200 pastJointAngles = {.angles = currentAngles, .time =
DateTime::Now()};
203 clock.waitFor(sleepTime);
210 else if (movementTimedOut)
212 ARMARX_INFO <<
"Movement timed out! Joints stopped moving. Not waiting anymore until "
213 "target postion is reached";
218 <<
"Skill aborted. Not waiting anymore until target position is reached";
226 ShapeHand::getAccuracy(
const std::string& jointName)
230 const auto accuracy = params.waitUntilFinished->accuracy;
231 const auto accuracyOverride = params.waitUntilFinished->accuracyOverride;
233 return accuracyOverride.count(jointName) > 0 ? accuracyOverride.at(jointName) : accuracy;
248 using Parameters = arondto::ShapeHandParams;
253 [inParams](Parameters& params)
255 params.hand = inParams.hand;
256 params.shapeName =
"Open";
257 params.waitUntilFinished =
258 inParams.waitUntilFinished;
281 using Parameters = arondto::ShapeHandParams;
286 [&inParams](Parameters& params)
288 params.hand = inParams.hand;
289 params.shapeName =
"Close";
290 params.waitUntilFinished =
291 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 &)
::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.