#include <gadget/Type/Analog.h>
Inheritance diagram for gadget::Analog:


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_t & | getAnalogDataBuffer () |
| 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 &) |
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.
Definition at line 65 of file Analog.h.
| gadget::Analog::Analog | ( | ) |
Constructor.
Definition at line 46 of file Analog.cpp.
| gadget::Analog::~Analog | ( | ) | [virtual] |
| gadget::Analog::Analog | ( | const gadget::Analog & | d | ) | [inline, protected] |
| 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.
| 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.
| devNum | Device unit number to access. |
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.
| 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.
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().
| 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.
Definition at line 261 of file Analog.cpp.
| float gadget::Analog::getMax | ( | ) | const [protected] |
| void gadget::Analog::setMin | ( | float | mIn | ) | [protected] |
| void gadget::Analog::setMax | ( | float | mAx | ) | [protected] |
| void gadget::Analog::operator= | ( | const gadget::Analog & | ) | [inline, protected] |
1.5.1