SimpleHandoverObject.cpp
Go to the documentation of this file.
1#include <SimoxUtility/algorithm/string/string_tools.h>
3
4#include <VirtualRobot/Grasping/GraspSet.h>
5#include <VirtualRobot/ManipulationObject.h>
6#include <VirtualRobot/RobotNodeSet.h>
7
9
10namespace armarx::skills
11{
13 .skillId = {.skillName = "SimpleHandoverObject"},
14 .description = "Handover an object already in hand of robot",
15 .timeout = armarx::Duration::MilliSeconds(40000),
16 .parametersType = grasp_object::arondto::PutdownObjectAcceptedType::ToAronType(),
17 };
18
19 /*SimpleHandoverObject::SimpleHandoverObject(armem::client::MemoryNameSystem& mns, armarx::viz::Client& arviz, GraspControlSkillContext& context) :
20 Base(GraspObjectDesc, mns, arviz, context),
21 movePlatformToPose(mns, arviz, context.platformControlSkillContext)
22 {
23 }
24
25 void SimpleHandoverObject::reset()
26 {
27 Base::reset();
28 movePlatformToPose.reset();
29 }
30
31 void SimpleHandoverObject::notifyStopped()
32 {
33 Base::notifyStopped();
34 movePlatformToPose.notifyStopped();
35 }
36
37 void SimpleHandoverObject::notifyTimeoutReached()
38 {
39 Base::notifyTimeoutReached();
40 movePlatformToPose.notifyTimeoutReached();
41 }
42
43 Skill::Status SimpleHandoverObject::execute(const grasp_object::arondto::PutdownObjectAcceptedType& in, const CallbackT& callback)
44 {
45 // Members
46 armem::robot_state::VirtualRobotReader robotReader(mns);
47 armem::obj::instance::Reader objectReader(mns);
48 armem::grasping::known_grasps::Reader graspReader(mns);
49 armem::obj::instance::Writer objectWriter(mns);
50
51 robotReader.connect();
52 objectReader.connect();
53 graspReader.connect();
54
55 objectWriter.connect();
56
57 // //////////////////////////////
58 // check args
59 // //////////////////////////////
60 auto split = simox::alg::split(in.objectEntityId, "/");
61 auto objectId = in.objectEntityId;
62 if (split.size() > 3)
63 {
64 ARMARX_ERROR << "Unknown structure of object entitiy id!";
65 return Skill::Status::Failed;
66 }
67 else if (split.size() == 3)
68 {
69 objectId = split[0] + "/" + split[1];
70 }
71
72 if (in.kinematicChainName.empty())
73 {
74 ARMARX_ERROR << "No kinematic chain candidate set";
75 return Skill::Status::Failed;
76 }
77
78 auto now = armem::Time::now();
79 // //////////////////////////////
80 // get robot
81 // //////////////////////////////
82 auto robot = robotReader.getSynchronizedRobot(in.robot, now, VirtualRobot::RobotIO::RobotDescription::eStructure);
83
84 auto kinematicChain = robot->getRobotNodeSet(in.kinematicChainName);
85 const auto tcpName = kinematicChain->getTCP()->getName();
86 const auto kinematicChainJointNames = kinematicChain->getNodeNames();
87
88
89 // //////////////////////////////
90 // get object pose
91 // //////////////////////////////
92 auto objInstanceOpt = objectReader.queryObjectByEntityID(in.objectEntityId, now);
93 if (!objInstanceOpt)
94 {
95 ARMARX_ERROR << "Lost object pose.";
96 return Skill::Status::Failed;
97 }
98 auto objInstance = *objInstanceOpt;
99
100 // //////////////////////////////
101 // Setup vars
102 // //////////////////////////////
103 auto skillRet = Skill::Status::Succeeded;
104
105 auto handRootNodeName = "Hand L Base";
106 if (simox::alg::starts_with(in.kinematicChainName, "Right"))
107 {
108 handRootNodeName = "Hand R Base";
109 }
110 const FramedPosePtr handRoot = new FramedPose(robot->getRobotNode(handRootNodeName)->getPoseInRootFrame(), robot->getRootNode()->getName(), robot->getName());
111 const FramedPosePtr tcp = new FramedPose(robot->getRobotNode(tcpName)->getPoseInRootFrame(), robot->getRootNode()->getName(), robot->getName());
112 const Eigen::Matrix4f handRootPoseGlobal = handRoot->toGlobalEigen(robot);
113 const Eigen::Matrix4f tcpPoseGlobal = tcp->toGlobalEigen(robot);
114
115 const Eigen::Matrix4f tcp2handRoot = tcpPoseGlobal.inverse() * handRootPoseGlobal;
116
117
118 // //////////////////////////////
119 // Move arm back
120 // //////////////////////////////
121 {
122 auto otherArmKinematicChain = robot->getRobotNodeSet(in.otherArmKinematicChainName);
123 auto otherArmJointNames = otherArmKinematicChain->getNodeNames();
124
125 ARMARX_CHECK_EQUAL(otherArmJointNames.size(), 7); // TODO
126
127 joint_control::arondto::MoveJointsToPositionAcceptedType nextArgs;
128 nextArgs.accelerationTime = 500;
129 nextArgs.jointMaxSpeed = 0.75;
130 nextArgs.jointTargetTolerance = 0.03;
131 nextArgs.setVelocitiesToZeroAtEnd = true;
132 nextArgs.targetJointMap[otherArmJointNames[0]] = 0.47;
133 nextArgs.targetJointMap[otherArmJointNames[1]] = 0.0;
134 nextArgs.targetJointMap[otherArmJointNames[2]] = 0.0;
135 nextArgs.targetJointMap[otherArmJointNames[3]] = 0.32;
136 nextArgs.targetJointMap[otherArmJointNames[4]] = 0.47;
137 skillRet = moveJointsToPosition._execute(nextArgs, callback);
138 }
139
140 if (skillRet != Skill::Status::Succeeded)
141 {
142 clearLayer();
143 return skillRet;
144 }
145
146 // //////////////////////////////
147 // Move platform relative
148 // //////////////////////////////
149 {
150 // not used here
151 }
152
153 if (skillRet != Skill::Status::Succeeded)
154 {
155 clearLayer();
156 return skillRet;
157 }
158
159
160 // //////////////////////////////
161 // Move TCP to handover pose
162 // //////////////////////////////
163 {
164 now = IceUtil::Time::now();
165 robotReader.synchronizeRobot(*robot, now);
166
167 FramedPose tcp(robot->getRobotNode(tcpName)->getPoseInRootFrame(), robot->getRootNode()->getName(), robot->getName());
168 //const Eigen::Matrix4f robotRoot = robotRootFramedPose.toEigen();
169 //const Eigen::Matrix4f robotRootPoseGlobalEigen = robotRootFramedPose.toGlobalEigen(robot);
170
171 // query obj target location from grasp
172 auto objPoseRobot = objInstance.pose.objectPoseRobot;
173 objPoseRobot(1,3) += 40; // move further away
174 FramedPose targetObjectFramedPoseRoot(objPoseRobot, robot->getRootNode()->getName(), robot->getName());
175 const auto targetObjectPoseGlobalEigen = targetObjectFramedPoseRoot.toGlobalEigen(robot);
176
177 const auto transformationFromTCPToObject = objInstance.pose.attachment.poseInFrame;
178
179 const auto placePoseGlobal = Eigen::Matrix4f(targetObjectPoseGlobalEigen * transformationFromTCPToObject.inverse());
180
181 Eigen::Matrix4f handRootTargetPoseGlobal = placePoseGlobal * tcp2handRoot;
182 handRootTargetPoseGlobal(2,3) += 20;
183
184 auto l = arviz.layer(layerName);
185 auto o = armarx::viz::Robot("grasp");
186 o.file("RobotAPI", "RobotAPI/robots/Armar3/ArmarIII-LeftHand.xml"); // TODO!
187 o.pose(handRootTargetPoseGlobal);
188 l.add(o);
189 arviz.commit(l);
190
191 {
192 tcp_control::arondto::MoveTCPToTargetPoseAcceptedType nextArgs;
193 nextArgs.kinematicChainName = in.kinematicChainName;
194 nextArgs.orientationalAccuracy = in.orientationalAccuracy;
195 nextArgs.positionalAccuracy = in.positionalAccuracy;
196 nextArgs.targetPoseGlobal = handRootTargetPoseGlobal;
197 skillRet = moveTcpToTargetSkill.execute(nextArgs, callback);
198 }
199
200 clearLayer();
201 }
202
203
204 if (skillRet != Skill::Status::Succeeded)
205 {
206 clearLayer();
207 return skillRet;
208 }
209
210
211 // //////////////////////////////
212 // Open hand and detach from memory
213 // //////////////////////////////
214 {
215 now = IceUtil::Time::now();
216 robotReader.synchronizeRobot(*robot, now);
217
218 FramedPose robotRootFramedPose(robot->getRootNode()->getPoseInRootFrame(), robot->getRootNode()->getName(), robot->getName());
219 //const Eigen::Matrix4f robotRoot = robotRootFramedPose.toEigen();
220 const Eigen::Matrix4f robotRootGlobal = robotRootFramedPose.toGlobalEigen(robot);
221
222 FramedPose objInRobotRootFramedPose(objInstance.pose.objectPoseRobot, robot->getRootNode()->getName(), robot->getName());
223 const auto objPoseGlobalEigen = objInRobotRootFramedPose.toGlobalEigen(robot);
224 const auto objPoseRootEigen = objInRobotRootFramedPose.toEigen();
225
226 //objInstanceUpdate.pose.providerName = description.skillName;
227 objInstance.pose.attachmentValid = false;
228 objInstance.pose.attachment.resetHard();
229 objInstance.pose.objectPoseGlobal = objPoseGlobalEigen;
230 objInstance.pose.objectPoseRobot = objPoseRootEigen;
231 objInstance.pose.robotPose = robotRootGlobal;
232 objInstance.pose.robotConfig = robot->getJointValues();
233
234 objectWriter.commitObject(objInstance, objInstance.pose.providerName, IceUtil::Time::now());
235
236 skillRet = openHand.execute(nullptr, callback);
237 }
238
239 if (skillRet != Skill::Status::Succeeded)
240 {
241 clearLayer();
242 return skillRet;
243 }
244
245
246 // //////////////////////////////
247 // Move TCP to retreat pose
248 // //////////////////////////////
249 {
250 const float xRetreatOffset = 90;
251 const float yRetreatOffset = 10;
252
253 Eigen::Matrix4f offset = Eigen::Matrix4f::Identity();
254 offset(0,3) = xRetreatOffset;
255 offset(1,3) = yRetreatOffset;
256
257 now = IceUtil::Time::now();
258 robotReader.synchronizeRobot(*robot, now);
259
260 auto TcpPoseGlobalEigen = robot->getRobotNode(tcpName)->getGlobalPose();
261 Eigen::Matrix4f newTCPPoseGlobalEigen = TcpPoseGlobalEigen * offset;
262
263 tcp_control::arondto::MoveTCPToTargetPoseAcceptedType nextArgs;
264 nextArgs.kinematicChainName = in.kinematicChainName;
265 nextArgs.orientationalAccuracy = in.orientationalAccuracy;
266 nextArgs.positionalAccuracy = in.positionalAccuracy;
267 nextArgs.targetPoseGlobal = newTCPPoseGlobalEigen;
268 skillRet = moveTcpToTargetSkill.execute(nextArgs, callback);
269 }
270
271 if (skillRet != Skill::Status::Succeeded)
272 {
273 clearLayer();
274 return skillRet;
275 }
276
277 // //////////////////////////////
278 // Move joints to zero position
279 // //////////////////////////////
280 {
281 joint_control::arondto::MoveJointsToPositionAcceptedType nextArgs;
282 nextArgs.accelerationTime = 500;
283 nextArgs.jointMaxSpeed = 0.75;
284 nextArgs.jointTargetTolerance = 0.03;
285 nextArgs.setVelocitiesToZeroAtEnd = true;
286 for (const auto& j : kinematicChainJointNames)
287 {
288 nextArgs.targetJointMap[j] = 0.0;
289 }
290 for (const auto& j : robot->getRobotNodeSet(in.otherArmKinematicChainName)->getNodeNames())
291 {
292 nextArgs.targetJointMap[j] = 0.0;
293 }
294 skillRet = moveJointsToPosition._execute(nextArgs, callback);
295 }
296
297 clearLayer();
298 return skillRet;
299 }*/
300} // namespace armarx::skills
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition Duration.cpp:48
This file is part of ArmarX.
const SkillDescription GraspObjectSkillDesc