BeoLogger.C

00001 /*!@file Robots2/Beobot2/Hardware/BeoLogger.C Ice Module to log data    */
00002 // //////////////////////////////////////////////////////////////////// //
00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00004 // University of Southern California (USC) and the iLab at USC.         //
00005 // See http://iLab.usc.edu for information about this project.          //
00006 // //////////////////////////////////////////////////////////////////// //
00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00009 // in Visual Environments, and Applications'' by Christof Koch and      //
00010 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00011 // pending; application number 09/912,225 filed July 23, 2001; see      //
00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00013 // //////////////////////////////////////////////////////////////////// //
00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00015 //                                                                      //
00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00017 // redistribute it and/or modify it under the terms of the GNU General  //
00018 // Public License as published by the Free Software Foundation; either  //
00019 // version 2 of the License, or (at your option) any later version.     //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00024 // PURPOSE.  See the GNU General Public License for more details.       //
00025 //                                                                      //
00026 // You should have received a copy of the GNU General Public License    //
00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00029 // Boston, MA 02111-1307 USA.                                           //
00030 // //////////////////////////////////////////////////////////////////// //
00031 //
00032 // Primary maintainer for this file: Christian Siagian <siagian@usc.edu>
00033 // $HeadURL: svn://ilab.usc.edu/trunk/saliency/src/Robots/Beobot2/BeoLogger.C
00034 // $ $Id: BeoLogger.C 12962 2010-03-06 02:13:53Z irock $
00035 //
00036 //////////////////////////////////////////////////////////////////////////
00037 
00038 #include "Robots/Beobot2/BeoLogger.H"
00039 #include "Ice/BeobotEvents.ice.H"
00040 
00041 #include "Raster/Raster.H"
00042 #include "Util/sformat.H"
00043 #include "Image/Image.H"
00044 #include "Ice/IceImageUtils.H"
00045 
00046 #include <sys/stat.h>
00047 
00048 #define  LOG_FOLDER "../data/logs/"
00049 
00050 // ######################################################################
00051 BeoLogger::BeoLogger(OptionManager& mgr,
00052                const std::string& descrName, const std::string& tagName) :
00053   RobotBrainComponent(mgr, descrName, tagName),
00054   itsTimer(1000000)
00055   //  itsOfs(new OutputFrameSeries(mgr))
00056 {
00057   //  addSubComponent(itsOfs);
00058 
00059 }
00060 
00061 // ######################################################################
00062 BeoLogger::~BeoLogger()
00063 { }
00064 
00065 // ######################################################################
00066 void BeoLogger::start1()
00067 {
00068   initLogFile();
00069 
00070   // set start time
00071   itsTimer.reset();
00072 }
00073 
00074 // ######################################################################
00075 bool BeoLogger::initLogFile()
00076 {
00077   // get the time of day
00078   time_t rawtime; struct tm * timeinfo;
00079   time ( &rawtime );
00080   timeinfo = localtime ( &rawtime );
00081   char buffer [80];
00082   strftime (buffer,80,
00083             "%Y_%m_%d__%H_%M_%S",timeinfo);
00084   std::string startTime(buffer);
00085 
00086   itsLogFoldername =
00087     std::string(sformat("%s%s", LOG_FOLDER, startTime.c_str()));
00088   LINFO("logFoldername: %s", itsLogFoldername.c_str());
00089 
00090   // create a log directory
00091   if (mkdir(itsLogFoldername.c_str(), 0777) == -1)
00092     {
00093       LFATAL("Cannot create log folder: %s", itsLogFoldername.c_str());
00094       return(EXIT_FAILURE);
00095     }
00096 
00097   std::string logFilename
00098     (sformat("%s/Log_%s.txt", itsLogFoldername.c_str(), startTime.c_str()));
00099   LINFO("logFilename: %s", itsLogFilename.c_str());
00100 
00101   std::string cTime = std::string("Time of day: ") + startTime;
00102   LINFO("%s", cTime.c_str());
00103   cTime += std::string("\n");
00104 
00105   // save  in a file by appending to the file
00106   itsLogFilename = logFilename;
00107   FILE *rFile = fopen(itsLogFilename.c_str(), "at");
00108   if (rFile != NULL)
00109     {
00110       LDEBUG("saving result to %s", logFilename.c_str());
00111       fputs(cTime.c_str(), rFile);
00112       fclose (rFile);
00113     }
00114   else LFATAL("can't create file: %s", itsLogFilename.c_str());
00115 
00116   return true;
00117 }
00118 
00119 // ######################################################################
00120 void BeoLogger::registerTopics()
00121 {
00122   // subscribe to all sensor data
00123   this->registerSubscription("CameraMessageTopic");
00124   this->registerSubscription("IMUMessageTopic");
00125   this->registerSubscription("LRFMessageTopic");
00126   this->registerSubscription("GPSMessageTopic");
00127   this->registerSubscription("MotorMessageTopic");
00128   this->registerSubscription("CurrentLocationMessageTopic");
00129 }
00130 
00131 // ######################################################################
00132 void BeoLogger::evolve()
00133 { }
00134 
00135 // ######################################################################
00136 void BeoLogger::updateMessage(const RobotSimEvents::EventMessagePtr& eMsg,
00137     const Ice::Current&)
00138 {
00139   // record the time
00140   uint64 time = itsTimer.get();
00141 
00142   // camera message
00143   if(eMsg->ice_isA("::BeobotEvents::CameraMessage"))
00144   {
00145     // store the image
00146     BeobotEvents::CameraMessagePtr cameraMsg =
00147       BeobotEvents::CameraMessagePtr::dynamicCast(eMsg);
00148 
00149     int currRequestID = cameraMsg->RequestID;
00150     Image<PixRGB<byte> > ima = Ice2Image<PixRGB<byte> >(cameraMsg->image);
00151 
00152     LINFO("Got a CameraMessage with Request ID = %d", currRequestID);
00153 
00154     std::string saveFName(sformat("%s/image_%015d.ppm",
00155                                   itsLogFoldername.c_str(), currRequestID));
00156     LINFO("saving: %s",saveFName.c_str());
00157     Raster::WriteRGB(ima, saveFName);
00158 
00159     std::string line =
00160       sformat("[%15.3f] CAM filename: %s", time/1000.0, saveFName.c_str());
00161     line += std::string("\n");
00162 
00163     writeToLogFile(line);
00164   }
00165 
00166   // IMU message
00167   else if(eMsg->ice_isA("::BeobotEvents::IMUMessage"))
00168   {
00169     // store the IMU data
00170     BeobotEvents::IMUMessagePtr imuMsg =
00171       BeobotEvents::IMUMessagePtr::dynamicCast(eMsg);
00172 
00173     int currRequestID = imuMsg->RequestID;
00174     LINFO("Got a IMUMessage with Request ID = %d", currRequestID);
00175 
00176     // get roll, pitch, and yaw
00177     if(imuMsg->validRollPitchYaw)
00178       {
00179         float roll  = imuMsg->roll;
00180         float pitch = imuMsg->pitch;
00181         float yaw   = imuMsg->yaw;
00182 
00183         std::string line =
00184          sformat("[%15.3f] IMU Euler Angle  r:%15.6f p:%15.6f y:%15.6f",
00185                  time/1000.0, roll, pitch, yaw);
00186         line += std::string("\n");
00187 
00188         writeToLogFile(line);
00189       }
00190     // get magnetometer
00191     else if(imuMsg->validMagnetometer)
00192       {
00193         float magX  = imuMsg->magX;
00194         float magY  = imuMsg->magY;
00195         float magZ  = imuMsg->magZ;
00196 
00197         std::string line =
00198          sformat("[%15.3f] IMU Magnetometer  X:%15.6f Y:%15.6f Z:%15.6f",
00199                  time/1000.0, magX, magY, magZ);
00200         line += std::string("\n");
00201 
00202         writeToLogFile(line);
00203       }
00204   }
00205 
00206   // LRF message
00207   else if(eMsg->ice_isA("::BeobotEvents::LRFMessage"))
00208   {
00209     // store the LRF data
00210     BeobotEvents::LRFMessagePtr lrfMsg =
00211       BeobotEvents::LRFMessagePtr::dynamicCast(eMsg);
00212 
00213     int currRequestID = lrfMsg->RequestID;
00214     LINFO("Got a LRFMessage with Request ID = %d", currRequestID);
00215 
00216     std::string line = sformat("[%15.3f] LRF ", time/1000.0);
00217     uint size = lrfMsg->distances.size();
00218                 float lrfMax = 0.0,lrfMin = 100000.0;
00219     for(uint i = 0; i < size; i++)
00220       {
00221         if(i%10 == 0) line += std::string("\n");
00222         line+= sformat("%9.2f ", lrfMsg->distances[i]);
00223         //msg->angles   [i];
00224                                 if(lrfMsg->distances[i] > lrfMax)
00225                                         lrfMax = lrfMsg->distances[i];
00226                                 if(lrfMsg->distances[i] < lrfMin)
00227                                         lrfMin = lrfMsg->distances[i];
00228       }
00229     line += std::string("\n");
00230     line += sformat("[%15.3f] LRF MIN:%9.2f MAX:%9.2f",
00231                     time/1000.0, lrfMin, lrfMax);
00232     line += std::string("\n");
00233 
00234     writeToLogFile(line);
00235   }
00236 
00237   // GPS message
00238   else if(eMsg->ice_isA("::BeobotEvents::GPSMessage"))
00239   {
00240     // store the GPS data
00241     BeobotEvents::GPSMessagePtr gpsMsg =
00242       BeobotEvents::GPSMessagePtr::dynamicCast(eMsg);
00243 
00244     int currRequestID = gpsMsg->RequestID;
00245     LINFO("Got a GPSMessage with Request ID = %d", currRequestID);
00246 
00247     double lat = gpsMsg->latitude;
00248     double lon = gpsMsg->longitude;
00249 
00250     std::string line =
00251       sformat("[%15.3f] GPS lat:%15.6f lon:%15.6f",
00252               time/1000.0, lat, lon);
00253     line += std::string("\n");
00254 
00255     writeToLogFile(line);
00256   }
00257 
00258   //  message
00259   else if(eMsg->ice_isA("::BeobotEvents::MotorMessage"))
00260   {
00261     // store the Motor data
00262     BeobotEvents::MotorMessagePtr mtrMsg =
00263       BeobotEvents::MotorMessagePtr::dynamicCast(eMsg);
00264 
00265     int currRequestID = mtrMsg->RequestID;
00266     LINFO("Got a MotorMessage with Request ID = %d", currRequestID);
00267 
00268     int rcMode = mtrMsg->rcMode;
00269 
00270     //int motor1 = mtrMsg->motor1;
00271     //int motor2 = mtrMsg->motor2;
00272 
00273     double transVel = mtrMsg->transVel;
00274     double rotVel   = mtrMsg->rotVel;
00275 
00276     double encoderX   = mtrMsg->encoderX;
00277     double encoderY   = mtrMsg->encoderY;
00278     double encoderOri = mtrMsg->encoderOri;
00279 
00280     double rcTransVel = mtrMsg->rcTransVel;
00281     double rcRotVel   = mtrMsg->rcRotVel;
00282 
00283     std::string line =
00284       sformat("[%15.3f] MTR rcMode: %d, transVel: %f  rotVel: %f "
00285               "encoderX: %f encoderY: %f encoderOri: %f "
00286               "rcTransVel: %f rcRotVel: %f",
00287               time/1000.0, rcMode, transVel, rotVel,
00288               encoderX,  encoderY, encoderOri,
00289               rcTransVel, rcRotVel);
00290     line += std::string("\n");
00291 
00292     writeToLogFile(line);
00293   }
00294 
00295   // current location message
00296   else if(eMsg->ice_isA("::BeobotEvents::CurrentLocationMessage"))
00297     {
00298       BeobotEvents::CurrentLocationMessagePtr clMsg =
00299         BeobotEvents::CurrentLocationMessagePtr::dynamicCast(eMsg);
00300       LINFO("Got a CurrentLocationMessage with Request ID = %d "
00301             "loc: (%d, %f)",
00302              clMsg->RequestID, clMsg->segNum, clMsg->lenTrav);
00303 
00304       std::string line =
00305         sformat("[%15.3f] LOC seg:%5d ltrav:%15.6f",
00306                 time/1000.0, clMsg->segNum, clMsg->lenTrav);
00307       line += std::string("\n");
00308 
00309       writeToLogFile(line);
00310     }
00311 }
00312 
00313 // ######################################################################
00314 void BeoLogger::writeToLogFile(std::string line)
00315 {
00316   its_logFilename_mutex.lock();
00317   FILE *rFile = fopen(itsLogFilename.c_str(), "at");
00318   if (rFile != NULL)
00319     {
00320       fputs(line.c_str(), rFile);
00321       fclose (rFile);
00322     }
00323   else LFATAL("can't append to file: %s", itsLogFilename.c_str());
00324 
00325   its_logFilename_mutex.unlock();
00326 }
00327 
00328 // ######################################################################
00329 /* So things look consistent in everyone's emacs... */
00330 /* Local Variables: */
00331 /* indent-tabs-mode: nil */
00332 /* End: */
Generated on Sun May 8 08:41:17 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3