orca-robotics INTRODUCTION Overview Download and Install Quick Start Documentation Publications REPOSITORY Interfaces Components Libraries Utilities Software Map DEVELOPER Tutorials Examples Dev Guide Dashboard Wiki login/pass: orca/orca PEOPLE Contributors Users Project Download Mailing lists
|
snapshotloggers.h00001 #ifndef ORCALOGFACTORY_SNAPSHOTLOGGERS_H 00002 #define ORCALOGFACTORY_SNAPSHOTLOGGERS_H 00003 00004 #include <IceUtil/Mutex.h> 00005 #include <orcalog/snapshotloggerfactory.h> 00006 #include <orcalog/snapshotlogbuffer.h> 00007 #include <orcalogfactory/logwriters.h> 00008 #include <orcaice/connectutils.h> 00009 00010 namespace orcalogfactory { 00011 00012 // 00013 // A set of SnapshotLoggers: ie loggers which run continuously as soon as they're 00014 // initialised 00015 // 00016 00018 00023 template<class DataType, 00024 class ConsumerType, 00025 class ConsumerPrxType, 00026 class PrxType, 00027 class LogWriterType> 00028 class GenericSnapshotLogger : public ConsumerType, public orcalog::SnapshotLogger 00029 { 00030 public: 00031 00032 GenericSnapshotLogger() 00033 : isTakingSnapshot_(false) 00034 {} 00035 virtual ~GenericSnapshotLogger() {} 00036 00037 // Assumes the ConsumerType's method is called setData 00038 virtual void setData(const DataType& data, const Ice::Current&) 00039 { 00040 { 00041 // Don't accept new data while taking a snapshot. 00042 IceUtil::Mutex::Lock lock(mutex_); 00043 if ( isTakingSnapshot_ ) return; 00044 } 00045 snapshotLogBuffer_->addItem( data ); 00046 } 00047 00048 // Inherited from orcalog::SnapshotLogger 00049 virtual void init( const std::string &format, double timeWindowSec ) 00050 { 00051 LogWriterType dummyLogWriter; 00052 dummyLogWriter.checkFormat( format ); 00053 snapshotLogBuffer_.reset( new orcalog::SnapshotLogBuffer<DataType, 00054 LogWriterType>( timeWindowSec ) ); 00055 } 00056 00057 virtual void subscribe( orcaice::Context &context, const std::string &interfaceTag ) 00058 { 00059 // may throw 00060 PrxType objectPrx; 00061 orcaice::connectToInterfaceWithTag<PrxType>( context, 00062 objectPrx, 00063 interfaceTag ); 00064 00065 Ice::ObjectPtr consumer = this; 00066 ConsumerPrxType callbackPrx = 00067 orcaice::createConsumerInterface<ConsumerPrxType>( context, 00068 consumer ); 00069 00070 objectPrx->subscribe( callbackPrx ); 00071 } 00072 00073 virtual void prepareForSnapshot( const orcalog::LogWriterInfo &logWriterInfo, 00074 orcalog::MasterFileWriter &masterFileWriter ) 00075 { 00076 { 00077 IceUtil::Mutex::Lock lock(mutex_); 00078 isTakingSnapshot_ = true; 00079 } 00080 logWriter_.reset( new LogWriterType ); 00081 logWriter_->init( logWriterInfo, masterFileWriter ); 00082 00083 // Allow interface-specific stuff to happen (eg getting description) 00084 doSpecialisedSnapshotPreparation( *logWriter_ ); 00085 } 00086 00087 virtual void finaliseSnapshot() 00088 { 00089 { 00090 IceUtil::Mutex::Lock lock(mutex_); 00091 isTakingSnapshot_ = false; 00092 } 00093 } 00094 00095 virtual uint snapshotBufferSize() const 00096 { return snapshotLogBuffer_->bufferSize(); } 00097 virtual const orca::Time &oldestArrivalTime() const 00098 { return snapshotLogBuffer_->oldestArrivalTime(); } 00099 virtual void writeOldestObjToLogAndDiscard() 00100 { snapshotLogBuffer_->writeOldestObjToLogAndDiscard(*logWriter_); } 00101 00102 protected: 00103 00104 // Called during initialisation 00105 virtual void doSpecialisedSnapshotPreparation( LogWriterType &logWriter ) {} 00106 00107 private: 00108 00109 bool isTakingSnapshot_; 00110 std::auto_ptr< orcalog::SnapshotLogBuffer<DataType,LogWriterType> > snapshotLogBuffer_; 00111 std::auto_ptr<LogWriterType> logWriter_; 00112 IceUtil::Mutex mutex_; 00113 }; 00114 00116 00117 typedef GenericSnapshotLogger<orca::CpuData, 00118 orca::CpuConsumer, 00119 orca::CpuConsumerPrx, 00120 orca::CpuPrx, 00121 CpuLogWriter> CpuSnapshotLogger; 00122 00124 00125 class DriveBicycleSnapshotLogger : public GenericSnapshotLogger<orca::DriveBicycleData, 00126 orca::DriveBicycleConsumer, 00127 orca::DriveBicycleConsumerPrx, 00128 orca::DriveBicyclePrx, 00129 DriveBicycleLogWriter> 00130 { 00131 private: 00132 void doSpecialisedSnapshotPreparation( DriveBicycleLogWriter &logWriter ) 00133 { 00134 // may throw 00135 orca::DriveBicyclePrx objectPrx; 00136 orcaice::connectToInterfaceWithTag( logWriter.logWriterInfo().context, 00137 objectPrx, 00138 logWriter.logWriterInfo().interfaceTag ); 00139 logWriter.write( objectPrx->getDescription() ); 00140 } 00141 }; 00142 00144 00145 typedef GenericSnapshotLogger<orca::ImuData, 00146 orca::ImuConsumer, 00147 orca::ImuConsumerPrx, 00148 orca::ImuPrx, 00149 ImuLogWriter> ImuSnapshotLogger; 00150 00152 00153 class LaserScanner2dSnapshotLogger : public orca::RangeScanner2dConsumer, public orcalog::SnapshotLogger 00154 { 00155 public: 00156 00157 LaserScanner2dSnapshotLogger() 00158 : isTakingSnapshot_(false) 00159 {} 00160 virtual ~LaserScanner2dSnapshotLogger() {} 00161 00162 virtual void setData(const orca::RangeScanner2dDataPtr& data, const Ice::Current &c) 00163 { 00164 { 00165 // Don't accept new data while taking a snapshot. 00166 IceUtil::Mutex::Lock lock(mutex_); 00167 if ( isTakingSnapshot_ ) return; 00168 } 00169 // special case: do not copy to other loggers! 00170 // we assume that the data is really LaserScanner2dDataPtr but it has to be cast 00171 // @todo: what if it's not the right type? 00172 orca::LaserScanner2dDataPtr laserData = orca::LaserScanner2dDataPtr::dynamicCast( data ); 00173 snapshotLogBuffer_->addItem( laserData ); 00174 } 00175 00176 virtual void init( const std::string &format, double timeWindowSec ) 00177 { 00178 LaserScanner2dLogWriter dummyLogWriter; 00179 dummyLogWriter.checkFormat( format ); 00180 snapshotLogBuffer_.reset( new orcalog::SnapshotLogBuffer<orca::LaserScanner2dDataPtr, 00181 LaserScanner2dLogWriter>( timeWindowSec ) ); 00182 } 00183 00184 virtual void subscribe( orcaice::Context &context, const std::string &interfaceTag ) 00185 { 00186 orca::LaserScanner2dPrx objectPrx; 00187 orcaice::connectToInterfaceWithTag( context, 00188 objectPrx, 00189 interfaceTag ); 00190 00191 Ice::ObjectPtr consumer = this; 00192 orca::RangeScanner2dConsumerPrx callbackPrx = 00193 orcaice::createConsumerInterface<orca::RangeScanner2dConsumerPrx>( context, 00194 consumer ); 00195 00196 objectPrx->subscribe( callbackPrx ); 00197 } 00198 00199 virtual void prepareForSnapshot( const orcalog::LogWriterInfo &logWriterInfo, 00200 orcalog::MasterFileWriter &masterFileWriter ) 00201 { 00202 { 00203 IceUtil::Mutex::Lock lock(mutex_); 00204 isTakingSnapshot_ = true; 00205 } 00206 logWriter_.reset( new LaserScanner2dLogWriter ); 00207 logWriter_->init( logWriterInfo, masterFileWriter ); 00208 00209 // Allow interface-specific stuff to happen (eg getting description) 00210 // doSpecialisedSnapshotPreparation( *logWriter_ ); 00211 // may throw 00212 orca::LaserScanner2dPrx objectPrx; 00213 orcaice::connectToInterfaceWithTag( logWriter_->logWriterInfo().context, 00214 objectPrx, 00215 logWriter_->logWriterInfo().interfaceTag ); 00216 logWriter_->write( objectPrx->getDescription() ); 00217 } 00218 virtual void finaliseSnapshot() 00219 { 00220 { 00221 IceUtil::Mutex::Lock lock(mutex_); 00222 isTakingSnapshot_ = false; 00223 } 00224 } 00225 virtual uint snapshotBufferSize() const 00226 { return snapshotLogBuffer_->bufferSize(); } 00227 virtual const orca::Time &oldestArrivalTime() const 00228 { return snapshotLogBuffer_->oldestArrivalTime(); } 00229 virtual void writeOldestObjToLogAndDiscard() 00230 { snapshotLogBuffer_->writeOldestObjToLogAndDiscard(*logWriter_); } 00231 00232 private: 00233 00234 bool isTakingSnapshot_; 00235 std::auto_ptr< orcalog::SnapshotLogBuffer<orca::LaserScanner2dDataPtr, 00236 LaserScanner2dLogWriter> > snapshotLogBuffer_; 00237 std::auto_ptr<LaserScanner2dLogWriter> logWriter_; 00238 IceUtil::Mutex mutex_; 00239 00240 }; 00241 00243 00244 class Localise2dSnapshotLogger : public GenericSnapshotLogger<orca::Localise2dData, 00245 orca::Localise2dConsumer, 00246 orca::Localise2dConsumerPrx, 00247 orca::Localise2dPrx, 00248 Localise2dLogWriter> 00249 { 00250 private: 00251 void doSpecialisedSnapshotPreparation( Localise2dLogWriter &logWriter ) 00252 { 00253 // may throw 00254 orca::Localise2dPrx objectPrx; 00255 orcaice::connectToInterfaceWithTag( logWriter.logWriterInfo().context, 00256 objectPrx, 00257 logWriter.logWriterInfo().interfaceTag ); 00258 logWriter.write( objectPrx->getVehicleGeometry() ); 00259 } 00260 }; 00261 00263 00264 class Localise3dSnapshotLogger : public GenericSnapshotLogger<orca::Localise3dData, 00265 orca::Localise3dConsumer, 00266 orca::Localise3dConsumerPrx, 00267 orca::Localise3dPrx, 00268 Localise3dLogWriter> 00269 { 00270 private: 00271 void doSpecialisedSnapshotPreparation( Localise3dLogWriter &logWriter ) 00272 { 00273 // may throw 00274 orca::Localise3dPrx objectPrx; 00275 orcaice::connectToInterfaceWithTag( logWriter.logWriterInfo().context, 00276 objectPrx, 00277 logWriter.logWriterInfo().interfaceTag ); 00278 logWriter.write( objectPrx->getVehicleGeometry() ); 00279 } 00280 }; 00281 00283 00284 class Odometry2dSnapshotLogger : public GenericSnapshotLogger<orca::Odometry2dData, 00285 orca::Odometry2dConsumer, 00286 orca::Odometry2dConsumerPrx, 00287 orca::Odometry2dPrx, 00288 Odometry2dLogWriter> 00289 { 00290 private: 00291 void doSpecialisedSnapshotPreparation( Odometry2dLogWriter &logWriter ) 00292 { 00293 // may throw 00294 orca::Odometry2dPrx objectPrx; 00295 orcaice::connectToInterfaceWithTag( logWriter.logWriterInfo().context, 00296 objectPrx, 00297 logWriter.logWriterInfo().interfaceTag ); 00298 logWriter.write( objectPrx->getDescription() ); 00299 } 00300 }; 00301 00303 00304 class Odometry3dSnapshotLogger : public GenericSnapshotLogger<orca::Odometry3dData, 00305 orca::Odometry3dConsumer, 00306 orca::Odometry3dConsumerPrx, 00307 orca::Odometry3dPrx, 00308 Odometry3dLogWriter> 00309 { 00310 private: 00311 void doSpecialisedSnapshotPreparation( Odometry3dLogWriter &logWriter ) 00312 { 00313 // may throw 00314 orca::Odometry3dPrx objectPrx; 00315 orcaice::connectToInterfaceWithTag( logWriter.logWriterInfo().context, 00316 objectPrx, 00317 logWriter.logWriterInfo().interfaceTag ); 00318 logWriter.write( objectPrx->getDescription() ); 00319 } 00320 }; 00321 00323 00324 typedef GenericSnapshotLogger<orca::PolarFeature2dData, 00325 orca::PolarFeature2dConsumer, 00326 orca::PolarFeature2dConsumerPrx, 00327 orca::PolarFeature2dPrx, 00328 PolarFeature2dLogWriter> PolarFeature2dSnapshotLogger; 00329 00331 00332 typedef GenericSnapshotLogger<orca::PowerData, 00333 orca::PowerConsumer, 00334 orca::PowerConsumerPrx, 00335 orca::PowerPrx, 00336 PowerLogWriter> PowerSnapshotLogger; 00337 00339 00340 typedef GenericSnapshotLogger<orca::WifiData, 00341 orca::WifiConsumer, 00342 orca::WifiConsumerPrx, 00343 orca::WifiPrx, 00344 WifiLogWriter> WifiSnapshotLogger; 00345 00347 00348 class GpsSnapshotLogger : public GenericSnapshotLogger<orca::GpsData, 00349 orca::GpsConsumer, 00350 orca::GpsConsumerPrx, 00351 orca::GpsPrx, 00352 GpsLogWriter> 00353 { 00354 private: 00355 void doSpecialisedSnapshotPreparation( GpsLogWriter &logWriter ) 00356 { 00357 // may throw 00358 orca::GpsPrx objectPrx; 00359 orcaice::connectToInterfaceWithTag( logWriter.logWriterInfo().context, 00360 objectPrx, 00361 logWriter.logWriterInfo().interfaceTag ); 00362 logWriter.write( objectPrx->getDescription() ); 00363 } 00364 }; 00365 00366 } 00367 00368 #endif |
Webmaster: Tobias Kaupp (tobasco at users.sourceforge.net)