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

gadget::Analog Class Reference

Analog is the abstract base class that devices with Analog data derive from. More...

#include <Analog.h>

Inheritance diagram for gadget::Analog:

Inheritance graph
[legend]
Collaboration diagram for gadget::Analog:

Collaboration graph
[legend]
List of all members.

Public Types

typedef gadget::SampleBuffer<
AnalogData
SampleBuffer_t

Public Methods

 Analog ()
 Constructor. More...

virtual ~Analog ()
virtual vpr::ReturnStatus writeObject (vpr::ObjectWriter *writer)
virtual vpr::ReturnStatus readObject (vpr::ObjectReader *reader)
virtual bool config (jccl::ConfigElementPtr e)
 Just call base class config. More...

AnalogData getAnalogData (int devNum=0)
 Returns "analog data". More...

void addAnalogSample (const std::vector< AnalogData > &anaSample)
 Helper method to add a sample to the sample buffers. More...

void swapAnalogBuffers ()
 Swaps the analog data buffers. More...

const SampleBuffer_t::buffer_t & getAnalogDataBuffer ()
virtual std::string getBaseType ()

Protected Methods

void normalizeMinToMax (const float &plainJaneValue, float &normedFromMinToMax)
 Given a value that will range from [min() <= n <= max()]. More...

float getMin () const
 Gets the minimum "real" data value (real == from hardware). More...

float getMax () const
void setMin (float mIn)
void setMax (float mAx)
 Analog (const gadget::Analog &d)
void operator= (const gadget::Analog &d)

Detailed Description

Analog is the abstract base class that devices with Analog data derive from.

Analog is the base class that analog devices must derive from. Analog inherits from Input, so it has pure virtual function constraints from Input in the following functions: StartSampling, StopSampling, Sample, and UpdateData.

Analog adds one new pure virtual function, getAnalogData() for retreiving the analog data, similar to the type specific get data methods in gadget::Position and gadget::Digital.

Definition at line 64 of file Analog.h.


Member Typedef Documentation

typedef gadget::SampleBuffer<AnalogData> gadget::Analog::SampleBuffer_t
 

Definition at line 67 of file Analog.h.


Constructor & Destructor Documentation

gadget::Analog::Analog  
 

Constructor.

Postcondition:
Set device abilities.
Note:
Must be called from all derived classes.

Definition at line 46 of file Analog.cpp.

00047    : mMin(0.0f), mMax(0.0f)
00048 {}

gadget::Analog::~Analog   [virtual]
 

Definition at line 50 of file Analog.cpp.

00051 {}

gadget::Analog::Analog const gadget::Analog &    d [inline, protected]
 

Definition at line 154 of file Analog.h.

00154 : vpr::SerializableObject() {;}


Member Function Documentation

vpr::ReturnStatus gadget::Analog::writeObject vpr::ObjectWriter *    writer [virtual]
 

Reimplemented in gadget::InputMixer< InputMixer< SimInput, Input >, Analog >.

Definition at line 53 of file Analog.cpp.

References gadget::SampleBuffer< AnalogData >::lock, gadget::MSG_DATA_ANALOG, gadget::SampleBuffer< AnalogData >::stableBuffer, and gadget::SampleBuffer< AnalogData >::unlock.

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 }

vpr::ReturnStatus gadget::Analog::readObject vpr::ObjectReader *    reader [virtual]
 

Reimplemented in gadget::InputMixer< InputMixer< SimInput, Input >, Analog >.

Definition at line 106 of file Analog.cpp.

References gadget::SampleBuffer< AnalogData >::addSample, gadget::SampleBuffer< AnalogData >::lock, gadget::MSG_DATA_ANALOG, swapAnalogBuffers, and gadget::SampleBuffer< AnalogData >::unlock.

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 }

bool gadget::Analog::config jccl::ConfigElementPtr    e [virtual]
 

Just call base class config.

Note:
Let constructor set device abilities.

Reimplemented in gadget::SimAnalog.

Definition at line 167 of file Analog.cpp.

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 }

AnalogData gadget::Analog::getAnalogData int    devNum = 0
 

Returns "analog data".

