root/juggler/branches/2.2/modules/gadgeteer/gadget/Type/Analog.cpp

Revision 19729, 8.6 kB (checked in by patrick, 2 years ago)

Copyright update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998-2007 by Iowa State University
4  *
5  * Original Authors:
6  *   Allen Bierbaum, Christopher Just,
7  *   Patrick Hartling, Kevin Meinert,
8  *   Carolina Cruz-Neira, Albert Baker
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  *************** <auto-copyright.pl END do not edit this line> ***************/
26
27 #include <gadget/gadgetConfig.h>
28
29 #include <boost/concept_check.hpp>
30 #include <vpr/IO/ObjectWriter.h>
31 #include <vpr/IO/ObjectReader.h>
32 #include <vpr/Util/Debug.h>
33 #include <gadget/Type/Analog.h>
34 #include <gadget/Util/DeviceSerializationTokens.h>
35
36
37 namespace gadget
38 {
39
40 Analog::Analog()
41    : mMin(0.0f)
42    , mMax(0.0f)
43 {
44    /* Do nothing. */ ;
45 }
46
47 Analog::~Analog()
48 {
49    /* Do nothing. */ ;
50 }
51
52 void Analog::writeObject(vpr::ObjectWriter* writer)
53 {
54    //std::cout << "[Remote Input Manager] In Analog write" << std::endl;
55
56    SampleBuffer_t::buffer_t& stable_buffer = mAnalogSamples.stableBuffer();
57    writer->beginTag(Analog::getInputTypeName());
58    writer->beginAttribute(gadget::tokens::DataTypeAttrib);
59       writer->writeUint16(MSG_DATA_ANALOG);                                   // Write out the data type so that we can assert if reading in wrong place
60    writer->endAttribute();
61
62    if ( !stable_buffer.empty() )
63    {
64       mAnalogSamples.lock();
65       writer->beginAttribute(gadget::tokens::SampleBufferLenAttrib);
66          writer->writeUint16(stable_buffer.size());                           // Write the # of vectors in the stable buffer
67       writer->endAttribute();
68       for ( unsigned j = 0; j < stable_buffer.size(); ++j )                   // For each vector in the stable buffer
69       {
70          writer->beginTag(gadget::tokens::BufferSampleTag);
71          writer->beginAttribute(gadget::tokens::BufferSampleLenAttrib);
72             writer->writeUint16(stable_buffer[j].size());                     // Write the # of AnalogDatas in the vector
73          writer->endAttribute();
74          for ( unsigned i = 0;i < stable_buffer[j].size(); ++i )              // For each AnalogData in the vector
75          {
76             writer->beginTag(gadget::tokens::AnalogValue);
77             writer->beginAttribute(gadget::tokens::TimeStamp);
78                writer->writeUint64(stable_buffer[j][i].getTime().usec());        // Write Time Stamp vpr::Uint64
79             writer->endAttribute();
80             writer->writeFloat(stable_buffer[j][i].getAnalog());              // Write Analog Data(int)
81             writer->endTag();
82          }
83          writer->endTag();
84       }
85       mAnalogSamples.unlock();
86    }
87    else        // No data or request out of range, return default value
88    {
89       writer->beginTag(gadget::tokens::BufferSampleTag);
90          writer->beginAttribute(gadget::tokens::BufferSampleLenAttrib);
91             writer->writeUint16(0);
92          writer->endAttribute();
93       writer->endTag();
94       vprDEBUG(vprDBG_ALL, vprDBG_WARNING_LVL)
95          << "WARNING: [gadget::Analog::writeObject()] Stable buffer is empty.  "
96          << "If this is not the first write, then this is a problem.\n"
97          << vprDEBUG_FLUSH;
98    }
99    writer->endTag();
100 }
101
102 void Analog::readObject(vpr::ObjectReader* reader)
103 {
104    vprASSERT(reader->attribExists("rim.timestamp.delta"));
105    vpr::Uint64 delta = reader->getAttrib<vpr::Uint64>("rim.timestamp.delta");
106
107    reader->beginTag(Analog::getInputTypeName());
108    reader->beginAttribute(gadget::tokens::DataTypeAttrib);
109       vpr::Uint16 temp = reader->readUint16();
110    reader->endAttribute();
111
112    // ASSERT if this data is really not Analog Data
113    // XXX: Should there be error checking for the case when vprASSERT is
114    // compied out?  -PH 8/21/2003
115    vprASSERT(temp==MSG_DATA_ANALOG && "[Remote Input Manager] Not Analog Data");
116    boost::ignore_unused_variable_warning(temp);
117
118    std::vector<AnalogData> dataSample;
119
120    unsigned numAnalogDatas;
121    float value;
122    vpr::Uint64 timeStamp;
123    AnalogData temp_analog_data;
124    reader->beginAttribute(gadget::tokens::SampleBufferLenAttrib);
125       unsigned numVectors = reader->readUint16();
126    reader->endAttribute();
127
128    mAnalogSamples.lock();
129    for ( unsigned i = 0; i < numVectors; ++i )
130    {
131       reader->beginTag(gadget::tokens::BufferSampleTag);
132       reader->beginAttribute(gadget::tokens::BufferSampleLenAttrib);
133          numAnalogDatas = reader->readUint16();
134       reader->endAttribute();
135
136       dataSample.clear();
137
138       for ( unsigned j = 0; j < numAnalogDatas; ++j )
139       {
140          reader->beginTag(gadget::tokens::AnalogValue);
141          reader->beginAttribute(gadget::tokens::TimeStamp);
142             timeStamp = reader->readUint64();                  // Write Time Stamp vpr::Uint64
143          reader->endAttribute();
144          value = reader->readFloat();                       // Write Analog Data(int)
145          reader->endTag();
146
147          temp_analog_data.setAnalog(value);
148          temp_analog_data.setTime(vpr::Interval(timeStamp + delta,vpr::Interval::Usec));
149          dataSample.push_back(temp_analog_data);
150       }
151
152       mAnalogSamples.addSample(dataSample);
153       reader->endTag();
154    }
155    mAnalogSamples.unlock();
156    swapAnalogBuffers();
157
158    reader->endTag();
159 }
160
161 bool Analog::config(jccl::ConfigElementPtr element)
162 {
163    mMin = element->getProperty<float>("min");
164    mMax = element->getProperty<float>("max");
165
166    vprDEBUG(vprDBG_ALL, vprDBG_VERB_LVL)
167       << "[gadget::Analog::config()] min:" << mMin
168       << " max:" << mMax << "\n" << vprDEBUG_FLUSH;
169
170    return true;
171 }
172
173 // XXX: Add a "sample" filter that does the normalization in here instead
174 // of in the driver.
175 AnalogData Analog::getAnalogData(int devNum)
176 {
177    SampleBuffer_t::buffer_t& stable_buffer = mAnalogSamples.stableBuffer();
178
179    if ( (!stable_buffer.empty()) &&
180         (stable_buffer.back().size() > (unsigned)devNum) )  // If Have entry && devNum in range
181    {
182       return stable_buffer.back()[devNum];
183    }
184    else        // No data or request out of range, return default value
185    {
186       if ( stable_buffer.empty() )
187       {
188          vprDEBUG(vprDBG_ALL, vprDBG_WARNING_LVL)
189             << "WARNING: [gadget::Analog::getAnalogData()] "
190             << "Stable buffer is empty.  If this is not the first read, "
191             << "then this is a problem.\n" << vprDEBUG_FLUSH;
192       }
193       else
194       {
195          vprDEBUG(vprDBG_ALL, vprDBG_CONFIG_LVL)
196             << "WARNING: [gadget::Analog::getAnalogData()] "
197             << "Requested devNum (" << devNum << ") is not within the "
198             << "available range.  This is probably a configuration error.\n"
199             << vprDEBUG_FLUSH;
200       }
201       return mDefaultValue;
202    }
203 }
204
205 void Analog::addAnalogSample(const std::vector<AnalogData>& anaSample)
206 {
207    // Locks and then swaps the indices.
208    mAnalogSamples.lock();
209    mAnalogSamples.addSample(anaSample);
210    mAnalogSamples.unlock();
211 }
212
213 void Analog::swapAnalogBuffers()
214 {
215    mAnalogSamples.swapBuffers();
216 }
217
218 const Analog::SampleBuffer_t::buffer_t& Analog::getAnalogDataBuffer()
219 {
220    return mAnalogSamples.stableBuffer();
221 }
222
223 std::string Analog::getInputTypeName()
224 {
225    return std::string("Analog");
226 }
227
228 // Given a value that will range from [min() <= n <= max()].
229 // This returns a value that is normalized to [0,1]
230 // if n < mMin or n > mMax, then result = mMin or mMax respectively.
231 void Analog::normalizeMinToMax(const float& plainJaneValue,
232                                float& normedFromMinToMax)
233 {
234    float value = plainJaneValue;
235
236    // first clamp the value so that min<=value<=max
237    if ( value < mMin ) value = mMin;
238    if ( value > mMax ) value = mMax;
239
240    // slide everything to 0.0 (subtract all by mMin)
241    // Then divide by max to get normalized value
242    float tmax( mMax - mMin),
243          tvalue(value - mMin);
244
245    // since [tmin/tmax...tmax/tmax] == [0.0f...1.0f], the normalized value will be value/tmax
246    normedFromMinToMax = tvalue / tmax;
247 }
248
249 float Analog::getMin() const
250 {
251    return mMin;
252 }
253
254 float Analog::getMax() const
255 {
256    return mMax;
257 }
258
259 void Analog::setMin(float mIn)
260 {
261    mMin = mIn;
262 }
263
264 void Analog::setMax(float mAx)
265 {
266    mMax = mAx;
267 }
268
269 } // End of gadget namespace
270
Note: See TracBrowser for help on using the browser.