scavenger.cpp
Go to the documentation of this file.
00001 
00002 #include <ros/ros.h>
00003 #include <ros/package.h>
00004 #include "std_msgs/String.h"
00005 #include "bwi_kr_execution/ExecutePlanAction.h"
00006 #include <actionlib/client/simple_action_client.h>
00007 
00008 #include <stdlib.h>     /* srand, rand */
00009 #include <time.h>       /* time */
00010 #include <boost/thread.hpp>
00011 
00012 #include "ScavTask.h"
00013 #include "TaskManager.h"
00014 #include "SearchPlanner.h"
00015 
00016 #include "ScavTaskColorShirt.h"
00017 #include "ScavTaskWhiteBoard.h"
00018 #include "ScavTaskFetchObject.h"
00019 #include "ScavTaskHumanFollowing.h"
00020 #include "bwi_msgs/ScavHunt.h"
00021 
00022 #define TIMEOUT (600) // return failure if not finished in 10 minutes
00023 #define NUM_OF_TASKS (5)
00024 #define NUM_OF_TASK_TYPES (4)
00025 
00026 using namespace scav_task_human_following; 
00027 TaskManager* task_manager; 
00028 TaskWithStatus *curr_task; 
00029 ros::NodeHandle *nh;
00030 
00031 void publishThread() {
00032     ros::Rate r(5); 
00033     while (ros::ok() and r.sleep() and false == task_manager->allFinished()) {
00034         // ROS_INFO_STREAM("publishing scavenger hunt status"); 
00035         task_manager->publishStatus(); 
00036     }
00037 }
00038 
00039 bool callback_srv_scav(bwi_msgs::ScavHunt::Request &req, 
00040                        bwi_msgs::ScavHunt::Response &res) {
00041  
00042     ROS_INFO("callback_srv_scav called"); 
00043     ROS_INFO_STREAM("current task status: 0 ongoing, 1 finished, 2 todo): " 
00044         << curr_task->status);
00045 
00046     if ((int) req.type == bwi_msgs::ScavHuntRequest::SCAV_PAUSE) {
00047         curr_task->status = TODO; 
00048         ROS_INFO("calling curr_task->task->stopEarly()");
00049         curr_task->task->stopEarly(); 
00050         task_manager->paused = true; 
00051 
00052         ROS_INFO("stopping movements at kr and motion levels"); 
00053         actionlib::SimpleActionClient<bwi_kr_execution::ExecutePlanAction> 
00054             client("/action_executor/execute_plan", true);
00055 
00056         ros::spinOnce(); 
00057         client.cancelAllGoals (); 
00058         ros::spinOnce(); 
00059 
00060         ros::Publisher pub = nh->advertise<actionlib_msgs::GoalID>
00061             ("/move_base/cancel", 10);
00062 
00063         actionlib_msgs::GoalID msg; 
00064         msg.id = ""; 
00065         // msg.stamp = ros::Time::now();
00066         
00067         ros::spinOnce(); 
00068         pub.publish(msg);
00069         ros::spinOnce(); 
00070         ROS_INFO("the robot is supposed to stop moving now"); 
00071 
00072     } else if ((int) req.type == bwi_msgs::ScavHuntRequest::SCAV_RESUME) {
00073         task_manager->paused  = false; 
00074     }
00075     return true; 
00076 }
00077 
00078 int main(int argc, char **argv) {
00079 
00080     ros::init(argc, argv, "scavenger");
00081     nh = new ros::NodeHandle();
00082     task_manager = new TaskManager(nh); 
00083 
00084     std::string dir(ros::package::getPath("bwi_logging") + "/log_files/"); 
00085 
00086     ros::ServiceServer scav_srv = nh->advertiseService("/scav_control", callback_srv_scav);
00087     ROS_INFO("Ready to provide scav task pause/resume service"); 
00088     ros::AsyncSpinner spinner(2); 
00089     spinner.start(); 
00090     // ros::spin(); 
00091     
00092     int cnt = 0; 
00093     srand(time(NULL)); 
00094     while (cnt < NUM_OF_TASKS) {
00095 
00096         cnt++; 
00097         ros::Duration(0.1).sleep();       
00098         int r = rand() % NUM_OF_TASK_TYPES; 
00099 
00100         // to initialize a task and label its status as Todo
00101 
00102         if (r == 0) {
00103             Color color = static_cast<Color>(rand() % COLOR_LENGTH);
00104             task_manager->addTask(new TaskWithStatus(new ScavTaskColorShirt(nh, dir, color), TODO)); 
00105         } else if (r == 1) {
00106             task_manager->addTask(new TaskWithStatus(new ScavTaskWhiteBoard(nh, dir), TODO)); 
00107         } else if (r == 2) {
00108             task_manager->addTask(new TaskWithStatus(new ScavTaskFetchObject(nh, dir), TODO)); 
00109         } else if (r == 3) {
00110             task_manager->addTask(new TaskWithStatus(new ScavTaskHumanFollowing(nh, dir), TODO)); 
00111         } else {
00112             cnt--; 
00113         }
00114     }
00115 
00116     task_manager->updateStatusGui(); 
00117     boost::thread p_thread( &publishThread); 
00118 
00119     ros::Rate r(2); 
00120     while (ros::ok() and r.sleep() and task_manager->allFinished() == false) {
00121  
00122         ROS_INFO_STREAM("paused? " << task_manager->paused);
00123         if (task_manager->paused) {
00124             continue; 
00125         }
00126 
00127         ROS_INFO_STREAM("selecting next task"); 
00128         curr_task = task_manager->selectNextTask();
00129 
00130         ROS_INFO_STREAM("updating GUI"); 
00131         task_manager->updateStatusGui(); 
00132 
00133         ROS_INFO_STREAM("executing next task"); 
00134         task_manager->executeNextTask(TIMEOUT, curr_task); 
00135     }
00136 
00137     p_thread.detach(); 
00138 
00139     return 0; 
00140 }
00141 
00142 


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