Precondition:
Give the device number you wish to access.
Postcondition:
Returns a value that ranges from 0.0f to 1.0f.
Parameters:
devNum  Device unit number to access
Note:
For example, if you are sampling a potentiometer, and it returns reading from 0, 255. This function will normalize those values (using Analog::normalizeMinToMax()). For another example, if your potentiometer's turn radius is limited mechanically to return, say, the values 176 to 200 (yes this is really low res), this function will still return 0.0f to 1.0f.
To specify these min/max values, you must set in your Analog (or analog device) config file the field "min" and "max". By default (if these values do not appear), "min" and "max" are set to 0.0f and 1.0f respectivly.
TO ALL ANALOG DEVICE DRIVER WRITERS, you *must* normalize your data using Analog::normalizeMinToMax().

Definition at line 181 of file Analog.cpp.

References gadget::SampleBuffer< AnalogData >::stableBuffer.

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 }

void gadget::Analog::addAnalogSample const std::vector< AnalogData > &    anaSample
 

Helper method to add a sample to the sample buffers.

This MUST be called by all analog devices to add a new sample. The data samples passed in will then be modified by any local filters.

Postcondition:
Sample is added to the buffers and the local filters are run on that sample.

Definition at line 208 of file Analog.cpp.

References gadget::SampleBuffer< AnalogData >::addSample, gadget::SampleBuffer< AnalogData >::lock, and gadget::SampleBuffer< AnalogData >::unlock.

Referenced by gadget::SimAnalog::updateData.

00209 {
00210    // Locks and then swaps the indices.
00211    mAnalogSamples.lock();
00212    mAnalogSamples.addSample(anaSample);
00213    mAnalogSamples.unlock();
00214 }

void gadget::Analog::swapAnalogBuffers  
 

Swaps the analog data buffers.

Postcondition:
If ready has values, then copy values from ready to stable. If not, then stable keeps its old values.

Definition at line 216 of file Analog.cpp.

References gadget::SampleBuffer< AnalogData >::swapBuffers.

Referenced by readObject, and gadget::SimAnalog::updateData.

00217 {
00218    mAnalogSamples.swapBuffers();
00219 }

const Analog::SampleBuffer_t::buffer_t & gadget::Analog::getAnalogDataBuffer  
 

Definition at line 222 of file Analog.cpp.

References gadget::SampleBuffer< AnalogData >::stableBuffer.

00223 {
00224    return mAnalogSamples.stableBuffer();
00225 }

std::string gadget::Analog::getBaseType   [virtual]
 

Reimplemented in gadget::InputMixer< InputMixer< SimInput, Input >, Analog >.

Definition at line 227 of file Analog.cpp.

00228 {
00229    return std::string("Analog");
00230 }

void gadget::Analog::normalizeMinToMax const float &    plainJaneValue,
float &    normedFromMinToMax
[protected]
 

Given a value that will range from [min() <= n <= max()].

This returns a value that is normalized to [0,1]

Definition at line 235 of file Analog.cpp.

Referenced by gadget::SimAnalog::updateData.

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 }

float gadget::Analog::getMin   const [protected]
 

Gets the minimum "real" data value (real == from hardware).

This value is used to normalize the return value of getAnalogData.

Note:
this function is not needed by an application author.

Definition at line 253 of file Analog.cpp.

Referenced by gadget::SimAnalog::updateData.

00254 {
00255    return mMin;
00256 }

float gadget::Analog::getMax   const [protected]
 

Definition at line 258 of file Analog.cpp.

Referenced by gadget::SimAnalog::updateData.

00259 {
00260    return mMax;
00261 }

void gadget::Analog::setMin float    mIn [protected]
 

Definition at line 263 of file Analog.cpp.

00264 {
00265    mMin = mIn;
00266 }

void gadget::Analog::setMax float    mAx [protected]
 

Definition at line 268 of file Analog.cpp.

00269 {
00270    mMax = mAx;
00271 }

void gadget::Analog::operator= const gadget::Analog &    d [inline, protected]
 

Definition at line 155 of file Analog.h.

00155 {;}


The documentation for this class was generated from the following files:
Generated on Sun May 2 14:26:53 2004 for Gadgeteer by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002