lsm303: rename to lsm303dlh

There are a variety of LSM303 devices out there with various
incompatibilities and differing capabilities.  The current lsm303
driver in UPM only supports the LSM303DLH variant, so it has been
renamed to lsm303dlh to avoid confusion and to make it clear which
variant is actually supported.

All examples and source files have been renamed, including header
files.  In addition, the class name, LSM303, has been renamed to
LSM303DLH.  No other functionality or behavior has been changed.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson
2017-04-12 11:49:20 -06:00
parent d9fd1af272
commit 19c58ebba2
13 changed files with 95 additions and 82 deletions

View File

@ -0,0 +1,5 @@
set (libname "lsm303dlh")
set (libdescription "Triaxial Accelerometer/magnetometer")
set (module_src ${libname}.cxx)
set (module_hpp ${libname}.hpp)
upm_module_init(mraa)

View File

@ -0,0 +1,32 @@
%module javaupm_lsm303dlh
%include "../upm.i"
%{
#include "lsm303dlh.hpp"
%}
%typemap(jni) int16_t* "jshortArray"
%typemap(jstype) int16_t* "short[]"
%typemap(jtype) int16_t* "short[]"
%typemap(javaout) int16_t* {
return $jnicall;
}
%typemap(out) int16_t *getRawAccelData {
$result = JCALL1(NewShortArray, jenv, 3);
JCALL4(SetShortArrayRegion, jenv, $result, 0, 3, (jshort*)$1);
}
%include "lsm303dlh.hpp"
%pragma(java) jniclasscode=%{
static {
try {
System.loadLibrary("javaupm_lsm303dlh");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);
}
}
%}

View File

@ -0,0 +1,15 @@
%module jsupm_lsm303dlh
%include "../upm.i"
%include "../carrays_int16_t.i"
// Adding this typemap because SWIG is converting int16 into a short by default
// This forces SWIG to convert it correctly
%typemap(out) int16_t* {
jsresult = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_int16Array, 0 | 0 );
}
%{
#include "lsm303dlh.hpp"
%}
%include "lsm303dlh.hpp"

206
src/lsm303dlh/lsm303dlh.cxx Normal file
View File

