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

Revision 19729, 6.3 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 #ifndef _GADGET_ANALOG_H_
28 #define _GADGET_ANALOG_H_
29
30 #include <gadget/gadgetConfig.h>
31 #include <vector>
32
33 #include <gadget/Type/AnalogData.h>
34 #include <gadget/Type/SampleBuffer.h>
35
36 #include <jccl/Config/ConfigElement.h>
37 #include <vpr/IO/SerializableObject.h>
38
39
40 namespace gadget
41 {
42
43 const unsigned short MSG_DATA_ANALOG = 421;
44
45 /** \class Analog Analog.h gadget/Type/Analog.h
46  *
47  * Analog is the abstract base class from which devices returning analog data
48  * must derive (through the use of gadget::InputMixer).  This is in addition
49  * to gadget::Input.  gadget::Input provides pure virtual function constraints
50  * in the following functions: startSampling(), stopSampling(), sample(), and
51  * updateData().
52  *
53  * gadget::Analog adds the function getAnalogData() for retreiving the
54  * received analog data.  This is similar to the additions made by
55  * gadget::Position and gadget::Digital.
56  *
57  * @see Input, InputMixer
58  */
59 class GADGET_CLASS_API Analog : public vpr::SerializableObject
60 {
61 public:
62    typedef gadget::SampleBuffer<AnalogData> SampleBuffer_t;
63
64 public:
65
66    /**
67     * Constructor.
68     * @post Set device abilities.
69     * @note Must be called from all derived classes.
70     */
71    Analog();
72
73    virtual ~Analog();
74
75    /**
76     * Serializes this object into the given object writer.
77     *
78     * @param writer The object writer to which this object will be serialized.
79     *
80     * @throw vpr::IOException is thrown if serialization fails.
81     */
82    virtual void writeObject(vpr::ObjectWriter* writer);
83
84    /**
85     * De-serializes this object.
86     *
87     * @param reader The object reader from which this object will be
88     *               de-serialized.
89     *
90     * @throw vpr::IOException is thrown if de-serialization fails.
91     */
92    virtual void readObject(vpr::ObjectReader* reader);
93
94    /**
95     * Reads the minimum and maximum value configuration information for this
96     * analog device.
97     *
98     * @post mMin and mMax are set to the values contained in the given config
99     *       element.
100     *
101     * @param element The config element for an analog device.  It must derive
102     *                from the base config element type 'analog'.
103     */
104    virtual bool config(jccl::ConfigElementPtr element);
105
106    /**
107     * Returns analog data.
108     *
109     * @post Returns a value that ranges from 0.0f to 1.0f.
110     *
111     * @param devNum Device unit number to access.
112     *
113     * @note For example, if you are sampling a potentiometer, and it returns
114     *       reading from 0, 255.  This function will normalize those values
115     *       (using Analog::normalizeMinToMax()).  For another example, if
116     *       your potentiometer's turn radius is limited mechanically to return,
117     *       say, the values 176 to 200 (yes this is really low res), this
118     *       function will still return 0.0f to 1.0f.
119     * @note To specify these min/max values, you must set in your Analog (or
120     *       analog device) config file the field "min" and "max".  By default
121     *       (if these values do not appear), "min" and "max" are set to 0.0f
122     *       and 1.0f respectivly.
123     * @note TO ALL ANALOG DEVICE DRIVER WRITERS, you *must* normalize your
124     *       data using Analog::normalizeMinToMax().
125     */
126    AnalogData getAnalogData(int devNum = 0);
127
128    /**
129     * Helper method to add a collection of analog samples to the sample
130     * buffers.  This MUST be called by all analog devices to add new samples.
131     * The data samples passed in will then be modified by any local filters.
132     *
133     * @post The given analog samples are added to the buffers, and the local
134     *       filters are run on the new samples.
135     *
136     * @param anaSample A vector of AnalogData objects that represent the
137     *                  newest samples taken.
138     */
139    void addAnalogSample(const std::vector< AnalogData >& anaSample);
140
141    /**
142     * Swaps the analog data buffers.
143     *
144     * @post If the ready queue has values, then those values are copied from
145     *       the ready queue to the stable queue.  If not, then stable queue
146     *       is not changed.
147     */
148    void swapAnalogBuffers();
149
150    /**
151     * Returns the current stable sample buffers for this device.
152     */
153    const SampleBuffer_t::buffer_t& getAnalogDataBuffer();
154
155    virtual std::string getInputTypeName();
156
157 protected:
158    /**
159     * Given a value that will range from [min() <= n <= max()].
160     * This returns a value that is normalized to [0,1]
161     */
162    void normalizeMinToMax(const float& plainJaneValue,
163                           float& normedFromMinToMax);
164
165 protected:
166    /**
167     * Gets the minimum "real" data value (real == from hardware).
168     * This value is used to normalize the return value of getAnalogData.
169     * @note this function is not needed by an application author.
170     */
171    float getMin() const;
172    float getMax() const;
173    void setMin(float mIn);
174    void setMax(float mAx);
175
176    // gadget::SampleBuffer<T> is not copyable, so neither are we.
177    Analog(const gadget::Analog& d)
178       : vpr::SerializableObject(d)
179    {;}
180
181    void operator=(const gadget::Analog&) {;}
182
183 private:
184    float mMin, mMax;
185
186 private:
187    SampleBuffer_t    mAnalogSamples;  /**< Position samples */
188    AnalogData        mDefaultValue;   /**< Default analog value to return */
189 };
190
191 } // End of gadget namespace
192
193
194 #endif   /* _GADGET_ANALOG_H_ */
Note: See TracBrowser for help on using the browser.