utils.cpp
Go to the documentation of this file.
00001 /*
00002  *  navigation_handler.hpp
00003  *  LICENSE : BSD - https://raw.github.com/yujinrobot/yujin_ocs/license/LICENSE
00004  */
00005 
00006 #include "yocs_navigator/semantic_navigator.hpp"
00007 
00008 namespace yocs_navigator {
00009 bool SemanticNavigator::getGoalLocation(const std::string location,yocs_msgs::Waypoint& waypoint)
00010 {
00011   unsigned int i;
00012   for(i = 0; i < waypointlist_.waypoints.size(); i ++)
00013   {
00014     yocs_msgs::Waypoint w = waypointlist_.waypoints[i];
00015     if(!location.compare(w.name)) // if matches
00016     {
00017       waypoint = w;
00018       return true;
00019     }
00020   }
00021   return false;
00022 }
00023 
00024 void SemanticNavigator::terminateNavigation(bool success, const std::string message) 
00025 {
00026   yocs_msgs::NavigateToResult result;
00027 
00028   result.success = success;
00029   result.message = message;
00030   result.distance = distance_to_goal_;
00031 
00032   navigation_in_progress_ = false;
00033   as_navi_.setSucceeded(result);
00034 
00035   return;
00036 }
00037 
00038 void SemanticNavigator::feedbackNavigation(const int status, const double distance, const double remain_time, const std::string message)
00039 {
00040   yocs_msgs::NavigateToFeedback feedback;
00041   feedback.status = status;
00042   feedback.distance = distance;
00043   feedback.remain_time = remain_time;
00044   feedback.message = message;
00045   //loginfo(message);
00046   
00047   as_navi_.publishFeedback(feedback);
00048 }
00049 
00050 bool SemanticNavigator::cancelMoveBaseGoal()
00051 {
00052   double timeout = 2.0;
00053   ac_move_base_.cancelAllGoals();
00054   if (ac_move_base_.waitForResult(ros::Duration(timeout)) == false)
00055   {
00056     logwarn("Failed to cancel move_base goal...");
00057     return false;
00058   }
00059 
00060   loginfo("move_base goal has cancelled");
00061   return true;
00062 }
00063 
00064 bool SemanticNavigator::clearCostmaps()
00065 {
00066   ros::Time t0 = ros::Time::now();
00067 
00068   ros::NodeHandle nh;
00069   ros::ServiceClient client = nh.serviceClient<std_srvs::Empty>(SemanticNavigatorDefaultParam::CLEAR_COSTMAP);
00070   std_srvs::Empty srv;
00071 
00072   if (client.call(srv) == true)
00073   {
00074     ROS_INFO("Successfully cleared costmaps (%f seconds)", (ros::Time::now() - t0).toSec());
00075     return true;
00076   }
00077   else
00078   {
00079     ROS_ERROR("Failed to clear costmaps (%f seconds)", (ros::Time::now() - t0).toSec());
00080     return false;
00081   }
00082 }
00083 
00084 void SemanticNavigator::determineNavigationState(int& navi_result, const int move_base_result, const actionlib::SimpleClientGoalState  move_base_state)
00085 {
00086   int result;
00087     
00088   if(move_base_result == NAVI_TIMEOUT)
00089   {
00090     result = NAVI_TIMEOUT;
00091   }
00092   else {
00093     actionlib::SimpleClientGoalState state = ac_move_base_.getState();
00094 
00095     if(state == actionlib::SimpleClientGoalState::SUCCEEDED)
00096     {
00097       loginfo("Arrived the destination");
00098       result = NAVI_SUCCESS;
00099     }
00100     else if(state == actionlib::SimpleClientGoalState::ABORTED)
00101     {
00102       loginfo("movebase Aborted");
00103       result = NAVI_RETRY;
00104     }
00105     else if(state == actionlib::SimpleClientGoalState::REJECTED)
00106     {
00107       loginfo("movebase rejected");
00108       result = NAVI_FAILED;
00109     }
00110     else if(state == actionlib::SimpleClientGoalState::PREEMPTED) 
00111     {
00112       loginfo("movebase preempted");
00113       result = NAVI_FAILED;
00114     }
00115     else if(state == actionlib::SimpleClientGoalState::LOST)
00116     {
00117       loginfo("robot Lost");
00118       result = NAVI_FAILED;
00119     }
00120     else {
00121       std::stringstream message; 
00122       message << "Move base unknown result : " << move_base_result;
00123       loginfo(message.str());
00124       result = NAVI_UNKNOWN;
00125     }
00126   }
00127 
00128   ROS_INFO("Navi : %d", result);
00129   navi_result = result;
00130 }
00131 
00132 void SemanticNavigator::nextState(bool& retry,bool& final_result,std::string& message, const int navi_result,const ros::Time started_time)
00133 {
00134   if(navi_result == NAVI_TIMEOUT)
00135   {
00136     cancelMoveBaseGoal();
00137 
00138     retry = false;
00139     final_result = false;
00140     message = "Navigation Timed out";
00141   }
00142   else if(navi_result == NAVI_SUCCESS)
00143   { 
00144     retry = false;
00145     final_result = true;
00146     message = "SUCCESS!";
00147   }
00148   else if(navi_result == NAVI_FAILED)
00149   {
00150     retry = false;
00151     final_result = false;
00152     message = "Navigation Failed";
00153   }
00154   else if(navi_result == NAVI_UNKNOWN)
00155   {
00156     retry = false;
00157     final_result = false;
00158     message = "Navigation has got unknown error....";     
00159   }
00160   else if(navi_result == NAVI_RETRY)
00161   {
00162     retry = true;
00163     final_result = false;
00164     message = "Retry...";
00165   }
00166   else {
00167     retry = false;
00168     final_result = false;
00169     message = "Something got wrong... What is going on";
00170   }
00171 
00172   return;
00173 }
00174 
00175 void SemanticNavigator::waitForMoveBase(int& move_base_result, const ros::Time& start_time, const double timeout)
00176 {
00177   int result = NAVI_UNKNOWN;
00178   while(ros::ok() && ac_move_base_.waitForResult(ros::Duration(0.5)) == false)
00179   {
00180     ros::Time current_time = ros::Time::now();
00181     // timed out. Navigation Failed..
00182     double diff = (current_time - start_time).toSec();
00183     double remain_time = timeout - diff; 
00184     //ROS_INFO("Diff = %.4f,  timeout = %.4f",diff, timeout);
00185     if(diff > timeout)
00186     {
00187       result = NAVI_TIMEOUT;
00188       break;
00189     }
00190 
00191     if(as_navi_.isPreemptRequested())
00192     {
00193       cancelMoveBaseGoal();
00194     }
00195 
00196     feedbackNavigation(yocs_msgs::NavigateToFeedback::STATUS_INPROGRESS, distance_to_goal_, remain_time, "In Progress");
00197   }
00198   
00199   ROS_INFO("Movebase : %d", result);
00200 }
00201 
00202 void SemanticNavigator::loginfo(const std::string& msg)
00203 {
00204   ROS_INFO_STREAM(ros::this_node::getName() << " : " << msg);
00205 }
00206 
00207 void SemanticNavigator::logwarn(const std::string& msg)
00208 {
00209   ROS_WARN_STREAM(ros::this_node::getName() << " : " <<  msg);
00210 }
00211 }


yocs_navigator
Author(s): Jihoon Lee, Jorge Simon Santos
autogenerated on Thu Jun 6 2019 21:47:35