gadget::Analog Class Reference

Analog is the abstract base class from which devices returning analog data must derive (through the use of gadget::InputMixer). More...

#include <gadget/Type/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 Member Functions

 Analog ()
 Constructor.
virtual ~Analog ()
virtual vpr::ReturnStatus writeObject (vpr::ObjectWriter *writer)
 Serializes this object.
virtual vpr::ReturnStatus readObject (vpr::ObjectReader *reader)
 De-serializes this object.
virtual bool config (jccl::ConfigElementPtr element)
 Reads the minimum and maximum value configuration information for this analog device.
AnalogData getAnalogData (int devNum=0)
 Returns analog data.
void addAnalogSample (const std::vector< AnalogData > &anaSample)
 Helper method to add a collection of analog samples to the sample buffers.
void swapAnalogBuffers ()
 Swaps the analog data buffers.
const SampleBuffer_t::buffer_tgetAnalogDataBuffer ()
 Returns the current stable sample buffers for this device.
virtual std::string getInputTypeName ()

Protected Member Functions

void normalizeMinToMax (const float &plainJaneValue, float &normedFromMinToMax)
 Given a value that will range from [min() <= n <= max()].
float getMin () const
 Gets the minimum "real" data value (real == from hardware).
float getMax () const
void setMin (float mIn)
void setMax (float mAx)
 Analog (const gadget::Analog &d)
void operator= (const gadget::Analog &)

Detailed Description

Analog is the abstract base class from which devices returning analog data must derive (through the use of gadget::InputMixer).

This is in addition to gadget::Input. gadget::Input provides pure virtual function constraints in the following functions: startSampling(), stopSampling(), sample(), and updateData().

gadget::Analog adds the function getAnalogData() for retreiving the received analog data. This is similar to the additions made by gadget::Position and gadget::Digital.

See also:
Input, InputMixer

Definition at line 65 of file Analog.h.


Member Typedef Documentation

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

Definition at line 68 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)
00048    , mMax(0.0f)
00049 {
00050    /* Do nothing. */ ;
00051 }

gadget::Analog::~Analog (  )  [virtual]

Definition at line 53 of file Analog.cpp.

00054 {
00055    /* Do nothing. */ ;
00056 }

gadget::Analog::Analog ( const gadget::Analog d  )  [inline, protected]

Definition at line 170 of file Analog.h.

00171       : vpr::SerializableObject(d)
00172    {;}


Member Function Documentation

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

Serializes this object.

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

Definition at line 58 of file Analog.cpp.

References gadget::tokens::AnalogValue(), gadget::tokens::BufferSampleLenAttrib(), gadget::tokens::BufferSampleTag(), gadget::tokens::DataTypeAttrib(), getInputTypeName(), gadget::SampleBuffer< DATA_TYPE, MAX_BUFFER_SIZE >::lock(), gadget::MSG_DATA_ANALOG, gadget::tokens::SampleBufferLenAttrib(), gadget::SampleBuffer< DATA_TYPE, MAX_BUFFER_SIZE >::stableBuffer(), gadget::tokens::TimeStamp(), and gadget::SampleBuffer< DATA_TYPE, MAX_BUFFER_SIZE >::unlock().

00059 {
00060    //std::cout << "[Remote Input Manager] In Analog write" << std::endl;
00061 
00063    SampleBuffer_t::buffer_t& stable_buffer = mAnalogSamples.stableBuffer();
00064    writer->beginTag(Analog::getInputTypeName());
00065    writer->beginAttribute(gadget::tokens::DataTypeAttrib);
00066       writer->writeUint16(MSG_DATA_ANALOG);                                   // Write out the data type so that we can assert if reading in wrong place
00067    writer->endAttribute();
00068 
00069    if ( !stable_buffer.empty() )
00070    {
00071       mAnalogSamples.lock();
00072       writer->beginAttribute(gadget::tokens::SampleBufferLenAttrib);
00073          writer->writeUint16(stable_buffer.size());                           // Write the # of vectors in the stable buffer
00074       writer->endAttribute();
00075       for ( unsigned j = 0; j < stable_buffer.size(); ++j )                   // For each vector in the stable buffer
00076       {
00077          writer->beginTag(gadget::tokens::BufferSampleTag);
00078          writer->beginAttribute(gadget::tokens::BufferSampleLenAttrib);
00079             writer->writeUint16(stable_buffer[j].size());                     // Write the # of AnalogDatas in the vector
00080          writer->endAttribute();
00081          for ( unsigned i = 0;i < stable_buffer[j].size(); ++i )              // For each AnalogData in the vector
00082          {
00083             writer->beginTag(gadget::tokens::AnalogValue);
00084             writer->beginAttribute(gadget::tokens::TimeStamp);
00085                writer->writeUint64(stable_buffer[j][i].getTime().usec());        // Write Time Stamp vpr::Uint64
00086             writer->endAttribute();
00087             writer->writeFloat(stable_buffer[j][i].getAnalog());              // Write Analog Data(int)
00088             writer->endTag();
00089          }
00090          writer->endTag();
00091       }
00092       mAnalogSamples.unlock();
00093    }
00094    else        // No data or request out of range, return default value
00095    {
00096       writer->beginTag(gadget::tokens::BufferSampleTag);
00097          writer->beginAttribute(gadget::tokens::BufferSampleLenAttrib);
00098             writer->writeUint16(0);
00099          writer->endAttribute();
00100       writer->endTag();
00101       vprDEBUG(vprDBG_ALL, vprDBG_WARNING_LVL)
00102          << "WARNING: [gadget::Analog::writeObject()] Stable buffer is empty.  "
00103          << "If this is not the first write, then this is a problem.\n"
00104          << vprDEBUG_FLUSH;
00105    }
00106    writer->endTag();
00107 
00108    return vpr::ReturnStatus::Succeed;
00110 }

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

