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
00030 #include <Q3GroupBox>
00031 #include <Inventor/Qt/SoQt.h>
00032 #include <Inventor/Qt/viewers/SoQtExaminerViewer.h>
00033 #include <Inventor/sensors/SoIdleSensor.h>
00034 #include <QLayout>
00035
00036 #ifdef Q_WS_X11
00037 #include <unistd.h>
00038 #endif
00039
00040 #include "graspitGUI.h"
00041 #include "mainWindow.h"
00042 #include "ivmgr.h"
00043 #include "world.h"
00044 #include "mytools.h"
00045 #include "SoComplexShape.h"
00046 #include "SoArrow.h"
00047 #include "SoTorquePointer.h"
00048 #include "plugin.h"
00049 #include "debug.h"
00050
00051 #ifdef CGDB_ENABLED
00052 #include "dbase_grasp.h"
00053 #include "taskDispatcher.h"
00054 #endif
00055
00056 bool GraspItGUI::initialized = false;
00057 int GraspItGUI::initResult = SUCCESS;
00058
00059
00060 extern int optind,optopt,opterr;
00061 extern char *optarg;
00062
00064
00065 #ifdef GRASPITDBG
00066 FILE *debugfile;
00067 #endif
00068
00070 GraspItGUI *graspItGUI = 0;
00071
00073
00084 GraspItGUI::GraspItGUI(int argc,char **argv) : mDispatch(NULL)
00085 {
00086 if (!initialized) {
00087 mainWindow = new MainWindow;
00088 SoQt::init(mainWindow->mWindow);
00089
00090
00091 SoComplexShape::initClass();
00092 SoArrow::initClass();
00093 SoTorquePointer::initClass();
00094
00095 ivmgr = new IVmgr((QWidget *)mainWindow->mUI->viewerHolder,"myivmgr");
00096
00097
00098
00099
00100 ivmgr->getViewer()->getWidget()->setFocusPolicy(Qt::StrongFocus);
00101
00102 initialized = true;
00103 graspItGUI = this;
00104 mExitCode = 0;
00105 initResult = processArgs(argc,argv);
00106 }
00107 }
00108
00112 GraspItGUI::~GraspItGUI()
00113 {
00114
00115
00116
00117
00118
00119
00120
00121 stopAllPlugins();
00122 for (size_t i=0; i<mPluginCreators.size(); i++) {
00123 delete mPluginCreators[i];
00124 }
00125
00126 #ifdef CGDB_ENABLED
00127 if (mDispatch) {
00128 delete mDispatch;
00129 }
00130 #endif
00131 delete ivmgr;
00132 delete mainWindow;
00133 }
00134
00142 int
00143 GraspItGUI::processArgs(int argc, char** argv)
00144 {
00145 QString filename;
00146 int errflag=0; (void*)&errflag;
00147 QString graspitRoot = QString(getenv("GRASPIT"));
00148 if (graspitRoot.isNull() ) {
00149 std::cerr << "Please set the GRASPIT environment variable to the root directory of graspIt." << std::endl;
00150 return FAILURE;
00151 }
00152
00153
00154 for (int i=1; i<argc; i++) {
00155 QString arg(argv[i]);
00156 if (arg.section(':',0,0)=="plugin") {
00157 QString libName = arg.section(':',1,1);
00158 PluginCreator* creator = PluginCreator::loadFromLibrary(libName.toStdString());
00159 if (creator) {
00160 mPluginCreators.push_back(creator);
00161 } else {
00162 DBGA("Failed to load plugin: " << libName.latin1());
00163 }
00164 }
00165 }
00166
00167
00168 mPluginSensor = new SoIdleSensor(GraspItGUI::sensorCB, (void*)this);
00169 for (size_t i=0; i<mPluginCreators.size(); i++) {
00170 if (mPluginCreators[i]->autoStart()) {
00171 startPlugin(mPluginCreators[i], mPluginCreators[i]->defaultArgs());
00172 }
00173 }
00174
00175 if(argc > 1){
00176 #ifdef CGDB_ENABLED
00177 if(!strcmp(argv[1],"dbase")){
00178 DBaseBatchPlanner *dbp = new DBaseBatchPlanner(graspItGUI->getIVmgr(), this);
00179 dbp->processArguments(argc, argv);
00180 dbp->startPlanner();
00181 }
00182 if (!strcmp(argv[1],"db_dispatch")) {
00183 mDispatch = new TaskDispatcher();
00184 if (mDispatch->connect("wgs36",5432,"willow","willow","household_objects")) {
00185 std::cerr << "DBase dispatch failed to connect to database\n";
00186 return TERMINAL_FAILURE;
00187 }
00188 std::cerr << "DBase dispatch connected to database\n";
00189 mDispatch->mainLoop();
00190 if (mDispatch->getStatus() != TaskDispatcher::RUNNING) {
00191 mExitCode = mDispatch->getStatus();
00192 return TERMINAL_FAILURE;
00193 }
00194 }
00195 #endif
00196 }
00197
00198 #ifdef Q_WS_X11
00199 char c;
00200 while((c=getopt(argc, argv, "r:w:o:b:")) != EOF) {
00201 switch(c) {
00202 case 'r':
00203 filename = graspitRoot + QString("/models/robots/")+
00204 QString(optarg) + QString("/") + QString(optarg) + QString(".cfg");
00205 if (ivmgr->getWorld()->importRobot(filename)==NULL)
00206 ++errflag;
00207 break;
00208 case 'w':
00209 filename = graspitRoot + QString("/worlds/")+ QString(optarg) +
00210 QString(".wld");
00211 if (ivmgr->getWorld()->load(filename)==FAILURE)
00212 ++errflag;
00213 else
00214 mainWindow->mUI->worldBox->setTitle(filename);
00215 break;
00216 case 'o':
00217 filename = graspitRoot + QString("/models/objects/")+ QString(optarg) +
00218 QString(".iv");
00219 if (!ivmgr->getWorld()->importBody("GraspableBody",filename))
00220 ++errflag;
00221 break;
00222 case 'b':
00223 filename = graspitRoot + QString("/models/obstacles/")+ QString(optarg) +
00224 QString(".iv");
00225 if (!ivmgr->getWorld()->importBody("Body",filename))
00226 ++errflag;
00227 break;
00228 default:
00229 ++errflag;
00230 break;
00231 }
00232 }
00233 if (errflag) {
00234 std::cerr << "Usage: graspit [-w worldname] [-r robotname] [-o objectname] [-b obstaclename]" << std::endl;
00235 return FAILURE;
00236 }
00237 #endif
00238 return SUCCESS;
00239 }
00240
00244 void
00245 GraspItGUI::startMainLoop()
00246 {
00247 SoQt::show(mainWindow->mWindow);
00248 mainWindow->setMainWorld(ivmgr->getWorld());
00249 mainWindow->mWindow->resize(QSize(1070,937));
00250 SoQt::mainLoop();
00251 }
00252
00256 void
00257 GraspItGUI::exitMainLoop()
00258 {
00259 SoQt::exitMainLoop();
00260 }
00261
00262 bool
00263 GraspItGUI::terminalFailure() const
00264 {
00265 return initResult == TERMINAL_FAILURE;
00266 }
00267
00268 void
00269 GraspItGUI::sensorCB(void*, SoSensor*)
00270 {
00271 graspItGUI->processPlugins();
00272 }
00273
00274 void
00275 GraspItGUI::startPlugin(PluginCreator* creator, std::string args)
00276 {
00277 Plugin *plugin = creator->createPlugin(args);
00278 if (plugin) mActivePlugins.push_back( std::pair<Plugin*, std::string>(plugin, creator->type()) );
00279 if (!mActivePlugins.empty()) {
00280 mPluginSensor->schedule();
00281 }
00282 }
00283
00284 void GraspItGUI::processPlugins()
00285 {
00286 std::list< std::pair<Plugin*, std::string> >::iterator it = mActivePlugins.begin();
00287 while (it != mActivePlugins.end()) {
00288 if (it->first->mainLoop() == SUCCESS) {
00289 it++;
00290 } else{
00291 delete it->first;
00292 it = mActivePlugins.erase(it);
00293 }
00294 }
00295 if (!mActivePlugins.empty()) {
00296 mPluginSensor->schedule();
00297 }
00298 }
00299
00300 void GraspItGUI::stopPlugin(Plugin *plugin)
00301 {
00302 std::list< std::pair<Plugin*, std::string> >::iterator it;
00303 for (it = mActivePlugins.begin(); it!=mActivePlugins.end(); it++) {
00304 if (it->first == plugin) {
00305 delete it->first;
00306 mActivePlugins.erase(it);
00307 return;
00308 }
00309 }
00310 DBGA("Stop plugin: plugin not found");
00311 }
00312
00313 void GraspItGUI::stopAllPlugins()
00314 {
00315 std::list< std::pair<Plugin*, std::string> >::iterator it = mActivePlugins.begin();
00316 while (it != mActivePlugins.end()) {
00317 delete it->first;
00318 it = mActivePlugins.erase(it);
00319 }
00320 }