00001 #ifndef MAIN_HPP
00002 #define MAIN_HPP
00003
00004 #include <iostream>
00005 #include <getopt.h>
00006
00007 #include <graphmap/graphmap.hpp>
00008 #include <graphmap/graphManager.hpp>
00009 #include <graphmap/display.hpp>
00010 #include <graphmap/dummyMappingProcess.hpp>
00011 #include <graphmap/inputManager.hpp>
00012 #include <graphmap/outputManager.hpp>
00013
00014 #include <boost/graph/adjacency_list.hpp>
00015
00016 #include "kernel/keyValueFile.hpp"
00017
00018 typedef boost::shared_ptr<jafar::graphmap::DummyMappingProcess> dummy_mapping_process_ptr_t;
00019 typedef boost::shared_ptr<jafar::graphmap::GraphManager> graph_manager_ptr_t;
00020 typedef boost::shared_ptr<jafar::graphmap::display::Viewer> viewer_ptr_t;
00021 typedef boost::shared_ptr<jafar::graphmap::LinkInputManager> link_manager_ptr_t;
00022 typedef boost::shared_ptr<jafar::graphmap::RobotPoseInputManager> robot_pose_manager_ptr_t;
00023 typedef boost::shared_ptr<jafar::graphmap::GlobalRobotPoseOutputManager> global_robot_pose_manager_ptr_t;
00024 typedef boost::shared_ptr<jafar::graphmap::LocalRobotPoseOutputManager> local_robot_pose_manager_ptr_t;
00025
00026
00027
00028
00029 enum { iDispQt = 0, iDispGdhe, iPause, iVerbose, iSimu, nIntOpts };
00030 int intOpts[nIntOpts] = {0};
00031 const int nFirstIntOpt = 0, nLastIntOpt = nIntOpts-1;
00032
00033 enum { sConfigSetup, nStrOpts };
00034 std::string strOpts[nStrOpts];
00035 const int nFirstStrOpt = nIntOpts, nLastStrOpt = nIntOpts+nStrOpts-1;
00036
00037 enum { bHelp = 0, bUsage, nBreakingOpts };
00038 const int nFirstBreakingOpt = nIntOpts+nStrOpts, nLastBreakingOpt = nIntOpts+nStrOpts+nBreakingOpts-1;
00039
00041
00042 struct option long_options[] = {
00043
00044 {"disp-2d", optional_argument, 0, 0},
00045 {"disp-3d", optional_argument, 0, 0},
00046 {"pause", optional_argument, 0, 0},
00047 {"verbose", optional_argument, 0, 0},
00048 {"simu", optional_argument, 0, 0},
00049
00050 {"config-setup", required_argument, 0, 0},
00051
00052 {"help",no_argument,0,0},
00053 {"usage",no_argument,0,0},
00054 };
00055
00056 class ConfigSetup: public jafar::kernel::KeyValueFileSaveLoad
00057 {
00058 public:
00059 unsigned ROBOT_ID;
00060 jblas::vec6 ROBOT_POSE;
00061 private:
00062 void processKeyValueFile(jafar::kernel::KeyValueFile& keyValueFile, bool read);
00063 public:
00064 virtual void loadKeyValueFile(jafar::kernel::KeyValueFile const& keyValueFile);
00065 virtual void saveKeyValueFile(jafar::kernel::KeyValueFile& keyValueFile);
00066 } configSetup;
00067
00068 dummy_mapping_process_ptr_t slamPtr;
00069 graph_manager_ptr_t graphManagerPtr;
00070 viewer_ptr_t viewerPtr;
00071 link_manager_ptr_t linkManagerPtr;
00072 robot_pose_manager_ptr_t robotPoseManagerPtr;
00073 global_robot_pose_manager_ptr_t globalRobotPoseManagerPtr;
00074 local_robot_pose_manager_ptr_t localRobotPoseManagerPtr;
00075
00076 const int display_period = 100;
00077
00078 void demo_graphmap_init()
00079 {
00080 using namespace jafar;
00081 using namespace graphmap;
00082
00083 JFR_GLOBAL_TRY
00084
00085 if (strOpts[sConfigSetup] == "#!@")
00086 {
00087 strOpts[sConfigSetup] = "data/setup.cfg";
00088 }
00089
00090 std::cout << "Loading config file " << strOpts[sConfigSetup] << std::endl;
00091 configSetup.load(strOpts[sConfigSetup]);
00092
00093 graphManagerPtr.reset(new GraphManager(configSetup.ROBOT_ID));
00094
00095 linkManagerPtr.reset(new LinkInputManager(graphManagerPtr));
00096 graphManagerPtr->addInputManager(linkManagerPtr);
00097
00098 robotPoseManagerPtr.reset(new RobotPoseInputManager(graphManagerPtr));
00099 graphManagerPtr->addInputManager(robotPoseManagerPtr);
00100
00101 globalRobotPoseManagerPtr.reset(new GlobalRobotPoseOutputManager(graphManagerPtr));
00102 graphManagerPtr->addOutputManager(globalRobotPoseManagerPtr);
00103
00104 localRobotPoseManagerPtr.reset(new LocalRobotPoseOutputManager(graphManagerPtr));
00105 graphManagerPtr->addOutputManager(localRobotPoseManagerPtr);
00106
00107 if(intOpts[iSimu]){
00108 slamPtr.reset(new DummyMappingProcess(configSetup.ROBOT_ID));
00109 }
00110
00111 #ifndef HAVE_MODULE_QDISPLAY
00112 intOpts[iDispQt] = 0;
00113 #endif
00114 #ifndef HAVE_MODULE_GDHE
00115 intOpts[iDispGdhe] = 0;
00116 #endif
00117
00118 if(intOpts[iDispQt]){
00119 viewerPtr.reset(new display::Viewer());
00120 }
00121
00122
00123
00124
00125
00126 switch (intOpts[iVerbose])
00127 {
00128 case 0: debug::DebugStream::setLevel("graphmap", debug::DebugStream::Off); break;
00129 case 1: debug::DebugStream::setLevel("graphmap", debug::DebugStream::Trace); break;
00130 case 2: debug::DebugStream::setLevel("graphmap", debug::DebugStream::Warning); break;
00131 case 3: debug::DebugStream::setLevel("graphmap", debug::DebugStream::Debug); break;
00132 case 4: debug::DebugStream::setLevel("graphmap", debug::DebugStream::VerboseDebug); break;
00133 default: debug::DebugStream::setLevel("graphmap", debug::DebugStream::VeryVerboseDebug); break;
00134 }
00135
00136
00138 JFR_GLOBAL_CATCH
00139 }
00140
00141
00142 void demo_graphmap_display(graph_manager_ptr_t *graph_manager)
00143 {
00144 if(intOpts[iDispQt] != 0){
00145 viewerPtr->bufferize((*graph_manager)->_graph);
00146 viewerPtr->render();
00147 }
00148 }
00149
00150
00151 void demo_graphmap_main(graph_manager_ptr_t *graph_manager)
00152 {
00153 JFR_DEBUG("main function" << std::endl);
00155
00160 while(!(*graph_manager)->exit()){
00161
00162 if(intOpts[iSimu]){
00163 slamPtr->update();
00164 if(slamPtr->link_poster_updated){
00165 linkManagerPtr->addInput(slamPtr->link_poster);
00166 slamPtr->link_poster_updated = false;
00167 }
00168 if(slamPtr->robot_pose_poster_updated){
00169 robotPoseManagerPtr->addInput(slamPtr->robot_pose_poster);
00170 slamPtr->robot_pose_poster_updated = false;
00171 }
00172 }
00173
00175
00176
00177
00178
00179
00180
00181
00182 graphManagerPtr->updateGraph();
00183
00184
00185 graphManagerPtr->optmizeLoops();
00186
00187
00188 graphManagerPtr->updateVertexData();
00189
00191
00192
00193
00194 }
00195
00196
00197 (*graph_manager)->dumpToMemory();
00198
00199
00200 (*graph_manager)->saveGraphImage("demo_graphmap_graph.dot");
00201
00202 }
00203
00204
00205 void demo_graphmap_exit(graph_manager_ptr_t *graph_manager, boost::thread *thread_main) {
00206 JFR_DEBUG("exit function" << std::endl);
00207 (*graph_manager)->exit(true);
00208 thread_main->join();
00209 }
00210
00211 void demo_graphmap_run()
00212 {
00213 using namespace jafar;
00214 using namespace graphmap;
00215
00216 JFR_DEBUG("run function" << std::endl);
00217 #ifdef HAVE_MODULE_QDISPLAY
00218 qdisplay::QtAppStart((qdisplay::FUNC)&demo_graphmap_display, 0,
00219 (qdisplay::FUNC)&demo_graphmap_main, 0,
00220 display_period,
00221 &graphManagerPtr,
00222 (qdisplay::EXIT_FUNC)&demo_graphmap_exit);
00223 #else
00224 std::cout << "Please install qdisplay module if you want 2D display" << std::endl;
00225 #endif
00226 }
00227
00228
00229 #define KeyValueFile_processItem(k) { read ? keyValueFile.getItem(#k, k) : keyValueFile.setItem(#k, k); }
00230 #define KeyValueFile_processItem_def(k, def) { read ? keyValueFile.getItem(#k, k, def) : keyValueFile.setItem(#k, k); }
00231
00232 void ConfigSetup::loadKeyValueFile(jafar::kernel::KeyValueFile const& keyValueFile)
00233 {
00234 jafar::kernel::KeyValueFile &keyValueFile2 = const_cast<jafar::kernel::KeyValueFile&>(keyValueFile);
00235 processKeyValueFile(keyValueFile2, true);
00236 }
00237
00238 void ConfigSetup::saveKeyValueFile(jafar::kernel::KeyValueFile& keyValueFile)
00239 {
00240 processKeyValueFile(keyValueFile, false);
00241 }
00242
00243 void ConfigSetup::processKeyValueFile(jafar::kernel::KeyValueFile& keyValueFile, bool read)
00244 {
00245 KeyValueFile_processItem(ROBOT_ID);
00246 KeyValueFile_processItem(ROBOT_POSE);
00247 }
00248
00249 #endif // MAIN_HPP