00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "taskDispatcher.h"
00027
00028 #include <Inventor/sensors/SoTimerSensor.h>
00029
00030 #ifdef ROS_DATABASE_MANAGER
00031 #include "DBPlanner/ros_database_manager.h"
00032 #endif
00033 #include "DBPlanner/sql_database_manager.h"
00034
00035 #include "graspitGUI.h"
00036 #include "graspit_db_model.h"
00037
00038 #include "graspPlanningTask.h"
00039 #include "preGraspCheckTask.h"
00040 #include "graspTransferCheckTask.h"
00041 #include "graspClusteringTask.h"
00042 #include "tableCheckTask.h"
00043 #include "compliantGraspCopyTask.h"
00044
00045 #include "debug.h"
00046
00047 TaskDispatcher::TaskDispatcher() : mDBMgr(NULL) , mCurrentTask(NULL), mStatus(READY)
00048 {
00049
00050 mSensor = new SoTimerSensor(sensorCB, this);
00051 mSensor->setInterval( SbTime( 3.0 ));
00052
00053
00054 mMaxTasks = -1;
00055 mCompletedTasks = 0;
00056 }
00057
00058 TaskDispatcher::~TaskDispatcher()
00059 {
00060 if (mCurrentTask) {
00061
00062 DBGA("Dispatcher: deleting current task on cleanup");
00063 delete mCurrentTask;
00064 }
00065 delete mDBMgr;
00066 delete mSensor;
00067 }
00068
00069 int TaskDispatcher::connect(std::string host, int port, std::string username,
00070 std::string password, std::string database)
00071 {
00072 delete mDBMgr;
00073
00074
00075 std::ostringstream port_str;
00076 port_str << port;
00077 #ifdef ROS_DATABASE_MANAGER
00078 mDBMgr = new db_planner::RosDatabaseManager(host, port_str.str(), username, password, database,
00079 NULL,NULL);
00080
00081 GeomGraspitDBModelAllocator* allocator = new GeomGraspitDBModelAllocator(mDBMgr);
00082 mDBMgr->SetModelAllocator(allocator);
00083 if (!mDBMgr->isConnected()) {
00084 DBGA("DBase operator: Connection failed");
00085 delete mDBMgr; mDBMgr = NULL;
00086 return -1;
00087 }
00088 return 0;
00089 #else
00090 DBGA("Task dispatcher only tested using the ROS database manager, which is not available");
00091 return -1;
00092 #endif
00093 }
00094
00103 void TaskDispatcher::startNewTask()
00104 {
00105
00106 assert(!mCurrentTask);
00107
00108
00109 if (mMaxTasks >= 0 && mCompletedTasks >= mMaxTasks) {
00110 mStatus = DONE;
00111 return;
00112 }
00113
00114 db_planner::TaskRecord rec;
00115 if (!mDBMgr->AcquireNextTask(&rec)) {
00116 DBGA("Dispatcher: error reading next task");
00117 mStatus = ERROR;
00118 return;
00119 }
00120 DBGA("Task id: " << rec.taskId);
00121
00122 if (!rec.taskType.empty()) {
00123 DBGA("Dispatcher: no tasks to be executed");
00124 mStatus = NO_TASK;
00125 return;
00126 }
00127 mCurrentTask = mFactory.getTask(this, mDBMgr,rec);
00128 if (!mCurrentTask) {
00129 DBGA("Dispatcher: can not understand task type: " << rec.taskType);
00130 mStatus = ERROR;
00131 return;
00132 }
00133
00134
00135 mCurrentTask->start();
00136
00137 if (mCurrentTask->getStatus() == Task::RUNNING) {
00138
00139 mStatus = RUNNING;
00140 DBGA("Dispatcher: started task of type " << rec.taskType);
00141 } else if (mCurrentTask->getStatus() == Task::DONE) {
00142
00143 mStatus = READY;
00144 DBGA("Dispatcher: completed one-shot task of type " << rec.taskType);
00145 } else {
00146
00147 mStatus = READY;
00148 DBGA("Dispatcher: error starting task of type " << rec.taskType);
00149 return;
00150 }
00151 }
00152
00164 void TaskDispatcher::checkCurrentTask()
00165 {
00166 assert(mCurrentTask);
00167
00168 switch (mCurrentTask->getStatus()) {
00169 case Task::RUNNING:
00170 break;
00171 case Task::ERROR:
00172 mStatus = READY;
00173
00174 if (!mDBMgr->SetTaskStatus(mCurrentTask->getRecord().taskId, "ERROR")) {
00175 DBGA("Dispatcher: error marking completed task");
00176 mStatus = ERROR;
00177 }
00178 delete mCurrentTask; mCurrentTask = NULL;
00179 break;
00180 case Task::DONE:
00181 mStatus = READY;
00182 mCompletedTasks++;
00183
00184 if (!mDBMgr->SetTaskStatus(mCurrentTask->getRecord().taskId, "COMPLETED")) {
00185 DBGA("Dispatcher: error marking completed task");
00186 mStatus = ERROR;
00187 }
00188 delete mCurrentTask; mCurrentTask = NULL;
00189 break;
00190 }
00191 }
00192
00198 void TaskDispatcher::mainLoop()
00199 {
00200 if (mSensor->isScheduled()) {
00201 mSensor->unschedule();
00202 }
00203 while(1){
00204 if(mCurrentTask) {
00205 checkCurrentTask();
00206 }
00207 if (mStatus == READY) {
00208 startNewTask();
00209 }
00210 switch(mStatus) {
00211 case DONE:
00212 graspItGUI->exitMainLoop();
00213 return;
00214 case ERROR:
00215 graspItGUI->exitMainLoop();
00216 return;
00217 case NO_TASK:
00218 graspItGUI->exitMainLoop();
00219 return;
00220 case RUNNING:
00221 mSensor->schedule();
00222 return;
00223 case READY:
00224 break;
00225 }
00226 }
00227 }
00228
00229 void TaskDispatcher::sensorCB(void *data, SoSensor*)
00230 {
00231 TaskDispatcher *dispatch = static_cast<TaskDispatcher*>(data);
00232 dispatch->mainLoop();
00233 }
00234
00235 Task* TaskFactory::getTask(TaskDispatcher *op, db_planner::DatabaseManager *mgr,
00236 db_planner::TaskRecord rec)
00237 {
00238 if (rec.taskType == "EMPTY") return new EmptyTask(op, mgr, rec);
00239 else if (rec.taskType == "EMPTY_ONE_SHOT") return new EmptyOneShotTask(op, mgr, rec);
00240 else if (rec.taskType == "GRASP_PLANNING") return new GraspPlanningTask(op, mgr, rec);
00241 else if (rec.taskType == "PREGRASP_CHECK") return new PreGraspCheckTask(op, mgr, rec);
00242 else if (rec.taskType == "GRASP_CLUSTERING") return new GraspClusteringTask(op, mgr, rec);
00243 else if (rec.taskType == "GRASP_TRANSFER") return new GraspTransferCheckTask(op, mgr, rec);
00244 else if (rec.taskType == "TABLE_CHECK") return new TableCheckTask(op, mgr, rec);
00245 else if (rec.taskType == "COMPLIANT_COPY")return new CompliantGraspCopyTask(op, mgr, rec);
00246 else return NULL;
00247 }
00248
00249 void EmptyTask::start()
00250 {
00251 mSensor = new SoTimerSensor(sensorCB, this);
00252 mSensor->setInterval( SbTime( 4.0 ));
00253 mSensor->schedule();
00254 mStatus = RUNNING;
00255 DBGA("Empty task has started");
00256 }
00257
00258 void EmptyTask::finish()
00259 {
00260 DBGA("Empty task has finished");
00261 mStatus = DONE;
00262 }
00263
00264 EmptyTask::~EmptyTask()
00265 {
00266 DBGA("Empty task deleted");
00267 delete mSensor;
00268 }
00269
00270 void EmptyTask::sensorCB(void *data, SoSensor*)
00271 {
00272 EmptyTask *task = static_cast<EmptyTask*>(data);
00273 task->finish();
00274 }
00275
00276 EmptyOneShotTask::~EmptyOneShotTask()
00277 {
00278 DBGA("Empty one-shot task deleted");
00279 }
00280