2014-09-05 18:57:56 +01:00
|
|
|
/*
|
|
|
|
* 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>
|
2015-09-10 13:26:21 -06:00
|
|
|
#include <string>
|
|
|
|
#include <stdexcept>
|
2014-09-05 18:57:56 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2017-04-12 11:49:20 -06:00
|
|
|
#include "lsm303dlh.hpp"
|
2014-09-05 18:57:56 +01:00
|
|
|
|
|
|
|
using namespace upm;
|
|
|
|
|
2017-04-12 11:49:20 -06:00
|
|
|
LSM303DLH::LSM303DLH(int bus, int addrMag, int addrAcc, int accScale) :
|
|
|
|
m_i2c(bus)
|
2014-09-05 18:57:56 +01:00
|
|
|
{
|
|
|
|
m_addrMag = addrMag;
|
|
|
|
m_addrAcc = addrAcc;
|
|
|
|
|
2014-11-07 10:53:42 +00:00
|
|
|
// 0x27 is the 'normal' mode with X/Y/Z enable
|
|
|
|
setRegisterSafe(m_addrAcc, CTRL_REG1_A, 0x27);
|
2014-09-05 18:57:56 +01:00
|
|
|
|
2014-11-10 09:20:10 +00:00
|
|
|
// scale can be 2, 4 or 8
|
|
|
|
if (2 == accScale) {
|
2017-04-12 11:49:20 -06:00
|
|
|
setRegisterSafe(m_addrAcc, CTRL_REG4_A, 0x00);
|
2014-11-10 09:20:10 +00:00
|
|
|
} else if (4 == accScale) {
|
2017-04-12 11:49:20 -06:00
|
|
|
setRegisterSafe(m_addrAcc, CTRL_REG4_A, 0x10);
|
2014-11-10 09:20:10 +00:00
|
|
|
} else { // default; equivalent to 8g
|
2017-04-12 11:49:20 -06:00
|
|
|
setRegisterSafe(m_addrAcc, CTRL_REG4_A, 0x30);
|
2014-11-10 09:20:10 +00:00
|
|
|
}
|
2014-09-05 18:57:56 +01:00
|
|
|
|
2014-11-07 10:53:42 +00:00
|
|
|
// 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);
|
2014-09-05 18:57:56 +01:00
|
|
|
|
|
|
|
// 0x00 = continouous conversion mode
|
2014-11-07 10:53:42 +00:00
|
|
|
setRegisterSafe(m_addrMag, MR_REG_M, 0x00);
|
2014-09-05 18:57:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float
|
2017-04-12 11:49:20 -06:00
|
|
|
LSM303DLH::getHeading()
|
2014-09-05 18:57:56 +01:00
|
|
|
{
|
2015-09-02 14:56:13 +03:00
|
|
|
if (getCoordinates() != mraa::SUCCESS) {
|
2014-09-05 18:57:56 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-10 09:20:10 +00:00
|
|
|
float heading = 180.0 * atan2(double(coor[Y]), double(coor[X]))/M_PI;
|
2014-09-05 18:57:56 +01:00
|
|
|
|
2014-11-10 09:20:10 +00:00
|
|
|
if (heading < 0.0)
|
|
|
|
heading += 360.0;
|
2014-09-05 18:57:56 +01:00
|
|
|
|
|
|
|
return heading;
|
|
|
|
}
|
|
|
|
|
2014-11-10 09:20:10 +00:00
|
|
|
int16_t*
|
2017-04-12 11:49:20 -06:00
|
|
|
LSM303DLH::getRawAccelData()
|
2014-09-05 18:57:56 +01:00
|
|
|
{
|
|
|
|
return &accel[0];
|
|
|
|
}
|
|
|
|
|
2014-11-10 09:20:10 +00:00
|
|
|
int16_t*
|
2017-04-12 11:49:20 -06:00
|
|
|
LSM303DLH::getRawCoorData()
|
2014-09-05 18:57:56 +01:00
|
|
|
{
|
|
|
|
return &coor[0];
|
|
|
|
}
|
|
|
|
|
2014-11-10 09:20:10 +00:00
|
|
|
int16_t
|
2017-04-12 11:49:20 -06:00
|
|
|
LSM303DLH::getAccelX()
|
2014-09-18 16:31:58 +01:00
|
|
|
{
|
2017-04-12 11:49:20 -06:00
|
|
|
return accel[X];
|
2014-09-18 16:31:58 +01:00
|
|
|
}
|
|
|
|
|
2014-11-10 09:20:10 +00:00
|
|
|
int16_t
|
2017-04-12 11:49:20 -06:00
|
|
|
LSM303DLH::getAccelY()
|
2014-09-18 16:31:58 +01:00
|
|
|
{
|
2017-04-12 11:49:20 -06:00
|
|
|
return accel[Y];
|
2014-09-18 16:31:58 +01:00
|
|
|
}
|
|
|
|
|
2014-11-10 09:20:10 +00:00
|
|
|
int16_t
|
2017-04-12 11:49:20 -06:00
|
|
|
LSM303DLH::getAccelZ()
|
2014-09-18 16:31:58 +01:00
|
|
|
{
|
2017-04-12 11:49:20 -06:00
|
|
|
return accel[Z];
|
2014-09-18 16:31:58 +01:00
|
|
|
}
|
|
|
|
|
2015-09-02 14:56:13 +03:00
|
|
|
mraa::Result
|
2017-04-12 11:49:20 -06:00
|
|
|
LSM303DLH::getCoordinates()
|
2014-09-05 18:57:56 +01:00
|
|
|
{
|
2015-09-02 14:56:13 +03:00
|
|
|
mraa::Result ret = mraa::SUCCESS;
|
2014-09-05 18:57:56 +01:00
|
|
|
|
|
|
|
memset(&buf[0], 0, sizeof(uint8_t)*6);
|
2015-09-02 14:56:13 +03:00
|
|
|
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);
|
2014-09-05 18:57:56 +01:00
|
|
|
if (num != 6) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
// convert to coordinates
|
|
|
|
for (int i=0; i<3; i++) {
|
2014-11-10 09:20:10 +00:00
|
|
|
coor[i] = (int16_t(buf[2*i] << 8))
|
2017-04-12 11:49:20 -06:00
|
|
|
| int16_t(buf[(2*i)+1]);
|
2014-09-05 18:57:56 +01:00
|
|
|
}
|
2014-11-10 09:20:10 +00:00
|
|
|
// swap elements 1 and 2 to get things in natural XYZ order
|
|
|
|
int16_t t = coor[2];
|
|
|
|
coor[2] = coor[1];
|
|
|
|
coor[1] = t;
|
2014-09-05 18:57:56 +01:00
|
|
|
//printf("X=%x, Y=%x, Z=%x\n", coor[X], coor[Y], coor[Z]);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-10 09:20:10 +00:00
|
|
|
int16_t
|
2017-04-12 11:49:20 -06:00
|
|
|
LSM303DLH::getCoorX() {
|
|
|
|
return coor[X];
|
2014-11-10 09:20:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int16_t
|
2017-04-12 11:49:20 -06:00
|
|
|
LSM303DLH::getCoorY() {
|
|
|
|
return coor[Y];
|
2014-11-10 09:20:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int16_t
|
2017-04-12 11:49:20 -06:00
|
|
|
LSM303DLH::getCoorZ() {
|
|
|
|
return coor[Z];
|
2014-11-10 09:20:10 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 18:57:56 +01:00
|
|
|
// helper function that writes a value to the acc and then reads
|
2014-11-10 09:20:10 +00:00
|
|
|
// FIX: shouldn't this be write-then-read?
|
2014-09-05 18:57:56 +01:00
|
|
|
int
|
2017-04-12 11:49:20 -06:00
|
|
|
LSM303DLH::readThenWrite(uint8_t reg)
|
2014-09-05 18:57:56 +01:00
|
|
|
{
|
2015-09-02 14:56:13 +03:00
|
|
|
m_i2c.address(m_addrAcc);
|
|
|
|
m_i2c.writeByte(reg);
|
|
|
|
m_i2c.address(m_addrAcc);
|
|
|
|
return (int) m_i2c.readByte();
|
2014-09-05 18:57:56 +01:00
|
|
|
}
|
|
|
|
|
2015-09-02 14:56:13 +03:00
|
|
|
mraa::Result
|
2017-04-12 11:49:20 -06:00
|
|
|
LSM303DLH::getAcceleration()
|
2014-09-05 18:57:56 +01:00
|
|
|
{
|
2015-09-02 14:56:13 +03:00
|
|
|
mraa::Result ret = mraa::SUCCESS;
|
2014-09-05 18:57:56 +01:00
|
|
|
|
2014-11-10 09:20:10 +00:00
|
|
|
accel[X] = (int16_t(readThenWrite(OUT_X_H_A)) << 8)
|
2017-04-12 11:49:20 -06:00
|
|
|
| int16_t(readThenWrite(OUT_X_L_A));
|
2014-11-10 09:20:10 +00:00
|
|
|
accel[Y] = (int16_t(readThenWrite(OUT_Y_H_A)) << 8)
|
2017-04-12 11:49:20 -06:00
|
|
|
| int16_t(readThenWrite(OUT_Y_L_A));
|
2014-11-10 09:20:10 +00:00
|
|
|
accel[Z] = (int16_t(readThenWrite(OUT_Z_H_A)) << 8)
|
2017-04-12 11:49:20 -06:00
|
|
|
| int16_t(readThenWrite(OUT_Z_L_A));
|
2014-09-05 18:57:56 +01:00
|
|
|
//printf("X=%x, Y=%x, Z=%x\n", accel[X], accel[Y], accel[Z]);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2014-11-07 10:53:42 +00:00
|
|
|
|
|
|
|
// helper function that sets a register and then checks the set was succesful
|
2015-09-02 14:56:13 +03:00
|
|
|
mraa::Result
|
2017-04-12 11:49:20 -06:00
|
|
|
LSM303DLH::setRegisterSafe(uint8_t slave, uint8_t sregister, uint8_t data)
|
2014-11-07 10:53:42 +00:00
|
|
|
{
|
|
|
|
buf[0] = sregister;
|
|
|
|
buf[1] = data;
|
2015-09-10 13:26:21 -06:00
|
|
|
|
2015-09-02 14:56:13 +03:00
|
|
|
if (m_i2c.address(slave) != mraa::SUCCESS) {
|
2015-09-10 13:26:21 -06:00
|
|
|
throw std::invalid_argument(std::string(__FUNCTION__) +
|
|
|
|
": mraa_i2c_address() failed");
|
2015-09-02 14:56:13 +03:00
|
|
|
return mraa::ERROR_INVALID_HANDLE;
|
2014-11-07 10:53:42 +00:00
|
|
|
}
|
2015-09-02 14:56:13 +03:00
|
|
|
if (m_i2c.write(buf, 2) != mraa::SUCCESS) {
|
2015-09-10 13:26:21 -06:00
|
|
|
throw std::invalid_argument(std::string(__FUNCTION__) +
|
|
|
|
": mraa_i2c_write() failed");
|
2015-09-02 14:56:13 +03:00
|
|
|
return mraa::ERROR_INVALID_HANDLE;
|
2014-11-07 10:53:42 +00:00
|
|
|
}
|
2015-09-02 14:56:13 +03:00
|
|
|
uint8_t val = m_i2c.readReg(sregister);
|
2014-11-07 10:53:42 +00:00
|
|
|
if (val != data) {
|
2015-09-10 13:26:21 -06:00
|
|
|
throw std::invalid_argument(std::string(__FUNCTION__) +
|
|
|
|
": failed to set register correctly");
|
2015-09-02 14:56:13 +03:00
|
|
|
return mraa::ERROR_UNSPECIFIED;
|
2014-11-07 10:53:42 +00:00
|
|
|
}
|
2015-09-02 14:56:13 +03:00
|
|
|
return mraa::SUCCESS;
|
2014-11-07 10:53:42 +00:00
|
|
|
}
|