cb_base_navigation
global_planner_interface.cpp
Go to the documentation of this file.
2 
5 
6 namespace cb_global_planner {
7 
9 {
10  // Clean things up
11  global_planner_.reset();
12 }
13 
15  gp_loader_("cb_base_navigation", "cb_global_planner::GlobalPlannerPlugin"),
16  tf_(tf),
17  costmap_(costmap)
18 {
19  ros::NodeHandle nh("~");
20  ros::NodeHandle gh;
21 
22  // Parameter setup
23  std::string global_planner;
24  nh.param("global_planner", global_planner, std::string("cb_global_planner::AStarPlannerGPP"));
25  nh.param("robot_base_frame", robot_base_frame_, std::string("base_link"));;
26  nh.param("global_frame", global_frame_, std::string("map"));
27 
28  // Initialize the global planner
29  try {
30  if(!gp_loader_.isClassAvailable(global_planner)){
31  std::vector<std::string> classes = gp_loader_.getDeclaredClasses();
32  for(unsigned int i = 0; i < classes.size(); ++i){
33  if(global_planner == gp_loader_.getName(classes[i])){
34  global_planner = classes[i]; break;
35  }
36  }
37  }
38  global_planner_ = gp_loader_.createInstance(global_planner);
39  global_planner_->initialize(gp_loader_.getName(global_planner), tf_, costmap_);
40  } catch (const pluginlib::PluginlibException& ex)
41  {
42  ROS_FATAL_NAMED("GPI", "Failed to create the %s planner, are you sure it is properly registered and that the containing library is built? Exception: %s", global_planner.c_str(), ex.what());
43  exit(0);
44  }
45 
46  // Register the ROS Service Servers
47  get_plan_srv_ = nh.advertiseService("get_plan_srv", &GlobalPlannerInterface::getPlan, this);
48  check_plan_srv_ = nh.advertiseService("check_plan_srv", &GlobalPlannerInterface::checkPlan, this);
49 
50  // Pose callback and plan pub
51  pose_sub_ = nh.subscribe("/move_base_simple/goal", 1, &GlobalPlannerInterface::poseCallback, this);
52  plan_pub_ = gh.advertise<cb_base_navigation_msgs::LocalPlannerActionGoal>("local_planner/action_server/goal",1);
53 
54  ROS_INFO_STREAM_NAMED("GPI", "Subsribed to '" << pose_sub_.getTopic() << "' for simple pose callbacks and I will send the plans to '" << plan_pub_.getTopic() << "'.");
55 }
56 
57 void GlobalPlannerInterface::poseCallback(const geometry_msgs::PoseStampedConstPtr& pose)
58 {
59  ROS_INFO_NAMED("GPI", "Simple Pose callback");
60 
61  cb_base_navigation_msgs::GetPlanRequest req;
62  cb_base_navigation_msgs::GetPlanResponse resp;
63 
64  // Get if the robot pose is available
65  if (!costmap_->getRobotPose(req.start))
66  {
67  ROS_ERROR_NAMED("GPI", "Could not get global robot pose. We can't generate a plan, sorry.");
68  return;
69  }
70 
71  cb_base_navigation_msgs::PositionConstraint pc;
72  pc.frame = global_frame_;
74  str << "(x-" << pose->pose.position.x << ")^2 + (y-" << pose->pose.position.y << ")^2 < .05^2";
75  pc.constraint = str.str();
76  req.goal_position_constraints.push_back(pc);
77 
78  if (getPlan(req,resp)) {
79  if(resp.succes) {
80  cb_base_navigation_msgs::OrientationConstraint oc;
81  oc.frame = global_frame_;
82  tf2::Quaternion quat_tf;
83  tf2::fromMsg(pose->pose.orientation, quat_tf);
84  oc.look_at.x = pose->pose.position.x + cos(tf2::getYaw(quat_tf));
85  oc.look_at.y = pose->pose.position.y + sin(tf2::getYaw(quat_tf));
86 
87  cb_base_navigation_msgs::LocalPlannerActionGoal goal;
88  goal.goal.orientation_constraint = oc;
89  goal.goal.plan = resp.plan;
90  plan_pub_.publish(goal);
91  ROS_INFO_NAMED("GPI", "Succesfully published plan to local planner");
92  } else {
93  ROS_INFO_NAMED("GPI", "I can't find a valid plan to there");
94  }
95  } else {
96  ROS_ERROR_NAMED("GPI", "Could not get a plan");
97  }
98 }
99 
100 bool GlobalPlannerInterface::checkPlan(cb_base_navigation_msgs::CheckPlanRequest &req, cb_base_navigation_msgs::CheckPlanResponse &resp)
101 {
102  // Lock the costmap for a sec
103  boost::unique_lock< costmap_2d::Costmap2D::mutex_t > lock(*(costmap_->getCostmap()->getMutex()));
104 
105  if(req.plan.empty())
106  {
107  ROS_ERROR_NAMED("GPI", "No plan specified so no check can be performed.");
108  return false;
109  }
110  resp.valid = global_planner_->checkPlan(req.plan);
111  return true;
112 }
113 
114 bool GlobalPlannerInterface::getPlan(cb_base_navigation_msgs::GetPlanRequest &req, cb_base_navigation_msgs::GetPlanResponse &resp)
115 {
116  if (req.start.header.frame_id.empty())
117  {
118  ROS_ERROR_NAMED("GPI", "Start pose can't be empty");
119  return false;
120  }
121 
122  // Lock the costmap for a sec
123  boost::unique_lock< costmap_2d::Costmap2D::mutex_t > lock(*(costmap_->getCostmap()->getMutex()));
124 
125  // Check the input
126  if(req.goal_position_constraints.size() > 1) { ROS_ERROR_NAMED("GPI", "You have specified more than 1 constraint, this is not yet supported."); return false; }
127  if(req.goal_position_constraints.size() == 0) { ROS_ERROR_NAMED("GPI", "No goal position constraint specified, planner cannot create plan."); return false; }
128 
129  // Container for goal positions in map frame
130  std::vector<tf2::Vector3> goal_positions;
131 
132  // Plan the global path
133  if(global_planner_->makePlan(req.start, req.goal_position_constraints[0], resp.plan, goal_positions)) {
134  // Visualize me something
135  vis_.publishGlobalPlanMarker(resp.plan);
137  vis_.publishGoalPositionsMarker(goal_positions);
138  resp.succes = true;
139  } else {
140  resp.succes = false;
141  }
142  return true;
143 }
144 
145 }
cb_global_planner::GlobalPlannerInterface::check_plan_srv_
ros::ServiceServer check_plan_srv_
Definition: global_planner_interface.h:50
std::string
cb_global_planner::GlobalPlannerInterface::checkPlan
bool checkPlan(cb_base_navigation_msgs::CheckPlanRequest &req, cb_base_navigation_msgs::CheckPlanResponse &resp)
Definition: global_planner_interface.cpp:100
cb_global_planner::GlobalPlannerInterface::gp_loader_
pluginlib::ClassLoader< GlobalPlannerPlugin > gp_loader_
Definition: global_planner_interface.h:61
cb_global_planner::Visualization::publishGlobalPlanMarkerArray
void publishGlobalPlanMarkerArray(const std::vector< geometry_msgs::PoseStamped > &plan, const std::string &frame="map")
Publishes a specified plan with poses.
Definition: global_planner/visualization.cpp:45
std::vector
std::vector::size
T size(T... args)
costmap_2d.h
cb_global_planner::Visualization::publishGlobalPlanMarker
void publishGlobalPlanMarker(const std::vector< geometry_msgs::PoseStamped > &plan, const std::string &frame="map")
Publishes a specified plan.
Definition: global_planner/visualization.cpp:20
cb_global_planner::GlobalPlannerInterface::GlobalPlannerInterface
GlobalPlannerInterface(costmap_2d::Costmap2DROS *costmap, tf2_ros::Buffer *tf)
Definition: global_planner_interface.cpp:14
std::stringstream
costmap_2d_ros.h
cb_global_planner::GlobalPlannerInterface::poseCallback
void poseCallback(const geometry_msgs::PoseStampedConstPtr &pose)
Definition: global_planner_interface.cpp:57
cb_global_planner::GlobalPlannerInterface::costmap_
costmap_2d::Costmap2DROS * costmap_
Costmaps.
Definition: global_planner_interface.h:68
cb_global_planner::GlobalPlannerInterface::tf_
tf2_ros::Buffer * tf_
Definition: global_planner_interface.h:65
cb_global_planner::GlobalPlannerInterface::global_frame_
std::string global_frame_
Definition: global_planner_interface.h:64
std::string::c_str
T c_str(T... args)
cb_global_planner::GlobalPlannerInterface::get_plan_srv_
ros::ServiceServer get_plan_srv_
Connections to the outside world.
Definition: global_planner_interface.h:50
costmap_2d::Costmap2D::getMutex
mutex_t * getMutex()
cb_global_planner::GlobalPlannerInterface::robot_base_frame_
std::string robot_base_frame_
Frame names + Tranforms.
Definition: global_planner_interface.h:64
cb_global_planner::GlobalPlannerInterface::plan_pub_
ros::Publisher plan_pub_
Definition: global_planner_interface.h:57
costmap_2d::Costmap2DROS::getRobotPose
bool getRobotPose(geometry_msgs::PoseStamped &global_pose) const
cb_global_planner::GlobalPlannerInterface::~GlobalPlannerInterface
~GlobalPlannerInterface()
Definition: global_planner_interface.cpp:8
cb_global_planner::GlobalPlannerInterface::global_planner_
boost::shared_ptr< GlobalPlannerPlugin > global_planner_
Planners + loaders.
Definition: global_planner_interface.h:60
std::stringstream::str
T str(T... args)
cb_global_planner::GlobalPlannerInterface::vis_
Visualization vis_
Visualization.
Definition: global_planner_interface.h:71
cb_global_planner
Definition: a_star_planner.cpp:5
costmap_2d::Costmap2DROS::getCostmap
Costmap2D * getCostmap() const
cb_global_planner::GlobalPlannerInterface::pose_sub_
ros::Subscriber pose_sub_
Pose callback and publisher.
Definition: global_planner_interface.h:55
tf_
tf2_ros::Buffer * tf_
cb_global_planner::Visualization::publishGoalPositionsMarker
void publishGoalPositionsMarker(const std::vector< tf2::Vector3 > &positions, const std::string &frame="map")
Publishes the goal area.
Definition: global_planner/visualization.cpp:94
global_planner_interface.h
cb_global_planner::GlobalPlannerInterface::getPlan
bool getPlan(cb_base_navigation_msgs::GetPlanRequest &req, cb_base_navigation_msgs::GetPlanResponse &resp)
Definition: global_planner_interface.cpp:114
costmap_2d::Costmap2DROS