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 "Robots/Beobot2/Navigation/GistSal_Navigation/GistSal_Grapher.H"
00040 #include "Ice/BeobotEvents.ice.H"
00041
00042 #include "Raster/Raster.H"
00043 #include "Util/sformat.H"
00044 #include "Image/Image.H"
00045 #include "Ice/IceImageUtils.H"
00046
00047 #include <sys/stat.h>
00048 #include <stdio.h>
00049 #include <dirent.h>
00050 #include <unistd.h>
00051
00052
00053
00054 #define INDOOR
00055
00056
00057
00058 #ifdef INDOOR
00059
00060 #define LOG_FOLDER "../data/logs/IROS10_Equad"
00061 #else
00062 #define LOG_FOLDER "../data/logs/IROS10_Equad"
00063 #endif
00064
00065
00066 GistSal_Grapher::GistSal_Grapher(OptionManager& mgr,
00067 const std::string& descrName, const std::string& tagName) :
00068 RobotBrainComponent(mgr, descrName, tagName),
00069 itsTimer(1000000),
00070 itsDispImage(1600,600,ZEROS),
00071 itsXwin(itsDispImage,"GistSal Grapher")
00072
00073 {
00074
00075
00076 }
00077
00078
00079 GistSal_Grapher::~GistSal_Grapher()
00080 { }
00081
00082
00083 void GistSal_Grapher::start1()
00084 {
00085
00086
00087
00088 itsTimer.reset();
00089 searchLogFile();
00090 }
00091
00092
00093 bool GistSal_Grapher::initLogFile()
00094 {
00095
00096 time_t rawtime; struct tm * timeinfo;
00097 time ( &rawtime );
00098 timeinfo = localtime ( &rawtime );
00099 char buffer [80];
00100 strftime (buffer,80,
00101 "%Y_%m_%d__%H_%M_%S",timeinfo);
00102 std::string startTime(buffer);
00103
00104 itsLogFoldername =
00105 std::string(sformat("%s%s", LOG_FOLDER, startTime.c_str()));
00106 LINFO("logFoldername: %s", itsLogFoldername.c_str());
00107
00108
00109 if (mkdir(itsLogFoldername.c_str(), 0777) == -1)
00110 {
00111 LFATAL("Cannot create log folder: %s", itsLogFoldername.c_str());
00112 return(EXIT_FAILURE);
00113 }
00114
00115 std::string logFilename
00116 (sformat("%s/Log_%s.txt", itsLogFoldername.c_str(), startTime.c_str()));
00117 LINFO("logFilename: %s", itsLogFilename.c_str());
00118
00119 std::string cTime = std::string("Time of day: ") + startTime;
00120 LINFO("%s", cTime.c_str());
00121 cTime += std::string("\n");
00122
00123
00124 itsLogFilename = logFilename;
00125 FILE *rFile = fopen(itsLogFilename.c_str(), "at");
00126 if (rFile != NULL)
00127 {
00128 LDEBUG("saving result to %s", logFilename.c_str());
00129 fputs(cTime.c_str(), rFile);
00130 fclose (rFile);
00131 }
00132 else LFATAL("can't create file: %s", itsLogFilename.c_str());
00133
00134 return true;
00135 }
00136
00137
00138 void GistSal_Grapher::registerTopics()
00139 {
00140
00141 }
00142
00143
00144 void GistSal_Grapher::evolve()
00145 {
00146 updateGUI();
00147 }
00148
00149
00150 void GistSal_Grapher::updateMessage(const RobotSimEvents::EventMessagePtr& eMsg,
00151 const Ice::Current&)
00152 {
00153
00154
00155
00156 }
00157
00158
00159 void GistSal_Grapher::writeToLogFile(std::string line)
00160 {
00161 FILE *rFile = fopen(itsLogFilename.c_str(), "at");
00162 if (rFile != NULL)
00163 {
00164 fputs(line.c_str(), rFile);
00165 fclose (rFile);
00166 }
00167 else LFATAL("can't append to file: %s", itsLogFilename.c_str());
00168
00169 }
00170
00171
00172 void GistSal_Grapher::searchLogFile()
00173 {
00174 LINFO("Search for all log file");
00175 DIR *dir_p;
00176 struct dirent *entry_p;
00177 dir_p = ::opendir(LOG_FOLDER);
00178 if(dir_p == NULL)
00179 LFATAL("Count Not Open ../data/logs directory");
00180
00181 std::vector<std::string> subdir;
00182 while((entry_p = ::readdir(dir_p)))
00183 {
00184 std::string subdirname(entry_p->d_name);
00185 if(subdirname != "." && subdirname !="..")
00186 {
00187
00188 if(subdirname.c_str()[0] == 'S')
00189 subdir.push_back(subdirname);
00190 }
00191 }
00192 (void)::closedir(dir_p);
00193
00194
00195
00196 std::sort(subdir.begin(),subdir.end());
00197 for(int i = 0;i < (int)subdir.size();i++)
00198 {
00199 LINFO("File[%d]:[%s]",i+1,subdir[i].c_str());
00200 }
00201 #ifdef MANUAL
00202 int option;
00203
00204 do{
00205 LINFO("Please Choose a File Number:");
00206 scanf("%d",&option);
00207 if(option <1 ||option >int(subdir.size()))
00208 LINFO("Option Invalid, please try again");
00209 }while(option < 1 || option > int(subdir.size()));
00210 #endif
00211
00212
00213
00214
00215
00216 #ifndef MANUAL
00217 for(int option = 1;option <=int(subdir.size());option++){
00218 #endif
00219
00220 #ifdef INDOOR
00221 int run,segt;
00222 char buf[255];
00223 sscanf(subdir[option-1].c_str(),"S%d_R%d_%s",&run,&segt,buf);
00224 #endif
00225 std::string logFileName(
00226 sformat(
00227 #ifdef INDOOR
00228 "%s/%s/Log_%s.txt",
00229 #else
00230 "%s/%s/%s.txt",
00231 #endif
00232 LOG_FOLDER,
00233 subdir[option-1].c_str(),
00234 #ifdef INDOOR
00235 buf
00236 #else
00237 subdir[option-1].c_str()
00238 #endif
00239 ));
00240 FILE *logFile = fopen(logFileName.c_str(),"r");
00241
00242
00243 if(logFile == NULL)
00244 {
00245 LFATAL("can't not open file: %s",logFileName.c_str());
00246 }else
00247 {
00248
00249 std::string cvsFileName(
00250 sformat("%s/%s/%s.csv",
00251 LOG_FOLDER,
00252 subdir[option-1].c_str(),
00253 subdir[option-1].c_str()
00254 ));
00255 itsLogFilename = cvsFileName;
00256
00257 FILE *rFile = fopen(cvsFileName.c_str(), "w");
00258 if (rFile != NULL)
00259 {
00260 fclose (rFile);
00261 }
00262 else LFATAL("can't create file: %s", itsLogFilename.c_str());
00263
00264 char line[512];
00265 float x_loc = 0.0;
00266 float y_loc = 0.0;
00267 float gs = 0.0;
00268 int seg = -1;
00269 int totalGS = 0;
00270 int segCount[4] = {0,0,0,0};
00271 int rc = -1;
00272 int frame = 0;
00273 while(fgets(line,sizeof line,logFile)!= NULL)
00274 {
00275 float lenTrav;
00276 int segNum;
00277 float time;
00278 int ret = sscanf(line,"[%f] LOC seg:%d ltrav:%f",&time,&segNum,&lenTrav);
00279 if(ret == 3)
00280 {
00281 locData loc;
00282 loc.time = time;
00283 loc.segNum = segNum;
00284 loc.lenTrav = lenTrav;
00285
00286 itsLogLocData.push_back(loc);
00287 itsLogSegData.push_back(lenTrav);
00288 gs = lenTrav;
00289 seg = segNum;
00290 totalGS ++;
00291 segCount[seg]++;
00292 if(rc == 3)
00293 frame ++;
00294
00295 }
00296 float transVel,rotVel,encoderX,encoderY,encoderOri,rcTransVel,rcRotVel;
00297 int rcMode;
00298 ret = sscanf (line,"[%f] MTR rcMode: %d, transVel: %f rotVel: %f encoderX: %f encoderY: %f encoderOri: %f rcTransVel: %f rcRotVel: %f",
00299 &time, &rcMode, &transVel, &rotVel, &encoderX, &encoderY, &encoderOri, &rcTransVel, &rcRotVel);
00300 if(ret == 9)
00301 {
00302 motorData tmp;
00303 tmp.time = time;
00304 tmp.rcMode = rcMode;
00305 tmp.transVel = transVel;
00306 tmp.rotVel = rotVel;
00307 tmp.encoderX = encoderX;
00308 tmp.encoderY = encoderY;
00309 tmp.encoderOri = encoderOri;
00310 tmp.rcTransVel = rcTransVel;
00311 tmp.rcRotVel = rcRotVel;
00312 itsLogMotorData.push_back(tmp);
00313
00314 x_loc += encoderX;
00315 y_loc += encoderY;
00316 std::string csvLine (sformat( "%15f,%10f,%10f,%3f,%2d,%3d,%10f,%10f\n",time,x_loc,y_loc,gs,seg,rcMode,transVel,rotVel));
00317 writeToLogFile(csvLine);
00318
00319 rc = rcMode;
00320 }
00321
00322
00323 float lrfMin,lrfMax;
00324 ret = sscanf(line,"[%f] LRF MIN:%f MAX:%f", &time,&lrfMin,&lrfMax);
00325 if(ret == 3)
00326 {
00327 lrfData lrf;
00328 lrf.time = time;
00329 lrf.lrfMin = lrfMin;
00330 lrf.lrfMax = lrfMax;
00331 LINFO("Got Lrf Data [%f] min[%f] max[%f]",time,lrfMin,lrfMax);
00332 itsLogLrfData.push_back(lrf);
00333 }
00334 }
00335
00336
00337 float sr[4] = {
00338 ((float)segCount[0]/(float)totalGS)*100.0,
00339 ((float)segCount[1]/(float)totalGS)*100.0,
00340 ((float)segCount[2]/(float)totalGS)*100.0,
00341 ((float)segCount[3]/(float)totalGS)*100.0
00342 };
00343 std::string segLine (sformat( "T,%4d,%4d,%4d,%4d,%4d,%5.2f%%,%5.2f%%,%5.2f%%,%5.2f%%\n",
00344 totalGS,segCount[0],segCount[1],segCount[2],segCount[3],sr[0],sr[1],sr[2],sr[3]));
00345
00346
00347
00348 LINFO("Load %s with %d LOC lines ,auto mode %d",subdir[option-1].c_str(),(int)itsLogLocData.size(),frame);
00349 itsLogLocData.clear();
00350
00351
00352
00353
00354 }
00355 #ifndef MANUAL
00356 }
00357 #endif
00358 }
00359
00360 void GistSal_Grapher::updateGUI()
00361 {
00362 std::vector<std::vector<float> > lines;
00363 lines.push_back(itsLogSegData);
00364
00365
00366 std::vector<PixRGB<byte> > linesColor;
00367 linesColor.push_back(PixRGB<byte>(255,0,0));
00368
00369 Image<PixRGB<byte> > plotImage = multilinePlot(
00370
00371
00372 lines,
00373 1600,600,
00374 0.0f,1.0f,
00375 "","","",
00376 linesColor,
00377 PixRGB<byte>(255,255,255),
00378 PixRGB<byte>(0,0,0)
00379
00380 );
00381 if(itsXwin.pressedCloseButton()) exit(0);
00382 itsXwin.drawImage(plotImage);
00383
00384
00385 }
00386
00387
00388
00389
00390