app-Beobot2_GistSalLocalizerMaster.C

Go to the documentation of this file.
00001 /*!@file                                                                */
00002 // //////////////////////////////////////////////////////////////////// //
00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00004 // University of Southern California (USC) and the iLab at USC.         //
00005 // See http://iLab.usc.edu for information about this project.          //
00006 // //////////////////////////////////////////////////////////////////// //
00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00009 // in Visual Environments, and Applications'' by Christof Koch and      //
00010 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00011 // pending; application number 09/912,225 filed July 23, 2001; see      //
00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00013 // //////////////////////////////////////////////////////////////////// //
00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00015 //                                                                      //
00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00017 // redistribute it and/or modify it under the terms of the GNU General  //
00018 // Public License as published by the Free Software Foundation; either  //
00019 // version 2 of the License, or (at your option) any later version.     //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00024 // PURPOSE.  See the GNU General Public License for more details.       //
00025 //                                                                      //
00026 // You should have received a copy of the GNU General Public License    //
00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00029 // Boston, MA 02111-1307 USA.                                           //
00030 // //////////////////////////////////////////////////////////////////// //
00031 //
00032 // Primary maintainer for this file: Christian Siagian <siagian@usc.edu>
00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/Beobot2/Localization/app-Beobot2_GistSalLocalizerMaster.C $
00034 // $Id: app-Beobot2_GistSalLocalizerMaster.C 12962 2010-03-06 02:13:53Z irock $
00035 //
00036 
00037 // ######################################################################
00038 // ######################################################################
00039 #include "Component/ModelManager.H"
00040 #include "Component/ModelComponent.H"
00041 #include "Component/ModelOptionDef.H"
00042 #include "Robots/Beobot2/Localization/Beobot2_GistSalLocalizerMaster.H"
00043 #include <Ice/Ice.h>
00044 #include <Ice/Service.h>
00045 #include "Ice/RobotSimEvents.ice.H"
00046 #include "Ice/RobotBrainObjects.ice.H"
00047 #include "Ice/SimEventsUtils.H"
00048 #include "Ice/IceImageUtils.H"
00049 
00050 #include "Beobot/beobot-GSnav-def.H"
00051 #include "Beobot/GSnavResult.H"
00052 
00053 #include <sys/stat.h>
00054 #include <errno.h>
00055 // ######################################################################
00056 class RobotBrainServiceService : public Ice::Service {
00057   protected:
00058     virtual bool start(int, char* argv[]);
00059     virtual bool stop() {
00060       if (itsMgr)
00061         delete itsMgr;
00062       return true;
00063     }
00064 
00065   private:
00066     Ice::ObjectAdapterPtr itsAdapter;
00067     ModelManager *itsMgr;
00068 };
00069 
00070 // ######################################################################
00071 bool RobotBrainServiceService::start(int argc, char* argv[])
00072 {
00073   char adapterStr[255];
00074 
00075   LINFO("Creating Topic!");
00076   //Create the topics
00077 //  SimEventsUtils::createTopic(communicator(), "LandmarkDBWorkerMessageTopic");
00078 
00079   //Create the adapter
00080   int port = RobotBrainObjects::RobotBrainPort;
00081   bool connected = false;
00082   LDEBUG("Opening Connection");
00083 
00084   while(!connected)
00085   {
00086     try
00087     {
00088       LINFO("Trying Port:%d", port);
00089       sprintf(adapterStr, "default -p %i", port);
00090       itsAdapter = communicator()->createObjectAdapterWithEndpoints
00091         ("LandmarkDBWorker", adapterStr);
00092       connected = true;
00093     }
00094     catch(Ice::SocketException)
00095     {
00096       port++;
00097     }
00098   }
00099 
00100   //Create the manager and its objects
00101   itsMgr = new ModelManager("Beobot2_GistSalLocalizerMasterService");
00102 
00103   LINFO("Starting Beobot2_GistSalLocalizerMaster");
00104   nub::ref<Beobot2_GistSalLocalizerMasterI> gslm
00105     (new Beobot2_GistSalLocalizerMasterI
00106      (*itsMgr, "Beobot2_GistSalLocalizerMaster",
00107       "Beobot2_GistSalLocalizerMaster"));
00108   LINFO("Beobot2_GistSalLocalizerMaster Created");
00109   itsMgr->addSubComponent(gslm);
00110   LINFO("Beobot2_GistSalLocalizerMaster Added As Sub Component");
00111   gslm->init(communicator(), itsAdapter);
00112   LINFO("Beobot2_GistSalLocalizerMaster Initiated");
00113 
00114   itsMgr->exportOptions(MC_RECURSE);
00115   if (itsMgr->parseCommandLine((const int)argc, (const char**)argv,
00116                                "<environment file> "
00117                                "<number of workers> "
00118                                "[test run file prefix] ",
00119                                2, 3)
00120       == false) return(1);
00121 
00122 
00123   // initialize an environment - even if the .env file does not exist
00124   // automatically create a blank environment, ready to be built
00125   std::string envFName = itsMgr->getExtraArg(0);
00126 
00127   // get the environment file and folder to save
00128   std::string saveFilePath = "";
00129   std::string::size_type lspos = envFName.find_last_of('/');
00130   int ldpos = envFName.find_last_of('.');
00131   std::string envPrefix;
00132   if(lspos != std::string::npos)
00133     {
00134       saveFilePath = envFName.substr(0, lspos+1);
00135       envPrefix =  envFName.substr(lspos+1, ldpos - lspos - 1);
00136     }
00137   else
00138     envPrefix =  envFName.substr(0, ldpos - 1);
00139   LINFO("Env file: %s", envFName.c_str());
00140 
00141   // get the time of day
00142   time_t rawtime; struct tm * timeinfo;
00143   time ( &rawtime );
00144   timeinfo = localtime ( &rawtime );
00145   char buffer [80];
00146   strftime (buffer,80, "results_%Y_%m_%d__%H_%M_%S",timeinfo);
00147   std::string testRunFPrefix(buffer);
00148   if(itsMgr->numExtraArgs() >  2)
00149     testRunFPrefix = itsMgr->getExtraArgAs<std::string>(2);
00150 
00151   std::string testRunFolder =
00152     saveFilePath + testRunFPrefix + std::string("/");
00153 
00154   std::string resultPrefix = testRunFolder + envPrefix;
00155   LINFO("result prefix: %s", resultPrefix.c_str());
00156 
00157   // create the session result folder
00158   if (mkdir(testRunFolder.c_str(), 0777) == -1 && errno != EEXIST)
00159     {
00160       LFATAL("Cannot create log folder: %s", testRunFolder.c_str());
00161     }
00162 
00163 //   // print the results summary
00164 //   GSnavResult r1;
00165 //   r1.read(resultPrefix, 9);
00166 //   r1.createSummaryResult();
00167 //   Raster::waitForKey();
00168 //   // HACK: results for paper
00169 //   reportResults(resultPrefix, 9);
00170 //   Raster::waitForKey();
00171 
00172   // link the environment to the localizer
00173   rutz::shared_ptr<Environment> env(new Environment(envFName));
00174   gslm->setEnvironment(env);
00175   gslm->setNumWorkers(itsMgr->getExtraArgAs<uint>(1));
00176 
00177   gslm->setSavePrefix(resultPrefix);
00178 
00179   // initialize the particles
00180   // in case we have a starting belief
00181   uint fstart = 0;
00182 
00183   std::string sBelFName = resultPrefix +
00184     sformat("_bel_%07d.txt", fstart-1);
00185   LINFO("Starting belief file: %s", sBelFName.c_str());
00186   gslm->initParticles(sBelFName);
00187 
00188   itsAdapter->activate();
00189 
00190   itsMgr->start();
00191 
00192   return true;
00193 }
00194 
00195 // ######################################################################
00196 int main(int argc, char** argv) {
00197 
00198   LINFO("Creating Service...");
00199   RobotBrainServiceService svc;
00200   LINFO("Service Created...");
00201   return svc.main(argc, argv);
00202 }
00203 
00204 // ######################################################################
00205 /* So things look consistent in everyone's emacs... */
00206 /* Local Variables: */
00207 /* indent-tabs-mode: nil */
00208 /* End: */
Generated on Sun May 8 08:05:36 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3