@ -0,0 +1,206 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2014 Intel Corporation.
*
* Code based on LSM303DLH sample by Jim Lindblom SparkFun Electronics
* and the CompensatedCompass.ino by Frankie Chu from SeedStudio
*
* 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 <string>
#include <stdexcept>
#include <unistd.h>
#include <stdlib.h>
#include "lsm303dlh.hpp"
using namespace upm;
LSM303DLH::LSM303DLH(int bus, int addrMag, int addrAcc, int accScale) :
m_i2c(bus)
{
m_addrMag = addrMag;
m_addrAcc = addrAcc;
// 0x27 is the 'normal' mode with X/Y/Z enable
setRegisterSafe(m_addrAcc, CTRL_REG1_A, 0x27);
// scale can be 2, 4 or 8
if (2 == accScale) {
setRegisterSafe(m_addrAcc, CTRL_REG4_A, 0x00);
} else if (4 == accScale) {
setRegisterSafe(m_addrAcc, CTRL_REG4_A, 0x10);
} else { // default; equivalent to 8g
setRegisterSafe(m_addrAcc, CTRL_REG4_A, 0x30);
}
// 0x10 = minimum datarate ~15Hz output rate
setRegisterSafe(m_addrMag, CRA_REG_M, 0x10);
// magnetic scale = +/-1.3
// Gaussmagnetic scale = +/-1.3Gauss (0x20)
// +-8.1Gauss (0xe0)
setRegisterSafe(m_addrMag, CRB_REG_M, 0xe0);
// 0x00 = continouous conversion mode
setRegisterSafe(m_addrMag, MR_REG_M, 0x00);
}
float
LSM303DLH::getHeading()
{
if (getCoordinates() != mraa::SUCCESS) {
return -1;
}
float heading = 180.0 * atan2(double(coor[Y]), double(coor[X]))/M_PI;
if (heading < 0.0)
heading += 360.0;
return heading;
}
int16_t*
LSM303DLH::getRawAccelData()
{
return &accel[0];
}
int16_t*
LSM303DLH::getRawCoorData()
{
return &coor[0];
}
int16_t
LSM303DLH::getAccelX()
{
return accel[X];
}
int16_t
LSM303DLH::getAccelY()
{
return accel[Y];
}
int16_t
LSM303DLH::getAccelZ()
{
return accel[Z];
}
mraa::Result
LSM303DLH::getCoordinates()
{
mraa::Result ret = mraa::SUCCESS;
memset(&buf[0], 0, sizeof(uint8_t)*6);
ret = m_i2c.address(m_addrMag);
ret = m_i2c.writeByte(OUT_X_H_M);
ret = m_i2c.address(m_addrMag);
int num = m_i2c.read(buf, 6);
if (num != 6) {
return ret;
}
// convert to coordinates
for (int i=0; i<3; i++) {
coor[i] = (int16_t(buf[2*i] << 8))
| int16_t(buf[(2*i)+1]);
}
// swap elements 1 and 2 to get things in natural XYZ order
int16_t t = coor[2];
coor[2] = coor[1];
coor[1] = t;
//printf("X=%x, Y=%x, Z=%x\n", coor[X], coor[Y], coor[Z]);
return ret;
}
int16_t
LSM303DLH::getCoorX() {
return coor[X];
}
int16_t
LSM303DLH::getCoorY() {
return coor[Y];
}
int16_t
LSM303DLH::getCoorZ() {
return coor[Z];
}
// helper function that writes a value to the acc and then reads
// FIX: shouldn't this be write-then-read?
int
LSM303DLH::readThenWrite(uint8_t reg)
{
m_i2c.address(m_addrAcc);
m_i2c.writeByte(reg);
m_i2c.address(m_addrAcc);
return (int) m_i2c.readByte();
}
mraa::Result
LSM303DLH::getAcceleration()
{
mraa::Result ret = mraa::SUCCESS;
accel[X] = (int16_t(readThenWrite(OUT_X_H_A)) << 8)
| int16_t(readThenWrite(OUT_X_L_A));
accel[Y] = (int16_t(readThenWrite(OUT_Y_H_A)) << 8)
| int16_t(readThenWrite(OUT_Y_L_A));
accel[Z] = (int16_t(readThenWrite(OUT_Z_H_A)) << 8)
| int16_t(readThenWrite(OUT_Z_L_A));
//printf("X=%x, Y=%x, Z=%x\n", accel[X], accel[Y], accel[Z]);
return ret;
}
// helper function that sets a register and then checks the set was succesful
mraa::Result
LSM303DLH::setRegisterSafe(uint8_t slave, uint8_t sregister, uint8_t data)
{
buf[0] = sregister;
buf[1] = data;
if (m_i2c.address(slave) != mraa::SUCCESS) {
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_i2c_address() failed");
return mraa::ERROR_INVALID_HANDLE;
}
if (m_i2c.write(buf, 2) != mraa::SUCCESS) {
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_i2c_write() failed");
return mraa::ERROR_INVALID_HANDLE;
}
uint8_t val = m_i2c.readReg(sregister);
if (val != data) {
throw std::invalid_argument(std::string(__FUNCTION__) +
": failed to set register correctly");
return mraa::ERROR_UNSPECIFIED;
}
return mraa::SUCCESS;
}

181
src/lsm303dlh/lsm303dlh.hpp Normal file
View File

