Giter Site home page Giter Site logo

node-lsm303's Introduction

Node.js LSM303 Accelerometer and Magnetometer Module

This module allows you to integrate LSM303 in your Node.js Project

Beaglebone Black Setup

Connect the SCL and SDA of LSM303 on P9_19 and P9_20 respectively. Check whether the device is connected properly using i2cdetect ( #i2cdetect -r 1 ).

This module takes 0x19 and 0x1e as the default addresses for Accelerometer and Magnetometer respectively. This can be changed by passing the address during device object creation. Similarly the device path can also be changed.

Installation

npm install lsm303

Usage

Checkout example/example1.js for basic usage Magnetometer calibration application also available in examples

var lsm303 = require('lsm303');

var ls  = new lsm303();

var accel = ls.accelerometer();
var mag = ls.magnetometer();

accel.readAxes(function(err,axes){
    if(err){
        console.log("Error reading Accelerometer Axes : " + err);
    }
    if (axes) {
        console.log(axes);
    }
});

mag.readAxes(function(err,axes){
    if(err){
        console.log("Error reading Magnetometer Axes : " + err);
    }
    if (axes) {
        console.log(axes);
    }
});

// Non-tilt-compensated readHeading function
mag.readHeading(function(err, heading){
		if(err){
				console.log("Error reading Magnetometer Heading : " + err);
      }   
      if (heading) {
        headingTemp = heading;
      }   
    });

mag.readTemp(function(err,temp){
    if(err){
        console.log("Error reading Temperature : " + err);
    }
    if (temp) {
        console.log(temp);
    }
});

node-lsm303's People

Contributors

actionnerd avatar damoclark avatar ir-fuel avatar praneshkmr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-lsm303's Issues

Is it possible to add tilt compensation feature on magnetometer without using another sensor?

Magnetometer (compass) skews when tilted out of the XY plane. Investigate if it is practical to try to implement a tilt-compensation feature.

From ST Micro paper on tilt:

Some microcontrollers may not support floating point operation and are timing critical. In
order to build a standalone tilt compensated electronic compass, the following
recommendations may be helpful:
● Use look-up tables for sin, cos, arcsin, and arctan functions to reduce clock cycles
● Use assembly code to implement signed integer multiplication and division subroutines
to reduce clock cycles
● If some sensor calibration parameters are very small, the user can multiply the whole
accelerometer and magnetic sensor calibration parameter matrix with a big constant
integer, then divide it before the pitch/roll/heading calculation
● Use internal EEPROM to save sensor calibration parameters
● Implement some kind of digital filtering or simple moving average function onto the
sensor raw measurements to reduce the noise level and improve the pitch/roll/heading
accuracy

Pololu Github repo on tilt compensation for this sensor controlled with a ATMega chip is here.

Feature Request for Calibration Values Inclusion

Hi!

Love the library. I have used this pretty extensively over the past few days. I would like to propose a few feature requests. I've done the coding for them, however, I've never done pull requests, so you will have to bear with me.

I still have a tiny bit of work to do, but it will mostly be in util.js:

Utils.x_offset = 0;
Utils.y_offset = 0;
Utils.z_offset = 0;

And then a config function to change these from a main program:

Utils.setOffset = function(x, y, z) {
  this.x_offset = x;
  this.y_offset = y;
  this.z_offset = z;
  return;
}

And finally, an addition to buffToXYZMag that would look like this for x, y, and z:

    x: (this.twoscomp((buffer[0] << 8) | buffer[1],16) - this.x_offset),

I would love to contribute here, and if you give me a day or two, I will have the pull request ready.

i2c error

When I try installing this package, I get this error.

make: Entering directory '/home/pi/zem1dash/backend/node_modules/i2c/build'
  CXX(target) Release/obj.target/i2c/src/i2c.o
In file included from ../node_modules/nan/nan.h:190:0,
                 from ../src/i2c.cc:3:
../node_modules/nan/nan_maybe_43_inl.h: In function ‘Nan::Maybe<bool> Nan::ForceSet(v8::Local<v8::Object>, v8::Local<v8::Value>, v8::Local<v8::Value>, v8::PropertyAttribute)’:
../node_modules/nan/nan_maybe_43_inl.h:88:15: error: ‘class v8::Object’ has no member named ‘ForceSet’
   return obj->ForceSet(GetCurrentContext(), key, value, attribs);
               ^
In file included from ../src/i2c.cc:3:0:
../node_modules/nan/nan.h: In function ‘v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object>, v8::Local<v8::Function>, int, v8::Local<v8::Value>*)’:
../node_modules/nan/nan.h:817:60: warning: ‘v8::Local<v8::Value> node::MakeCallback(v8::Isolate*, v8::Local<v8::Object>, v8::Local<v8::Function>, int, v8::Local<v8::Value>*)’ is deprecated (declared at /home/pi/.node-gyp/10.11.0/include/node/node.h:174): Use MakeCallback(..., async_context) [-Wdeprecated-declarations]
         v8::Isolate::GetCurrent(), target, func, argc, argv);
                                                            ^
../node_modules/nan/nan.h: In function ‘v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object>, v8::Local<v8::String>, int, v8::Local<v8::Value>*)’:
../node_modules/nan/nan.h:831:62: warning: ‘v8::Local<v8::Value> node::MakeCallback(v8::Isolate*, v8::Local<v8::Object>, v8::Local<v8::String>, int, v8::Local<v8::Value>*)’ is deprecated (declared at /home/pi/.node-gyp/10.11.0/include/node/node.h:167): Use MakeCallback(..., async_context) [-Wdeprecated-declarations]
         v8::Isolate::GetCurrent(), target, symbol, argc, argv);
                                                              ^
