drive2object.cpp
Go to the documentation of this file.
1 
2 
3 #include <std_srvs/SetBool.h>
4 #include <ros/ros.h>
5 #include <std_msgs/String.h>
6 #include <move_base_msgs/MoveBaseAction.h>
7 #include <geometry_msgs/PointStamped.h>
8 #include <std_srvs/Trigger.h>
12 
14 
17 
18 move_base_msgs::MoveBaseGoal get_pre_pick_pose();
19 bool base_cmd(move_base_msgs::MoveBaseGoal goal);
20 
22 
23 bool moving=false;
24 std::string object_name;
26 
27 
28 
29 move_base_msgs::MoveBaseGoal get_pre_pick_pose(geometry_msgs::Point point) {
30  tf::Transform dest_transform;
31  move_base_msgs::MoveBaseGoal goal;
32  try{
33 
34  tf::StampedTransform transform_base;
35  try {
36  listener_ptr->lookupTransform("map", "base_link", ros::Time(0), transform_base);
37 
38  }
39  catch (tf::TransformException ex){
40  ROS_ERROR("%s",ex.what());
41  return goal;
42  }
43  tf::Vector3 v_obj(point.x,point.y,point.z);
44 
45  tf::Vector3 v_base =transform_base.getOrigin();
46 
47  tf::Vector3 v=v_obj-v_base;
48  double yaw=atan2(v.y(),v.x());
49  double away=base_distance_from_object/sqrt(v.x()*v.x()+v.y()*v.y());
50  tf::Vector3 dest=v_base+v*(1-away);
51 
52  dest_transform.setOrigin( dest );
53  dest.setZ(0);
55  q.setRPY(0.0, 0, yaw);
56  dest_transform.setRotation(q);
57 
58  //std::cout<< map_object_pose.pose.position<<std::endl;
59 
60  }
61 
62  catch (tf::TransformException ex){
63  ROS_ERROR("%s",ex.what());
64  return goal;
65  }
66 
67  goal.target_pose.header.frame_id = "map";
68  goal.target_pose.header.stamp = ros::Time::now();
69  goal.target_pose.pose.position.x=dest_transform.getOrigin().x();
70  goal.target_pose.pose.position.y=dest_transform.getOrigin().y();
71  goal.target_pose.pose.position.z=0;
72  goal.target_pose.pose.orientation.x=dest_transform.getRotation().x();
73  goal.target_pose.pose.orientation.y=dest_transform.getRotation().y();
74  goal.target_pose.pose.orientation.z=dest_transform.getRotation().z();
75  goal.target_pose.pose.orientation.w=dest_transform.getRotation().w();
76  return goal;
77 }
78 
79 
80 
81 bool drive_go_cb(std_srvs::Trigger::Request &req,
82  std_srvs::Trigger::Response &res)
83 {
84 
85 
87  ros::ServiceClient uc_client = n.serviceClient<std_srvs::SetBool>("update_collision_objects");
88  std_srvs::SetBool srv;
89 
90  std::vector<std::string> ids;
91  ids.push_back(object_name);
92  std::map<std::string, moveit_msgs::CollisionObject> poses=planning_scene_interface_ptr->getObjects(ids);
93  std::map<std::string, moveit_msgs::CollisionObject>::iterator it;
94 
95  it = poses.find(object_name);
96 
97  if (it != poses.end()) {
98  moveit_msgs::CollisionObject obj=it->second;
99 
100  if (!moving) moving=true;
101 
102  move_base_msgs::MoveBaseGoal goal=get_pre_pick_pose(obj.primitive_poses[0].position);
103 
104 
105 
106  srv.request.data=false;
107  uc_client.call(srv);
108 
109  if (base_cmd(goal)) {
110  ROS_INFO("Reached position");
111  res.message="Reached pre-picking position";
112  res.success=true;
113  }
114  else{
115  ROS_INFO("move_base failed");
116  res.message="move_base failed";
117  res.success=false;
118  }
119  moving=false;
120  srv.request.data=true;
121  uc_client.call(srv);
122  return true;
123  }
124  else {
125  ROS_ERROR("No object found");
126  res.message="No object found";
127  res.success=false;
128  srv.request.data=true;
129  uc_client.call(srv);
130  return true;
131  }
132 
133 
134 
135 
136 }
137 
138 
139 
140 bool base_cmd(move_base_msgs::MoveBaseGoal goal) {
141 
142  ROS_INFO("[%s]: Sending goal", ros::this_node::getName().c_str());
143 
144  moveBaseClient_ptr->sendGoal(goal);
145  moveBaseClient_ptr->waitForResult();
146 
147  if(moveBaseClient_ptr->getState() == actionlib::SimpleClientGoalState::SUCCEEDED) {
148  return true;
149  }
150  else {
151  ROS_ERROR("[%s]: Navigation failed ", ros::this_node::getName().c_str());
152  return false;
153  }
154 }
155 int main(int argc, char **argv) {
156 
157  ros::init(argc, argv, "drive2object_node");
158  ros::NodeHandle n;
159  ros::NodeHandle pn("~");
160 
161 
162  pn.param<double>("base_distance_from_object", base_distance_from_object, 0.55);
163  pn.param<std::string>("object_name", object_name, "can");
164 
165  MoveBaseClient moveBaseClient("move_base", true);
166  //wait for the action server to come up
167  while(!moveBaseClient.waitForServer(ros::Duration(5.0))){
168  ROS_INFO("Waiting for the move_base action server to come up");
169  }
170 
171  moveBaseClient_ptr=&moveBaseClient;
172 
173 
174  ros::ServiceServer service = n.advertiseService("drive2object_go", drive_go_cb);
175 
176  tf::TransformListener listener;
177  listener_ptr=&listener;
178 
180  planning_scene_interface_ptr=&planning_scene_interface;
181 
182  ros::ServiceClient uc_client = n.serviceClient<std_srvs::SetBool>("update_collision_objects");
183  ROS_INFO("Waiting for update_collision service...");
184  uc_client.waitForExistence();
185  ROS_INFO("Ready! run: 'rosservice call /drive2object_go' to initiate");
186  std_srvs::SetBool srv;
187  srv.request.data=true;
188  uc_client.call(srv);
189 
190  ros::spin();
191 
192  return 0;
193 }
194 
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
TFSIMD_FORCE_INLINE void setRotation(const Quaternion &q)
move_base_msgs::MoveBaseGoal get_pre_pick_pose()
actionlib::SimpleActionClient< move_base_msgs::MoveBaseAction > MoveBaseClient
bool waitForResult(const ros::Duration &timeout=ros::Duration(0, 0))
std::string object_name
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
ROSCPP_DECL const std::string & getName()
std::map< std::string, moveit_msgs::CollisionObject > getObjects(const std::vector< std::string > &object_ids=std::vector< std::string >())
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
ROSCPP_DECL void spin(Spinner &spinner)
bool drive_go_cb(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
bool moving
bool base_cmd(move_base_msgs::MoveBaseGoal goal)
TFSIMD_FORCE_INLINE const tfScalar & x() const
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
void setRPY(const tfScalar &roll, const tfScalar &pitch, const tfScalar &yaw)
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
TFSIMD_FORCE_INLINE const tfScalar & y() const
double base_distance_from_object
MoveBaseClient * moveBaseClient_ptr
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
tf::TransformListener * listener_ptr
void sendGoal(const Goal &goal, SimpleDoneCallback done_cb=SimpleDoneCallback(), SimpleActiveCallback active_cb=SimpleActiveCallback(), SimpleFeedbackCallback feedback_cb=SimpleFeedbackCallback())
Quaternion getRotation() const
static Time now()
SimpleClientGoalState getState() const
#define ROS_ERROR(...)
TFSIMD_FORCE_INLINE void setOrigin(const Vector3 &origin)
moveit::planning_interface::PlanningSceneInterface * planning_scene_interface_ptr
int main(int argc, char **argv)


robotican_common
Author(s):
autogenerated on Wed Jan 3 2018 03:48:33