MRXT: The Multi-Robot eXploration Tool
Multi-Robot autonomous exploration and mapping simulator.
|
00001 /* 00002 * 00003 * Author: Miguel Julia <mjulia@umh.es> 00004 * 00005 * Date: 2008 00006 * 00007 * Class team 00008 * 00009 * Implements a team of robots with global SLAM 00010 * 00011 */ 00012 00013 #include "team.h" 00014 #include "OGMReflectProb.h" 00015 00016 #ifndef WIN32 00017 #include <unistd.h> 00018 #endif 00019 00020 // Constructor 00021 team::team(): 00022 nrobots(0), 00023 theRobots(0), 00024 globalSlam(0), 00025 scene(0), 00026 teamName(0) 00027 { 00028 printf("[TEAM] Team created\n"); 00029 } 00030 00031 // destructor 00032 team::~team(){ 00033 // printf("[TEAM] [%s] Team destroyer...\n", teamName); 00034 stop(); 00035 if (globalSlam) delete globalSlam; 00036 if (teamName) delete teamName; 00037 if (theRobots){ 00038 for (int i = 0; i < nrobots; i++) if (theRobots[i]) delete theRobots[i]; 00039 delete[] theRobots; 00040 } 00041 printf("[TEAM] Team finished\n"); 00042 } 00043 00044 // constructor 00045 team::team(int nr, worldModelInterface& s, ConfigFile& stratConfig, ConfigFile& slamConfig, int robotType, const char* name): 00046 nrobots(nr), 00047 theRobots(new robot*[nrobots]), 00048 globalSlam(slamFactory::Instance().CreateObject(slamConfig.read<int>("SLAM"),nr,slamConfig)), 00049 scene(&s), 00050 teamName(0) 00051 { 00052 printf("[TEAM] Building the new team...\n"); 00053 for(int r =0; r < nrobots; r++){ 00054 theRobots[r] = robotFactory::Instance().CreateObject(robotType, r, *globalSlam, *scene, stratConfig); 00055 } 00056 if (teamName) delete[] teamName; 00057 teamName = new char[strlen(name)+1]; 00058 strcpy(teamName,name); 00059 globalSlam->setWorldModel(s); 00060 globalSlam->setWindowName(teamName); 00061 char mapname[100]; 00062 sprintf(mapname,"%somap.saved",teamName); 00063 globalSlam->setGridMapName(mapname); 00064 printf("[TEAM] Team created\n"); 00065 } 00066 00067 // initializer 00068 void team::initialize(int nr, worldModelInterface& s, ConfigFile& stratConfig, ConfigFile& slamConfig, int robotType, const char* name){ 00069 nrobots=nr; 00070 if (globalSlam) delete globalSlam; 00071 globalSlam = slamFactory::Instance().CreateObject(slamConfig.read<int>("SLAM"),nr,slamConfig); 00072 scene= &s; 00073 if (theRobots){ 00074 for (int i = 0; i < nrobots; i++) delete theRobots[i]; 00075 delete[] theRobots; 00076 } 00077 theRobots = new robot*[nrobots]; 00078 for(int r =0; r< nr; r++){ 00079 // theRobots[r].initialize(r, *globalSlam, *scene, config); 00080 theRobots[r] = robotFactory::Instance().CreateObject(robotType, r, *globalSlam, *scene, stratConfig); 00081 } 00082 if (teamName) delete[] teamName; 00083 teamName = new char[strlen(name)+1]; 00084 strcpy(teamName,name); 00085 globalSlam->setWorldModel(s); 00086 globalSlam->setWindowName(teamName); 00087 } 00088 00089 // sets the name of the log file 00090 void team::setLogName(const char* str){ 00091 globalSlam->setLogName(str); 00092 for(int r =0; r< nrobots; r++) 00093 theRobots[r]->setLogName(str); 00094 } 00095 00096 // set the world model 00097 void team::setWorldModel(worldModelInterface& scene){ 00098 for(int r =0; r< nrobots; r++){ 00099 theRobots[r]->setWorldModel(scene); 00100 } 00101 globalSlam->setWorldModel(scene); 00102 } 00103 00104 // team exploration, it blocks the thread until the exploration is concluded 00105 void team::start(){ 00106 00107 globalSlam->run(); 00108 00109 int r; 00110 for(r =0; r< nrobots; r++){ 00111 theRobots[r]->start(); 00112 } 00113 00114 // sleepms(100000); 00115 } 00116 00117 void team::waitForTaskFinished(){ 00118 for(int r =0; r< nrobots; r++){ 00119 theRobots[r]->endEvent().waitForStep(1); 00120 theRobots[r]->disable(); 00121 } 00122 printf("[TEAM] [%s] All robots finished, stopping SLAM...\n",teamName); 00123 globalSlam->stop(); 00124 printf("[TEAM] [%s] Task completed\n",teamName); 00125 } 00126 00127 // stops the explortion 00128 void team::stop(){ 00129 //printf("[TEAM] stoping the robots...\n"); 00130 for(int r =0; r< nrobots; r++){ 00131 theRobots[r]->stop(); 00132 theRobots[r]->disable(); 00133 } 00134 //printf("[TEAM] stoping the slam...\n"); 00135 globalSlam->stop(); 00136 // printf("[TEAM] stoped\n"); 00137 } 00138