De-serializes this object.

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

Definition at line 112 of file Analog.cpp.

References gadget::SampleBuffer< DATA_TYPE, MAX_BUFFER_SIZE >::addSample(), gadget::tokens::AnalogValue(), gadget::tokens::BufferSampleLenAttrib(), gadget::tokens::BufferSampleTag(), gadget::tokens::DataTypeAttrib(), getInputTypeName(), gadget::SampleBuffer< DATA_TYPE, MAX_BUFFER_SIZE >::lock(), gadget::MSG_DATA_ANALOG, gadget::tokens::SampleBufferLenAttrib(), gadget::AnalogData::setAnalog(), gadget::InputData::setTime(), swapAnalogBuffers(), gadget::tokens::TimeStamp(), and gadget::SampleBuffer< DATA_TYPE, MAX_BUFFER_SIZE >::unlock().

00113 {
00114    vprASSERT(reader->attribExists("rim.timestamp.delta"));
00115    vpr::Uint64 delta = reader->getAttrib<vpr::Uint64>("rim.timestamp.delta");
00116 
00117    reader->beginTag(Analog::getInputTypeName());
00118    reader->beginAttribute(gadget::tokens::DataTypeAttrib);
00119       vpr::Uint16 temp = reader->readUint16();
00120    reader->endAttribute();
00121 
00122    // ASSERT if this data is really not Analog Data
00123    // XXX: Should there be error checking for the case when vprASSERT is
00124    // compied out?  -PH 8/21/2003
00125    vprASSERT(temp==MSG_DATA_ANALOG && "[Remote Input Manager] Not Analog Data");
00126    boost::ignore_unused_variable_warning(temp);
00127 
00128    std::vector<AnalogData> dataSample;
00129 
00130    unsigned numAnalogDatas;
00131    float value;
00132    vpr::Uint64 timeStamp;
00133    AnalogData temp_analog_data;
00134    reader->beginAttribute(gadget::tokens::SampleBufferLenAttrib);
00135       unsigned numVectors = reader->readUint16();
00136    reader->endAttribute();
00137 
00138    mAnalogSamples.lock();
00139    for ( unsigned i = 0; i < numVectors; ++i )
00140    {
00141       reader->beginTag(gadget::tokens::BufferSampleTag);
00142       reader->beginAttribute(gadget::tokens::BufferSampleLenAttrib);
00143          numAnalogDatas = reader->readUint16();
00144       reader->endAttribute();
00145 
00146       dataSample.clear();
00147 
00148       for ( unsigned j = 0; j < numAnalogDatas; ++j )
00149       {
00150          reader->beginTag(gadget::tokens::AnalogValue);
00151          reader->beginAttribute(gadget::tokens::TimeStamp);
00152             timeStamp = reader->readUint64();                  // Write Time Stamp vpr::Uint64
00153          reader->endAttribute();
00154          value = reader->readFloat();                       // Write Analog Data(int)
00155          reader->endTag();
00156 
00157          temp_analog_data.setAnalog(value);
00158          temp_analog_data.setTime(vpr::Interval(timeStamp + delta,vpr::Interval::Usec));
00159          dataSample.push_back(temp_analog_data);
00160       }
00161 
00162       mAnalogSamples.addSample(dataSample);
00163       reader->endTag();
00164    }
00165    mAnalogSamples.unlock();
00166    swapAnalogBuffers();
00167 
00168    reader->endTag();
00169 
00170    return vpr::ReturnStatus::Succeed;
00171 }

bool gadget::Analog::config ( jccl::ConfigElementPtr  element  )  [virtual]

Reads the minimum and maximum value configuration information for this analog device.

Postcondition:
mMin and mMax are set to the values contained in the given config element.
Parameters:
element The config element for an analog device. It must derive from the base config element type 'analog'.

Definition at line 173 of file Analog.cpp.

Referenced by gadget::SimAnalog::config().