@ -0,0 +1,181 @@
/*
* Author: Brendan Le Foll<brendan.le.foll@intel.com>
* Copyright (c) 2014 Intel Corporation.
*
* Code based on LSM303DLH sample by Jim Lindblom SparkFun Electronics
* and the CompensatedCompass.ino by Frankie Chu from SeedStudio
*
* 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.h>
#include <mraa/i2c.hpp>
#include <math.h>
namespace upm {
/* LSM303DLH Address definitions */
#define LSM303DLH_MAG 0x1E
#define LSM303DLH_ACC 0x19
/* LSM303DLH Register definitions */
#define CTRL_REG1_A 0x20
#define CTRL_REG2_A 0x21
#define CTRL_REG3_A 0x22
#define CTRL_REG4_A 0x23
#define CTRL_REG5_A 0x24
#define CRA_REG_M 0x00
#define CRB_REG_M 0x01
#define MR_REG_M 0x02
#define OUT_X_H_M 0x03
#define OUT_X_L_A 0x28
#define OUT_X_H_A 0x29
#define OUT_Y_L_A 0x2A
#define OUT_Y_H_A 0x2B
#define OUT_Z_L_A 0x2C
#define OUT_Z_H_A 0x2D
#define X 0
#define Y 1
#define Z 2
/**
* @brief LSM303DLH Accelerometer/Compass library
* @defgroup lsm303dlh libupm-lsm303dlh
* @ingroup seeed adafruit stmicro i2c accelerometer compass
*/
/**
* @library lsm303dlh
* @sensor lsm303dlh
* @comname Triaxial Accelerometer/magnetometer
* @altname Grove 6-Axis Accelerometer & Compass
* @type accelerometer compass
* @man seeed adafruit stmicro
* @web http://www.seeedstudio.com/wiki/Grove_-_6-Axis_Accelerometer%26Compass
* @con i2c
*
* @brief API for the LSM303DLH Accelerometer & Compass
*
* This module defines the LSM303DLHDLH 3-axis magnetometer/3-axis accelerometer.
* This module was tested with the Seeed Studio* Grove 6-Axis Accelerometer & Compass
* module used over I2C. The magnetometer and acceleromter are accessed
* at two seperate I2C addresses.
*
* @image html lsm303dlh.jpeg
* @snippet lsm303dlh.cxx Interesting
*/
class LSM303DLH {
public:
/**
* Instantiates an LSM303DLH object
*
* @param bus I2C bus to use
* @param addrMag I2C address of the Magnetometer (default 0x1E)
* @param addrAcc I2C address of the Accelerometer (default 0x19)
* @param accScale Accelerometer scale, can be 2, 4 or 8 (default 8)
*/
LSM303DLH (int bus,
int addrMag=LSM303DLH_MAG,
int addrAcc=LSM303DLH_ACC,
int accScale=8);
/**
* LSM303DLH object destructor
* where is no more need for this here - I2c connection will be stopped
* automatically when m_i2c variable will go out of scope
* ~LSM303DLH ();
**/
/**
* Gets the current heading; headings <0 indicate an error has occurred
*
* @return float
*/
float getHeading();
/**
* Gets the coordinates in the XYZ order
*/
mraa::Result getCoordinates();
/**
* Gets accelerometer values
* Should be called before other "get" functions for acceleration
*/
mraa::Result getAcceleration();
/**
* Gets raw coordinate data; it is updated when getCoordinates() is called
*/
int16_t* getRawCoorData();
/**
* Gets the X component of the coordinates data
*/
int16_t getCoorX();
/**
* Gets the Y component of the coordinates data
*/
int16_t getCoorY();
/**
* Gets the Z component of the coordinates data
*/
int16_t getCoorZ();
/**
* Gets raw accelerometer data; it is updated when getAcceleration() is called
*/
int16_t* getRawAccelData();
/**
* Gets the X component of the acceleration data
*/
int16_t getAccelX();
/**
* Gets the Y component of the acceleration data
*/
int16_t getAccelY();
/**
* Gets the Z component of the acceleration data
*/
int16_t getAccelZ();
private:
int readThenWrite(uint8_t reg);
mraa::Result setRegisterSafe(uint8_t slave, uint8_t sregister, uint8_t data);
mraa::I2c m_i2c;
int m_addrMag;
int m_addrAcc;
uint8_t buf[6];
int16_t coor[3];
int16_t accel[3];
};
}

View File

@ -0,0 +1,18 @@
// Include doxygen-generated documentation
%include "pyupm_doxy2swig.i"
%module pyupm_lsm303dlh
%include "../upm.i"
%include "../carrays_int16_t.i"
%feature("autodoc", "3");
// Adding this typemap because SWIG is converting int16 into a short by default
// This forces SWIG to convert it correctly
%typemap(out) int16_t* {
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_int16Array, 0 | 0 );
}
%include "lsm303dlh.hpp"
%{
#include "lsm303dlh.hpp"
%}