From d9866544550db521d0137ed0df0fb3618196771b Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Fri, 25 Apr 2014 14:48:43 +0100 Subject: [PATCH] initial commit of upm with hmc5883l support Signed-off-by: Brendan Le Foll --- CMakeLists.txt | 14 ++++ README | 5 ++ examples/CMakeLists.txt | 5 ++ examples/compass.cxx | 34 ++++++++++ src/CMakeLists.txt | 1 + src/hmc5883l/CMakeLists.txt | 5 ++ src/hmc5883l/hmc5883l.cxx | 130 ++++++++++++++++++++++++++++++++++++ src/hmc5883l/hmc5883l.h | 45 +++++++++++++ 8 files changed, 239 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 README create mode 100644 examples/CMakeLists.txt create mode 100644 examples/compass.cxx create mode 100644 src/CMakeLists.txt create mode 100644 src/hmc5883l/CMakeLists.txt create mode 100644 src/hmc5883l/hmc5883l.cxx create mode 100644 src/hmc5883l/hmc5883l.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..93958359 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required (VERSION 2.8) +project (upm) + +set (SWIG_EXECUTABLE /usr/bin/swig) +find_package (SWIG REQUIRED) +include (${SWIG_USE_FILE}) + +find_package(PkgConfig REQUIRED) +pkg_check_modules(MAA maa>=0.1.1) + +set (CMAKE_SWIG_FLAGS "") + +add_subdirectory (src) +add_subdirectory (examples) diff --git a/README b/README new file mode 100644 index 00000000..c0f6fd04 --- /dev/null +++ b/README @@ -0,0 +1,5 @@ +UPM - Sensor/Actuator repository for Maa + +UPM is a high level repository for sensors that use maa. + +For more information on maa, see the maa README diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 00000000..c06fc58a --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable (compass compass.cxx) + +include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) + +target_link_libraries (compass hmc5883l) diff --git a/examples/compass.cxx b/examples/compass.cxx new file mode 100644 index 00000000..c72337ae --- /dev/null +++ b/examples/compass.cxx @@ -0,0 +1,34 @@ +/* + * Author: Brendan Le Foll + * Copyright (c) 2014 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 "hmc5883l.h" + +int +main(int argc, char **argv) +{ + upm::Hmc5883l* compass = new upm::Hmc5883l(); + fprintf(stdout, "heading: %f\n", compass->heading()); + + return 0; +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 00000000..d665c158 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory (hmc5883l) diff --git a/src/hmc5883l/CMakeLists.txt b/src/hmc5883l/CMakeLists.txt new file mode 100644 index 00000000..5baf4278 --- /dev/null +++ b/src/hmc5883l/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library (hmc5883l SHARED hmc5883l.cxx) + +include_directories (${MAA_INCLUDE_DIR}) + +target_link_libraries (hmc5883l ${MAA_LIBRARIES}) diff --git a/src/hmc5883l/hmc5883l.cxx b/src/hmc5883l/hmc5883l.cxx new file mode 100644 index 00000000..9ebd6dad --- /dev/null +++ b/src/hmc5883l/hmc5883l.cxx @@ -0,0 +1,130 @@ +/* + * Author: Brendan Le Foll + * Copyright (c) 2014 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 "math.h" +#include "hmc5883l.h" + +#define MAX_BUFFER_LENGTH 6 +#define HMC5883L_I2C_ADDR 0x1E + +//configuration registers +#define HMC5883L_CONF_REG_A 0x00 +#define HMC5883L_CONF_REG_B 0x01 + +//mode register +#define HMC5883L_MODE_REG 0x02 + +//data register +#define HMC5883L_X_MSB_REG 0 +#define HMC5883L_X_LSB_REG 1 +#define HMC5883L_Z_MSB_REG 2 +#define HMC5883L_Z_LSB_REG 3 +#define HMC5883L_Y_MSB_REG 4 +#define HMC5883L_Y_LSB_REG 5 +#define DATA_REG_SIZE 6 + +//status register +#define HMC5883L_STATUS_REG 0x09 + +//ID registers +#define HMC5883L_ID_A_REG 0x0A +#define HMC5883L_ID_B_REG 0x0B +#define HMC5883L_ID_C_REG 0x0C + +#define HMC5883L_CONT_MODE 0x00 +#define HMC5883L_DATA_REG 0x03 + +//scales +#define GA_0_88_REG 0x00 << 5 +#define GA_1_3_REG 0x01 << 5 +#define GA_1_9_REG 0x02 << 5 +#define GA_2_5_REG 0x03 << 5 +#define GA_4_0_REG 0x04 << 5 +#define GA_4_7_REG 0x05 << 5 +#define GA_5_6_REG 0x06 << 5 +#define GA_8_1_REG 0x07 << 5 + +//digital resolutions +#define SCALE_0_73_MG 0.73 +#define SCALE_0_92_MG 0.92 +#define SCALE_1_22_MG 1.22 +#define SCALE_1_52_MG 1.52 +#define SCALE_2_27_MG 2.27 +#define SCALE_2_56_MG 2.56 +#define SCALE_3_03_MG 3.03 +#define SCALE_4_35_MG 4.35 + +using namespace upm; + +Hmc5883l::Hmc5883l() +{ + i2c = new maa::I2CSlave(26, 27); + + i2c->address(HMC5883L_I2C_ADDR); + rx_tx_buf[0] = HMC5883L_CONF_REG_B; + rx_tx_buf[1] = GA_1_3_REG; + i2c->write(rx_tx_buf, 2); + + i2c->address(HMC5883L_I2C_ADDR); + rx_tx_buf[0] = HMC5883L_MODE_REG; + rx_tx_buf[1] = HMC5883L_CONT_MODE; + i2c->write(rx_tx_buf, 2); + + Hmc5883l::update(); +} + +int +Hmc5883l::update(void) +{ + i2c->address(HMC5883L_I2C_ADDR); + i2c->write(HMC5883L_DATA_REG); + + i2c->address(HMC5883L_I2C_ADDR); + i2c->read(rx_tx_buf, DATA_REG_SIZE); + + // x + coor[0] = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_X_LSB_REG] ; + // z + coor[2] = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_Z_LSB_REG] ; + // y + coor[1] = (rx_tx_buf[HMC5883L_Y_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_Y_LSB_REG] ; +} + +float +Hmc5883l::direction(void) +{ + return atan2(coor[1] * SCALE_0_92_MG, coor[0] * SCALE_0_92_MG); +} + +float +Hmc5883l::heading(void) +{ + return Hmc5883l::direction() * 180/M_PI; +} + +int* +Hmc5883l::coordinates(void) +{ + return &coor[0]; +} diff --git a/src/hmc5883l/hmc5883l.h b/src/hmc5883l/hmc5883l.h new file mode 100644 index 00000000..33f32197 --- /dev/null +++ b/src/hmc5883l/hmc5883l.h @@ -0,0 +1,45 @@ +/* + * Author: Brendan Le Foll + * Copyright (c) 2014 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. + */ +#pragma once + +#include + +#define MAX_BUFFER_LENGTH 6 + +namespace upm { + +class Hmc5883l { +public: + Hmc5883l(); + float direction(); + float heading(); + int* coordinates(); + int update(); +private: + int coor[3]; + char rx_tx_buf[MAX_BUFFER_LENGTH]; + maa::I2CSlave* i2c; +}; + +}