adxl335: Initial implementation

The was tested with the Grove 3-Axis Analog Gyro.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: John Van Drasek <john.r.van.drasek@intel.com>
This commit is contained in:
Jon Trulson
2015-01-26 16:32:19 -07:00
committed by John Van Drasek
parent db7d6d7f5f
commit b03676222c
8 changed files with 414 additions and 0 deletions

View File

@ -0,0 +1,5 @@
set (libname "adxl335")
set (libdescription "upm adxl335 grove 3-axis anaolog accelerometer")
set (module_src ${libname}.cxx)
set (module_h ${libname}.h)
upm_module_init()

102
src/adxl335/adxl335.cxx Normal file
View File

@ -0,0 +1,102 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2015 Intel Corporation.
*
* Adapted from the seeedstudio example
* https://github.com/Seeed-Studio/Accelerometer_ADXL335
*
* 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.
*/
#include <iostream>
#include "adxl335.h"
using namespace std;
using namespace upm;
ADXL335::ADXL335(int pinX, int pinY, int pinZ, float aref)
{
m_aref = aref;
m_zeroX = 0.0;
m_zeroY = 0.0;
m_zeroZ = 0.0;
if ( !(m_aioX = mraa_aio_init(pinX)) )
{
cerr << __FUNCTION__ << ": mraa_aio_init(X) failed" << endl;
return;
}
if ( !(m_aioY = mraa_aio_init(pinY)) )
{
cerr << __FUNCTION__ << ": mraa_aio_init(Y) failed" << endl;
return;
}
if ( !(m_aioZ = mraa_aio_init(pinZ)) )
{
cerr << __FUNCTION__ << ": mraa_aio_init(Z) failed" << endl;
return;
}
}
ADXL335::~ADXL335()
{
mraa_aio_close(m_aioX);
mraa_aio_close(m_aioY);
mraa_aio_close(m_aioZ);
}
void ADXL335::values(int *xVal, int *yVal, int *zVal)
{
*xVal = mraa_aio_read(m_aioX);
*yVal = mraa_aio_read(m_aioY);
*zVal = mraa_aio_read(m_aioZ);
}
void ADXL335::acceleration(float *xAccel, float *yAccel, float *zAccel)
{
int x, y, z;
float xVolts, yVolts, zVolts;
values(&x, &y, &z);
xVolts = float(x) * m_aref / 1024.0;
yVolts = float(y) * m_aref / 1024.0;
zVolts = float(z) * m_aref / 1024.0;
*xAccel = (xVolts - m_zeroX) / ADXL335_SENSITIVITY;
*yAccel = (yVolts - m_zeroY) / ADXL335_SENSITIVITY;
*zAccel = (zVolts - m_zeroZ) / ADXL335_SENSITIVITY;
}
void ADXL335::calibrate()
{
// make sure the sensor is still before running calibration.
int x, y, z;
usleep(10000);
values(&x, &y, &z);
setZeroX(float(x) * m_aref / 1024.0);
setZeroY(float(y) * m_aref / 1024.0);
setZeroZ(float(z) * m_aref / 1024.0);
}

119
src/adxl335/adxl335.h Normal file
View File

@ -0,0 +1,119 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2015 Intel Corporation.
*
* Adapted from the seeedstudio example
* https://github.com/Seeed-Studio/Accelerometer_ADXL335
*
* 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.
*/
#pragma once
#include <string>
#include <mraa/aio.h>
#define ADXL335_DEFAULT_AREF 5.0
#define ADXL335_SENSITIVITY 0.25 // 0.25v/g
namespace upm {
/**
* @brief C++ API for the ADXL335 3-axis analog acclerometer
*
* UPM module for the ADXL335 3-axis analog accelerometer. This
* was tested on a Grove 3-axis Analog Accelerometer. It uses 3
* analog pins, one each for X, Y, and Z axis.
*
* @ingroup grove analog
* @snippet adxl335.cxx Interesting
*/
class ADXL335 {
public:
/**
* ADXL335 constructor
*
* @param pinX analog pin to use for axis X
* @param pinY analog pin to use for axis Y
* @param pinZ analog pin to use for axis Z
* @param aref analog voltage reference, default 5.0
*/
ADXL335(int pinX, int pinY, int pinZ, float aref=ADXL335_DEFAULT_AREF);
/**
* ADXL335 Destructor
*/
~ADXL335();
/**
* Set the "zero" value of the X axis, determined through calibration
*
* @param zeroX The "zero" value of the X axis
*/
void setZeroX(float zeroX) { m_zeroX = zeroX; };
/**
* Set the "zero" value of the Y axis, determined through calibration
*
* @param zeroX The "zero" value of the Y axis
*/
void setZeroY(float zeroY) { m_zeroY = zeroY; };
/**
* Set the "zero" value of the Z axis, determined through calibration
*
* @param zeroX The "zero" value of the Z axis
*/
void setZeroZ(float zeroZ) { m_zeroZ = zeroZ; };
/**
* Get the analog values for the 3 axes
*
* @param xVal pointer to returned X value
* @param yVal pointer to returned Y value
* @param zVal pointer to returned Z value
*/
void values(int *xVal, int *yVal, int *zVal);
/**
* Get the acceleration along all 3 axes
*
* @param xAccel pointer to returned X value
* @param yAccel pointer to returned Y value
* @param zAccel pointer to returned Z value
*/
void acceleration(float *xAccel, float *yAccel, float *zAccel);
/**
* While the sensor is still, measure the X, Y, and Z values and
* use those values as our zero values.
*
*/
void calibrate();
private:
mraa_aio_context m_aioX;
mraa_aio_context m_aioY;
mraa_aio_context m_aioZ;
float m_aref;
float m_zeroX, m_zeroY, m_zeroZ;
};
}

View File

@ -0,0 +1,13 @@
%module jsupm_adxl335
%include "../upm.i"
%include "stdint.i"
%include "carrays.i"
%array_class(int, intPointer);
%array_class(float, floatPointer);
%{
#include "adxl335.h"
%}
%include "adxl335.h"

View File

@ -0,0 +1,9 @@
%module pyupm_adxl335
%include "../upm.i"
%feature("autodoc", "3");
%include "adxl335.h"
%{
#include "adxl335.h"
%}