navigation_handler.cpp
Go to the documentation of this file.
1 /*
2  * navigation_handler.hpp
3  * LICENSE : BSD - https://raw.github.com/yujinrobot/yujin_ocs/license/LICENSE
4  */
5 
6 
8 
9 namespace yocs_navigator {
10 void SemanticNavigator::processNavigation(yocs_msgs::NavigateToGoal::ConstPtr goal)
11 {
12  std::string location = goal->location;
13  int approach_type = goal->approach_type;
14  unsigned int num_retry = goal->num_retry;
15  double distance = goal->distance;
16  double timeout = goal->timeout;
17 
18  yocs_msgs::Waypoint waypoint;
19  bool result;
20 
21  result = getGoalLocation(location, waypoint);
22  if(!result) // if it fails to find the requested waypoint
23  {
24  std::stringstream ss;
25  ss << "failed to find the requested destination : " << location;
26  terminateNavigation(false, ss.str());
27  return;
28  }
29 
30  if(mtk::sameFrame(waypoint.header.frame_id, global_frame_) == false)
31  {
32  terminateNavigation(false, "Target is not in global frame");
33  return;
34  }
35  clearCostmaps();
36 
37  switch(approach_type) {
38  case yocs_msgs::NavigateToGoal::APPROACH_NEAR:
39  loginfo("Approach Type : NEAR");
40  goNear(waypoint, distance, num_retry, timeout);
41  break;
42  case yocs_msgs::NavigateToGoal::APPROACH_ON:
43  loginfo("Approach Type : ON");
44  goOn(waypoint, distance, num_retry, timeout);
45  break;
46  default:
47  terminateNavigation(false, "Invalid Approach Type");
48  break;
49  }
50 }
51 
52 void SemanticNavigator::goOn(const yocs_msgs::Waypoint waypoint, const double in_distance, const int num_retry, const double timeout)
53 {
54  // Note that in_distance variable is not used yet.. for On
55  geometry_msgs::PoseStamped target;
56  target.pose = waypoint.pose;
57  target.header = waypoint.header;
58 
59  int attempt = 0;
60  int move_base_result;
61  int navi_result;
62  bool retry;
63  bool final_result;
64  std::string message;
65  move_base_msgs::MoveBaseGoal mb_goal;
66 
67  ros::Time started_time = ros::Time::now();
68 
69  while(ros::ok())
70  {
71  move_base_result = NAVI_UNKNOWN;
72  mb_goal.target_pose = target;
75  boost::bind(&SemanticNavigator::processMoveBaseFeedback, this, _1, target));
76 
77  waitForMoveBase(move_base_result, started_time, timeout);
78  determineNavigationState(navi_result, move_base_result, ac_move_base_.getState());
79  nextState(retry, final_result, message, navi_result, started_time);
80 
81  if(retry == false) break;
82  else if(attempt > num_retry)
83  {
84  final_result = false;
85  message = "Tried enough... I failed to navigate..";
86  break;
87  }
88 
89  attempt++;
90  std::stringstream ss;
91  ss << "Reattempt to naviate.. " << attempt;
92 
93  ros::Time current_time = ros::Time::now();
94  double diff = (current_time - started_time).toSec();
95  double remain_time = timeout - diff;
96 
97  feedbackNavigation(yocs_msgs::NavigateToFeedback::STATUS_RETRY, distance_to_goal_, remain_time, ss.str());
98  clearCostmaps();
99  }
100 
101  terminateNavigation(final_result, message);
102 }
103 
104 void SemanticNavigator::goNear(const yocs_msgs::Waypoint waypoint, const double distance, const int num_retry, const double timeout)
105 {
106  geometry_msgs::PoseStamped target;
107  target.pose = waypoint.pose;
108  target.header = waypoint.header;
109 
110  int attempt = 0;
111  int move_base_result;
112  int navi_result;
113  bool retry;
114  bool final_result;
115  std::string message;
116  move_base_msgs::MoveBaseGoal mb_goal;
117 
118  ros::Time started_time = ros::Time::now();
119 
120  while(attempt < num_retry && ros::ok())
121  {
122  move_base_result = NAVI_UNKNOWN;
123  mb_goal.target_pose = target;
126  boost::bind(&SemanticNavigator::processMoveBaseFeedback, this, _1, target));
127  waitForMoveBase(move_base_result, started_time, timeout);
128  determineNavigationState(navi_result, move_base_result, ac_move_base_.getState());
129  nextState(retry, final_result, message, navi_result, started_time);
130 
131  if(retry == false) break;
132 
133  attempt++;
134 
135  if(attempt > num_retry)
136  {
137  final_result = false;
138  message = "Tried enough... I failed to navigate..";
139  break;
140  }
141  }
142 
143  terminateNavigation(final_result, message);
144 }
145 
146 
147 void SemanticNavigator::processMoveBaseFeedback(const move_base_msgs::MoveBaseFeedback::ConstPtr& feedback, const geometry_msgs::PoseStamped& target_pose)
148 {
149  geometry_msgs::PoseStamped robot_pose = feedback->base_position;
150 
151  // compute distance
152  double distance = mtk::distance2D(robot_pose.pose.position.x, robot_pose.pose.position.y, target_pose.pose.position.x, target_pose.pose.position.y);
153  distance_to_goal_ = distance;
154 }
155 }
void processNavigation(yocs_msgs::NavigateToGoal::ConstPtr goal)
void nextState(bool &retry, bool &final_result, std::string &message, const int navi_result, const ros::Time started_time)
Definition: utils.cpp:132
void processMoveBaseFeedback(const move_base_msgs::MoveBaseFeedback::ConstPtr &feedback, const geometry_msgs::PoseStamped &target)
bool getGoalLocation(const std::string location, yocs_msgs::Waypoint &waypoint)
Definition: utils.cpp:9
TFSIMD_FORCE_INLINE tfScalar distance(const Vector3 &v) const
bool sameFrame(const std::string &frame_a, const std::string &frame_b)
double distance2D(double ax, double ay, double bx, double by)
boost::function< void()> SimpleActiveCallback
actionlib::SimpleActionClient< move_base_msgs::MoveBaseAction > ac_move_base_
ROSCPP_DECL bool ok()
void goOn(const yocs_msgs::Waypoint waypoint, const double in_distance, const int num_retry, const double timeout)
boost::function< void(const SimpleClientGoalState &state, const ResultConstPtr &result)> SimpleDoneCallback
void feedbackNavigation(const int status, const double distance, const double remain_time, const std::string message)
Definition: utils.cpp:38
void sendGoal(const Goal &goal, SimpleDoneCallback done_cb=SimpleDoneCallback(), SimpleActiveCallback active_cb=SimpleActiveCallback(), SimpleFeedbackCallback feedback_cb=SimpleFeedbackCallback())
void loginfo(const std::string &msg)
Definition: utils.cpp:202
void waitForMoveBase(int &move_base_result, const ros::Time &start_time, const double timeout)
Definition: utils.cpp:175
static Time now()
void determineNavigationState(int &navi_result, const int move_base_result, const actionlib::SimpleClientGoalState move_base_state)
Definition: utils.cpp:84
void goNear(const yocs_msgs::Waypoint waypoint, const double in_distance, const int num_retry, const double timeout)
SimpleClientGoalState getState() const
void terminateNavigation(bool success, const std::string message)
Definition: utils.cpp:24


yocs_navigator
Author(s): Jihoon Lee, Jorge Simon Santos
autogenerated on Mon Jun 10 2019 15:53:58