cob_pick_place_action.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <ros/ros.h>
19 #include <iostream>
20 #include <fstream>
21 #include <math.h>
22 
28 
29 #include <algorithm>
30 
32 {
33  pub_co = nh_.advertise<moveit_msgs::CollisionObject>("collision_object", 10);
34  pub_ao = nh_.advertise<moveit_msgs::AttachedCollisionObject>("attached_collision_object", 10);
35 
36  //ToDo: Generate this mapping from GraspTable.txt?
37  map_classid_to_classname[11]="sauerkraut";
38  map_classid_to_classname[13]="fruittea";
39  map_classid_to_classname[16]="orangemarmelade";
40  map_classid_to_classname[18]="yellowsaltcube";
41  map_classid_to_classname[27]="hotpot";
42  map_classid_to_classname[30]="bluesaltcube";
43  map_classid_to_classname[31]="yellowsaltcylinder";
44  map_classid_to_classname[44]="knaeckebrot";
45  map_classid_to_classname[46]="liviosunfloweroil";
46  map_classid_to_classname[50]="instantsoup";
47  map_classid_to_classname[52]="hotpot2";
48  map_classid_to_classname[57]="mueslibars";
49  map_classid_to_classname[65]="fruitdrink";
50  map_classid_to_classname[99]="ruskwholemeal";
51  map_classid_to_classname[101]="koalacandy";
52  map_classid_to_classname[103]="instanttomatosoup";
53  map_classid_to_classname[106]="bakingsoda";
54  map_classid_to_classname[107]="sweetener";
55  map_classid_to_classname[109]="chocoicing";
56  map_classid_to_classname[119]="tomatoherbsauce";
57  map_classid_to_classname[122]="peanuts";
58  map_classid_to_classname[126]="herbsalt";
59  map_classid_to_classname[128]="bathdetergent";
60 
61  //non-KIT objects
62  map_classid_to_classname[5001]="pringles";
63 
64 
65  static const std::string COB_PICKUP_ACTION_NAME = "cob_pick_action";
66  as_pick.reset(new actionlib::SimpleActionServer<cob_pick_place_action::CobPickAction>(nh_, COB_PICKUP_ACTION_NAME, boost::bind(&CobPickPlaceActionServer::pick_goal_cb, this, _1), false));
67  as_pick->start();
68 
69  static const std::string COB_PLACE_ACTION_NAME = "cob_place_action";
71  as_place->start();
72 
73  last_grasp_valid = false;
74  last_object_name.clear();
75 
76 
77  static const std::string QUERY_GRASPS_OR_ACTION_NAME = "query_grasps";
79  ROS_INFO("Waiting for action server \"%s\" to start...", QUERY_GRASPS_OR_ACTION_NAME.c_str());
80  ac_grasps_or->waitForServer(); //will wait for infinite time
81  ROS_INFO("Action server \"%s\" started.", QUERY_GRASPS_OR_ACTION_NAME.c_str());
82 }
83 
85 {
86  ROS_INFO("cob_pick_action...spinning");
87  ros::spin();
88 }
89 
90 void CobPickPlaceActionServer::pick_goal_cb(const cob_pick_place_action::CobPickGoalConstPtr &goal)
91 {
92  ROS_INFO("PickGoalCB: Received new goal: Trying to pick %s", goal->object_name.c_str());
93  cob_pick_place_action::CobPickResult result;
94  std::string response;
95  bool success = false;
96 
97  ROS_DEBUG_STREAM(*(goal.get()));
98 
99  if(goal->gripper_type.empty())
100  {
101  ROS_ERROR("Pick failed: No gripper_type specified!");
102  result.success.data=false;
103  response="Pick failed: No gripper_type specified!";
104  as_pick->setAborted(result, response);
105  last_grasp_valid = false;
106  last_object_name.clear();
107  return;
108  }
109 
111  std::vector<moveit_msgs::Grasp> grasps;
112  if(goal->grasp_database=="KIT")
113  {
114  ROS_INFO("Using KIT grasp table");
115  if(goal->grasp_id!=0)
116  {
117  ROS_INFO("Using specific grasp_id: %d", goal->grasp_id);
118  fillSingleGraspKIT(goal->object_class, goal->gripper_type, goal->grasp_id, goal->object_pose, grasps);
119  }
120  else
121  {
122  ROS_INFO("Using all grasps");
123  fillAllGraspsKIT(goal->object_class, goal->gripper_type, goal->object_pose, grasps);
124  }
125  }
126  else if(goal->grasp_database=="OpenRAVE")
127  {
128  ROS_INFO("Using OpenRAVE grasp table");
129  fillGraspsOR(goal->object_class, goal->gripper_type, goal->gripper_side, goal->grasp_id, goal->object_pose, grasps);
130  }
131  else if(goal->grasp_database=="ALL")
132  {
133  ROS_INFO("Using all available databases");
134  std::vector<moveit_msgs::Grasp> grasps_OR, grasps_KIT;
135  fillAllGraspsKIT(goal->object_class, goal->gripper_type, goal->object_pose, grasps_KIT);
136  fillGraspsOR(goal->object_class, goal->gripper_type, goal->gripper_side, goal->grasp_id, goal->object_pose, grasps_OR);
137 
138  grasps = grasps_KIT;
139  std::vector<moveit_msgs::Grasp>::iterator it = grasps.end();
140  grasps.insert(it, grasps_OR.begin(), grasps_OR.end());
141  }
142  else
143  {
144  ROS_ERROR("Grasp_Database %s not supported! Please use \"KIT\" or \"OpenRAVE\" or \"ALL\" instead", goal->grasp_database.c_str());
145  result.success.data=false;
146  response="Pick failed: Grasp Database not supported!";
147  as_pick->setAborted(result, response);
148  last_grasp_valid = false;
149  last_object_name.clear();
150  return;
151  }
152 
153  if(!grasps.empty())
154  {
155  ROS_INFO("PickGoalCB: Found %lu grasps for this object", grasps.size());
156  for(unsigned int i=0; i<grasps.size(); i++)
157  {
158  ROS_DEBUG_STREAM("Grasp "<< i << ": " << grasps[i]);
159  }
160  }
161  else
162  {
163  ROS_ERROR("No grasps found for object %s in database %s using gripper_type %s", goal->object_name.c_str(), goal->grasp_database.c_str(), goal->gripper_type.c_str());
164  result.success.data=false;
165  response="Pick failed: No grasps found!";
166  as_pick->setAborted(result, response);
167  last_grasp_valid = false;
168  last_object_name.clear();
169  return;
170  }
171 
173  insertObject(goal->object_name, goal->object_class, goal->object_pose);
174 
175  if(!(goal->support_surface.empty()))
176  {
177  ROS_INFO("Setting SupportSurface to %s", goal->support_surface.c_str());
178  group.setSupportSurfaceName(goal->support_surface);
179  }
180 
182  group.setPlanningTime(300.0); //default is 5.0 s
183  moveit::planning_interface::MoveItErrorCode error_code = group.pick(goal->object_name, grasps);
184 
185  if(error_code == moveit_msgs::MoveItErrorCodes::SUCCESS)
186  {
187  std::string msg = "PICK SUCCEEDED: " + boost::lexical_cast<std::string>(error_code);
188  ROS_INFO_STREAM(msg);
189  result.success.data=true;
190  response=msg;
191  as_pick->setSucceeded(result, response);
192  last_grasp_valid = true;
193  last_object_name = goal->object_name;
194  }
195  else
196  {
197  std::string msg = "PICK FAILED: " + boost::lexical_cast<std::string>(error_code);
198  ROS_ERROR_STREAM(msg);
199  result.success.data=false;
200  response=msg;
201  as_pick->setAborted(result, response);
202  last_grasp_valid = false;
203  last_object_name.clear();
204  }
205 }
206 
207 void CobPickPlaceActionServer::place_goal_cb(const cob_pick_place_action::CobPlaceGoalConstPtr &goal)
208 {
209  ROS_INFO("PlaceCB: Received new goal: Trying to Place %s", goal->object_name.c_str());
210  cob_pick_place_action::CobPlaceResult result;
211  std::string response;
212  bool success = false;
213 
214  if(!last_grasp_valid || last_object_name.empty() || last_object_name != goal->object_name)
215  {
216  ROS_ERROR("Object %s cannot be placed", goal->object_name.c_str());
217  result.success.data = false;
218  response = "Object has not been picked before";
219  as_place->setAborted(result, response);
220  last_grasp_valid = false;
221  last_object_name.clear();
222  return;
223  }
224 
225  if(goal->destinations.empty())
226  {
227  ROS_ERROR("Object %s cannot be placed", goal->object_name.c_str());
228  result.success.data = false;
229  response = "No destinations given";
230  as_place->setAborted(result, response);
231  last_grasp_valid = false;
232  last_object_name.clear();
233  return;
234  }
235 
236  std::vector<moveit_msgs::PlaceLocation> locations;
237  trajectory_msgs::JointTrajectory pre_grasp_posture; //use cyl_open by default
238  pre_grasp_posture.header.stamp = ros::Time::now();
239  pre_grasp_posture.joint_names.resize(7);
240  pre_grasp_posture.joint_names[0] = "sdh_knuckle_joint";
241  pre_grasp_posture.joint_names[1] = "sdh_finger_12_joint";
242  pre_grasp_posture.joint_names[2] = "sdh_finger_13_joint";
243  pre_grasp_posture.joint_names[3] = "sdh_finger_22_joint";
244  pre_grasp_posture.joint_names[4] = "sdh_finger_23_joint";
245  pre_grasp_posture.joint_names[5] = "sdh_thumb_2_joint";
246  pre_grasp_posture.joint_names[6] = "sdh_thumb_3_joint";
247 
248  trajectory_msgs::JointTrajectoryPoint point;
249  point.positions.assign(7,0.0);
250  point.velocities.assign(7,0.0);
251  point.accelerations.assign(7,0.0);
252  point.effort.assign(7,0.0);
253  point.time_from_start = ros::Duration(3.0);
254 
255  point.positions[0] = 0.0;
256  point.positions[1] = -0.9854;
257  point.positions[2] = 0.9472;
258  point.positions[3] = -0.9854;
259  point.positions[4] = 0.9472;
260  point.positions[5] = -0.9854;
261  point.positions[6] = 0.9472;
262  pre_grasp_posture.points.push_back(point);
263 
264  for(unsigned int i=0; i<goal->destinations.size(); i++)
265  {
266  moveit_msgs::PlaceLocation place_location;
267 
268  place_location.id = "Last_"+goal->object_name+"_grasp";
269  place_location.post_place_posture = pre_grasp_posture;
270  place_location.place_pose = goal->destinations[i];
271  place_location.pre_place_approach.direction.header.frame_id = "/base_footprint";
272  place_location.pre_place_approach.direction.vector.z = -1.0;
273  place_location.pre_place_approach.min_distance = 0.1;
274  place_location.pre_place_approach.desired_distance = 0.15;
275  place_location.post_place_retreat.direction.header.frame_id = "/base_footprint";
276  place_location.post_place_retreat.direction.vector.z = 1.0;
277  place_location.post_place_retreat.min_distance = 0.1;
278  place_location.post_place_retreat.desired_distance = 0.15;
279 
280  locations.push_back(place_location);
281  }
282 
283  if(!(goal->support_surface.empty()))
284  {
285  ROS_INFO("Setting SupportSurface to %s", goal->support_surface.c_str());
286  group.setSupportSurfaceName(goal->support_surface);
287  }
288  group.setPlanningTime(300.0); //default is 5.0 s
289 
290  moveit::planning_interface::MoveItErrorCode error_code = group.place(goal->object_name, locations);
291 
292  if(error_code == moveit_msgs::MoveItErrorCodes::SUCCESS)
293  {
294  std::string msg = "PLACE SUCCEEDED: " + boost::lexical_cast<std::string>(error_code);
295  ROS_INFO_STREAM(msg);
296  result.success.data=true;
297  response=msg;
298  as_place->setSucceeded(result, response);
299  last_grasp_valid = false;
300  last_object_name.clear();
301  }
302  else
303  {
304  std::string msg = "PLACE FAILED: " + boost::lexical_cast<std::string>(error_code);
305  ROS_ERROR_STREAM(msg);
306  result.success.data=false;
307  response=msg;
308  as_place->setAborted(result, response);
309  last_grasp_valid = false;
310  last_object_name.clear();
311  }
312 }
313 
314 void CobPickPlaceActionServer::insertObject(std::string object_name, unsigned int object_class, geometry_msgs::PoseStamped object_pose)
315 {
316  ROS_INFO("Adding object to MoveIt! environment..");
317 
318  moveit_msgs::CollisionObject co;
319  co.header.stamp = object_pose.header.stamp;//ros::Time::now();
320  co.header.frame_id = object_pose.header.frame_id;
321 
322  // remove object
323  co.id = object_name;
324  co.operation = co.REMOVE;
325  pub_co.publish(co);
326 
327  // add object as Mesh
328  co.id = object_name;
329  co.operation = co.ADD;
330 
331  std::string mesh_name = map_classid_to_classname[object_class];
332  std::transform(mesh_name.begin(), mesh_name.end(), mesh_name.begin(), ::tolower);
333 
334  boost::scoped_ptr<shapes::Mesh> mesh;
335  mesh.reset(shapes::createMeshFromResource("package://cob_grasp_generation/files/meshes/"+mesh_name+".stl"));
336  shapes::ShapeMsg shape_msg;
337  shapes::constructMsgFromShape(mesh.get(), shape_msg);
338  co.meshes.push_back(boost::get<shape_msgs::Mesh>(shape_msg));
339  co.mesh_poses.push_back(object_pose.pose);
340  pub_co.publish(co);
341 
342 
343  tf::Transform transform;
344  transform.setOrigin( tf::Vector3(object_pose.pose.position.x, object_pose.pose.position.y, object_pose.pose.position.z) );
345  transform.setRotation( tf::Quaternion(object_pose.pose.orientation.x, object_pose.pose.orientation.y, object_pose.pose.orientation.z, object_pose.pose.orientation.w) );
346  tf_broadcaster_.sendTransform(tf::StampedTransform(transform, ros::Time::now(), object_pose.header.frame_id, object_name));
347 
348 
349  ros::Duration(1.0).sleep();
350 }
351 
352 
353 
354 void CobPickPlaceActionServer::fillAllGraspsKIT(unsigned int objectClassId, std::string gripper_type, geometry_msgs::PoseStamped object_pose, std::vector<moveit_msgs::Grasp> &grasps)
355 {
356  grasps.clear();
357  Grasp *current_grasp = NULL;
358 
360  std::string path = ros::package::getPath("cob_grasp_generation")+std::string("/files/")+gripper_type+std::string("_grasptable_kit.txt");
361  GraspTableIniFile = const_cast<char*>(path.c_str());
362  m_GraspTable = new GraspTable();
364  if(error<0)
365  {
366  ROS_ERROR("Failed to initialize GraspTables");
367  return;
368  }
369 
370  m_GraspTable->ResetReadPtr(objectClassId);
371  unsigned int grasp_index = 0;
372 
373  current_grasp = m_GraspTable->GetNextGrasp(objectClassId);
374 
375  while(current_grasp)
376  {
377  convertGraspKIT(current_grasp, object_pose, grasps);
378 
379  current_grasp = m_GraspTable->GetNextGrasp(objectClassId);
380  grasp_index++;
381  }
382 }
383 
384 void CobPickPlaceActionServer::fillSingleGraspKIT(unsigned int objectClassId, std::string gripper_type, unsigned int grasp_id, geometry_msgs::PoseStamped object_pose, std::vector<moveit_msgs::Grasp> &grasps)
385 {
386  grasps.clear();
387  Grasp *current_grasp = NULL;
388 
390  std::string path = ros::package::getPath("cob_grasp_generation")+std::string("/files/")+gripper_type+std::string("_grasptable_kit.txt");
391  GraspTableIniFile = const_cast<char*>(path.c_str());
392  m_GraspTable = new GraspTable();
394  if(error<0)
395  {
396  ROS_ERROR("Failed to initialize GraspTables");
397  return;
398  }
399 
400  current_grasp = m_GraspTable->GetGrasp(objectClassId, grasp_id);
401 
402  if(current_grasp)
403  {
404  ROS_INFO("GraspIndex %d found",grasp_id);
405 
406  convertGraspKIT(current_grasp, object_pose, grasps);
407  }
408  else
409  ROS_ERROR("Grasp %d NOT found", grasp_id);
410 }
411 
412 void CobPickPlaceActionServer::convertGraspKIT(Grasp* current_grasp, geometry_msgs::PoseStamped object_pose, std::vector<moveit_msgs::Grasp> &grasps)
413 {
414  bool debug = true;
415  moveit_msgs::Grasp g;
416 
417  //HandPreGraspConfig
418  std::vector<double> current_hand_pre_config = current_grasp->GetHandPreGraspConfig();
419  sensor_msgs::JointState pre_grasp_posture;
420  pre_grasp_posture.position.clear();
421  for (unsigned int i=0; i<current_hand_pre_config.size(); i++)
422  {
423  pre_grasp_posture.position.push_back(current_hand_pre_config[i]);
424  }
425  g.pre_grasp_posture = MapHandConfiguration(pre_grasp_posture);
426 
427  //HandGraspConfig
428  std::vector<double> current_hand_config = current_grasp->GetHandOptimalGraspConfig();
429  sensor_msgs::JointState grasp_posture;
430  grasp_posture.position.clear();
431  for (unsigned int i=0; i<current_hand_config.size(); i++)
432  {
433  grasp_posture.position.push_back(current_hand_config[i]);
434  }
435  g.grasp_posture= MapHandConfiguration(grasp_posture);
436 
437  //~~~ TCPGraspPose ~~~
439 
440  // O_from_SDH
441  std::vector<double> current_grasp_pose = current_grasp->GetTCPGraspPose();
442  tf::Transform transform_grasp_O_from_SDH = tf::Transform(
443  tf::createQuaternionFromRPY(current_grasp_pose[3], current_grasp_pose[4], current_grasp_pose[5] ),
444  0.001*tf::Vector3(current_grasp_pose[0],current_grasp_pose[1],current_grasp_pose[2]));
445  //debug
446  if (debug)
447  {
448  geometry_msgs::Transform msg_grasp_O_from_SDH;
449  tf::transformTFToMsg(transform_grasp_O_from_SDH, msg_grasp_O_from_SDH);
450  ROS_DEBUG_STREAM("msg_grasp_O_from_SDH:" << msg_grasp_O_from_SDH);
451  }
452 
453  // HEADER_from_O (given)
454  tf::Transform transform_HEADER_from_O = tf::Transform(
455  tf::Quaternion(object_pose.pose.orientation.x, object_pose.pose.orientation.y, object_pose.pose.orientation.z, object_pose.pose.orientation.w),
456  tf::Vector3(object_pose.pose.position.x, object_pose.pose.position.y, object_pose.pose.position.z));
457  //debug
458  if (debug)
459  {
460  geometry_msgs::Transform msg_HEADER_from_O;
461  tf::transformTFToMsg(transform_HEADER_from_O, msg_HEADER_from_O);
462  ROS_DEBUG_STREAM("msg_HEADER_from_O:" << msg_HEADER_from_O);
463  }
464 
465  // FOOTPRINT_from_ARM7
466  tf::Transform transform_grasp_FOOTPRINT_from_ARM7 = transformPose(transform_grasp_O_from_SDH, transform_HEADER_from_O, object_pose.header.frame_id);
467  //debug
468  if (debug)
469  {
470  geometry_msgs::Transform msg_grasp_FOOTPRINT_from_ARM7;
471  tf::transformTFToMsg(transform_grasp_FOOTPRINT_from_ARM7, msg_grasp_FOOTPRINT_from_ARM7);
472  ROS_DEBUG_STREAM("msg_grasp_FOOTPRINT_from_ARM7:" << msg_grasp_FOOTPRINT_from_ARM7);
473  }
474 
475  // convert to PoseStamped
476  geometry_msgs::Transform msg_transform_grasp_FOOTPRINT_from_ARM7;
477  tf::transformTFToMsg(transform_grasp_FOOTPRINT_from_ARM7, msg_transform_grasp_FOOTPRINT_from_ARM7);
478  geometry_msgs::PoseStamped msg_pose_grasp_FOOTPRINT_from_ARM7;
479  msg_pose_grasp_FOOTPRINT_from_ARM7.header.stamp = ros::Time::now();
480  msg_pose_grasp_FOOTPRINT_from_ARM7.header.frame_id = "/base_footprint";
481  msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.x = msg_transform_grasp_FOOTPRINT_from_ARM7.translation.x;
482  msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.y = msg_transform_grasp_FOOTPRINT_from_ARM7.translation.y;
483  msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.z = msg_transform_grasp_FOOTPRINT_from_ARM7.translation.z;
484  msg_pose_grasp_FOOTPRINT_from_ARM7.pose.orientation = msg_transform_grasp_FOOTPRINT_from_ARM7.rotation;
485 
486  if (debug)
487  {
488  ROS_DEBUG_STREAM("msg_pose_grasp_FOOTPRINT_from_ARM7:" << msg_pose_grasp_FOOTPRINT_from_ARM7);
489  }
490 
491  g.grasp_pose = msg_pose_grasp_FOOTPRINT_from_ARM7;
492 
493  //~~~ ApproachDirection ~~~
494  // O_from_SDH
495  std::vector<double> current_pre_grasp_pose = current_grasp->GetTCPPreGraspPose();
496  tf::Transform transform_pre_O_from_SDH = tf::Transform(
497  tf::createQuaternionFromRPY(current_pre_grasp_pose[3], current_pre_grasp_pose[4], current_pre_grasp_pose[5] ),
498  0.001*tf::Vector3(current_pre_grasp_pose[0],current_pre_grasp_pose[1],current_pre_grasp_pose[2]));
499  //debug
500  if (debug)
501  {
502  geometry_msgs::Transform msg_pre_O_from_SDH;
503  tf::transformTFToMsg(transform_pre_O_from_SDH, msg_pre_O_from_SDH);
504  ROS_DEBUG_STREAM("msg_pre_O_from_SDH:" << msg_pre_O_from_SDH);
505  }
506 
507  // FOOTPRINT_from_ARM7
508  tf::Transform transform_pre_FOOTPRINT_from_ARM7 = transformPose(transform_pre_O_from_SDH, transform_HEADER_from_O, object_pose.header.frame_id);
509  //debug
510  if (debug)
511  {
512  geometry_msgs::Transform msg_pre_FOOTPRINT_from_ARM7;
513  tf::transformTFToMsg(transform_pre_FOOTPRINT_from_ARM7, msg_pre_FOOTPRINT_from_ARM7);
514  ROS_DEBUG_STREAM("msg_pre_FOOTPRINT_from_ARM7:" << msg_pre_FOOTPRINT_from_ARM7);
515  }
516 
517  // convert to PoseStamped
518  geometry_msgs::Transform msg_transform_pre_FOOTPRINT_from_ARM7;
519  tf::transformTFToMsg(transform_pre_FOOTPRINT_from_ARM7, msg_transform_pre_FOOTPRINT_from_ARM7);
520  geometry_msgs::PoseStamped msg_pose_pre_FOOTPRINT_from_ARM7;
521  msg_pose_pre_FOOTPRINT_from_ARM7.header.stamp = ros::Time::now();
522  msg_pose_pre_FOOTPRINT_from_ARM7.header.frame_id = "/base_footprint";
523  msg_pose_pre_FOOTPRINT_from_ARM7.pose.position.x = msg_transform_pre_FOOTPRINT_from_ARM7.translation.x;
524  msg_pose_pre_FOOTPRINT_from_ARM7.pose.position.y = msg_transform_pre_FOOTPRINT_from_ARM7.translation.y;
525  msg_pose_pre_FOOTPRINT_from_ARM7.pose.position.z = msg_transform_pre_FOOTPRINT_from_ARM7.translation.z;
526  msg_pose_pre_FOOTPRINT_from_ARM7.pose.orientation = msg_transform_pre_FOOTPRINT_from_ARM7.rotation;
527  if (debug)
528  {
529  ROS_DEBUG_STREAM("msg_pose_pre_FOOTPRINT_from_ARM7:" << msg_pose_pre_FOOTPRINT_from_ARM7);
530  }
531 
532  g.pre_grasp_approach = calculateApproachDirection(msg_pose_grasp_FOOTPRINT_from_ARM7.pose, msg_pose_pre_FOOTPRINT_from_ARM7.pose);
533 
534  //~~~ RetreatDirection ~~~
535  g.post_grasp_retreat.direction.header.frame_id = g.pre_grasp_approach.direction.header.frame_id;
536  g.post_grasp_retreat.direction.vector.x = -g.pre_grasp_approach.direction.vector.x;
537  g.post_grasp_retreat.direction.vector.y = -g.pre_grasp_approach.direction.vector.y;
538  g.post_grasp_retreat.direction.vector.z = -g.pre_grasp_approach.direction.vector.z + 0.5; //also lift the object a little bit
539 
540  g.post_grasp_retreat.min_distance = g.pre_grasp_approach.min_distance;
541  g.post_grasp_retreat.desired_distance = g.pre_grasp_approach.desired_distance;
542 
543  g.post_place_retreat = g.post_grasp_retreat;
544 
545  grasps.push_back(g);
546 }
547 
548 
549 
550 
551 void CobPickPlaceActionServer::fillGraspsOR(unsigned int objectClassId, std::string gripper_type, std::string gripper_side, unsigned int grasp_id, geometry_msgs::PoseStamped object_pose, std::vector<moveit_msgs::Grasp> &grasps)
552 {
553  bool finished_before_timeout;
554  grasps.clear();
555 
556  //ToDo: resolve object_class_name from objectClassId
557  if(map_classid_to_classname.find(objectClassId) == map_classid_to_classname.end())
558  {
559  ROS_ERROR("Unable to resolve class_name for class_id %d", objectClassId);
560  return;
561  }
562 
563 
564  cob_grasp_generation::QueryGraspsGoal goal_query_grasps;
565  goal_query_grasps.object_name = map_classid_to_classname.find(objectClassId)->second;
566  goal_query_grasps.gripper_type = gripper_type;
567  goal_query_grasps.gripper_side = gripper_side;
568  goal_query_grasps.grasp_id = grasp_id;
569  goal_query_grasps.num_grasps = 0;
570  goal_query_grasps.threshold = 0;//0.012;
571 
572  ac_grasps_or->sendGoal(goal_query_grasps);
573 
574  ROS_INFO("Querying grasps...");
575  finished_before_timeout = ac_grasps_or->waitForResult(ros::Duration(70.0));
576  actionlib::SimpleClientGoalState state_grasps_or = ac_grasps_or->getState();
578  if (finished_before_timeout)
579  {
580  ROS_INFO("Action finished: %s",state_grasps_or.toString().c_str());
581 
582  ROS_INFO("Found %lu grasps for this object", result_query_grasps.get()->grasp_list.size());
583  for(unsigned int i=0; i<result_query_grasps.get()->grasp_list.size(); i++)
584  {
585  ROS_DEBUG_STREAM("Grasp "<< i << ": " << result_query_grasps.get()->grasp_list[i]);
586  }
587 
588  for(unsigned int i=0; i<result_query_grasps.get()->grasp_list.size(); i++)
589  {
590  moveit_msgs::Grasp current_grasp;
591  //~~~ HandGraspConfig ~~~
592  current_grasp.grasp_posture = result_query_grasps.get()->grasp_list[i].grasp_posture;
593  //for(unsigned int k=0; k<current_grasp.grasp_posture.points[0].positions.size(); k++)
594  //{
595  //if(current_grasp.grasp_posture.points[0].positions[k] < -1.5707) current_grasp.grasp_posture.points[0].positions[k] = -1.5707;
596  //if(current_grasp.grasp_posture.points[0].positions[k] > 1.5707) current_grasp.grasp_posture.points[0].positions[k] = 1.5707;
597  //}
598  //~~~ HandPreGraspConfig ~~~
599  current_grasp.pre_grasp_posture = result_query_grasps.get()->grasp_list[i].pre_grasp_posture;
600 
601  //~~~ TCPGraspPose ~~~
603 
604  // O_from_SDH
605  geometry_msgs::Pose current_grasp_pose = result_query_grasps.get()->grasp_list[i].grasp_pose.pose;
606  tf::Transform transform_grasp_O_from_SDH = tf::Transform(
607  tf::Quaternion(current_grasp_pose.orientation.x, current_grasp_pose.orientation.y, current_grasp_pose.orientation.z, current_grasp_pose.orientation.w),
608  tf::Vector3(current_grasp_pose.position.x, current_grasp_pose.position.y, current_grasp_pose.position.z));
609 
610  //debug
611  geometry_msgs::Transform msg_grasp_O_from_SDH;
612  tf::transformTFToMsg(transform_grasp_O_from_SDH, msg_grasp_O_from_SDH);
613  ROS_DEBUG_STREAM("msg_grasp_O_from_SDH:" << msg_grasp_O_from_SDH);
614 
615  // HEADER_from_O (given)
616  tf::Transform transform_HEADER_from_O = tf::Transform(
617  tf::Quaternion(object_pose.pose.orientation.x, object_pose.pose.orientation.y, object_pose.pose.orientation.z, object_pose.pose.orientation.w),
618  tf::Vector3(object_pose.pose.position.x, object_pose.pose.position.y, object_pose.pose.position.z));
619  //debug
620  geometry_msgs::Transform msg_HEADER_from_O;
621  tf::transformTFToMsg(transform_HEADER_from_O, msg_HEADER_from_O);
622  ROS_DEBUG_STREAM("msg_HEADER_from_O:" << msg_HEADER_from_O);
623 
624  // FOOTPRINT_from_ARM7
625  tf::Transform transform_grasp_FOOTPRINT_from_ARM7 = transformPose(transform_grasp_O_from_SDH, transform_HEADER_from_O, object_pose.header.frame_id);
626  //debug
627  geometry_msgs::Transform msg_grasp_FOOTPRINT_from_ARM7;
628  tf::transformTFToMsg(transform_grasp_FOOTPRINT_from_ARM7, msg_grasp_FOOTPRINT_from_ARM7);
629  ROS_DEBUG_STREAM("msg_grasp_FOOTPRINT_from_ARM7:" << msg_grasp_FOOTPRINT_from_ARM7);
630 
631  // convert to PoseStamped
632  geometry_msgs::Transform msg_transform_grasp_FOOTPRINT_from_ARM7;
633  tf::transformTFToMsg(transform_grasp_FOOTPRINT_from_ARM7, msg_transform_grasp_FOOTPRINT_from_ARM7);
634  geometry_msgs::PoseStamped msg_pose_grasp_FOOTPRINT_from_ARM7;
635  msg_pose_grasp_FOOTPRINT_from_ARM7.header.stamp = ros::Time::now();
636  msg_pose_grasp_FOOTPRINT_from_ARM7.header.frame_id = "/base_footprint";
637  msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.x = msg_transform_grasp_FOOTPRINT_from_ARM7.translation.x;
638  msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.y = msg_transform_grasp_FOOTPRINT_from_ARM7.translation.y;
639  msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.z = msg_transform_grasp_FOOTPRINT_from_ARM7.translation.z;
640  msg_pose_grasp_FOOTPRINT_from_ARM7.pose.orientation = msg_transform_grasp_FOOTPRINT_from_ARM7.rotation;
641  ROS_DEBUG_STREAM("msg_pose_grasp_FOOTPRINT_from_ARM7:" << msg_pose_grasp_FOOTPRINT_from_ARM7);
642 
643  current_grasp.grasp_pose = msg_pose_grasp_FOOTPRINT_from_ARM7;
644 
645  //~~~ ApproachDirection ~~~
646  //current_grasp.pre_grasp_approach.direction.header.frame_id = "/base_footprint";
647  //current_grasp.pre_grasp_approach.direction.vector.x = 0.0;
648  //current_grasp.pre_grasp_approach.direction.vector.y = 0.0;
649  //current_grasp.pre_grasp_approach.direction.vector.z = -1.0;
650  //current_grasp.pre_grasp_approach.min_distance = 0.18;
651  //current_grasp.pre_grasp_approach.desired_distance = 0.28;
652 
653 
654  ROS_INFO_STREAM("EndeffectorLink: " << group.getEndEffectorLink());
655  current_grasp.pre_grasp_approach.direction.header.frame_id = group.getEndEffectorLink();
656  current_grasp.pre_grasp_approach.direction.vector.x = 0.0;
657  current_grasp.pre_grasp_approach.direction.vector.y = 0.0;
658  current_grasp.pre_grasp_approach.direction.vector.z = 1.0;
659  current_grasp.pre_grasp_approach.min_distance = 0.18;
660  current_grasp.pre_grasp_approach.desired_distance = 0.28;
661 
662  //current_grasp.pre_grasp_approach.direction.header.frame_id = "/base_footprint";
663  //current_grasp.pre_grasp_approach.direction.vector.x = -msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.x+object_pose.pose.position.x;
664  //current_grasp.pre_grasp_approach.direction.vector.y = -msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.y+object_pose.pose.position.y;
665  //current_grasp.pre_grasp_approach.direction.vector.z = -msg_pose_grasp_FOOTPRINT_from_ARM7.pose.position.z+object_pose.pose.position.z;
666  //current_grasp.pre_grasp_approach.min_distance = 0.18;
667  //current_grasp.pre_grasp_approach.desired_distance = 0.28;
668 
669  //~~~ RetreatDirection ~~~
670  current_grasp.post_grasp_retreat.direction.header.frame_id = "/base_footprint";
671  current_grasp.post_grasp_retreat.direction.vector.x = 0.0;
672  current_grasp.post_grasp_retreat.direction.vector.y = 0.0;
673  current_grasp.post_grasp_retreat.direction.vector.z = 1.0;
674  current_grasp.post_grasp_retreat.min_distance = 0.05;
675  current_grasp.post_grasp_retreat.desired_distance = 0.1;
676 
677  // current_grasp.post_grasp_retreat.direction.header.frame_id = "/arm_7_link";
678  // current_grasp.post_grasp_retreat.direction.vector.x = 0.0;
679  // current_grasp.post_grasp_retreat.direction.vector.y = 0.0;
680  // current_grasp.post_grasp_retreat.direction.vector.z = -1.0;
681  // current_grasp.post_grasp_retreat.min_distance = 0.1;
682  // current_grasp.post_grasp_retreat.desired_distance = 0.15;
683 
684  current_grasp.post_place_retreat = current_grasp.post_grasp_retreat;
685 
686  grasps.push_back(current_grasp);
687  }
688 
689  }
690  else
691  ROS_ERROR("Grasps not queried within timeout");
692 }
693 
694 
695 
696 
697 
698 
699 trajectory_msgs::JointTrajectory CobPickPlaceActionServer::MapHandConfiguration(sensor_msgs::JointState table_config)
700 {
701  trajectory_msgs::JointTrajectory grasp_configuration;
702 
703  trajectory_msgs::JointTrajectoryPoint point;
704  point.positions.assign(7,0.0);
705  point.velocities.assign(7,0.0);
706  point.accelerations.assign(7,0.0);
707  point.effort.assign(7,0.0);
708  point.time_from_start = ros::Duration(3.0);
709 
710  point.positions[0] = table_config.position[0];
711  point.positions[1] = table_config.position[2];
712  point.positions[2] = table_config.position[3];
713  point.positions[3] = table_config.position[5];
714  point.positions[4] = table_config.position[6];
715  point.positions[5] = table_config.position[11];
716  point.positions[6] = table_config.position[12];
717  grasp_configuration.points.push_back(point);
718 
719  grasp_configuration.joint_names.resize(7);
720  grasp_configuration.joint_names[0]=("sdh_knuckle_joint");
721  grasp_configuration.joint_names[1]=("sdh_thumb_2_joint");
722  grasp_configuration.joint_names[2]=("sdh_thumb_3_joint");
723  grasp_configuration.joint_names[3]=("sdh_finger_12_joint");
724  grasp_configuration.joint_names[4]=("sdh_finger_13_joint");
725  grasp_configuration.joint_names[5]=("sdh_finger_22_joint");
726  grasp_configuration.joint_names[6]=("sdh_finger_23_joint");
727 
728  //cut joint_values according to joint_limits
729  for(unsigned int i=0; i<grasp_configuration.points[0].positions.size();i++)
730  {
731  if(grasp_configuration.points[0].positions[i]>1.57079){grasp_configuration.points[0].positions[i]=1.57079;}
732  if(grasp_configuration.points[0].positions[i]<-1.57079){grasp_configuration.points[0].positions[i]=-1.57079;}
733  }
734  return grasp_configuration;
735 }
736 
737 tf::Transform CobPickPlaceActionServer::transformPose(tf::Transform transform_O_from_SDH, tf::Transform transform_HEADER_from_O, std::string object_frame_id)
738 {
739  bool debug = false;
740 
741  // SDH_from_ARM7
742  tf::StampedTransform transform_SDH_from_ARM7;
743 
744  bool transform_available = false;
745  while(!transform_available)
746  {
747  try{
749  //tf_listener_.lookupTransform("/sdh_palm_link", group.getEndEffectorLink(), ros::Time(0), transform_SDH_from_ARM7);
750  tf_listener_.lookupTransform("/gripper_left_palm_link", group.getEndEffectorLink(), ros::Time(0), transform_SDH_from_ARM7);
751  transform_available = true;
752  }
753  catch (tf::TransformException ex){
754  ROS_WARN("Waiting for transform...(%s)",ex.what());
755  ros::Duration(0.1).sleep();
756  }
757  }
758 
759  //debug
760  geometry_msgs::TransformStamped msg_SDH_from_ARM7;
761  tf::transformStampedTFToMsg(transform_SDH_from_ARM7, msg_SDH_from_ARM7);
762 
763  if (debug)
764  ROS_DEBUG_STREAM("msg_SDH_from_ARM7:" << msg_SDH_from_ARM7);
765 
766  // O_from_ARM7 = O_from_SDH * SDH_from_ARM7
767  tf::Transform transform_O_from_ARM7 = transform_O_from_SDH * transform_SDH_from_ARM7;
768 
769  // FOOTPRINT_from_HEADER
770  tf::StampedTransform transform_FOOTPRINT_from_HEADER;
771  try
772  {
774  tf_listener_.waitForTransform("/base_footprint", object_frame_id, now, ros::Duration(10.0));
775  tf_listener_.lookupTransform("/base_footprint", object_frame_id, now, transform_FOOTPRINT_from_HEADER);
776  }
777  catch (tf::TransformException ex)
778  {
779  ROS_ERROR("%s",ex.what());
780  }
781  //debug
782  geometry_msgs::TransformStamped msg_FOOTPRINT_from_HEADER;
783  tf::transformStampedTFToMsg(transform_FOOTPRINT_from_HEADER, msg_FOOTPRINT_from_HEADER);
784 
785  if (debug)
786  ROS_DEBUG_STREAM("msg_FOOTPRINT_from_HEADER:" << msg_FOOTPRINT_from_HEADER);
787 
788  // FOOTPRINT_from_O = FOOTPRINT_from_HEADER * HEADER_from_O
789  tf::Transform transform_FOOTPRINT_from_O = transform_FOOTPRINT_from_HEADER * transform_HEADER_from_O;
790 
791  // FOOTPRINT_from_ARM7 = FOOTPRINT_from_O * O_from_ARM7
792  tf::Transform transform_FOOTPRINT_from_ARM7 = transform_FOOTPRINT_from_O * transform_O_from_ARM7;
793 
794  return transform_FOOTPRINT_from_ARM7;
795 }
796 
797 moveit_msgs::GripperTranslation CobPickPlaceActionServer::calculateApproachDirection(geometry_msgs::Pose msg_pose_grasp_FOOTPRINT_from_ARM7, geometry_msgs::Pose msg_pose_pre_FOOTPRINT_from_ARM7)
798 {
799  double finger_length = 0.18; //this is the lenght of the sdh ('home' configuration)
800  moveit_msgs::GripperTranslation approach;
801  approach.direction.header.frame_id = "/base_footprint";
802  //~ dis=sqrt((x1-x0)^2+(y1-y0)^2+(z1-z0)^2)
803  //~ direction.x= (x1-x0)/dis and likewise
804 
805  double distance = sqrt(
806  pow((msg_pose_grasp_FOOTPRINT_from_ARM7.position.x-msg_pose_pre_FOOTPRINT_from_ARM7.position.x),2) +
807  pow((msg_pose_grasp_FOOTPRINT_from_ARM7.position.y-msg_pose_pre_FOOTPRINT_from_ARM7.position.y),2) +
808  pow((msg_pose_grasp_FOOTPRINT_from_ARM7.position.z-msg_pose_pre_FOOTPRINT_from_ARM7.position.z),2));
809 
810  ROS_DEBUG("Distance between pre-grasp and grasp: %f", distance);
811 
812  approach.direction.vector.x = (msg_pose_grasp_FOOTPRINT_from_ARM7.position.x-msg_pose_pre_FOOTPRINT_from_ARM7.position.x)/distance;
813  approach.direction.vector.y = (msg_pose_grasp_FOOTPRINT_from_ARM7.position.y-msg_pose_pre_FOOTPRINT_from_ARM7.position.y)/distance;
814  approach.direction.vector.z = (msg_pose_grasp_FOOTPRINT_from_ARM7.position.z-msg_pose_pre_FOOTPRINT_from_ARM7.position.z)/distance;
815 
816  //min_distance means that we want to follow at least this length along the apporach_vector
817  if(distance < finger_length)
818  {
819  approach.min_distance = finger_length;
820  }
821  else
822  {
823  approach.min_distance = distance;
824  }
825  approach.desired_distance = approach.min_distance + 0.1;
826 
827  return approach;
828 }
829 
830 
831 
832 
833 
834 
835 
836 
837 
838 
839 
840 
841 
842 
843 
844 int main(int argc, char **argv)
845 {
846  ros::init (argc, argv, "cob_pick_place_action_server_node");
847  if( argc != 2 )
848  {
849  ROS_ERROR("No manipulator specified!");
850  return -1;
851  }
852  else
853  {
854  ROS_INFO("Starting PickPlace ActionServer for group %s", argv[1]);
855  }
856 
857  CobPickPlaceActionServer *cob_pick_place_action_server = new CobPickPlaceActionServer(std::string(argv[1]));
858 
859  cob_pick_place_action_server->initialize();
860  cob_pick_place_action_server->run();
861 
862  return 0;
863 }
string object_name
ADDING OBJECT.
#define NULL
Grasp * GetGrasp(unsigned int object_class_id, unsigned int &grasp_id)
Definition: GraspTable.cpp:206
int main(int argc, char **argv)
static tf::Quaternion createQuaternionFromRPY(double roll, double pitch, double yaw)
TFSIMD_FORCE_INLINE void setRotation(const Quaternion &q)
moveit_msgs::GripperTranslation calculateApproachDirection(geometry_msgs::Pose msg_pose_grasp_FOOTPRINT_from_ARM7, geometry_msgs::Pose msg_pose_pre_FOOTPRINT_from_ARM7)
double now()
boost::scoped_ptr< actionlib::SimpleActionServer< cob_pick_place_action::CobPlaceAction > > as_place
path
void publish(const boost::shared_ptr< M > &message) const
Grasp * GetNextGrasp(unsigned int object_class_id)
Definition: GraspTable.cpp:183
void fillGraspsOR(unsigned int objectClassId, std::string gripper_type, std::string gripper_side, unsigned int grasp_id, geometry_msgs::PoseStamped object_pose, std::vector< moveit_msgs::Grasp > &grasps)
MoveItErrorCode pick(const std::string &object, bool plan_only=false)
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
tf::TransformBroadcaster tf_broadcaster_
CobPickPlaceActionServer(std::string group_name)
boost::scoped_ptr< actionlib::SimpleActionServer< cob_pick_place_action::CobPickAction > > as_pick
std::vector< double > GetTCPGraspPose()
Definition: GraspTable.h:36
#define ROS_WARN(...)
boost::scoped_ptr< actionlib::SimpleActionClient< cob_grasp_generation::QueryGraspsAction > > ac_grasps_or
void insertObject(std::string object_name, unsigned int object_class, geometry_msgs::PoseStamped object_pose)
ROSCPP_DECL void spin(Spinner &spinner)
moveit::planning_interface::MoveGroupInterface group
const std::string & getEndEffectorLink() const
bool waitForTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, const ros::Duration &timeout, const ros::Duration &polling_sleep_duration=ros::Duration(0.01), std::string *error_msg=NULL) const
std::string toString() const
void fillSingleGraspKIT(unsigned int objectClassId, std::string gripper_type, unsigned int grasp_id, geometry_msgs::PoseStamped object_pose, std::vector< moveit_msgs::Grasp > &grasps)
success
def error(args, kwargs)
#define ROS_INFO(...)
void sendTransform(const StampedTransform &transform)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
MoveItErrorCode place(const std::string &object, bool plan_only=false)
int Init(char *iniFile, unsigned int table_size=MAX_NO_OF_OBJECTS)
Definition: GraspTable.cpp:124
#define ROS_DEBUG_STREAM(args)
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
ROSLIB_DECL std::string getPath(const std::string &package_name)
std::vector< double > GetHandPreGraspConfig()
Definition: GraspTable.h:40
tf::Transform transformPose(tf::Transform transform_O_from_SDH, tf::Transform transform_HEADER_from_OBJECT, std::string object_frame_id)
bool constructMsgFromShape(const Shape *shape, ShapeMsg &shape_msg)
Mesh * createMeshFromResource(const std::string &resource)
#define ROS_INFO_STREAM(args)
void place_goal_cb(const cob_pick_place_action::CobPlaceGoalConstPtr &goal)
trajectory_msgs::JointTrajectory MapHandConfiguration(sensor_msgs::JointState table_config)
std::vector< double > GetHandOptimalGraspConfig()
Definition: GraspTable.h:46
void setSupportSurfaceName(const std::string &name)
static Time now()
tf::TransformListener tf_listener_
static void transformStampedTFToMsg(const StampedTransform &bt, geometry_msgs::TransformStamped &msg)
void pick_goal_cb(const cob_pick_place_action::CobPickGoalConstPtr &goal)
#define ROS_ERROR_STREAM(args)
std::vector< double > GetTCPPreGraspPose()
Definition: GraspTable.h:33
std::map< unsigned int, std::string > map_classid_to_classname
void fillAllGraspsKIT(unsigned int objectClassId, std::string gripper_type, geometry_msgs::PoseStamped object_pose, std::vector< moveit_msgs::Grasp > &grasps)
#define ROS_ERROR(...)
TFSIMD_FORCE_INLINE void setOrigin(const Vector3 &origin)
double distance(const urdf::Pose &transform)
static void transformTFToMsg(const Transform &bt, geometry_msgs::Transform &msg)
boost::variant< shape_msgs::SolidPrimitive, shape_msgs::Mesh, shape_msgs::Plane > ShapeMsg
void ResetReadPtr(unsigned int object_class_id)
Definition: GraspTable.cpp:198
void convertGraspKIT(Grasp *current_grasp, geometry_msgs::PoseStamped object_pose, std::vector< moveit_msgs::Grasp > &grasps)
#define ROS_DEBUG(...)


cob_pick_place_action
Author(s): Felix Messmer
autogenerated on Mon Jun 10 2019 13:10:02