utils.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 
7 
8 namespace yocs_navigator {
9 bool SemanticNavigator::getGoalLocation(const std::string location,yocs_msgs::Waypoint& waypoint)
10 {
11  unsigned int i;
12  for(i = 0; i < waypointlist_.waypoints.size(); i ++)
13  {
14  yocs_msgs::Waypoint w = waypointlist_.waypoints[i];
15  if(!location.compare(w.name)) // if matches
16  {
17  waypoint = w;
18  return true;
19  }
20  }
21  return false;
22 }
23 
24 void SemanticNavigator::terminateNavigation(bool success, const std::string message)
25 {
26  yocs_msgs::NavigateToResult result;
27 
28  result.success = success;
29  result.message = message;
30  result.distance = distance_to_goal_;
31 
33  as_navi_.setSucceeded(result);
34 
35  return;
36 }
37 
38 void SemanticNavigator::feedbackNavigation(const int status, const double distance, const double remain_time, const std::string message)
39 {
40  yocs_msgs::NavigateToFeedback feedback;
41  feedback.status = status;
42  feedback.distance = distance;
43  feedback.remain_time = remain_time;
44  feedback.message = message;
45  //loginfo(message);
46 
47  as_navi_.publishFeedback(feedback);
48 }
49 
51 {
52  double timeout = 2.0;
54  if (ac_move_base_.waitForResult(ros::Duration(timeout)) == false)
55  {
56  logwarn("Failed to cancel move_base goal...");
57  return false;
58  }
59 
60  loginfo("move_base goal has cancelled");
61  return true;
62 }
63 
65 {
67 
68  ros::NodeHandle nh;
70  std_srvs::Empty srv;
71 
72  if (client.call(srv) == true)
73  {
74  ROS_INFO("Successfully cleared costmaps (%f seconds)", (ros::Time::now() - t0).toSec());
75  return true;
76  }
77  else
78  {
79  ROS_ERROR("Failed to clear costmaps (%f seconds)", (ros::Time::now() - t0).toSec());
80  return false;
81  }
82 }
83 
84 void SemanticNavigator::determineNavigationState(int& navi_result, const int move_base_result, const actionlib::SimpleClientGoalState move_base_state)
85 {
86  int result;
87 
88  if(move_base_result == NAVI_TIMEOUT)
89  {
90  result = NAVI_TIMEOUT;
91  }
92  else {
94 
96  {
97  loginfo("Arrived the destination");
98  result = NAVI_SUCCESS;
99  }
101  {
102  loginfo("movebase Aborted");
103  result = NAVI_RETRY;
104  }
106  {
107  loginfo("movebase rejected");
108  result = NAVI_FAILED;
109  }
111  {
112  loginfo("movebase preempted");
113  result = NAVI_FAILED;
114  }
116  {
117  loginfo("robot Lost");
118  result = NAVI_FAILED;
119  }
120  else {
121  std::stringstream message;
122  message << "Move base unknown result : " << move_base_result;
123  loginfo(message.str());
124  result = NAVI_UNKNOWN;
125  }
126  }
127 
128  ROS_INFO("Navi : %d", result);
129  navi_result = result;
130 }
131 
132 void SemanticNavigator::nextState(bool& retry,bool& final_result,std::string& message, const int navi_result,const ros::Time started_time)
133 {
134  if(navi_result == NAVI_TIMEOUT)
135  {
137 
138  retry = false;
139  final_result = false;
140  message = "Navigation Timed out";
141  }
142  else if(navi_result == NAVI_SUCCESS)
143  {
144  retry = false;
145  final_result = true;
146  message = "SUCCESS!";
147  }
148  else if(navi_result == NAVI_FAILED)
149  {
150  retry = false;
151  final_result = false;
152  message = "Navigation Failed";
153  }
154  else if(navi_result == NAVI_UNKNOWN)
155  {
156  retry = false;
157  final_result = false;
158  message = "Navigation has got unknown error....";
159  }
160  else if(navi_result == NAVI_RETRY)
161  {
162  retry = true;
163  final_result = false;
164  message = "Retry...";
165  }
166  else {
167  retry = false;
168  final_result = false;
169  message = "Something got wrong... What is going on";
170  }
171 
172  return;
173 }
174 
175 void SemanticNavigator::waitForMoveBase(int& move_base_result, const ros::Time& start_time, const double timeout)
176 {
177  int result = NAVI_UNKNOWN;
178  while(ros::ok() && ac_move_base_.waitForResult(ros::Duration(0.5)) == false)
179  {
180  ros::Time current_time = ros::Time::now();
181  // timed out. Navigation Failed..
182  double diff = (current_time - start_time).toSec();
183  double remain_time = timeout - diff;
184  //ROS_INFO("Diff = %.4f, timeout = %.4f",diff, timeout);
185  if(diff > timeout)
186  {
187  result = NAVI_TIMEOUT;
188  break;
189  }
190 
192  {
194  }
195 
196  feedbackNavigation(yocs_msgs::NavigateToFeedback::STATUS_INPROGRESS, distance_to_goal_, remain_time, "In Progress");
197  }
198 
199  ROS_INFO("Movebase : %d", result);
200 }
201 
202 void SemanticNavigator::loginfo(const std::string& msg)
203 {
204  ROS_INFO_STREAM(ros::this_node::getName() << " : " << msg);
205 }
206 
207 void SemanticNavigator::logwarn(const std::string& msg)
208 {
209  ROS_WARN_STREAM(ros::this_node::getName() << " : " << msg);
210 }
211 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
void nextState(bool &retry, bool &final_result, std::string &message, const int navi_result, const ros::Time started_time)
Definition: utils.cpp:132
void publishFeedback(const FeedbackConstPtr &feedback)
yocs_msgs::WaypointList waypointlist_
bool waitForResult(const ros::Duration &timeout=ros::Duration(0, 0))
bool getGoalLocation(const std::string location, yocs_msgs::Waypoint &waypoint)
Definition: utils.cpp:9
bool call(MReq &req, MRes &res)
ROSCPP_DECL const std::string & getName()
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
#define ROS_INFO(...)
actionlib::SimpleActionClient< move_base_msgs::MoveBaseAction > ac_move_base_
ROSCPP_DECL bool ok()
actionlib::SimpleActionServer< yocs_msgs::NavigateToAction > as_navi_
#define ROS_WARN_STREAM(args)
void feedbackNavigation(const int status, const double distance, const double remain_time, const std::string message)
Definition: utils.cpp:38
TFSIMD_FORCE_INLINE const tfScalar & w() const
void loginfo(const std::string &msg)
Definition: utils.cpp:202
#define ROS_INFO_STREAM(args)
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
SimpleClientGoalState getState() const
void logwarn(const std::string &msg)
Definition: utils.cpp:207
#define ROS_ERROR(...)
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