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
136  << " found! ";
137  return {TerminatedSkillStatus::Failed, nullptr};
138  }
139  else
140  {
141  ARMARX_INFO << "Using grasp: " << graspSetSelection.graspSetName;
142  }
143 
144  // //////////////////////////////
145  // CALL SUBSKILLS
146  // //////////////////////////////
147  // Open Hand async
148  {
149  skills::hand_control::arondto::OpenHandAcceptedType params;
150  params.kinematicChainName = kinematicChainSelection.kinematicChainName;
151 
152  SkillProxy prx(
154 
155  //if (prx.executeFullSkill(getSkillId().toString(in.executorName), params.toAron()).status != TerminatedSkillStatus::Succeeded)
156  //{
157  // return {TerminatedSkillStatus::Failed, nullptr};
158  //}
159  prx.begin_executeFullSkill(getSkillId().toString(in.executorName), params.toAron());
160  }
161 
162  // Move platform relative to obj
163  {
164  skills::grasp_object::arondto::MovePlatformForGraspAcceptedType params;
165  params.robotName = in.parameters.robotName;
166  params.orientationalAccuracy = in.parameters.platformOrientationalAccuracy;
167  params.positionalAccuracy = in.parameters.platformPositionalAccuracy;
168  params.objectEntityId = in.parameters.objectEntityId;
169  params.tcpName = kinematicChainSelection.tcpName;
170 
171  SkillProxy prx(manager, providerName, MovePlatformForGraspSkill::Description.skillName);
172 
173  if (prx.executeFullSkill(getSkillId().toString(in.executorName), params.toAron())
175  {
176  return {TerminatedSkillStatus::Failed, nullptr};
177  }
178  }
179 
180  // Execute the grasp set
181  {
182  skills::grasp_object::arondto::ExecuteGraspAcceptedType params;
183  params.robotName = in.parameters.robotName;
184  params.graspSetName = graspSetSelection.graspSetName;
185  params.objectEntityId = in.parameters.objectEntityId;
186  params.kinematicChainName = kinematicChainSelection.kinematicChainName;
187  params.orientationalAccuracy = in.parameters.tcpOrientationalAccuracy;
188  params.positionalAccuracy = in.parameters.tcpPositionalAccuracy;
189 
190  SkillProxy prx({manager, providerName, ExecuteGraspSkill::Description.skillName});
191 
192  if (prx.executeFullSkill(getSkillId().toString(in.executorName), params.toAron())
194  {
195  return {TerminatedSkillStatus::Failed, nullptr};
196  }
197  }
198 
199  // Close hand and attach to memory
200  {
201  skills::grasp_object::arondto::CloseHandAndAttachAcceptedType params;
202  params.kinematicChainName = kinematicChainSelection.kinematicChainName;
203  params.objectEntityId = in.parameters.objectEntityId;
204  params.robotName = in.parameters.robotName;
205 
206  SkillProxy prx(
207  {manager, providerName, skills::CloseHandAndAttachSkill::Description.skillName});
208 
209  if (prx.executeFullSkill(getSkillId().toString(in.executorName), params.toAron())
211  {
212  return {TerminatedSkillStatus::Failed, nullptr};
213  }
214  }
215 
216  // Move TCP a bit up
217  {
218  ARMARX_CHECK(robotReader.synchronizeRobot(*robot, armem::Time::Now()));
219  auto tcpPoseGlobal =
220  robot->getRobotNode(kinematicChainSelection.tcpName)->getGlobalPose();
221 
222  auto targetTCPPoseGlobal = tcpPoseGlobal;
223  targetTCPPoseGlobal(2, 3) += 100;
224 
225  skills::tcp_control::arondto::MoveTCPToTargetPoseAcceptedType params;
226  params.robotName = in.parameters.robotName;
227  params.kinematicChainName = kinematicChainSelection.kinematicChainName;
228  params.targetPoseGlobal = targetTCPPoseGlobal;
229  params.orientationalAccuracy = in.parameters.tcpOrientationalAccuracy;
230  params.positionalAccuracy = in.parameters.tcpPositionalAccuracy;
231 
232  SkillProxy prx({manager,
235 
236  if (prx.executeFullSkill(getSkillId().toString(in.executorName), params.toAron())
238  {
239  return {TerminatedSkillStatus::Failed, nullptr};
240  }
241  }
242 
243  // Move joints to zero position
244  {
245  skills::joint_control::arondto::MoveJointsToPositionAcceptedType params;
246  params.robotName = in.parameters.robotName;
247  params.jointTargetTolerance = 0.03; // Todo
248  params.jointMaxSpeed = 0.75;
249  params.accelerationTime = 500;
250  params.setVelocitiesToZeroAtEnd = true;
251 
252  for (const auto& j : robot->getRobotNodeSet(kinematicChainSelection.kinematicChainName)
253  ->getAllRobotNodes())
254  {
255  params.targetJointMap[j->getName()] = 0.0;
256  }
257 
258  SkillProxy prx({manager,
261 
262  if (prx.executeFullSkill(getSkillId().toString(in.executorName), params.toAron())
264  {
265  return {TerminatedSkillStatus::Failed, nullptr};
266  }
267  }
268 
269  // Move platform relative after grasp
270  {
271  skills::grasp_object::arondto::MovePlatformAfterGraspAcceptedType params;
272  params.robotName = in.parameters.robotName;
273  params.orientationalAccuracy = in.parameters.platformOrientationalAccuracy;
274  params.positionalAccuracy = in.parameters.platformPositionalAccuracy;
275 
276  SkillProxy prx(
277  manager, providerName, MovePlatformAfterGraspSkill::Description.skillName);
278 
279  if (prx.executeFullSkill(getSkillId().toString(in.executorName), params.toAron())
281  {
282  return {TerminatedSkillStatus::Failed, nullptr};
283  }
284  }
285 
286  clearLayer();
287 
288  return {TerminatedSkillStatus::Succeeded, nullptr};
289  }
290 } // 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:39
armarx::skills::Skill::manager
manager::dti::SkillManagerInterfacePrx manager
Definition: Skill.h:291
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:13
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:31
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:51
armarx::skills::GraspObjectSkill::ArgType
grasp_object::arondto::GraspObjectAcceptedType ArgType
Definition: GraspObject.h:31
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:29
GraspObjectUtil.h
armarx::armem::obj::instance::Reader
Definition: ObjectReader.h:41
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:29
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:29
armarx::skills::TwoArmGraspControlSkillContext::visualSearchSkillProvider
std::string visualSearchSkillProvider
Definition: GraspControlSkill.h:52
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
armarx::skills::Skill::MainResult
A result struct for th main method of a skill.
Definition: Skill.h:39
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:181
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:53
armarx::skills::TwoArmGraspControlSkill
Definition: GraspControlSkill.h:77
armarx::viz::toString
const char * toString(InteractionFeedbackType type)
Definition: Interaction.h:28
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:68
armarx::viz::Client
Definition: Client.h:117
armarx::skills::Skill::getSkillId
SkillID getSkillId() const
Get the id of the skill.
Definition: Skill.cpp:497
armarx::skills::GraspObjectSkill::Description
static SkillDescription Description
Definition: GraspObject.h:41
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:48
armarx::skills::MoveJointsToPosition::Description
static SkillDescription Description
Definition: MoveJointsToPosition.h:50