00001 00002 #include <std_msgs/String.h> 00003 #include <std_msgs/Int32.h> 00004 #include <boost/lexical_cast.hpp> 00005 #include <cstddef> 00006 00007 #include "bwi_msgs/QuestionDialog.h" 00008 #include "TaskManager.h" 00009 00010 TaskManager::TaskManager (ros::NodeHandle *nh) { 00011 this->nh = nh; 00012 gui_client = nh->serviceClient <bwi_msgs::QuestionDialog> ("question_dialog"); 00013 00014 pub = this->nh->advertise<bwi_msgs::ScavStatus>("/scav_hunt_status", 1000); 00015 ros::Rate loop_rate(10); 00016 paused = false; 00017 } 00018 00019 void TaskManager::addTask(TaskWithStatus *task_with_status) { 00020 tasks.push_back(*task_with_status); 00021 std::string str = task_with_status->task->task_name; 00022 for (int i=0; i<task_with_status->task->task_parameters.size(); i++) { 00023 str += " " + task_with_status->task->task_parameters[i]; 00024 } 00025 ROS_INFO_STREAM("Task added: " << str); 00026 } 00027 00028 TaskWithStatus* TaskManager::selectNextTask() { 00029 00030 for (int i=0; i<tasks.size(); i++) { 00031 if (tasks[i].status == ONGOING) { 00032 return NULL; // robot works on one task at a time 00033 } else if (tasks[i].status == TODO) { 00034 ROS_INFO_STREAM("Start to work on: " << tasks[i].task->task_name); 00035 tasks[i].status = ONGOING; 00036 return & (tasks[i]); 00037 } 00038 } 00039 } 00040 00041 void TaskManager::executeNextTask(int timeout, TaskWithStatus *task_with_status) 00042 { 00043 00044 TaskResult result; 00045 std::string certificate; 00046 00047 task_with_status->task->executeTask(timeout, result, certificate); 00048 task_with_status->task->certificate = certificate; 00049 task_with_status->status = FINISHED; 00050 } 00051 00052 void TaskManager::updateStatusGui() { 00053 00054 std::string message(""); 00055 00056 for (int i=0; i<tasks.size(); i++) { 00057 if (tasks[i].status == ONGOING) { 00058 message += "-->\t\t" + tasks[i].task->task_description; 00059 } else if (tasks[i].status == TODO) { 00060 message += "\t\t" + tasks[i].task->task_description; 00061 } else if (tasks[i].status == FINISHED) { 00062 message += "done\t" + tasks[i].task->task_description; 00063 } 00064 00065 for (int j=0; j<tasks[i].task->task_parameters.size(); j++) { 00066 message += " " + tasks[i].task->task_parameters[j]; 00067 } 00068 message += "\n"; 00069 } 00070 00071 bwi_msgs::QuestionDialog srv; 00072 00073 srv.request.type = 0; 00074 srv.request.message = message; 00075 00076 gui_client.call(srv); 00077 } 00078 00079 00080 bool TaskManager::allFinished() { 00081 for (int i=0; i<tasks.size(); i++) { 00082 if (tasks[i].status != FINISHED) 00083 return false; 00084 } 00085 return true; 00086 } 00087 00088 void TaskManager::publishStatus() { 00089 00090 // ROS_INFO("publishing scavenger hunt status"); 00091 msg.names.clear(); 00092 msg.statuses.clear(); 00093 msg.certificates.clear(); 00094 00095 for (int i=0; i<tasks.size(); i++) { 00096 msg.names.push_back(tasks[i].task->task_name); 00097 // ROS_INFO_STREAM(" name addedd: " << tasks[i].task->task_name); 00098 00099 if (tasks[i].status == ONGOING) 00100 msg.statuses.push_back(bwi_msgs::ScavStatus::ONGOING); 00101 else if (tasks[i].status == FINISHED) 00102 msg.statuses.push_back(bwi_msgs::ScavStatus::FINISHED); 00103 else if (tasks[i].status == TODO) 00104 msg.statuses.push_back(bwi_msgs::ScavStatus::TODO); 00105 00106 // full path to certificiate 00107 std::string certificate = tasks[i].task->certificate; 00108 std::size_t found = certificate.find_last_of("/"); 00109 // only the file name 00110 certificate = certificate.substr(found+1); 00111 msg.certificates.push_back(certificate); 00112 // ROS_INFO_STREAM(" status added.. "); 00113 } 00114 00115 ros::spinOnce(); 00116 ros::spinOnce(); 00117 pub.publish(msg); 00118 ros::spinOnce(); 00119 ros::spinOnce(); 00120 } 00121