app-Beobot2_GistSalLocalizerMaster.C
Go to the documentation of this file.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
00027
00028
00029
00030
00031
00032
00033
00034
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
00077
00078
00079
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
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
00124
00125 std::string envFName = itsMgr->getExtraArg(0);
00126
00127
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
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
00158 if (mkdir(testRunFolder.c_str(), 0777) == -1 && errno != EEXIST)
00159 {
00160 LFATAL("Cannot create log folder: %s", testRunFolder.c_str());
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
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
00180
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
00206
00207
00208