Mustafa Kanli / DimEngAcc3D
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DimEngAcc3D.cpp Source File

DimEngAcc3D.cpp

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  * Implementation of MaxSonar class.
00026  */
00027 
00028 #include "mbed.h"
00029 #include "DimEngAcc3D.h "
00030 
00031 DimEngAcc3D::DimEngAcc3D(PinName pin_x, PinName pin_y, PinName pin_z) {
00032 
00033     // Set defaults.
00034     initFromVoltage(3.3);
00035     this->ain_x = NULL;
00036     this->ain_y = NULL;
00037     this->ain_z = NULL;
00038 
00039     // Create analog inputs as required.
00040     if (pin_x != NC)
00041         this->ain_x = new AnalogIn(pin_x);
00042 
00043     if (pin_y != NC)
00044         this->ain_y = new AnalogIn(pin_y);
00045 
00046     if (pin_z != NC)
00047         this->ain_z = new AnalogIn(pin_z);
00048 
00049 }
00050 
00051 DimEngAcc3D::~DimEngAcc3D(void) {
00052 
00053     if (this->ain_x != NULL)
00054         delete this->ain_x;
00055 
00056     if (this->ain_y != NULL)
00057         delete this->ain_y;
00058 
00059     if (this->ain_z != NULL)
00060         delete this->ain_z;
00061 }
00062 
00063 void DimEngAcc3D::initFromVoltage(float voltage) {
00064     /*
00065      * The equation was obtained from the linear relationship
00066      * of the sensitivity and input/reference voltage and two
00067      * pairs of sensitivity & voltage value pairs:
00068      * 195 mV/g @ 2V and 360mV @ 3.6V
00069      *
00070      * s = sensitivity (V/g)
00071      * v = reference voltage (V)
00072      * s = 0.103125*v - 0.01125
00073      */
00074     this->ref_voltage = voltage;
00075     this->zero_point = this->ref_voltage / 2.0;
00076     this->sensitivity = (0.103125 * this->ref_voltage) - 0.01125;
00077     this->x = this->zero_point;
00078     this->y = this->zero_point;
00079     this->z = this->zero_point;
00080     
00081     printf("ref: %f, zero: %f, sens: %f \r\n", this->ref_voltage, this->zero_point, this->sensitivity);
00082 }
00083 
00084 void DimEngAcc3D::setVoltage(float voltage) {
00085     initFromVoltage(voltage);
00086 }
00087 
00088 void DimEngAcc3D::read(void) {
00089 
00090     // Read the acceleration outputs.
00091     if (this->ain_x != NULL)
00092         this->x = this->ain_x->read() * this->ref_voltage;
00093 
00094     if (this->ain_y != NULL)
00095         this->y = this->ain_y->read() * this->ref_voltage;
00096 
00097     if (this->ain_z != NULL)
00098         this->z = this->ain_z->read() * this->ref_voltage;
00099 
00100     // Convert raw voltage measurements to m/s/s
00101     this->x = ACC_G * (this->x - this->zero_point) / this->sensitivity;
00102     this->y = ACC_G * (this->y - this->zero_point) / this->sensitivity;
00103     this->z = ACC_G * (this->z - this->zero_point) / this->sensitivity;
00104 }
00105