Libbarrett  1.2.4
include/barrett/systems/detail/periodic_data_logger-inl.h
00001 /*
00002         Copyright 2010, 2011, 2012 Barrett Technology <support@barrett.com>
00003 
00004         This file is part of libbarrett.
00005 
00006         This version of libbarrett is free software: you can redistribute it
00007         and/or modify it under the terms of the GNU General Public License as
00008         published by the Free Software Foundation, either version 3 of the
00009         License, or (at your option) any later version.
00010 
00011         This version of libbarrett is distributed in the hope that it will be
00012         useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00013         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014         GNU General Public License for more details.
00015 
00016         You should have received a copy of the GNU General Public License along
00017         with this version of libbarrett.  If not, see
00018         <http://www.gnu.org/licenses/>.
00019 
00020         Further, non-binding information about licensing is available at:
00021         <http://wiki.barrett.com/libbarrett/wiki/LicenseNotes>
00022 */
00023 
00024 /*
00025  * periodic_data_logger-inl.h
00026  *
00027  *  Created on: Jan 6, 2010
00028  *      Author: dc
00029  */
00030 
00031 
00032 #include <boost/thread.hpp>
00033 #include <barrett/thread/abstract/mutex.h>
00034 
00035 
00036 namespace barrett {
00037 namespace systems {
00038 
00039 
00040 template<typename T, typename LogWriterType>
00041 PeriodicDataLogger<T, LogWriterType>::PeriodicDataLogger(ExecutionManager* em,
00042                 LogWriterType* logWriter, size_t periodMultiplier, const std::string& sysName) :
00043         System(sysName), SingleInput<T>(this),
00044         lw(logWriter), logging(true),
00045         ecCount(0), ecCountRollover(periodMultiplier)
00046 {
00047         if (em != NULL) {
00048                 em->startManaging(*this);
00049         }
00050 }
00051 
00052 template<typename T, typename LogWriterType>
00053 PeriodicDataLogger<T, LogWriterType>::~PeriodicDataLogger() {
00054         mandatoryCleanUp();
00055 
00056         if (isLogging()) {
00057                 closeLog();
00058         }
00059 }
00060 
00061 template<typename T, typename LogWriterType>
00062 inline bool PeriodicDataLogger<T, LogWriterType>::isLogging() {
00063         return logging;
00064 }
00065 
00066 template<typename T, typename LogWriterType>
00067 void PeriodicDataLogger<T, LogWriterType>::closeLog() {
00068         if (isLogging()) {
00069                 // One of the functions below is an interruption point. We must disable
00070                 // interruption (for the duration of this scope) so that a
00071                 // boost::thread_interrupted exception doesn't get thrown.
00072                 boost::this_thread::disable_interruption di;
00073 
00074                 thread::Mutex& emMutex = getEmMutex();
00075                 emMutex.lock();
00076                 logging = false;
00077                 emMutex.unlock();
00078 
00079                 // lw::close() is probably not real-time safe, so keep it out of the critical section.
00080                 lw->close();
00081                 delete lw;
00082                 lw = NULL;
00083         }
00084 }
00085 
00086 template<typename T, typename LogWriterType>
00087 inline bool PeriodicDataLogger<T, LogWriterType>::inputsValid() {
00088         ecCount = (ecCount + 1) % ecCountRollover;
00089         return logging  &&  ecCount == 0  &&  this->input.valueDefined();
00090 }
00091 
00092 template<typename T, typename LogWriterType>
00093 inline void PeriodicDataLogger<T, LogWriterType>::operate() {
00094         lw->putRecord(this->input.getValue());
00095 }
00096 
00097 
00098 }
00099 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Defines