Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Examples  

Analog.cpp

Go to the documentation of this file.
00001 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00002  *
00003  * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Christopher Just,
00007  *   Patrick Hartling, Kevin Meinert,
00008  *   Carolina Cruz-Neira, Albert Baker
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Library General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Library General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Library General Public
00021  * License along with this library; if not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023  * Boston, MA 02111-1307, USA.
00024  *
00025  * -----------------------------------------------------------------
00026  * File:          $RCSfile: Analog.cpp,v $
00027  * Date modified: $Date: 2003/11/07 17:28:18 $
00028  * Version:       $Revision: 1.14 $
00029  * -----------------------------------------------------------------
00030  *
00031  *************** <auto-copyright.pl END do not edit this line> ***************/
00032 
00033 #include <gadget/gadgetConfig.h>
00034 
00035 #include <boost/concept_check.hpp>
00036 #include <vpr/IO/ObjectWriter.h>
00037 #include <vpr/IO/ObjectReader.h>
00038 #include <vpr/Util/Debug.h>
00039 #include <gadget/Type/Analog.h>
00040 #include <gadget/Util/DeviceSerializationTokens.h>
00041 
00042 
00043 namespace gadget
00044 {
00045 
00046 Analog::Analog()
00047    : mMin(0.0f), mMax(0.0f)
00048 {}
00049 
00050 Analog::~Analog()
00051 {}
00052 
00053 vpr::ReturnStatus Analog::writeObject(vpr::ObjectWriter* writer)
00054 {
00055    //std::cout << "[Remote Input Manager] In Analog write" << std::endl;
00056 
00058    SampleBuffer_t::buffer_t& stable_buffer = mAnalogSamples.stableBuffer();
00059    writer->beginTag(Analog::getBaseType());
00060    writer->beginAttribute(gadget::tokens::DataTypeAttrib);
00061       writer->writeUint16(MSG_DATA_ANALOG);                                   // Write out the data type so that we can assert if reading in wrong place
00062    writer->endAttribute();
00063 
00064    if ( !stable_buffer.empty() )
00065    {
00066       mAnalogSamples.lock();
00067       writer->beginAttribute(gadget::tokens::SampleBufferLenAttrib);
00068          writer->writeUint16(stable_buffer.size());                           // Write the # of vectors in the stable buffer
00069       writer->endAttribute();
00070       for ( unsigned j=0;j<stable_buffer.size();j++ )                         // For each vector in the stable buffer
00071       {
00072          writer->beginTag(gadget::tokens::BufferSampleTag);
00073          writer->beginAttribute(gadget::tokens::BufferSampleLenAttrib);
00074             writer->writeUint16(stable_buffer[j].size());                     // Write the # of AnalogDatas in the vector
00075          writer->endAttribute();
00076          for ( unsigned i=0;i<stable_buffer[j].size();i++ )                   // For each AnalogData in the vector
00077          {
00078             writer->beginTag(gadget::tokens::AnalogValue);
00079             writer->beginAttribute(gadget::tokens::TimeStamp);
00080                writer->writeUint64(stable_buffer[j][i].getTime().usec());        // Write Time Stamp vpr::Uint64
00081             writer->endAttribute();
00082             writer->writeFloat(stable_buffer[j][i].getAnalog());              // Write Analog Data(int)
00083             writer->endTag();
00084          }
00085          writer->endTag();
00086       }
00087       mAnalogSamples.unlock();
00088    }
00089    else        // No data or request out of range, return default value
00090    {
00091       writer->beginTag(gadget::tokens::BufferSampleTag);
00092          writer->beginAttribute(gadget::tokens::BufferSampleLenAttrib);
00093             writer->writeUint16(0);
00094          writer->endAttribute();
00095       writer->endTag();
00096       vprDEBUG(vprDBG_ALL, vprDBG_WARNING_LVL)
00097          << "Warning: Analog::writeObject: Stable buffer is empty. If this is not the first write, then this is a problem.\n"
00098          << vprDEBUG_FLUSH;
00099    }
00100    writer->endTag();
00101 
00102    return vpr::ReturnStatus::Succeed;
00104 }
00105 
00106 vpr::ReturnStatus Analog::readObject(vpr::ObjectReader* reader)
00107 {
00108    vprASSERT(reader->attribExists("rim.timestamp.delta"));
00109    vpr::Uint64 delta = reader->getAttrib<vpr::Uint64>("rim.timestamp.delta");
00110 
00111    reader->beginTag(Analog::getBaseType());
00112    reader->beginAttribute(gadget::tokens::DataTypeAttrib);
00113       vpr::Uint16 temp = reader->readUint16();
00114    reader->endAttribute();
00115 
00116    // ASSERT if this data is really not Analog Data
00117    // XXX: Should there be error checking for the case when vprASSERT is
00118    // compied out?  -PH 8/21/2003
00119    vprASSERT(temp==MSG_DATA_ANALOG && "[Remote Input Manager] Not Analog Data");
00120    boost::ignore_unused_variable_warning(temp);
00121 
00122    std::vector<AnalogData> dataSample;
00123 
00124    unsigned numAnalogDatas;
00125    float value;
00126    vpr::Uint64 timeStamp;
00127    AnalogData temp_analog_data;
00128    reader->beginAttribute(gadget::tokens::SampleBufferLenAttrib);
00129       unsigned numVectors = reader->readUint16();
00130    reader->endAttribute();
00131 
00132    mAnalogSamples.lock();
00133    for ( unsigned i=0;i<numVectors;i++ )
00134    {
00135       reader->beginTag(gadget::tokens::BufferSampleTag);
00136       reader->beginAttribute(gadget::tokens::BufferSampleLenAttrib);
00137          numAnalogDatas = reader->readUint16();
00138       reader->endAttribute();
00139       //std::cout << "Analog Data Size: "    << numAnalogDatas << std::endl;
00140       dataSample.clear();
00141       //std::cout << "ME: ";
00142       for ( unsigned j=0;j<numAnalogDatas;j++ )
00143       {
00144          reader->beginTag(gadget::tokens::AnalogValue);
00145          reader->beginAttribute(gadget::tokens::TimeStamp);
00146             timeStamp = reader->readUint64();                  // Write Time Stamp vpr::Uint64
00147          reader->endAttribute();
00148          value = reader->readFloat();                       // Write Analog Data(int)
00149          reader->endTag();
00150 
00151          temp_analog_data.setAnalog(value);
00152          temp_analog_data.setTime(vpr::Interval(timeStamp + delta,vpr::Interval::Usec));
00153          dataSample.push_back(temp_analog_data);
00154       }
00155       //std::cout << std::endl;
00156       mAnalogSamples.addSample(dataSample);
00157       reader->endTag();
00158    }
00159    mAnalogSamples.unlock();
00160    swapAnalogBuffers();
00161 
00162    reader->endTag();
00163 
00164    return vpr::ReturnStatus::Succeed;
00165 }
00166 
00167 bool Analog::config(jccl::ConfigElementPtr e)
00168 {
00169    mMin = e->getProperty<float>("min");
00170    mMax = e->getProperty<float>("max");
00171 
00172    vprDEBUG(vprDBG_ALL, vprDBG_VERB_LVL)
00173       << " Analog::config() min:" << mMin
00174       << " max:" << mMax << "\n" << vprDEBUG_FLUSH;
00175 
00176    return true;
00177 }
00178 
00179 // XXX: Add a "sample" filter that does the normalization in here instead
00180 // of in the driver.
00181 AnalogData Analog::getAnalogData(int devNum)
00182 {
00183    SampleBuffer_t::buffer_t& stable_buffer = mAnalogSamples.stableBuffer();
00184 
00185    if ( (!stable_buffer.empty()) &&
00186         (stable_buffer.back().size() > (unsigned)devNum) )  // If Have entry && devNum in range
00187    {
00188       return stable_buffer.back()[devNum];
00189    }
00190    else        // No data or request out of range, return default value
00191    {
00192       if ( stable_buffer.empty() )
00193       {
00194          vprDEBUG(vprDBG_ALL, vprDBG_WARNING_LVL)
00195             << "Warning: Analog::getAnalogData: Stable buffer is empty. If this is not the first read, then this is a problem.\n"
00196             << vprDEBUG_FLUSH;
00197       }
00198       else
00199       {
00200          vprDEBUG(vprDBG_ALL, vprDBG_CONFIG_LVL)
00201             << "Warning: Analog::getAnalogData: Requested devNum is not in the range available.  May have configuration error\n"
00202             << vprDEBUG_FLUSH;
00203       }
00204       return mDefaultValue;
00205    }
00206 }
00207 
00208 void Analog::addAnalogSample(const std::vector<AnalogData>& anaSample)
00209 {
00210    // Locks and then swaps the indices.
00211    mAnalogSamples.lock();
00212    mAnalogSamples.addSample(anaSample);
00213    mAnalogSamples.unlock();
00214 }
00215 
00216 void Analog::swapAnalogBuffers()
00217 {
00218    mAnalogSamples.swapBuffers();
00219 }
00220 
00221 const Analog::SampleBuffer_t::buffer_t&
00222 Analog::getAnalogDataBuffer()
00223 {
00224    return mAnalogSamples.stableBuffer();
00225 }
00226 
00227 std::string Analog::getBaseType()
00228 {
00229    return std::string("Analog");
00230 }
00231 
00232 // Given a value that will range from [min() <= n <= max()].
00233 // This returns a value that is normalized to [0,1]
00234 // if n < mMin or n > mMax, then result = mMin or mMax respectively.
00235 void Analog::normalizeMinToMax(const float& plainJaneValue,
00236                                float& normedFromMinToMax)
00237 {
00238    float value = plainJaneValue;
00239 
00240    // first clamp the value so that min<=value<=max
00241    if ( value < mMin ) value = mMin;
00242    if ( value > mMax ) value = mMax;
00243 
00244    // slide everything to 0.0 (subtract all by mMin)
00245    // Then divide by max to get normalized value
00246    float tmax( mMax - mMin),
00247          tvalue(value - mMin);
00248 
00249    // since [tmin/tmax...tmax/tmax] == [0.0f...1.0f], the normalized value will be value/tmax
00250    normedFromMinToMax = tvalue / tmax;
00251 }
00252 
00253 float Analog::getMin() const
00254 {
00255    return mMin;
00256 }
00257 
00258 float Analog::getMax() const
00259 {
00260    return mMax;
00261 }
00262 
00263 void Analog::setMin(float mIn)
00264 {
00265    mMin = mIn;
00266 }
00267 
00268 void Analog::setMax(float mAx)
00269 {
00270    mMax = mAx;
00271 }
00272 
00273 } // End of gadget namespace

Generated on Sun May 2 14:25:13 2004 for Gadgeteer by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002