MRXT: The Multi-Robot eXploration Tool
Multi-Robot autonomous exploration and mapping simulator.
|
00001 #include "vslamFilter.h" 00002 00003 00004 // Constructor 00005 vslamFilter::vslamFilter(): 00006 nBots(0), 00007 th_high(0), 00008 th_low(0), 00009 width(0), 00010 height(0), 00011 resolution(0), 00012 gmwidth(0), 00013 gmheight(0), 00014 xorigin(0), 00015 yorigin(0), 00016 rbase(0), 00017 robotsEnabled(0), 00018 scene(0), 00019 badlocalized(0), 00020 logstr(0), 00021 displayomap(false), 00022 displayppmap(false), 00023 displayipmap(false), 00024 displayposes(false), 00025 displayfeatures(false), 00026 saveAviFile(false), 00027 alfa1(0.0f), 00028 alfa2(0.0f), 00029 alfa3(0.0f), 00030 alfa4(0.0f), 00031 drifttrans(0.0f), 00032 gmtype(0), 00033 vmtype(0), 00034 perfectMatching(false), 00035 nmarks(0), 00036 windowName (0) 00037 { 00038 printf("Slam Filter default constructor\n"); 00039 } 00040 00041 vslamFilter::vslamFilter(int nrobots, ConfigFile& configFile): 00042 nBots (nrobots), 00043 th_high (configFile.read<float>("THRESHOLD_HIGH")), 00044 th_low (configFile.read<float>("THRESHOLD_LOW")), 00045 width (configFile.read<float>("SCENE_WIDTH")), 00046 height (configFile.read<float>("SCENE_HEIGHT")), 00047 resolution (configFile.read<float>("RESOLUTION")), 00048 gmwidth ((int)floor(width/resolution+0.5)), 00049 gmheight ((int)floor(height/resolution+0.5)), 00050 xorigin (configFile.read<float>("XORIGIN")), 00051 yorigin (configFile.read<float>("YORIGIN")), 00052 rbase (new robotBase*[nBots]), 00053 robotsEnabled (new bool[nBots]), 00054 scene (0), 00055 badlocalized (new bool[nBots]), 00056 logstr (0), 00057 displayomap ((configFile.keyExists("DISPLAYOMAP"))? configFile.read<bool>("DISPLAYOMAP"): false), 00058 displayppmap ((configFile.keyExists("DISPLAYPPMAP"))? configFile.read<bool>("DISPLAYPPMAP"): false), 00059 displayipmap ((configFile.keyExists("DISPLAYIPMAP"))? configFile.read<bool>("DISPLAYIPMAP"): false), 00060 displayposes ((configFile.keyExists("DISPLAYPOSES"))? configFile.read<bool>("DISPLAYPOSES"): false), 00061 displayfeatures ((configFile.keyExists("DISPLAYFEATURES"))? configFile.read<bool>("DISPLAYFEATURES"): false), 00062 saveAviFile ((configFile.keyExists("SAVEAVIFILE"))? configFile.read<bool>("SAVEAVIFILE"): false), 00063 alfa1 (configFile.read<float>("alfa1")), 00064 alfa2 (configFile.read<float>("alfa2")), 00065 alfa3 (configFile.read<float>("alfa3")), 00066 alfa4 (configFile.read<float>("alfa4")), 00067 drifttrans (configFile.read<float>("drifttrans")), 00068 gmtype ((configFile.keyExists("GRIDMAP"))? configFile.read<int>("GRIDMAP"): 3), 00069 vmtype ((configFile.keyExists("VISUALMAP"))? configFile.read<int>("VISUALMAP"): 0), 00070 perfectMatching ((configFile.keyExists("PERFECTMATCHING"))? configFile.read<bool>("PERFECTMATCHING"): false), 00071 nmarks ((configFile.keyExists("NLANDMARKS"))? configFile.read<int>("NLANDMARKS"): 1000), 00072 windowName (0) 00073 { 00074 for (int r = 0; r< nBots; r++){ 00075 rbase[r] = 0; 00076 badlocalized[r]=false; 00077 } 00078 printf("[VSLAM] Multi-Robot SLAM created\n"); 00079 } 00080 00081 vslamFilter::~vslamFilter(){ 00082 //printf("slam destroyer..."); 00083 if (rbase) delete[] rbase; 00084 if (robotsEnabled) delete[] robotsEnabled; 00085 if (badlocalized) delete[] badlocalized; 00086 if (logstr) delete[] logstr; 00087 00088 //printf("Slam Filter destroyed\n"); 00089 } 00090 00091 // Initializer 00092 void vslamFilter::initialize(int nrobots, ConfigFile& configFile){ 00093 nBots = nrobots; 00094 th_high = configFile.read<float>("THRESHOLD_HIGH"); 00095 th_low = configFile.read<float>("THRESHOLD_LOW"); 00096 width = configFile.read<float>("SCENE_WIDTH"); 00097 height = configFile.read<float>("SCENE_HEIGHT"); 00098 resolution = configFile.read<float>("RESOLUTION"); 00099 gmwidth = (int)floor(width/resolution+0.5); 00100 gmheight = (int)floor(height/resolution+0.5); 00101 xorigin = configFile.read<float>("XORIGIN"); 00102 yorigin = configFile.read<float>("YORIGIN"); 00103 if (rbase) delete[] rbase; 00104 rbase = new robotBase*[nBots]; 00105 if (robotsEnabled) delete[] robotsEnabled; 00106 robotsEnabled =new bool[nBots]; 00107 scene = 0; 00108 if (badlocalized) delete[] badlocalized; 00109 badlocalized = new bool[nBots]; 00110 if (logstr) delete[] logstr; 00111 logstr = 0; 00112 displayomap = (configFile.keyExists("DISPLAYOMAP"))? configFile.read<bool>("DISPLAYOMAP") : false; 00113 displayppmap = (configFile.keyExists("DISPLAYOMAP"))? configFile.read<bool>("DISPLAYPPMAP") : false; 00114 displayipmap = (configFile.keyExists("DISPLAYOMAP"))? configFile.read<bool>("DISPLAYIPMAP") : false; 00115 displayposes = (configFile.keyExists("DISPLAYOMAP"))? configFile.read<bool>("DISPLAYPOSES") : false; 00116 displayfeatures = (configFile.keyExists("DISPLAYOMAP"))? configFile.read<bool>("DISPLAYFEATURES") : false; 00117 saveAviFile = (configFile.keyExists("DISPLAYOMAP"))? configFile.read<bool>("SAVEAVIFILE") : false; 00118 alfa1 = configFile.read<float>("alfa1"); 00119 alfa2 = configFile.read<float>("alfa2"); 00120 alfa3 = configFile.read<float>("alfa3"); 00121 alfa4 = configFile.read<float>("alfa4"); 00122 drifttrans = configFile.read<float>("drifttrans"); 00123 00124 gmtype = (configFile.keyExists("GRIDMAP"))? configFile.read<int>("GRIDMAP") : 3; 00125 vmtype = (configFile.keyExists("VISUALMAP"))? configFile.read<int>("VISUALMAP") : 0; 00126 perfectMatching = (configFile.keyExists("PERFECTMATCHING"))? configFile.read<bool>("PERFECTMATCHING") : false; 00127 nmarks = (configFile.keyExists("NLANDMARKS"))? configFile.read<int>("NLANDMARKS") : 1000; 00128 windowName = 0; 00129 resetCounter(); 00130 00131 for (int r = 0; r< nBots; r++){ 00132 rbase[r] = 0; 00133 badlocalized[r]=false; 00134 } 00135 //printf("[VSLAM] Multi-Robot SLAM initialized\n"); 00136 } 00137 00138 QPixmap* vslamFilter::getPixmap(){ 00139 return 0; 00140 } 00141 00142 00143 // odometry model 00144 pose vslamFilter::odometryModel(const pose& lastOdo, const pose& deltaOdo, const pose& lastPose){ 00145 00146 // modelo de 3 movimientos: rotacion rot1, avance trans y rotacion rot2. 00147 00148 float deltarot1 = atan2(deltaOdo.y, deltaOdo.x) - lastOdo.th; // cuanto ha rotado 00149 float deltatrans = drifttrans*sqrt((deltaOdo.x*deltaOdo.x+deltaOdo.y*deltaOdo.y)); // distancia avanzada 00150 float deltarot2 = deltaOdo.th - deltarot1; // incremento de orientacion aparte de la rotacion 00151 00152 float dtheta = fabs(atan2(sin(deltaOdo.th),cos(deltaOdo.th))); 00153 00154 float aux = (alfa1*dtheta + alfa2*deltatrans)/2; 00155 00156 float deltarot1g = (float)(normrnd(deltarot1, aux)); 00157 float deltatransg = (float)(normrnd(deltatrans, alfa3*deltatrans + alfa4*dtheta)); 00158 float deltarot2g = (float)(normrnd(deltarot2, aux)); 00159 00160 float aux2 = lastPose.th+deltarot1g; 00161 00162 return pose(lastPose.x + deltatransg * cos(aux2), 00163 lastPose.y + deltatransg * sin(aux2), 00164 lastPose.th + deltarot1g + deltarot2g); 00165 } 00166 00167 void vslamFilter::updatePPMap(const point& rpos, binMap& map, float disp, bool badloc){ 00168 if (disp < th_low) 00169 map.set(rpos.x, rpos.y, true); 00170 else 00171 map.set(rpos.x, rpos.y, false); 00172 if(disp > th_high){ 00173 for (int x = rpos.x-3; x<= rpos.x+3; x++) 00174 for (int y = rpos.y-3; y<= rpos.y+3; y++) 00175 map.set(x, y, false); 00176 } 00177 } 00178 00179 void vslamFilter::updateIPMap(const point& rpos, binMap& map, float disp, bool badloc){ 00180 if (disp > th_high) 00181 map.set(rpos.x, rpos.y, true); 00182 else if(!badloc){ 00183 for (int x = rpos.x-4; x<= rpos.x+4; x++) 00184 for (int y = rpos.y-4; y<= rpos.y+4; y++) 00185 map.set(x, y, false); 00186 } 00187 } 00188 00189 00190 void vslamFilter::setLogName(const char* str){ 00191 if (logstr) delete[] logstr; 00192 logstr = new char[strlen(str)+1]; 00193 strcpy(logstr,str); 00194 } 00195 00196 00197 void vslamFilter::setWindowName(const char* str){ 00198 if (windowName) delete[] windowName; 00199 windowName = new char[strlen(str)+1]; 00200 strcpy(windowName,str); 00201 } 00202 00203 void vslamFilter::disableRobotBase(int r){ 00204 if (robotsEnabled[r]){ 00205 // printf("[VSLAM] disabling robot %d...\n",r); 00206 rbase[r]->beginProduction(); 00207 robotsEnabled[r]=false; 00208 rbase[r]->endProduction(); 00209 // printf("[VSLAM] robot %d disabled\n",r); 00210 } 00211 }