ShapeHand.cpp
Go to the documentation of this file.
1 #include "ShapeHand.h"
2 
4 
6 {
8  ::armarx::skills::SpecializedSkill<ParamType>(GetSkillDescription()), srv_(srv)
9  {
10  }
11 
13  ShapeHand::main()
14  {
15  // verify exactly one of the parameters 'shapeName' and 'jointAngles' is set
16  if ((not getParameters().shapeName) and (not getParameters().jointAngles))
17  {
18  ARMARX_ERROR << "Exactly one of the parameters 'shapeName' and 'jointAngles' must be "
19  "set. However, neither has been set.";
20  return MakeFailedResult();
21  }
22  else if (getParameters().shapeName and getParameters().jointAngles)
23  {
24  ARMARX_ERROR << "Exactly one of the parameters 'shapeName' and 'jointAngles' must be "
25  "set. However, both have been set.";
26  return MakeFailedResult();
27  }
28 
29  // set shape/joinAngles
30 
31  auto& handUnit = srv_.getHandUnit(getParameters().hand);
32 
33  if (getParameters().shapeName)
34  {
35  const auto shape = getParameters().shapeName.value();
36  ARMARX_INFO << VAROUT(shape);
37 
38  // check that the shape is defined
39  const auto defShapes =
40  ::armarx::SingleTypeVariantListPtr::dynamicCast(handUnit->getShapeNames())
41  ->toStdVector<std::string>();
42 
43  if (not std::any_of(defShapes.begin(),
44  defShapes.end(),
45  [&shape](const auto& defShape) { return shape == defShape; }))
46  {
47  ARMARX_ERROR << "Shape with name '" << shape
48  << "' not defined. Defined shapes: " << defShapes;
49  return MakeFailedResult();
50  }
51 
52  // set shape
53  ARMARX_INFO << "Setting shape of hand '" << handUnit->getHandName() << "' to '" << shape
54  << "'.";
55 
56  handUnit->setShape(shape);
57 
58  if (getParameters().waitUntilFinished)
59  {
60  return waitUntilFinished(handUnit->getShapeJointValues(shape));
61  }
62  }
63 
64  else // getParameters().jointAngles
65  {
66  const auto jointAngles = getParameters().jointAngles.value();
67 
68  ARMARX_INFO << "Setting joint angles of hand '" << handUnit->getHandName()
69  << "' to: " << jointAngles;
70 
71  handUnit->setJointAngles(jointAngles);
72 
73  if (getParameters().waitUntilFinished)
74  {
75  return waitUntilFinished(jointAngles);
76  }
77  }
78  return MakeSucceededResult();
79  }
80 
82  ShapeHand::waitUntilFinished(const NameValueMap& targetAngles)
83  {
84  auto& handUnit = srv_.getHandUnit(getParameters().hand);
85  const auto movementTimeout = getParameters().waitUntilFinished->movementTimeout;
86 
87  ARMARX_INFO << "Waiting until target position is reached"
88  << (movementTimeout ? " or movement times out" : "");
89 
90  struct
91  {
92  NameValueMap angles;
93  ::armarx::DateTime time;
94  } pastJointAngles{handUnit->getCurrentJointValues(), DateTime::Now()};
95 
96  const Clock clock;
97  const auto sleepTime = Duration::MilliSeconds(20);
98 
99  bool reached = false;
100  bool movementTimedOut = false;
101 
102  while (not shouldSkillTerminate() and not reached and not movementTimedOut)
103  {
104  const auto currentAngles = handUnit->getCurrentJointValues();
105 
106  // check if target reached
107  reached = true;
108  for (const auto& [jointName, targetAngle] : targetAngles)
109  {
110  if (std::abs(targetAngle - currentAngles.at(jointName)) > getAccuracy(jointName))
111  {
112  reached = false;
114  << deactivateSpam(1) << "Delta for joint '" << jointName
115  << "' is: " << std::abs(targetAngle - currentAngles.at(jointName)) << " > "
116  << getAccuracy(jointName) << "(accuracy)";
117  break;
118  }
119  }
120 
121  // check if movement timed out
122  if (movementTimeout and (DateTime::Now() - pastJointAngles.time).toSecondsDouble() >=
123  movementTimeout.value())
124  {
125  movementTimedOut = true;
126  for (const auto& [jointName, targetAngle] : targetAngles)
127  {
128  if (std::abs(currentAngles.at(jointName) -
129  pastJointAngles.angles.at(jointName)) > getAccuracy(jointName))
130  {
131  movementTimedOut = false;
133  << deactivateSpam(1) << "Joint '" << jointName << "' still moving: "
134  << std::abs(currentAngles.at(jointName) -
135  pastJointAngles.angles.at(jointName))
136  << " > " << getAccuracy(jointName) << "(accuracy)"
137  << "in the last " << movementTimeout.value() << "s (movement timeout)";
138  break;
139  }
140  }
141  pastJointAngles = {currentAngles, DateTime::Now()};
142  }
143 
144  clock.waitFor(sleepTime);
145  }
146 
147  if (reached)
148  {
149  ARMARX_INFO << "Target position reached!";
150  }
151  else if (movementTimedOut)
152  {
153  ARMARX_INFO << "Movement timed out! Joints stopped moving. Not waiting anymore until "
154  "target postion is reached";
155  }
156  else if (shouldSkillTerminate())
157  {
159  << "Skill aborted. Not waiting anymore until target position is reached";
160  return MakeAbortedResult();
161  }
162 
163  return MakeSucceededResult();
164  }
165 
166  float
167  ShapeHand::getAccuracy(const std::string& jointName)
168  {
169  const auto accuracy = getParameters().waitUntilFinished->accuracy;
170  const auto& accuracyOverride = getParameters().waitUntilFinished->accuracyOverride;
171 
172  return accuracyOverride.count(jointName) > 0 ? accuracyOverride.at(jointName) : accuracy;
173  }
174 
175  OpenHand::OpenHand() : armarx::skills::SpecializedSkill<ParamType>(GetSkillDescription())
176  {
177  }
178 
180  OpenHand::main()
181  {
182  // call ShapeHand-skill
185  id.providerId = getSkillId().providerId;
186 
187  arondto::ShapeHandParams params;
188  params.hand = getParameters().hand;
189  params.shapeName = "Open";
190  params.waitUntilFinished = getParameters().waitUntilFinished;
191 
192  return {(*callSubskill(::armarx::skills::SkillProxy(manager, id), params.toAron())).status};
193  }
194 
195  CloseHand::CloseHand() : armarx::skills::SpecializedSkill<ParamType>(GetSkillDescription())
196  {
197  }
198 
200  CloseHand::main()
201  {
202  // call ShapeHand-skill
205  id.providerId = getSkillId().providerId;
206 
207  arondto::ShapeHandParams params;
208  params.hand = getParameters().hand;
209  params.shapeName = "Close";
210  params.waitUntilFinished = getParameters().waitUntilFinished;
211 
212  return {(*callSubskill(::armarx::skills::SkillProxy(manager, id), params.toAron())).status};
213  }
214 
215 } // namespace armarx::control::skills::skills
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
SingleTypeVariantList.h
armarx::skills::Skill::manager
manager::dti::SkillManagerInterfacePrx manager
Definition: Skill.h:327
armarx::control::skills::skills::ShapeHand::ShapeHand
ShapeHand(const HandUnitServices &)
Definition: ShapeHand.cpp:7
skills
This file is part of ArmarX.
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::control::skills::HandUnitServices
Definition: HandUnitServices.h:17
armarx::skills::SkillID::skillName
std::string skillName
Definition: SkillID.h:60
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::skills::SkillID::providerId
std::optional< ProviderID > providerId
Definition: SkillID.h:59
armarx::skills::SpecializedSkill< arondto::ShapeHandParams >::getParameters
arondto::ShapeHandParams getParameters() const
Overwrite getter for parameters. Shadow Skill::getParameters()
Definition: SpecializedSkill.h:31
armarx::skills::Skill::callSubskill
std::optional< TerminatedSkillStatusUpdate > callSubskill(const skills::SkillProxy &proxy)
Call a subskill with default parameters and block until the subskill terminates.
Definition: Skill.cpp:22
armarx::skills::SkillProxy
Definition: SkillProxy.h:11
armarx::skills::Skill::MakeFailedResult
static MainResult MakeFailedResult()
Definition: Skill.cpp:324
armarx::status
status
Definition: FiniteStateMachine.h:259
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
armarx::control::skills::skills::OpenHand::OpenHand
OpenHand()
Definition: ShapeHand.cpp:175
ShapeHand.h
armarx::skills::Skill::MakeAbortedResult
static MainResult MakeAbortedResult()
Definition: Skill.cpp:333
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::skills::Skill::MakeSucceededResult
static MainResult MakeSucceededResult(aron::data::DictPtr data=nullptr)
Definition: Skill.cpp:315
armarx::skills::Skill::MainResult
A result struct for th main method of a skill.
Definition: Skill.h:48
armarx::control::skills::HandUnitServices::getHandUnit
::armarx::HandUnitInterfacePrx & getHandUnit(const arondto::Hand hand)
Definition: HandUnitServices.h:23
armarx::control::skills::constants::SHAPE_HAND_SKILL_NAME
std::string SHAPE_HAND_SKILL_NAME
Definition: constants.cpp:12
armarx::control::njoint_controller::platform::platform_follower_controller::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformFollowerController.h:91
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::channels::KinematicUnitObserver::jointAngles
const KinematicUnitDatafieldCreator jointAngles("jointAngles")
armarx::skills::SpecializedSkill< arondto::ShapeHandParams >::ParamType
arondto::ShapeHandParams ParamType
Definition: SpecializedSkill.h:18
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::control::skills::skills
Definition: HomePose.cpp:12
armarx::skills::Skill::shouldSkillTerminate
bool shouldSkillTerminate() const
Returns whether the skill should terminate as soon as possible.
Definition: Skill.cpp:371
armarx::Logging::deactivateSpam
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.
Definition: Logging.cpp:92
armarx::skills::SkillID
Definition: SkillID.h:17
stopwatch::Clock
std::chrono::system_clock Clock
Definition: Stopwatch.h:10
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::skills::Skill::getSkillId
SkillID getSkillId() const
Get the id of the skill.
Definition: Skill.h:74
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:55
armarx::control::skills::skills::CloseHand::CloseHand
CloseHand()
Definition: ShapeHand.cpp:195