GistSal_Grapher.C

00001 /*!@file Robots2/Beobot2/Hardware/Navigation/GistSal_Navigation/GistSal_Grapher.C
00002  * Ice Module display log data in graph   */
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Chin-Kai Chang <chinkaic@usc.edu>
00034 // $HeadURL: svn://ilab.usc.edu/trunk/saliency/src/Robots/Beobot2/Navigation/GistSal_Navigation/GistSal_Grapher.C
00035 // $ $Id: GistSal_Grapher.C 13084 2010-03-30 02:42:00Z kai $
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 //#define  MANUAL
00056 
00057 
00058 #ifdef INDOOR
00059 //#define  LOG_FOLDER "../data/logs/IROS10_HNB"
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   //  itsOfs(new OutputFrameSeries(mgr))
00073 {
00074   //  addSubComponent(itsOfs);
00075 
00076 }
00077 
00078 // ######################################################################
00079 GistSal_Grapher::~GistSal_Grapher()
00080 { }
00081 
00082 // ######################################################################
00083 void GistSal_Grapher::start1()
00084 {
00085 //  initLogFile();
00086 
00087   // set start time
00088   itsTimer.reset();
00089         searchLogFile();
00090 }
00091 
00092 // ######################################################################
00093 bool GistSal_Grapher::initLogFile()
00094 {
00095   // get the time of day
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   // create a log directory
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   // save  in a file by appending to the file
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   // subscribe to all sensor data
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   // record the time
00154   //uint64 time = itsTimer.get();
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                         //LINFO("File[%d]:[%s]",i++,subdirname.c_str());
00188                         if(subdirname.c_str()[0] == 'S')
00189                                 subdir.push_back(subdirname);
00190                 }
00191         }
00192         (void)::closedir(dir_p);
00193 
00194 
00195         //sort filename
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);//FIXX Any better idea?
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 //        LINFO("Your Choose is [%d] filename[%s] ",option,subdir[option-1].c_str());
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                                 // save  in a file by creat a new file
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                                                 //LINFO("Got Loc Data [%f] seg[%d] len[%f]",time,segNum,lenTrav);
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                                                 //LINFO("Got Motor Data %f %d",time,rcMode);
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                                                 //LINFO("%s",csvLine.c_str());
00319                                                 rc = rcMode;
00320                                         }//end if(ret==9)
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                                 }//end while
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                                 //writeToLogFile(segLine);
00346                                 //LINFO("Seg Count: %s",segLine.c_str());
00347                                 //LINFO("Load Log file with %d motor command lines",(int)itsLogMotorData.size());
00348                                 LINFO("Load %s with %d LOC lines ,auto mode %d",subdir[option-1].c_str(),(int)itsLogLocData.size(),frame);
00349                                 itsLogLocData.clear();
00350                                 //LINFO("Load Log file with %d Lrf lines",(int)itsLogLrfData.size());
00351 
00352 
00353 
00354         }//        if(logFile == NULL)
00355 #ifndef MANUAL
00356 }//end for loop
00357 #endif
00358 }
00359 // ######################################################################
00360 void GistSal_Grapher::updateGUI()
00361 {
00362                 std::vector<std::vector<float> > lines;
00363                 lines.push_back(itsLogSegData);
00364 //                lines.push_back(itsVelocityTargetQue.getVector());
00365 
00366                 std::vector<PixRGB<byte> > linesColor;
00367                 linesColor.push_back(PixRGB<byte>(255,0,0));
00368                 //linesColor.push_back(PixRGB<byte>(255,165,0));
00369                 Image<PixRGB<byte> > plotImage = multilinePlot(
00370 
00371 //                                itsVelocityQue.getVector(),
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 /* So things look consistent in everyone's emacs... */
00388 /* Local Variables: */
00389 /* indent-tabs-mode: nil */
00390 /* End: */
Generated on Sun May 8 08:41:19 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3