00174 {
00175    mMin = element->getProperty<float>("min");
00176    mMax = element->getProperty<float>("max");
00177 
00178    vprDEBUG(vprDBG_ALL, vprDBG_VERB_LVL)
00179       << "[gadget::Analog::config()] min:" << mMin
00180       << " max:" << mMax << "\n" << vprDEBUG_FLUSH;
00181 
00182    return true;
00183 }

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

Returns analog data.

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 187 of file Analog.cpp.

References gadget::SampleBuffer< DATA_TYPE, MAX_BUFFER_SIZE >::stableBuffer().

00188 {
00189    SampleBuffer_t::buffer_t& stable_buffer = mAnalogSamples.stableBuffer();
00190 
00191    if ( (!stable_buffer.empty()) &&
00192         (stable_buffer.back().size() > (unsigned)devNum) )  // If Have entry && devNum in range
00193    {
00194       return stable_buffer.back()[devNum];
00195    }
00196    else        // No data or request out of range, return default value
00197    {
00198       if ( stable_buffer.empty() )
00199       {
00200          vprDEBUG(vprDBG_ALL, vprDBG_WARNING_LVL)
00201             << "WARNING: [gadget::Analog::getAnalogData()] "
00202             << "Stable buffer is empty.  If this is not the first read, "
00203             << "then this is a problem.\n" << vprDEBUG_FLUSH;
00204       }
00205       else
00206       {
00207          vprDEBUG(vprDBG_ALL, vprDBG_CONFIG_LVL)
00208             << "WARNING: [gadget::Analog::getAnalogData()] "
00209             << "Requested devNum (" << devNum << ") is not within the "
00210             << "available range.  This is probably a configuration error.\n"
00211             << vprDEBUG_FLUSH;
00212       }
00213       return mDefaultValue;
00214    }
00215 }

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

Helper method to add a collection of analog samples to the sample buffers.

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

Postcondition:
The given analog samples are added to the buffers, and the local filters are run on the new samples.
Parameters:
anaSample A vector of AnalogData objects that represent the newest samples taken.

Definition at line 217 of file Analog.cpp.

References gadget::SampleBuffer< DATA_TYPE, MAX_BUFFER_SIZE >::addSample(), gadget::SampleBuffer< DATA_TYPE, MAX_BUFFER_SIZE >::lock(), and gadget::SampleBuffer< DATA_TYPE, MAX_BUFFER_SIZE >::unlock().

00218 {
00219    // Locks and then swaps the indices.
00220    mAnalogSamples.lock();
00221    mAnalogSamples.addSample(anaSample);
00222    mAnalogSamples.unlock();
00223 }

void gadget::Analog::swapAnalogBuffers (  ) 

Swaps the analog data buffers.

Postcondition:
If the ready queue has values, then those values are copied from the ready queue to the stable queue. If not, then stable queue is not changed.

Definition at line 225 of file Analog.cpp.

References gadget::SampleBuffer< DATA_TYPE, MAX_BUFFER_SIZE >::swapBuffers().

Referenced by readObject().

00226 {
00227    mAnalogSamples.swapBuffers();
00228 }

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

Returns the current stable sample buffers for this device.

Definition at line 230 of file Analog.cpp.

References gadget::SampleBuffer< DATA_TYPE, MAX_BUFFER_SIZE >::stableBuffer().

00231 {
00232    return mAnalogSamples.stableBuffer();
00233 }

std::string gadget::Analog::getInputTypeName (  )  [virtual]

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

Definition at line 235 of file Analog.cpp.

Referenced by readObject(), and writeObject().

00236 {
00237    return std::string("Analog");
00238 }

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 243 of file Analog.cpp.

00245 {
00246    float value = plainJaneValue;
00247 
00248    // first clamp the value so that min<=value<=max
00249    if ( value < mMin ) value = mMin;
00250    if ( value > mMax ) value = mMax;
00251 
00252    // slide everything to 0.0 (subtract all by mMin)
00253    // Then divide by max to get normalized value
00254    float tmax( mMax - mMin),
00255          tvalue(value - mMin);
00256 
00257    // since [tmin/tmax...tmax/tmax] == [0.0f...1.0f], the normalized value will be value/tmax
00258    normedFromMinToMax = tvalue / tmax;
00259 }

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 261 of file Analog.cpp.

00262 {
00263    return mMin;
00264 }

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

Definition at line 266 of file Analog.cpp.

00267 {
00268    return mMax;
00269 }

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

Definition at line 271 of file Analog.cpp.

00272 {
00273    mMin = mIn;
00274 }

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

Definition at line 276 of file Analog.cpp.

00277 {
00278    mMax = mAx;
00279 }

void gadget::Analog::operator= ( const gadget::Analog  )  [inline, protected]

Definition at line 174 of file Analog.h.

00174 {;}


The documentation for this class was generated from the following files:
Generated on Thu Jan 4 10:44:48 2007 for Gadgeteer by  doxygen 1.5.1