A NodeJS driver for the QMC5883L magnetometer
npm install nodejs-qmc5883lnpm install --save nodejs-qmc5883l. Every magnetometer reading is a bit mistaken (caused e.g. by soft and hard iron effect), but that can be corrected by calibrating the compass. Calibration gives you an offset matrix and a scale matrix that needs to be applied on raw magnetometer data before any further computation. To do this, run node calibrate in the module's main directory, rotate the magnetometer in all directions until the script finishes running and the matrixes will be printed in the console, and then call setOffsetMatrix and setScaleMatrix on the module object. After doing so, you can read the compensated data via the readCorrectedData() function (if you use the readAzimuth function, then the azimuth is computed on the already compensated data).
node
var compass = require("nodejs-qmc5883l");
`
_1. initialize()_
- always returns true (in the future will return false if the compass cannot be initialized)
- is obligatory to be called before any data is read
- sets the compass mode to continuous, output data rate to 200Hz, the RNG to 8 Gauss and over-sampling ratio to 256
_2. readRawData()_
- returns a javascript Object of format { x: value, y: value, z: value } with raw magnetometer data (without scale & offset matrixes)
- if the initialize() function had not been called earlier, the x, y & z value are 0
_3. readCorrectedData()_
- returns a javascript Object of format { x: value, y: value, z: value } with compensated magnetometer data (with scale & offset matrixes applied)
- if the initialize() function had not been called earlier, the x, y & z value are 0
_4. setOffsetMatrix(offsetX, offsetY, offsetZ)_
- sets the offset matrix used by the readCorrectedData()
_5. setScaleMatrix(scaleX, scaleY, scaleZ)_
- sets the scale matrix used by the readCorrectedData()
_6. readAzimuth()_
- returns an azimuth value (calculated using the atan2 function) in degrees
- it takes the readCorrectedData() function for data input
- if the initialize() function had not been called earlier, returns 0
_7. setDeclinationAngle(declinationAngle)_
- takes a single declinationAngle parameter of any number type
- sets the declination angle that is added to the azimuth when calling readAzimuth() to correct the magnetic field differences
- you can calculate the angle using the following formula: declinationAngle = (degrees + (minutes / 60.0)) / (180 / PI)
_(see test.js)_
4. Example
A functional example is located in the test.js file in the module's main directory (you can also run it by executing npm run test` in it).