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 #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
00056 {
00057
00058
00059 }
00060
00061
00062 BeoLogger::~BeoLogger()
00063 { }
00064
00065
00066 void BeoLogger::start1()
00067 {
00068 initLogFile();
00069
00070
00071 itsTimer.reset();
00072 }
00073
00074
00075 bool BeoLogger::initLogFile()
00076 {
00077
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
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
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
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
00140 uint64 time = itsTimer.get();
00141
00142
00143 if(eMsg->ice_isA("::BeobotEvents::CameraMessage"))
00144 {
00145
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
00167 else if(eMsg->ice_isA("::BeobotEvents::IMUMessage"))
00168 {
00169
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
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
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
00207 else if(eMsg->ice_isA("::BeobotEvents::LRFMessage"))
00208 {
00209
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
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
00238 else if(eMsg->ice_isA("::BeobotEvents::GPSMessage"))
00239 {
00240
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
00259 else if(eMsg->ice_isA("::BeobotEvents::MotorMessage"))
00260 {
00261
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
00271
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
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
00330
00331
00332