Class to read acceleration measurements from Dimension Engineering Buffered +-3g Tri-axis Accelerometer device.

Files at this revision

API Documentation at this revision

Comitter:
mkanli
Date:
Sun Feb 20 08:11:35 2011 +0000
Commit message:
First cut

Changed in this revision

DimEngAcc3D.cpp Show annotated file Show diff for this revision Revisions of this file
DimEngAcc3D.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 9b3ea4450259 DimEngAcc3D.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DimEngAcc3D.cpp	Sun Feb 20 08:11:35 2011 +0000
@@ -0,0 +1,105 @@
+/**
+ * @file
+ * @section LICENSE
+ * Copyright (c) 2010 Mustafa Ozgur Kanli.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"),to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @section Description
+ * Implementation of MaxSonar class.
+ */
+
+#include "mbed.h"
+#include "DimEngAcc3D.h"
+
+DimEngAcc3D::DimEngAcc3D(PinName pin_x, PinName pin_y, PinName pin_z) {
+
+    // Set defaults.
+    initFromVoltage(3.3);
+    this->ain_x = NULL;
+    this->ain_y = NULL;
+    this->ain_z = NULL;
+
+    // Create analog inputs as required.
+    if (pin_x != NC)
+        this->ain_x = new AnalogIn(pin_x);
+
+    if (pin_y != NC)
+        this->ain_y = new AnalogIn(pin_y);
+
+    if (pin_z != NC)
+        this->ain_z = new AnalogIn(pin_z);
+
+}
+
+DimEngAcc3D::~DimEngAcc3D(void) {
+
+    if (this->ain_x != NULL)
+        delete this->ain_x;
+
+    if (this->ain_y != NULL)
+        delete this->ain_y;
+
+    if (this->ain_z != NULL)
+        delete this->ain_z;
+}
+
+void DimEngAcc3D::initFromVoltage(float voltage) {
+    /*
+     * The equation was obtained from the linear relationship
+     * of the sensitivity and input/reference voltage and two
+     * pairs of sensitivity & voltage value pairs:
+     * 195 mV/g @ 2V and 360mV @ 3.6V
+     *
+     * s = sensitivity (V/g)
+     * v = reference voltage (V)
+     * s = 0.103125*v - 0.01125
+     */
+    this->ref_voltage = voltage;
+    this->zero_point = this->ref_voltage / 2.0;
+    this->sensitivity = (0.103125 * this->ref_voltage) - 0.01125;
+    this->x = this->zero_point;
+    this->y = this->zero_point;
+    this->z = this->zero_point;
+    
+    printf("ref: %f, zero: %f, sens: %f \r\n", this->ref_voltage, this->zero_point, this->sensitivity);
+}
+
+void DimEngAcc3D::setVoltage(float voltage) {
+    initFromVoltage(voltage);
+}
+
+void DimEngAcc3D::read(void) {
+
+    // Read the acceleration outputs.
+    if (this->ain_x != NULL)
+        this->x = this->ain_x->read() * this->ref_voltage;
+
+    if (this->ain_y != NULL)
+        this->y = this->ain_y->read() * this->ref_voltage;
+
+    if (this->ain_z != NULL)
+        this->z = this->ain_z->read() * this->ref_voltage;
+
+    // Convert raw voltage measurements to m/s/s
+    this->x = ACC_G * (this->x - this->zero_point) / this->sensitivity;
+    this->y = ACC_G * (this->y - this->zero_point) / this->sensitivity;
+    this->z = ACC_G * (this->z - this->zero_point) / this->sensitivity;
+}
+
diff -r 000000000000 -r 9b3ea4450259 DimEngAcc3D.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DimEngAcc3D.h	Sun Feb 20 08:11:35 2011 +0000
@@ -0,0 +1,119 @@
+/**
+ * @file
+ * @section LICENSE
+ * Copyright (c) 2010 Mustafa Ozgur Kanli.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"),to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @section Description
+ * This class provides an object to obtain 3D acceleration measurements from a
+ * Dimension Engineering Buffered +-3g Tri-axis Accelerometer. This device uses
+ * the ADXL330. Measurements are obtained using 3 analog inputs.
+ *
+ */
+#ifndef DIMENG_ACC3D_H_
+#define DIMENG_ACC3D_H_
+
+#include "mbed.h"
+
+/**
+ * Acceleration due to gravity at sea level on Earth (m/s/s).
+ */
+#define ACC_G	9.80665
+
+/**
+ * Class to read acceleration measurements from Dimension Engineering
+ * Buffered +-3g Tri-axis Accelerometer device.
+ *
+ * Example
+ * @code
+ * #include "mbed.h"
+ * #include "DimEngAcc3D.h"
+ *
+ * int main() {
+ *     DimEngAcc3D *acc;
+ *
+ *     // Create and configure object for 3.3V powered device,
+ *     acc = new DimEngAcc3D(p18, p19, p20);
+ *     acc->setVoltage(3.3);
+ *
+ *     while(1) {
+ *         // Read.
+ *         acc->read();
+ *
+ *         // Print and delay 0.5s.
+ *         printf("%.3f, %.3f, %.3f\r\n", acc->x, acc->y, acc->z);
+ *         wait(0.5);
+ *     }
+ * }
+ * @endcode
+ */
+class DimEngAcc3D {
+
+private:
+    float ref_voltage;          //!< Supply/reference voltage (V).
+    float zero_point;			//!< Zero g voltage output (V).
+    float sensitivity;          //!< Sensitivity at reference voltage (V/g).
+    AnalogIn *ain_x;            //!< For analog reads of acceleration in x axis.
+    AnalogIn *ain_y;            //!< For analog reads of acceleration in y axis.
+    AnalogIn *ain_z;            //!< For analog reads of acceleration in z axis.
+
+    /**
+     * Initialize private variables according to the specified
+     * reference voltage
+     * @param   voltage The specified voltage.
+     */
+    void initFromVoltage(float voltage);
+
+public:
+    float x;					//!< Most recent acceleration in x axis (m/s/s).
+    float y;					//!< Most recent acceleration in y axis (m/s/s).
+    float z;					//!< Most recent acceleration in z axis (m/s/s).
+
+    /**
+     * Constructor.
+     *
+     * @param   pin_x    Pin connected to output x of device.
+     * @param   pin_y    Pin connected to output y of device.
+    * @param   pin_z    Pin connected to output z of device.
+     * @note    Unused/unconnected outputs may be specified as NC.
+     */
+    DimEngAcc3D(PinName pin_x, PinName pin_y, PinName pin_z);
+
+    /**
+     * Destructor.
+     */
+    ~DimEngAcc3D(void);
+
+    /**
+     * Specify the supply voltage used by the device.
+     *
+     * @param   voltage The specified voltage (default 3.3)
+     * @note    This is important for correct conversion of the voltage
+     *          from output pins of device into acceleration in m/s/s.
+     */
+    void setVoltage(float voltage);
+
+    /**
+     * Read acceleration and store in x, y & z.
+     */
+    void read(void);
+};
+
+#endif