../node_modules/nan/nan.h: In function ‘v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object>, const char*, int, v8::Local<v8::Value>*)’:
../node_modules/nan/nan.h:845:62: warning: ‘v8::Local<v8::Value> node::MakeCallback(v8::Isolate*, v8::Local<v8::Object>, const char*, int, v8::Local<v8::Value>*)’ is deprecated (declared at /home/pi/.node-gyp/10.11.0/include/node/node.h:160): Use MakeCallback(..., async_context) [-Wdeprecated-declarations]
         v8::Isolate::GetCurrent(), target, method, argc, argv);
                                                              ^
../node_modules/nan/nan.h: In member function ‘v8::Local<v8::Value> Nan::Callback::Call_(v8::Isolate*, v8::Local<v8::Object>, int, v8::Local<v8::Value>*) const’:
../node_modules/nan/nan.h:1463:5: warning: ‘v8::Local<v8::Value> node::MakeCallback(v8::Isolate*, v8::Local<v8::Object>, v8::Local<v8::Function>, int, v8::Local<v8::Value>*)’ is deprecated (declared at /home/pi/.node-gyp/10.11.0/include/node/node.h:174): Use MakeCallback(..., async_context) [-Wdeprecated-declarations]
     ));
     ^
../src/i2c.cc: In function ‘void Open(const Nan::FunctionCallbackInfo<v8::Value>&)’:
../src/i2c.cc:84:35: warning: ‘v8::String::Utf8Value::Utf8Value(v8::Local<v8::Value>)’ is deprecated (declared at /home/pi/.node-gyp/10.11.0/include/node/v8.h:2887): Use Isolate version [-Wdeprecated-declarations]
   String::Utf8Value device(info[0]);
                                   ^
i2c.target.mk:99: recipe for target 'Release/obj.target/i2c/src/i2c.o' failed
make: *** [Release/obj.target/i2c/src/i2c.o] Error 1
make: Leaving directory '/home/pi/zem1dash/backend/node_modules/i2c/build'
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/opt/nodejs/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:262:23)
gyp ERR! stack     at ChildProcess.emit (events.js:182:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12)
gyp ERR! System Linux 4.14.79-v7+
gyp ERR! command "/opt/nodejs/bin/node" "/opt/nodejs/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/pi/zem1dash/backend/node_modules/i2c
gyp ERR! node -v v10.11.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok 
npm WARN [email protected] No description
npm WARN [email protected] No repository field.
npm WARN [email protected] No license field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/pi/.npm/_logs/2019-02-21T14_50_44_897Z-debug.log

Magnetometer and Temperature never change

Any ideas why the Magnetometer and the temp sensor values never change?

Results:

Accel: { x: -1.9, y: -1.9, z: -1.9 }
Mag: { x: 0, z: 0, y: 0 }
Non-Tilt-Comp-Heading: 270
Temp: { temp: 18 }
Accel: { x: -1.8, y: -1.8, z: -1.8 }
Mag: { x: 0, z: 0, y: 0 }
Non-Tilt-Comp-Heading: 270
Temp: { temp: 18 }

Only modifications I made to the module was changing the location of where my lsm303c lives on the i2c.

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- 1d 1e --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

'1d' is the accel
'1e' is the mag

'20' is a GPIO expander

https://learn.sparkfun.com/tutorials/lsm303c-6dof-hookup-guide

Feature Request for Heading

I'm interested in contributing a feature for heading. This would allow a person to get straight magnetic heading 0 - 360 rather than have to fuss with translating cartesian to polar. It would require an addition of a toPolar function in util.js:

var toPolar = function(x, y) { // returns polar coordinates as an object (radians)
  var polarCoords = {};
  polarCoords.r = Math.sqrt(x * x + y * y);
  // atan2 provides CCW angle from the positive x axis; this piece normalizes it
  // to 0 - 2Pi (0 - 360 in radians) from the positive Y axis
  polarCoords.theta = Math.PI / 2 - Math.atan2(y, x);
  if ( polarCoords.theta < 0 ) {
    polarCoords.theta += 2 * Math.PI;
  }
  return (360 - (180 / Math.PI * polarCoords.theta)); // Returning theta alone (heading)
}

And then, the addition of a buffToHead function:

Utils.buffToHeadMag = function(buffer){
  var pos;
  pos = {
    x: (this.twoscomp((buffer[0] << 8) | buffer[1],16) - this.x_offset),
    z: (this.twoscomp((buffer[2] << 8) | buffer[3],16) - this.z_offset),
    y: (this.twoscomp((buffer[4] << 8) | buffer[5],16) - this.y_offset)
  }
  return toPolar(pos.x, pos.y);
}

Then, the addition of a readHeading function in Magnetometer:

Magnetometer.prototype.readHeading = function(callback){
    this.mag.readBytes(0x03, 6, function(err, res) {
        callback(err, utils.buffToHeadMag(res));
    });
}

I know there is still a little work to be done there, but I think I can have it ready in a few days. Thoughts?

Add calibration example

Add quick example showing how to get min / max values from readAxes. Complements wiki. Change example code in wiki to links to new example code.

I'll submit sometime this week. I may need a tiny bit of help doing the merge.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.