Home | History | Annotate | Download | only in java
      1 /*
      2  * Author: Brendan Le Foll <brendan.le.foll (at) intel.com>
      3  * Author: Thomas Ingleby <thomas.c.ingleby (at) intel.com>
      4  * Copyright (c) 2014 Intel Corporation.
      5  * Author: Petre Eftime <petre.p.eftime (at) intel.com>
      6  * Copyright (c) 2015 Intel Corporation.
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining
      9  * a copy of this software and associated documentation files (the
     10  * "Software"), to deal in the Software without restriction, including
     11  * without limitation the rights to use, copy, modify, merge, publish,
     12  * distribute, sublicense, and/or sell copies of the Software, and to
     13  * permit persons to whom the Software is furnished to do so, subject to
     14  * the following conditions:
     15  *
     16  * The above copyright notice and this permission notice shall be
     17  * included in all copies or substantial portions of the Software.
     18  *
     19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     26  */
     27 
     28 import mraa.I2c;
     29 
     30 public class I2cCompass {
     31     static {
     32         try {
     33             System.loadLibrary("mraajava");
     34         } catch (UnsatisfiedLinkError e) {
     35             System.err.println(
     36                     "Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" +
     37                             e);
     38             System.exit(1);
     39         }
     40     }
     41 
     42     final static byte MAX_BUFFER_LENGTH = 6;
     43     final static byte CONF_BUFFER_LENGTH = 2;
     44     final static short HMC5883L_I2C_ADDR = 0x1E;
     45 
     46     // configuration registers
     47     final static byte HMC5883L_CONF_REG_A = 0x00;
     48     final static byte HMC5883L_CONF_REG_B = 0x01;
     49 
     50     // mode register
     51     final static byte HMC5883L_MODE_REG = 0x02;
     52 
     53     // data register
     54     final static byte HMC5883L_X_MSB_REG = 0;
     55     final static byte HMC5883L_X_LSB_REG = 1;
     56     final static byte HMC5883L_Z_MSB_REG = 2;
     57     final static byte HMC5883L_Z_LSB_REG = 3;
     58     final static byte HMC5883L_Y_MSB_REG = 4;
     59     final static byte HMC5883L_Y_LSB_REG = 5;
     60     final static byte DATA_REG_SIZE = 6;
     61 
     62     // status register
     63     final static byte HMC5883L_STATUS_REG = 0x09;
     64 
     65     // ID registers
     66     final static byte HMC5883L_ID_A_REG = 0x0A;
     67     final static byte HMC5883L_ID_B_REG = 0x0B;
     68     final static byte HMC5883L_ID_C_REG = 0x0C;
     69 
     70     final static byte HMC5883L_CONT_MODE = 0x00;
     71     final static byte HMC5883L_DATA_REG = 0x03;
     72 
     73     // scales
     74     final static byte GA_0_88_REG = ((byte) (0x00 << 5));
     75     final static byte GA_1_3_REG = ((byte) (0x01 << 5));
     76     final static byte GA_1_9_REG = ((byte) (0x02 << 5));
     77     final static byte GA_2_5_REG = ((byte) (0x03 << 5));
     78     final static byte GA_4_0_REG = ((byte) (0x04 << 5));
     79     final static byte GA_4_7_REG = ((byte) (0x05 << 5));
     80     final static byte GA_5_6_REG = ((byte) (0x06 << 5));
     81     final static byte GA_8_1_REG = ((byte) (0x07 << 5));
     82 
     83     // digital resolutions
     84     final static float SCALE_0_73_MG = 0.73f;
     85     final static float SCALE_0_92_MG = 0.92f;
     86     final static float SCALE_1_22_MG = 1.22f;
     87     final static float SCALE_1_52_MG = 1.52f;
     88     final static float SCALE_2_27_MG = 2.27f;
     89     final static float SCALE_2_56_MG = 2.56f;
     90     final static float SCALE_3_03_MG = 3.03f;
     91     final static float SCALE_4_35_MG = 4.35f;
     92 
     93     public static void main(String[] args) throws InterruptedException {
     94         float direction = 0;
     95         int x, y, z;
     96         byte[] rx_tx_buf = new byte[MAX_BUFFER_LENGTH];
     97         byte[] conf_buf = new byte[CONF_BUFFER_LENGTH];
     98 
     99         //! [Interesting]
    100         I2c i2c = new I2c(0);
    101 
    102         i2c.address(HMC5883L_I2C_ADDR);
    103         conf_buf[0] = HMC5883L_CONF_REG_B;
    104         conf_buf[1] = GA_1_3_REG;
    105         i2c.write(conf_buf);
    106         //! [Interesting]
    107 
    108         i2c.address(HMC5883L_I2C_ADDR);
    109         conf_buf[0] = HMC5883L_MODE_REG;
    110         conf_buf[1] = HMC5883L_CONT_MODE;
    111         i2c.write(conf_buf);
    112 
    113         while (true) {
    114             i2c.address(HMC5883L_I2C_ADDR);
    115             i2c.writeByte(HMC5883L_DATA_REG);
    116 
    117             i2c.address(HMC5883L_I2C_ADDR);
    118             i2c.read(rx_tx_buf);
    119 
    120             x = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8) | rx_tx_buf[HMC5883L_X_LSB_REG];
    121             z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8) | rx_tx_buf[HMC5883L_Z_LSB_REG];
    122             y = (rx_tx_buf[HMC5883L_Y_MSB_REG] << 8) | rx_tx_buf[HMC5883L_Y_LSB_REG];
    123 
    124             direction = (float) Math.atan2(y * SCALE_0_92_MG, x * SCALE_0_92_MG);
    125 
    126             if (direction < 0)
    127                 direction += 2 * Math.PI;
    128 
    129             System.out.println(String.format("Compass scaled data x : %f, y : %f, z : %f\n", x * SCALE_0_92_MG, y * SCALE_0_92_MG,
    130                     z * SCALE_0_92_MG));
    131             System.out.println(String.format("Heading : %f\n", direction * 180 / Math.PI));
    132             Thread.sleep(1000);
    133         }
    134     }
    135 }
    136