pick_and_place_node.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <std_msgs/String.h>
3 #include <std_srvs/Trigger.h>
4 #include <geometry_msgs/PointStamped.h>
5 #include <trajectory_msgs/JointTrajectory.h>
6 #include <std_msgs/Empty.h>
9 #include <control_msgs/GripperCommandAction.h>
13 #include <std_srvs/SetBool.h>
14 #include <moveit_msgs/PickupAction.h>
15 #include <moveit_msgs/PlaceAction.h>
16 #include <std_msgs/Float64MultiArray.h>
17 
18 #define MAX_BOARD_PLACE 0.05
21 
22 struct Point_t {
23  float x;
24  float y;
25 
26  Point_t() {
27  x = y = 0.0;
28  }
29 
30 };
31 
32 void look_down();
33 
34 bool set_collision_update(bool state);
35 
36 moveit_msgs::PickupGoal BuildPickGoal(const std::string &objectName);
37 
38 moveit_msgs::PlaceGoal buildPlaceGoal(const std::string &objectName);
39 
40 bool pickAndPlaceCallBack(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res);
41 
42 double randBetweenTwoNum(int max, int min);
43 
44 bool exec = false;
49  std::string startPositionName ;
51 
52 
53 int main(int argc, char **argv) {
54 
55  ros::init(argc, argv, "pick_and_plce_node");
57  spinner.start();
58  srand((unsigned int) time(NULL));
60  ros::NodeHandle pn("~");
61  std::string object_name,table_name;
62 
63 
64 
65 
66  pn.param<std::string>("start_position_name", startPositionName, "pre_grasp2");
67  pn.param<std::string>("object_name", object_name, "can");
68  pn.param<std::string>("table_name", table_name, "table");
69 
70  ros::ServiceServer pickAndPlace = n.advertiseService("pick_go", &pickAndPlaceCallBack);
71  ROS_INFO("Hello");
73  group_ptr=&group;
74  //Config move group
75  //group.setMaxVelocityScalingFactor(0.1);
76  //group.setMaxAccelerationScalingFactor(0.5);
77  group.setPlanningTime(10.0);
78  group.setNumPlanningAttempts(50);
79  group.setPlannerId("RRTConnectkConfigDefault");
80  group.setPoseReferenceFrame("base_footprint");
81 
83  group.setNamedTarget(startPositionName);
85  if(group.plan(startPosPlan)) { //Check if plan is valid
86  group.execute(startPosPlan);
87  pub_controller_command = n.advertise<trajectory_msgs::JointTrajectory>("/pan_tilt_trajectory_controller/command", 2);
88  //grp_pos_pub = n.advertise<std_msgs::Float64MultiArray>("pan_tilt_controller/command", 5);
89  ROS_INFO("Waiting for the moveit action server to come up");
90  ros::ServiceClient uc_client = n.serviceClient<std_srvs::SetBool>("update_collision_objects");
91  ROS_INFO("Waiting for update_collision service...");
92  uc_client.waitForExistence();
93  uc_client_ptr = &uc_client;
95  ros::Duration(5.0).sleep();
96  look_down();
97  ROS_INFO("Looking down...");
98  ROS_INFO("Ready!");
99  }
100  else {
101  ROS_ERROR("Error");
102  }
104  return 0;
105 }
106 
107 moveit_msgs::PlaceGoal buildPlaceGoal(const std::string &objectName) {
108  moveit_msgs::PlaceGoal placeGoal;
109  placeGoal.group_name = "arm";
110  placeGoal.attached_object_name = objectName;
111  placeGoal.place_eef = false;
112  placeGoal.support_surface_name = "table";
113  placeGoal.planner_id = "RRTConnectkConfigDefault";
114  placeGoal.allowed_planning_time = 15.0;
115  placeGoal.planning_options.replan = true;
116  placeGoal.planning_options.replan_attempts = 5;
117  placeGoal.planning_options.replan_delay = 2.0;
118  placeGoal.planning_options.planning_scene_diff.is_diff = true;
119  placeGoal.planning_options.planning_scene_diff.robot_state.is_diff = true;
120  std::vector<moveit_msgs::PlaceLocation> locations;
121  moveit_msgs::PlaceLocation location;
122  location.pre_place_approach.direction.header.frame_id = "/base_footprint";
123  location.pre_place_approach.direction.vector.z = -1.0;
124  location.pre_place_approach.min_distance = 0.1;
125  location.pre_place_approach.desired_distance = 0.2;
126 
127  location.post_place_retreat.direction.header.frame_id = "/gripper_link";
128  location.post_place_retreat.direction.vector.x = -1.0;
129  location.post_place_retreat.min_distance = 0.0;
130  location.post_place_retreat.desired_distance = 0.2;
131 
132  location.place_pose.header.frame_id = placeGoal.support_surface_name;
133  bool inTheBoard;
134  do {
135  inTheBoard = true;
136  location.place_pose.pose.position.x = randBetweenTwoNum(10, -10);
137  if(fabs(point.x + location.place_pose.pose.position.x) >= MAX_BOARD_PLACE) {
138  inTheBoard = false;
139  }
140  } while (!inTheBoard);
141 
142  do {
143  inTheBoard = true;
144  location.place_pose.pose.position.y = randBetweenTwoNum(5, -5);
145  if(fabs(point.y + location.place_pose.pose.position.y) >= MAX_BOARD_PLACE) {
146  inTheBoard = false;
147  }
148  } while (!inTheBoard);
149 
150  location.place_pose.pose.position.z = 0.13;
151  location.place_pose.pose.orientation.w = 1.0;
152 
153  locations.push_back(location);
154  placeGoal.place_locations = locations;
155  return placeGoal;
156 }
157 
158 moveit_msgs::PickupGoal BuildPickGoal(const std::string &objectName) {
159  moveit_msgs::PickupGoal goal;
160  goal.target_name = objectName;
161  goal.group_name = "arm";
162  goal.end_effector = "eef";
163  goal.allowed_planning_time = 15.0;
164  goal.planning_options.replan_delay = 2.0;
165  goal.planning_options.planning_scene_diff.is_diff = true;
166  goal.planning_options.planning_scene_diff.robot_state.is_diff = true;
167  goal.planning_options.replan=true;
168  goal.planning_options.replan_attempts=5;
169  goal.planner_id = "RRTConnectkConfigDefault";
170 
171  goal.minimize_object_distance = true;
172  moveit_msgs::Grasp g;
173  g.max_contact_force = 1.0; //0.01
174  g.grasp_pose.header.frame_id = goal.target_name;
175  g.grasp_pose.pose.position.x = -0.02;
176  g.grasp_pose.pose.position.y = 0.0;
177  g.grasp_pose.pose.position.z = 0.0;
178  g.grasp_pose.pose.orientation.x = 0.0;
179  g.grasp_pose.pose.orientation.y = 0.0;
180  g.grasp_pose.pose.orientation.z = 0.0;
181  g.grasp_pose.pose.orientation.w = 1.0;
182 
183  g.pre_grasp_approach.direction.header.frame_id = "/base_footprint"; //gripper_link
184  g.pre_grasp_approach.direction.vector.x = 1.0;
185  g.pre_grasp_approach.min_distance = 0.01;
186  g.pre_grasp_approach.desired_distance = 0.2;
187 
188  g.post_grasp_retreat.direction.header.frame_id = "/base_footprint"; //gripper_link
189  g.post_grasp_retreat.direction.vector.z = 1.0;
190  g.post_grasp_retreat.min_distance = 0.1;
191  g.post_grasp_retreat.desired_distance = 0.2;
192 
193  g.pre_grasp_posture.joint_names.push_back("left_finger_joint");
194  g.pre_grasp_posture.joint_names.push_back("right_finger_joint");
195  g.pre_grasp_posture.points.resize(1);
196  g.pre_grasp_posture.points[0].positions.resize(g.pre_grasp_posture.joint_names.size());
197  g.pre_grasp_posture.points[0].positions[0] = 0.14;
198 
199  g.grasp_posture.joint_names = g.pre_grasp_posture.joint_names;
200  g.grasp_posture.points.resize(1);
201  g.grasp_posture.points[0].positions.resize(g.grasp_posture.joint_names.size());
202  g.grasp_posture.points[0].positions[0] = 0.01;
203  g.grasp_posture.points[0].effort.resize(g.grasp_posture.joint_names.size());
204  g.grasp_posture.points[0].effort[0] = 0.6;
205  goal.possible_grasps.push_back(g);
206  return goal;
207 }
208 
209 void look_down() {
210 
211  trajectory_msgs::JointTrajectory traj;
212  traj.header.stamp = ros::Time::now();
213  traj.joint_names.push_back("head_pan_joint");
214  traj.joint_names.push_back("head_tilt_joint");
215  traj.points.resize(1);
216  traj.points[0].time_from_start = ros::Duration(1.0);
217  std::vector<double> q_goal(2);
218  q_goal[0]=0.0;
219  q_goal[1]=0.7;
220  traj.points[0].positions=q_goal;
221  traj.points[0].velocities.push_back(0);
222  traj.points[0].velocities.push_back(0);
223  pub_controller_command.publish(traj);
224 /*
225  std_msgs::Float64MultiArray grp_pos_msg;
226  grp_pos_msg.data.push_back(0);
227  grp_pos_msg.data.push_back(0.7);
228  grp_pos_pub.publish(grp_pos_msg);
229 */
230 }
231 
232 bool set_collision_update(bool state){
233  std_srvs::SetBool srv;
234  srv.request.data=state;
235  if (uc_client_ptr->call(srv))
236  {
237  ROS_INFO("update_colision response: %s", srv.response.message.c_str());
238  return true;
239  }
240  else
241  {
242  ROS_ERROR("Failed to call service /find_objects_node/update_colision");
243  return false;
244  }
245 
246 }
247 
248 bool pickAndPlaceCallBack(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res) {
249  ros::NodeHandle pn("~");
250  ros::NodeHandle n;
251  std::string object_name;
252 
253  pn.param<std::string>("object_name", object_name, "can");
254 
255  ros::ServiceClient uc_client = n.serviceClient<std_srvs::SetBool>("update_collision_objects");
256  std_srvs::SetBool disableColl;
257  disableColl.request.data = false;
258 
259  if(uc_client.call(disableColl)) {
260  ROS_INFO("update_colision response: OFF ");
261  }
262 
263  PickClient pickClient("pickup", true);
264  pickClient.waitForServer();
265 
266  moveit_msgs::PickupGoal pickGoal = BuildPickGoal(object_name);
267  actionlib::SimpleClientGoalState pickStatus = pickClient.sendGoalAndWait(pickGoal);
269  res.success = (unsigned char) false;
270  res.message = pickStatus.getText();
271  point.x = point.y = 0;
272  }
273  else {
274  PlaceClient placeClient("place", true);
275  placeClient.waitForServer();
276  bool found = false;
277  do {
278  moveit_msgs::PlaceGoal placeGoal = buildPlaceGoal(object_name);
279  actionlib::SimpleClientGoalState placeStatus = placeClient.sendGoalAndWait(placeGoal);
280  if (placeStatus == actionlib::SimpleClientGoalState::SUCCEEDED) {
281  found = true;
282  res.success = (unsigned char) (found);
283  res.message = placeStatus.getText();
284  point.x += placeGoal.place_locations[0].place_pose.pose.position.x;
285  point.y += placeGoal.place_locations[0].place_pose.pose.position.y;
286  }
287  } while(!found);
288  }
289 
290  group_ptr->setStartStateToCurrentState();
291  group_ptr->setNamedTarget(startPositionName);
293  if(group_ptr->plan(startPosPlan)) { //Check if plan is valid
294  group_ptr->execute(startPosPlan);
295  }
296 
297  std_srvs::SetBool enableColl;
298  enableColl.request.data = true;
299  if(uc_client.call(enableColl)) {
300  ROS_INFO("update_colision response: ON ");
301  }
302 
303  return true;
304 
305 
306 }
307 double randBetweenTwoNum(int max, int min) {
308  int randomNum = rand()%(max-min + 1) + min;
309  return randomNum / 100.0;
310 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
moveit_msgs::PickupGoal BuildPickGoal(const std::string &objectName)
bool waitForServer(const ros::Duration &timeout=ros::Duration(0, 0)) const
ros::Publisher pub_controller_command
void publish(const boost::shared_ptr< M > &message) const
actionlib::SimpleActionClient< moveit_msgs::PickupAction > PickClient
bool exec
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
void setPoseReferenceFrame(const std::string &pose_reference_frame)
bool found
#define MAX_BOARD_PLACE
double randBetweenTwoNum(int max, int min)
void setPlannerId(const std::string &planner_id)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
int main(int argc, char **argv)
void spinner()
Point_t point
group
bool pickAndPlaceCallBack(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
ros::ServiceClient * uc_client_ptr
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
SimpleClientGoalState sendGoalAndWait(const Goal &goal, const ros::Duration &execute_timeout=ros::Duration(0, 0), const ros::Duration &preempt_timeout=ros::Duration(0, 0))
bool set_collision_update(bool state)
std::string getText() const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
MoveItErrorCode execute(const Plan &plan)
std::string startPositionName
void setNumPlanningAttempts(unsigned int num_planning_attempts)
moveit::planning_interface::MoveGroupInterface * group_ptr
void look_down()
static Time now()
moveit_msgs::PlaceGoal buildPlaceGoal(const std::string &objectName)
ros::Publisher grp_pos_pub
#define ROS_ERROR(...)
actionlib::SimpleActionClient< moveit_msgs::PlaceAction > PlaceClient
bool setNamedTarget(const std::string &name)
ROSCPP_DECL void waitForShutdown()


armadillo2_demos
Author(s):
autogenerated on Wed Jan 3 2018 03:47:44