rail_lab_location_server.cpp
Go to the documentation of this file.
00001 
00010 #include <carl_navigation/rail_lab_location_server.hpp>
00011 #include <move_base_msgs/MoveBaseActionGoal.h>
00012 #include <ros/package.h>
00013 #include <yaml-cpp/yaml.h>
00014 #include <sstream>
00015 #include <fstream>
00016 
00017 using namespace std;
00018 
00019 rail_lab_location_server::rail_lab_location_server() :
00020     move_base_("move_base", true), as_(node_, "move_carl", boost::bind(&rail_lab_location_server::execute, this, _1),
00021                                        false)
00022 {
00023   // private node handle
00024   ros::NodeHandle private_nh("~");
00025 
00026   // grab the config file
00027   stringstream ss;
00028   ss << ros::package::getPath("carl_navigation") << "/config/rail_lab_locations.yaml";
00029   string file;
00030   private_nh.param("locations_config", file, ss.str());
00031 
00032   // parse the configuration file
00033 
00034 #ifdef YAMLCPP_GT_0_5_0
00035   YAML::Node config = YAML::LoadFile(file);
00036   for (size_t i = 0; i < config.size(); i++)
00037   {
00038     // load the ID and name
00039     int id = config[i]["id"].as<int>();
00040     string name = config[i]["name"].as<std::string>();
00041 
00042     // load position information
00043     geometry_msgs::Pose pose;
00044     const YAML::Node &position = config[i]["position"];
00045     pose.position.x = position[0].as<float>();
00046     pose.position.y = position[1].as<float>();
00047     pose.position.z = position[2].as<float>();
00048 
00049     // load orientation information
00050     const YAML::Node &orientation = config[i]["orientation"];
00051     pose.orientation.x = orientation[0].as<float>();
00052     pose.orientation.y = orientation[1].as<float>();
00053     pose.orientation.z = orientation[2].as<float>();
00054     pose.orientation.w = orientation[3].as<float>();
00055 
00056     // store the location
00057     locations_.push_back(location(id, name, pose));
00058   }
00059 #else
00060   ifstream fin(file.c_str());
00061   YAML::Parser parser(fin);
00062   YAML::Node config;
00063   parser.GetNextDocument(config);
00064   for (size_t i = 0; i < config.size(); i++)
00065   {
00066     int id;
00067     config[i]["position"] >> id;
00068     string name;
00069     config[i]["position"] >> name;
00070 
00071     geometry_msgs::Pose pose;
00072     const YAML::Node &position = config[i]["position"];
00073     position[0] >> pose.position.x;
00074     position[1] >> pose.position.y;
00075     position[2] >> pose.position.z;
00076     const YAML::Node &orientation = config[i]["orientation"];
00077     orientation[0] >> pose.orientation.x;
00078     orientation[1] >> pose.orientation.y;
00079     orientation[2] >> pose.orientation.z;
00080     orientation[3] >> pose.orientation.w;
00081 
00082     locations_.push_back(location(id, name, pose));
00083   }
00084 #endif
00085 
00086   ROS_INFO("Connection to CARL low-level navigation...");
00087   // wait for the action server to start
00088   move_base_.waitForServer();
00089 
00090   // start the action server
00091   as_.start();
00092 
00093   ROS_INFO("CARL High Level Navigation Initialized with %d Locations", (int ) locations_.size());
00094 }
00095 
00096 void rail_lab_location_server::execute(const carl_navigation::MoveCarlGoalConstPtr &goal)
00097 {
00098   // search for the location
00099   int location_id = goal->location;
00100   int index = -1;
00101   for (uint i = 0; i < locations_.size(); i++)
00102   {
00103     if (locations_.at(i).get_id() == location_id)
00104     {
00105       index = i;
00106       break;
00107     }
00108   }
00109 
00110   // attempt to run the navigation
00111   bool success = false;
00112   if (index >= 0)
00113   {
00114     ROS_INFO("Attempting to move CARL to '%s'", locations_[index].get_name().c_str());
00115 
00116     // setup the goal
00117     move_base_msgs::MoveBaseGoal goal;
00118     goal.target_pose.header.frame_id = "map";
00119     goal.target_pose.pose = locations_[index].get_pose();
00120 
00121     // send the goal
00122     move_base_.sendGoal(goal, boost::bind(&rail_lab_location_server::done, this, _1, _2),
00123                         boost::bind(&rail_lab_location_server::active, this),
00124                         boost::bind(&rail_lab_location_server::feedback, this, _1));
00125 
00126     while (!move_base_.getState().isDone())
00127     {
00128       // check if we should cancel
00129       if (as_.isPreemptRequested() || as_.isNewGoalAvailable() || !ros::ok())
00130       {
00131         ROS_INFO("Canceling move CARL goals...");
00132         move_base_.cancelAllGoals();
00133         as_.setPreempted();
00134       }
00135     }
00136   }
00137   else
00138   {
00139     ROS_INFO("Unknown location ID.");
00140     as_.setAborted(result_);
00141   }
00142 }
00143 
00144 void rail_lab_location_server::done(const actionlib::SimpleClientGoalState& state,
00145                                     const move_base_msgs::MoveBaseResultConstPtr &result)
00146 {
00147   if (state == actionlib::SimpleClientGoalState::SUCCEEDED)
00148   {
00149     ROS_INFO("Move CARL Succeeded");
00150     as_.setSucceeded(result_);
00151   }
00152   else if (state == actionlib::SimpleClientGoalState::PREEMPTED)
00153   {
00154     ROS_INFO("Low-level navigation canceled.");
00155     as_.setPreempted(result_);
00156   }
00157   else
00158   {
00159     ROS_INFO("Move CARL Failed");
00160     as_.setAborted(result_);
00161   }
00162 }
00163 
00164 void rail_lab_location_server::active()
00165 {
00166   ROS_INFO("Low-level navigation goal activated.");
00167 }
00168 
00169 void rail_lab_location_server::feedback(const move_base_msgs::MoveBaseFeedbackConstPtr &feedback)
00170 {
00171   // forward the feedback
00172   feedback_.base_position = feedback->base_position;
00173   as_.publishFeedback(feedback_);
00174 }
00175 
00176 int main(int argc, char **argv)
00177 {
00178   // initialize ROS and the node
00179   ros::init(argc, argv, "rail_lab_location_server");
00180 
00181   // initialize the server
00182   rail_lab_location_server server;
00183 
00184   // continue until a ctrl-c has occurred
00185   ros::spin();
00186 }


carl_navigation
Author(s): Russell Toris , David Kent
autogenerated on Sat Jun 8 2019 20:26:04