GraspObject.cpp
Go to the documentation of this file.
1 #include "GraspObject.h"
2 
3 #include <VirtualRobot/Grasping/GraspSet.h>
4 #include <VirtualRobot/ManipulationObject.h>
5 #include <VirtualRobot/RobotNodeSet.h>
6 
8 #include <RobotAPI/libraries/skills/provider/SkillProxy.h>
9 
10 #include "util/GraspObjectUtil.h"
11 
12 namespace armarx::skills
13 {
14  namespace
15  {
16  grasp_object::arondto::GraspObjectAcceptedType
17  GetDefaultParameterization()
18  {
19  grasp_object::arondto::GraspObjectAcceptedType ret;
20  ret.platformOrientationalAccuracy = 0.1;
21  ret.platformPositionalAccuracy = 10;
22  ret.tcpOrientationalAccuracy = 0.1;
23  ret.tcpPositionalAccuracy = 20;
24  return ret;
25  }
26  } // namespace
27 
28  SkillDescription GraspObjectSkill::Description =
29  SkillDescription{"GraspObject",
30  "Grasp an object",
31  {},
33  grasp_object::arondto::GraspObjectAcceptedType::ToAronType(),
34  GetDefaultParameterization().toAron()};
35 
37  armarx::viz::Client& arviz,
39  TwoArmGraspControlSkill(mns, arviz, Description.skillName, context),
40  SimpleSpecializedSkill<ArgType>(Description)
41  {
42  }
43 
45  GraspObjectSkill::main(const SpecializedMainInput& in)
46  {
48  armem::obj::instance::Reader objectReader(mns);
50  armem::obj::instance::Writer objectWriter(mns);
51 
52  robotReader.connect();
53  objectReader.connect();
54  graspReader.connect();
55  objectWriter.connect();
56 
57  // //////////////////////////////
58  // get robot
59  // //////////////////////////////
60  auto robot =
61  robotReader.getSynchronizedRobot(in.parameters.robotName,
63  VirtualRobot::RobotIO::RobotDescription::eStructure);
64  if (!robot)
65  {
66  ARMARX_ERROR << "Lost robot.";
67  return {TerminatedSkillStatus::Failed, nullptr};
68  }
69 
70  // Relocalize the obj. May be removed later
71  {
72  skills::visual_search::arondto::WhatCanYouSeeNowAcceptedType params;
73  params.robotName = in.parameters.robotName;
74  params.objectEntityId = in.parameters.objectEntityId;
75 
76  SkillProxy prx(manager,
79 
80  if (prx.executeFullSkill(getSkillId().toString(), params.toAron()).status !=
82  {
83  return {TerminatedSkillStatus::Failed, nullptr};
84  }
85  }
86 
87  // //////////////////////////////
88  // get object pose
89  // //////////////////////////////
90  auto objInstance = objectReader.queryLatestObjectInstance(in.parameters.objectEntityId);
91  if (!objInstance)
92  {
93  ARMARX_ERROR << "Lost object pose.";
94  return {TerminatedSkillStatus::Failed, nullptr};
95  }
96 
97  // //////////////////////////////
98  // get all possible grasps
99  // //////////////////////////////
100  auto graspInfo = graspReader.queryKnownGraspInfoByEntityName(in.parameters.objectEntityId,
101  armem::Time::Now());
102  if (!graspInfo)
103  {
104  ARMARX_ERROR << "Lost grasp info.";
105  return {TerminatedSkillStatus::Failed, nullptr};
106  }
107 
108  // //////////////////////////////
109  // select a kinematic chain
110  // //////////////////////////////
111  auto kinematicChainSelection =
113  {*objInstance, *robot});
114  if (kinematicChainSelection.kinematicChainName.empty())
115  {
116  ARMARX_ERROR << "No kinematic chain found! ";
117  return {TerminatedSkillStatus::Failed, nullptr};
118  }
119  else
120  {
121  ARMARX_INFO << "Using kinematic chain: " << kinematicChainSelection.kinematicChainName;
122  }
123 
124  // //////////////////////////////
125  // select a grasp
126  // //////////////////////////////
128  {*graspInfo,
129  robot->getRobotNodeSet(kinematicChainSelection.kinematicChainName)
130  ->getTCP()
131  ->getName()});
132 
133  if (graspSetSelection.graspSetName.empty())
134  {
135  ARMARX_ERROR << "No grasp info for object " << in.parameters.objectEntityId << " found! ";
136  return {TerminatedSkillStatus::Failed, nullptr};
137  }
138  else
139  {
140  ARMARX_INFO << "Using grasp: " << graspSetSelection.graspSetName;
141  }
142 
143  // //////////////////////////////
144  // CALL SUBSKILLS
145  // //////////////////////////////
146  // Open Hand async
147  {
148  skills::hand_control::arondto::OpenHandAcceptedType params;
149  params.kinematicChainName = kinematicChainSelection.kinematicChainName;
150 
151  SkillProxy prx(
153 
154  //if (prx.executeFullSkill(getSkillId().toString(in.executorName), params.toAron()).status != TerminatedSkillStatus::Succeeded)
155  //{
156  // return {TerminatedSkillStatus::Failed, nullptr};
157  //}
158  prx.begin_executeFullSkill(getSkillId().toString(in.executorName), params.toAron());
159  }
160 
161  // Move platform relative to obj
162  {
163  skills::grasp_object::arondto::MovePlatformForGraspAcceptedType params;
164  params.robotName = in.parameters.robotName;
165  params.orientationalAccuracy = in.parameters.platformOrientationalAccuracy;
166  params.positionalAccuracy = in.parameters.platformPositionalAccuracy;
167  params.objectEntityId = in.parameters.objectEntityId;
168  params.tcpName = kinematicChainSelection.tcpName;
169 
170  SkillProxy prx(manager, providerName, MovePlatformForGraspSkill::Description.skillName);
171 
172  if (prx.executeFullSkill(getSkillId().toString(in.executorName), params.toAron())
174  {
175  return {TerminatedSkillStatus::Failed, nullptr};
176  }
177  }
178 
179  // Execute the grasp set
180  {
181  skills::grasp_object::arondto::ExecuteGraspAcceptedType params;
182  params.robotName = in.parameters.robotName;
183  params.graspSetName = graspSetSelection.graspSetName;
184  params.objectEntityId = in.parameters.objectEntityId;
185  params.kinematicChainName = kinematicChainSelection.kinematicChainName;
186  params.orientationalAccuracy = in.parameters.tcpOrientationalAccuracy;
187  params.positionalAccuracy = in.parameters.tcpPositionalAccuracy;
188 
189  SkillProxy prx({manager, providerName, ExecuteGraspSkill::Description.skillName});
190 
191  if (prx.executeFullSkill(getSkillId().toString(in.executorName), params.toAron())
193  {
194  return {TerminatedSkillStatus::Failed, nullptr};
195  }
196  }
197 
198  // Close hand and attach to memory
199  {
200  skills::grasp_object::arondto::CloseHandAndAttachAcceptedType params;
201  params.kinematicChainName = kinematicChainSelection.kinematicChainName;
202  params.objectEntityId = in.parameters.objectEntityId;
203  params.robotName = in.parameters.robotName;
204 
205  SkillProxy prx(
206  {manager, providerName, skills::CloseHandAndAttachSkill::Description.skillName});
207 
208  if (prx.executeFullSkill(getSkillId().toString(in.executorName), params.toAron())
210  {
211  return {TerminatedSkillStatus::Failed, nullptr};
212  }
213  }
214 
215  // Move TCP a bit up
216  {
217  ARMARX_CHECK(robotReader.synchronizeRobot(*robot, armem::Time::Now()));
218  auto tcpPoseGlobal =
219  robot->getRobotNode(kinematicChainSelection.tcpName)->getGlobalPose();
220 
221  auto targetTCPPoseGlobal = tcpPoseGlobal;
222  targetTCPPoseGlobal(2, 3) += 100;
223 
224  skills::tcp_control::arondto::MoveTCPToTargetPoseAcceptedType params;
225  params.robotName = in.parameters.robotName;
226  params.kinematicChainName = kinematicChainSelection.kinematicChainName;
227  params.targetPoseGlobal = targetTCPPoseGlobal;
228  params.orientationalAccuracy = in.parameters.tcpOrientationalAccuracy;
229  params.positionalAccuracy = in.parameters.tcpPositionalAccuracy;
230 
231  SkillProxy prx({manager,
234 
235  if (prx.executeFullSkill(getSkillId().toString(in.executorName), params.toAron())
237  {
238  return {TerminatedSkillStatus::Failed, nullptr};
239  }
240  }
241 
242  // Move joints to zero position
243  {
244  skills::joint_control::arondto::MoveJointsToPositionAcceptedType params;
245  params.robotName = in.parameters.robotName;
246  params.jointTargetTolerance = 0.03; // Todo
247  params.jointMaxSpeed = 0.75;
248  params.accelerationTime = 500;
249  params.setVelocitiesToZeroAtEnd = true;
250 
251  for (const auto& j : robot->getRobotNodeSet(kinematicChainSelection.kinematicChainName)
252  ->getAllRobotNodes())
253  {
254  params.targetJointMap[j->getName()] = 0.0;
255  }
256 
257  SkillProxy prx({manager,
260 
261  if (prx.executeFullSkill(getSkillId().toString(in.executorName), params.toAron())
263  {
264  return {TerminatedSkillStatus::Failed, nullptr};
265  }
266  }
267 
268  // Move platform relative after grasp
269  {
270  skills::grasp_object::arondto::MovePlatformAfterGraspAcceptedType params;
271  params.robotName = in.parameters.robotName;
272  params.orientationalAccuracy = in.parameters.platformOrientationalAccuracy;
273  params.positionalAccuracy = in.parameters.platformPositionalAccuracy;
274 
275  SkillProxy prx(
276  manager, providerName, MovePlatformAfterGraspSkill::Description.skillName);
277 
278  if (prx.executeFullSkill(getSkillId().toString(in.executorName), params.toAron())
280  {
281  return {TerminatedSkillStatus::Failed, nullptr};
282  }
283  }
284 
285  clearLayer();
286 
287  return {TerminatedSkillStatus::Succeeded, nullptr};
288  }
289 } // namespace armarx::skills
armarx::skills::mixin::MNSSkillMixin::mns
armem::client::MemoryNameSystem mns
Definition: MNSSkillMixin.h:11
armarx::armem::robot_state::VirtualRobotReader
The VirtualRobotReader class.
Definition: VirtualRobotReader.h:40
armarx::skills::Skill::manager
manager::dti::SkillManagerInterfacePrx manager
Definition: Skill.h:327
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::skills::TerminatedSkillStatus::Succeeded
@ Succeeded
armarx::skills::GraspObjectSkill::GraspObjectSkill
GraspObjectSkill(armem::client::MemoryNameSystem &mns, armarx::viz::Client &arviz, TwoArmGraspControlSkillContext &)
Definition: GraspObject.cpp:36
armarx::skills::CloseHandAndAttachSkill::Description
static SkillDescription Description
Definition: CloseHandAndAttach.h:28
armarx::skills
This file is part of ArmarX.
Definition: PeriodicUpdateWidget.cpp:11
armarx::skills::grasp_control::util::SelectBestGraspSetForObject
SelectBestGraspSetForObjectOutput SelectBestGraspSetForObject(const SelectBestGraspSetForObjectInput &in)
Definition: GraspObjectUtil.cpp:64
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::skills::GraspObjectSkill::ArgType
grasp_object::arondto::GraspObjectAcceptedType ArgType
Definition: GraspObject.h:30
armarx::skills::TwoArmGraspControlSkill::context
TwoArmGraspControlSkillContext & context
Definition: GraspControlSkill.h:84
armarx::skills::WhatCanYouSeeNow::Description
static SkillDescription Description
Definition: WhatCanYouSeeNow.h:46
armarx::skills::MovePlatformAfterGraspSkill::Description
static SkillDescription Description
Definition: MovePlatformAfterGrasp.h:27
GraspObjectUtil.h
armarx::armem::obj::instance::Reader
Definition: ObjectReader.h:40
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::skills::OpenHand::Description
static SkillDescription Description
Definition: OpenHand.h:46
armarx::skills::TwoArmGraspControlSkillContext::jointControlSkillProvider
std::string jointControlSkillProvider
Definition: GraspControlSkill.h:50
FramedPose.h
armarx::skills::MovePlatformForGraspSkill::Description
static SkillDescription Description
Definition: MovePlatformForGrasp.h:27
armarx::armem::grasping::known_grasps::Reader
Definition: KnownGraspCandidateReader.h:35
armarx::armem::obj::instance::Writer
Definition: ObjectWriter.h:36
armarx::skills::SimpleSpecializedSkill
Definition: SimpleSpecializedSkill.h:10
armarx::skills::ExecuteGraspSkill::Description
static SkillDescription Description
Definition: ExecuteGrasp.h:27
armarx::skills::TwoArmGraspControlSkillContext::visualSearchSkillProvider
std::string visualSearchSkillProvider
Definition: GraspControlSkill.h:52
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::skills::Skill::MainResult
A result struct for th main method of a skill.
Definition: Skill.h:48
GraspObject.h
armarx::skills::grasp_control::util::SelectBestKinematicChainForObjectGrasp
SelectBestKinematicChainForObjectGraspOutput SelectBestKinematicChainForObjectGrasp(const SelectBestKinematicChainForObjectGraspInput &in)
Definition: GraspObjectUtil.cpp:14
armarx::skills::TwoArmGraspControlSkillContext::tcpControlSkillProvider
std::string tcpControlSkillProvider
Definition: GraspControlSkill.h:49
armarx::skills::TerminatedSkillStatus::Failed
@ Failed
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::skills::SimpleSpecializedSkill< grasp_object::arondto::GraspObjectAcceptedType >::main
Skill::MainResult main() final
Definition: SimpleSpecializedSkill.h:71
armarx::skills::mixin::ArvizSkillMixin::clearLayer
void clearLayer()
Definition: ArvizSkillMixin.h:20
armarx::skills::MoveTCPToTargetPose::Description
static SkillDescription Description
Definition: MoveTCPToTargetPose.h:50
armarx::skills::TwoArmGraspControlSkill
Definition: GraspControlSkill.h:77
armarx::viz::toString
const char * toString(InteractionFeedbackType type)
Definition: Interaction.h:27
armarx::skills::TwoArmGraspControlSkillContext::handControlSkillProvider
std::string handControlSkillProvider
Definition: GraspControlSkill.h:48
armarx::skills::TwoArmGraspControlSkillContext
Definition: GraspControlSkill.h:46
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
armarx::viz::Client
Definition: Client.h:109
armarx::skills::Skill::getSkillId
SkillID getSkillId() const
Get the id of the skill.
Definition: Skill.h:74
armarx::skills::GraspObjectSkill::Description
static SkillDescription Description
Definition: GraspObject.h:38
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:55
armarx::skills::MoveJointsToPosition::Description
static SkillDescription Description
Definition: MoveJointsToPosition.h:48