Mustafa Kanli / DimEngAcc3D
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DimEngAcc3D.h Source File

DimEngAcc3D.h

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * @section LICENSE
00004  * Copyright (c) 2010 Mustafa Ozgur Kanli.
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a copy
00007  * of this software and associated documentation files (the "Software"),to deal
00008  * in the Software without restriction, including without limitation the rights
00009  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  * copies of the Software, and to permit persons to whom the Software is
00011  * furnished to do so, subject to the following conditions:
00012  *
00013  * The above copyright notice and this permission notice shall be included in
00014  * all copies or substantial portions of the Software.
00015  *
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022  * THE SOFTWARE.
00023  *
00024  * @section Description
00025  * This class provides an object to obtain 3D acceleration measurements from a
00026  * Dimension Engineering Buffered +-3g Tri-axis Accelerometer. This device uses
00027  * the ADXL330. Measurements are obtained using 3 analog inputs.
00028  *
00029  */
00030 #ifndef DIMENG_ACC3D_H_
00031 #define DIMENG_ACC3D_H_
00032 
00033 #include "mbed.h"
00034 
00035 /**
00036  * Acceleration due to gravity at sea level on Earth (m/s/s).
00037  */
00038 #define ACC_G   9.80665
00039 
00040 /**
00041  * Class to read acceleration measurements from Dimension Engineering
00042  * Buffered +-3g Tri-axis Accelerometer device.
00043  *
00044  * Example
00045  * @code
00046  * #include "mbed.h"
00047  * #include "DimEngAcc3D.h"
00048  *
00049  * int main() {
00050  *     DimEngAcc3D *acc;
00051  *
00052  *     // Create and configure object for 3.3V powered device,
00053  *     acc = new DimEngAcc3D(p18, p19, p20);
00054  *     acc->setVoltage(3.3);
00055  *
00056  *     while(1) {
00057  *         // Read.
00058  *         acc->read();
00059  *
00060  *         // Print and delay 0.5s.
00061  *         printf("%.3f, %.3f, %.3f\r\n", acc->x, acc->y, acc->z);
00062  *         wait(0.5);
00063  *     }
00064  * }
00065  * @endcode
00066  */
00067 class DimEngAcc3D {
00068 
00069 private:
00070     float ref_voltage;          //!< Supply/reference voltage (V).
00071     float zero_point;           //!< Zero g voltage output (V).
00072     float sensitivity;          //!< Sensitivity at reference voltage (V/g).
00073     AnalogIn *ain_x;            //!< For analog reads of acceleration in x axis.
00074     AnalogIn *ain_y;            //!< For analog reads of acceleration in y axis.
00075     AnalogIn *ain_z;            //!< For analog reads of acceleration in z axis.
00076 
00077     /**
00078      * Initialize private variables according to the specified
00079      * reference voltage
00080      * @param   voltage The specified voltage.
00081      */
00082     void initFromVoltage(float voltage);
00083 
00084 public:
00085     float x;                    //!< Most recent acceleration in x axis (m/s/s).
00086     float y;                    //!< Most recent acceleration in y axis (m/s/s).
00087     float z;                    //!< Most recent acceleration in z axis (m/s/s).
00088 
00089     /**
00090      * Constructor.
00091      *
00092      * @param   pin_x    Pin connected to output x of device.
00093      * @param   pin_y    Pin connected to output y of device.
00094     * @param   pin_z    Pin connected to output z of device.
00095      * @note    Unused/unconnected outputs may be specified as NC.
00096      */
00097     DimEngAcc3D(PinName pin_x, PinName pin_y, PinName pin_z);
00098 
00099     /**
00100      * Destructor.
00101      */
00102     ~DimEngAcc3D(void);
00103 
00104     /**
00105      * Specify the supply voltage used by the device.
00106      *
00107      * @param   voltage The specified voltage (default 3.3)
00108      * @note    This is important for correct conversion of the voltage
00109      *          from output pins of device into acceleration in m/s/s.
00110      */
00111     void setVoltage(float voltage);
00112 
00113     /**
00114      * Read acceleration and store in x, y & z.
00115      */
00116     void read(void);
00117 };
00118 
00119 #endif