ScavTaskFetchObject.cpp
Go to the documentation of this file.
00001 
00002 #include <actionlib/client/simple_action_client.h>
00003 #include <vector>
00004 #include <fstream>
00005 
00006 #include "ScavTaskFetchObject.h"
00007 #include "bwi_kr_execution/ExecutePlanAction.h"
00008 #include "bwi_msgs/QuestionDialog.h"
00009 
00010 std::string path_to_text; 
00011 
00012 ScavTaskFetchObject::ScavTaskFetchObject(ros::NodeHandle *nh, std::string dir) {
00013     this->nh = nh; 
00014     directory = dir; 
00015     task_description = "fetch an object from a place to another"; 
00016     task_name = "Fetch object"; 
00017     certificate = ""; 
00018     _target_detected = false; 
00019     task_completed = false; 
00020 }
00021 
00022 
00023 void ScavTaskFetchObject::executeTask(int timeout, TaskResult &result, std::string &record) {
00024 
00025     boost::thread motion( &ScavTaskFetchObject::motionThread, this);
00026     boost::thread hri( &ScavTaskFetchObject::hriThread, this);
00027 
00028     _target_detected = false; 
00029 
00030     ros::Rate r(2); 
00031     while (ros::ok() and r.sleep()) {
00032         if (task_completed) {
00033             search_planner_simple->cancelCurrentGoal(); 
00034             break; 
00035         }
00036     }
00037 
00038     motion.detach();
00039     hri.detach();
00040 
00041     search_planner_simple->cancelCurrentGoal(); 
00042 
00043     record = path_to_text;
00044     result = SUCCEEDED; 
00045 
00046 }
00047     
00048 // motion thread
00049 void ScavTaskFetchObject::motionThread() {
00050 
00051     std::string path_to_yaml; 
00052 
00053     if (false == nh->hasParam("path_to_search_points"))
00054         ROS_ERROR("path to yaml file of search points not set"); 
00055     ros::param::get("path_to_search_points", path_to_yaml); 
00056 
00057     search_planner_simple = new SearchPlannerSimple(nh);
00058     ros::Rate r(2);           
00059 
00060     while (ros::ok() and !_target_detected and r.sleep()) {
00061         search_planner_simple->moveToNextDoor();
00062     }
00063 }
00064 
00065 // human-robot interaction thread
00066 void ScavTaskFetchObject::hriThread() {
00067     gui_service_client = new ros::ServiceClient(nh->serviceClient <bwi_msgs::QuestionDialog> ("question_dialog")); 
00068 
00069     ros::Duration(0.5).sleep(); 
00070     bwi_msgs::QuestionDialog srv;
00071     
00072     // specify the task 
00073     srv.request.type = bwi_msgs::QuestionDialogRequest::CHOICE_QUESTION; 
00074     srv.request.message = "Please click the button, if you can help with specifying the 'fetch object' task"; 
00075     srv.request.options = std::vector<std::string>(1, "button");
00076     srv.request.timeout = bwi_msgs::QuestionDialogRequest::NO_TIMEOUT; 
00077     gui_service_client->waitForExistence(); 
00078     gui_service_client->call(srv); 
00079 
00080     _target_detected = true; 
00081 
00082     // what's the object's name? saved to room_from
00083     srv.request.type = 2;
00084     srv.request.message = "Please follow me until I stop.\nWhat is the object's name?"; 
00085     gui_service_client->call(srv);
00086     object_name = srv.response.text;
00087 
00088     // where to get the object, saved to room_from
00089     srv.request.type = 2;
00090     srv.request.message = "Where to get the object? E.g., l3_420."; 
00091     gui_service_client->call(srv);
00092     room_name_from = srv.response.text;
00093 
00094     // where to deliver the object, saved to room_to
00095     srv.request.type = 2;
00096     srv.request.message = "Where to deliver the object? E.g., l3_420."; 
00097     gui_service_client->call(srv);
00098     room_name_to = srv.response.text;
00099 
00100     actionlib::SimpleActionClient<bwi_kr_execution::ExecutePlanAction> asp_plan_client("/action_executor/execute_plan", true); 
00101     asp_plan_client.waitForServer();
00102 
00103     // print to gui: moving to the first room
00104     srv.request.type = 0;
00105     srv.request.message = "Moving to room: " + room_name_from; 
00106     gui_service_client->call(srv);
00107 
00108     // wrapping up a "goal" for moving the robot platform
00109     bwi_kr_execution::ExecutePlanGoal goal;
00110     bwi_kr_execution::AspRule rule;
00111     bwi_kr_execution::AspFluent fluent;
00112 
00113     // move to the room to ask for the object
00114     fluent.name = "not at";
00115     fluent.variables.push_back(room_name_from);
00116     rule.body.push_back(fluent);
00117     goal.aspGoal.push_back(rule);
00118     ROS_INFO("sending goal");
00119     asp_plan_client.sendGoalAndWait(goal);
00120 
00121     ROS_INFO(asp_plan_client.getState() == actionlib::SimpleClientGoalState::SUCCEEDED ? "Succeeded!" : "Failed");
00122 
00123     // request to load object
00124     srv.request.type = 1; 
00125     srv.request.message = "Please help me load object: " + object_name; 
00126     srv.request.options = std::vector <std::string> (1, "Loaded"); 
00127     gui_service_client->call(srv); 
00128 
00129     // print to gui: moving to the second room
00130     srv.request.type = 0;
00131     srv.request.message = "Moving to room: " + room_name_to; 
00132     gui_service_client->call(srv);
00133 
00134     // move to the second room for delivery
00135     fluent.variables.clear(); 
00136     rule.body.clear();
00137     goal.aspGoal.clear(); 
00138 
00139     fluent.name = "not at";
00140     fluent.variables.push_back(room_name_to);
00141     rule.body.push_back(fluent);
00142     goal.aspGoal.push_back(rule);
00143     ROS_INFO("sending goal");
00144     asp_plan_client.sendGoalAndWait(goal);
00145 
00146     ROS_INFO(asp_plan_client.getState() == actionlib::SimpleClientGoalState::SUCCEEDED ? "Succeeded!" : "Failed!");
00147 
00148     // request to unload object
00149     srv.request.type = 1; 
00150     srv.request.message = "Please help me unload object: " + object_name; 
00151     std::vector<std::string> buttons(1, "Unloaded"); 
00152     srv.request.options = buttons;
00153     gui_service_client->call(srv); 
00154 
00155     boost::posix_time::ptime curr_time = boost::posix_time::second_clock::local_time(); 
00156     std::string path_to_text = directory;
00157     path_to_text += "fetch_object_" + boost::posix_time::to_simple_string(curr_time); 
00158 
00159     if (false == boost::filesystem::is_directory(directory)) {
00160         boost::filesystem::path tmp_path(directory); 
00161         boost::filesystem::create_directory(tmp_path); 
00162     } else {
00163         std::ofstream fs;
00164         fs.open(path_to_text.c_str(), std::ofstream::app); 
00165         fs << "origin room: " << room_name_from + "\n" 
00166             << "object name: " << object_name + "\n" 
00167             << "destination room: "+ room_name_to + "\n"; 
00168         fs.close(); 
00169     }
00170 
00171     ROS_INFO("fetch_object_service task done"); 
00172     task_completed = true; 
00173 }
00174 
00175 void ScavTaskFetchObject::stopEarly() {
00176     task_completed = true; 
00177 }


bwi_scavenger
Author(s): Shiqi Zhang
autogenerated on Thu Jun 6 2019 17:57:53