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

@ -82,6 +82,7 @@ add_executable (rpr220-example rpr220.cxx)
add_executable (rpr220-intr-example rpr220-intr.cxx)
add_executable (mma7660-example mma7660.cxx)
add_executable (cjq4435-example cjq4435.cxx)
add_executable (adxl335-example adxl335.cxx)
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@ -149,6 +150,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/adxl345)
include_directories (${PROJECT_SOURCE_DIR}/src/rpr220)
include_directories (${PROJECT_SOURCE_DIR}/src/mma7660)
include_directories (${PROJECT_SOURCE_DIR}/src/cjq4435)
include_directories (${PROJECT_SOURCE_DIR}/src/adxl335)
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@ -234,3 +236,4 @@ target_link_libraries (rpr220-example rpr220 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (rpr220-intr-example rpr220 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (mma7660-example mma7660 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (cjq4435-example cjq4435 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (adxl335-example adxl335 ${CMAKE_THREAD_LIBS_INIT})

78
examples/adxl335.cxx Normal file
View File

@ -0,0 +1,78 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2015 Intel Corporation.
*
* 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 <unistd.h>
#include <iostream>
#include <signal.h>
#include "adxl335.h"
using namespace std;
int shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main ()
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate an ADXL335 accelerometer on analog pins A0, A1, and A2
upm::ADXL335* accel = new upm::ADXL335(0, 1, 2);
cout << "Please make sure the sensor is completely still. Sleeping for"
<< " 2 seconds." << endl;
sleep(2);
cout << "Calibrating..." << endl;
accel->calibrate();
while (shouldRun)
{
int x, y, z;
float aX, aY, aZ;
accel->values(&x, &y, &z);
cout << "Raw Values: X: " << x << " Y: " << y << " Z: " << z << endl;
accel->acceleration(&aX, &aY, &aZ);
cout << "Acceleration: X: " << aX << "g" << endl;
cout << "Acceleration: Y: " << aY << "g" << endl;
cout << "Acceleration: Z: " << aZ << "g" << endl;
cout << endl;
usleep(200000);
}
//! [Interesting]
cout << "Exiting" << endl;
delete accel;
return 0;
}

View File

@ -0,0 +1,85 @@
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:true */
/*
* Author: Zion Orent <zorent@ics.com>
* Copyright (c) 2015 Intel Corporation.
*
* 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.
*/
var analogGyro3Axis = require("jsupm_adxl335");
var g_addnumBool = true;
var g_cycleNum = 0.0;
var g_cycleCount = 0;
// Instantiate an ADXL335 accelerometer on analog pins A0, A1, and A2
var myAnalogGyro3Axis = new analogGyro3Axis.ADXL335(0, 1, 2);
console.log("Please make sure the sensor is completely still.");
console.log("Sleeping for 2 seconds");
var g_myInterval;
setTimeout(function()
{
console.log("Calibrating...");
myAnalogGyro3Axis.calibrate();
// Get values from accelerometer every 0.2 seconds
g_myInterval = setInterval(runAccelerometer, 200);
}, 2000);
var x = new analogGyro3Axis.intPointer(0);
var y = new analogGyro3Axis.intPointer(0);
var z = new analogGyro3Axis.intPointer(0);
var aX = new analogGyro3Axis.floatPointer(0);
var aY = new analogGyro3Axis.floatPointer(0);
var aZ = new analogGyro3Axis.floatPointer(0);
var outputStr;
function runAccelerometer()
{
myAnalogGyro3Axis.values(x, y, z);
outputStr = "Raw Values: X: " + x.getitem(0) +
" Y: " + y.getitem(0) +
" Z: " + z.getitem(0);
console.log(outputStr);
myAnalogGyro3Axis.acceleration(aX, aY, aZ);
console.log("Acceleration: X: " + aX.getitem(0) + "g");
console.log("Acceleration: Y: " + aY.getitem(0) + "g");
console.log("Acceleration: Z: " + aZ.getitem(0) + "g");
console.log(" ");
}
// When exiting: clear interval and print exit message
process.on('SIGINT', function()
{
clearInterval(g_myInterval);
console.log("Exiting...");
process.exit(0);
});

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"
%}