rwaldron / galileo-io

Intel Galileo & Intel Edison IO Plugin for Johnny-Five
http://johnny-five.io
MIT License
101 stars 26 forks source link

Possible Issue with I2C? #22

Closed ashishdatta closed 9 years ago

ashishdatta commented 9 years ago

Hello I really hope this is the right place for this...I am currently having some issues trying to get this Grove 3-axis accelerometer ADXL345 to work....

My code:

var Galileo = require('galileo-io');
var board   = new Galileo();

var accel = 
{
    0x53: "ADDRESS",
    0x32: "DATAX0",
    0x34: "DATAY0",
    0x2D: "POWER_CTL"
}

/* Shameless stolen from the blinkm tutorial...*/
Object.keys(accel).forEach(function(key) {
  // Turn the value into a key and
  // the key into an int value
  accel[accel[key]] = key | 0;
});

board.on("ready", function() {
  console.log("READY");
    this.i2cWrite(accel.ADDRESS, accel.POWER_CTL, 0);
    this.i2cWrite(accel.ADDRESS, accel.POWER_CTL, 16);
    this.i2cWrite(accel.ADDRESS, accel.POWER_CTL, 8);

    setInterval(function() {
        this.i2cRead(accel.ADDRESS, accel.DATAX0, 6, function(data) {
            console.log("X", data[1] << 8 | data[0]);
            console.log("Y", data[3] << 8 | data[2]);
            console.log("Z", data[5] << 8 | data[4]);

        });
    }.bind(this), 1000);
});

I am getting an output of

X 49135
Y 61373
Z 48575
X 49135
Y 61373
Z 48575
X 61256
Y 48575
Z 2563
X 49135
Y 61373
Z 48575

Now the weird part is that I am getting these values even when the sensor is unplugged... I know the address is correct because I RTFM ;) Also the i2c bus sees the address: i2cdetect -y -r 6 The output is

root@irelia:~# i2cdetect -y -r 6
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- 53 -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --  

I am still pretty new to I2C (yeah Im that bad), but I know that this data has to be faulty....so does anyone think that there is a problem with my code or maybe an issue with Galileo-IO i2c function? really at a loss at the moment again sorry if I am posting this in the wrong place.

PS: My MRAA version

root@irelia:~# opkg info libmraa0
Package: libmraa0
Version: 0.5.3.2

Verison

root@irelia:~# cat /etc/version
edison-weekly_build_68_2014-09-08_13-49-07

Also the blinkm example posted under examples worked on my blinkm...

rwaldron commented 9 years ago

This is super helpful. I think I have one of these sensors, but I have to look. Will post back as soon as I know more things :)

rwaldron commented 9 years ago

Turns out I have every single other model, but not this one. I should've looked on Sunday, would've been able to order on Monday :(

rwaldron commented 9 years ago

Doing some research here, and I think that there is nothing wrong with the i2c implementation in Galileo-IO. This sensor requires some finessing of the values it returns. The following is untested, but I believe it should be something like this:

var accel = {
  0x53: "ADDRESS",
  0x2D: "POWER_CTL", 
  0x31: "RANGE", 
  0xB2: "ALL_DATA"
};

/* Shameless stolen from the blinkm tutorial...*/
Object.keys(accel).forEach(function(key) {
  // Turn the value into a key and
  // the key into an int value
  accel[accel[key]] = key | 0;
});

board.on("ready", function() {

  var sensitivity = 0.00390625;

  // This is required to enable I2C
  this.i2cConfig();

  // Standby mode
  this.i2cWrite(accel.ADDRESS, accel.POWER_CTL, 0);

  // Enable measurements
  this.i2cWrite(accel.ADDRESS, accel.POWER_CTL, 8);

  // Set range (this is 2G range)
  this.i2cWrite(accel.ADDRESS, accel.RANGE, 8);

  // NOTE: This interval will soon be unnecessary. Upgrades
  // the IO plugin i2c read protocol will allow for continuous reads.
  setInterval(function() {
    // This will read every axis, previously only X was being read.
    this.i2cRead(accel.ADDRESS, accel.ALL_DATA, 6, function(data) {

      var x = (data[1] << 8) | data[0];
      var y = (data[3] << 8) | data[2];
      var z = (data[5] << 8) | data[4];

      // Wrap and clamp 16 bits; 
      var X = (x >> 15 ? ((x ^ 0xFFFF) + 1) * -1 : x) * sensitivity;
      var Y = (y >> 15 ? ((y ^ 0xFFFF) + 1) * -1 : y) * sensitivity;
      var Z = (z >> 15 ? ((z ^ 0xFFFF) + 1) * -1 : z) * sensitivity;

      console.log("X: ", X);
      console.log("Y: ", Y);
      console.log("Z: ", Z);

    });
  }.bind(this), 1000);
});

That aside, once the Johnny-Five Accelerometer class is refactored for device profiles, this will all be handled for you.

ashishdatta commented 9 years ago

Ah okay, so as I suspected my I2c knowledge was lacking and led to these errors. Thanks!

rwaldron commented 9 years ago

No problem! Glad we could get this sorted out :)