Hi @Gordon. It’s tempting to go to module on this project but it’s not quite ready for that as yet. There are a number of functions that have yet to be translated. The most important one being the gyroscope calibration function. There are interrupt functions that would be difficult to use with the Sparkfun board but could be of use with the more expensive Adafruit board that brings forth the interrupt pins. https://www.sparkfun.com/products/13944 https://www.adafruit.com/product/2021
As to the topic of modules, I’m looking at a module within a module design for the final product.
The lowest level module uses a table to initialize the chip and functions to read the data.
var xgstack=
[
{ "address": 107, "sub": 16, "data": 0 },
{ "address": 107, "sub": 17, "data": 0 },
{ "address": 107, "sub": 18, "data": 0 },
{ "address": 107, "sub": 30, "data": 58 },
{ "address": 107, "sub": 19, "data": 0 },
{ "address": 107, "sub": 31, "data": 56 },
{ "address": 107, "sub": 32, "data": 160 },
{ "address": 107, "sub": 33, "data": 0 }
];
var mstack=
[
{ "address": 30, "sub": 32, "data": 28 },
{ "address": 30, "sub": 33, "data": 0 },
{ "address": 30, "sub": 34, "data": 0 },
{ "address": 30, "sub": 35, "data": 12 },
{ "address": 30, "sub": 36, "data": 0 }
];
LSM9DS1.prototype.run=function(){
var i;
// To verify communication, we can read from the WHO_AM_I register //of each device. Store those in a variable so we can return them.
var mTest = this.mReadByte(Mags.WHO_AM_I_M);// Read the gyro
var xgTest = this.xgReadByte(Regs.WHO_AM_I_XG);//Read the accel/mag
var whoAmICombined = (xgTest << 8) | mTest;
console.log("who= ",whoAmICombined);
if (whoAmICombined != ((WHO_AM_I_AG_RSP << 8) | WHO_AM_I_M_RSP))
return 0;
for(i=0; i<this.xgstack.length;i++){
console.log(this.xgstack[i].address, this.xgstack[i].sub,this.xgstack[i].data);
this.i2c.writeTo(this.xgstack[i].address, this.xgstack[i].sub,this.xgstack[i].data);
}
for(i=0; i<this.mstack.length;i++){
console.log(this.mstack[i].address, this.mstack[i].sub,this.mstack[i].data);
this.i2c.writeTo(this.mstack[i].address, this.mstack[i].sub,this.mstack[i].data);
}
return whoAmICombined;
};
See attached file: aTestLSM9DS1_a.js
At the next level of module, which invokes the first level module, the init function parses the options and the begin function populates the xgstack and mstack arrays.
This mod level module uses more memory resources but allows the tweaking of the options. Once the user is satisfied with the tweak, the stacks and some other variables are copied and pasted into a new program that uses only the first module. This will reduce the memory usage in a final program.
A similar approach seems possible for adding the calibration and interrupt functions.
A lowest level module example
//TestM1.js 1 Dec 2016
function LSM9DS1() {
}
LSM9DS1.prototype.add=function(){
this.a=1;
this.b=20;
console.log(this.a+this.b);
};
// Create an instance of LSM9DS1
exports.connect = function() {
return new LSM9DS1();
};
A module one level up:
/TestM2.js 1 Dec 2016
exports.connect = function() {
var x= require("testm1").connect();
x.d=98;
x.sub= function() {
console.log(this.a-this.b);
};
return x;
};
Invoke TestM1
//tryTestM1.js 1 Dec 2016
var ads =require("testm1").connect();
ads.add();
Invoke TestM2
//tryTestM2.js 1 Dec 2016
var ads =require("testm2").connect();
ads.add();
console.log(ads.d);
ads.sub();
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hi @Gordon. It’s tempting to go to module on this project but it’s not quite ready for that as yet. There are a number of functions that have yet to be translated. The most important one being the gyroscope calibration function. There are interrupt functions that would be difficult to use with the Sparkfun board but could be of use with the more expensive Adafruit board that brings forth the interrupt pins.
https://www.sparkfun.com/products/13944
https://www.adafruit.com/product/2021
As to the topic of modules, I’m looking at a module within a module design for the final product.
The lowest level module uses a table to initialize the chip and functions to read the data.
See attached file: aTestLSM9DS1_a.js
At the next level of module, which invokes the first level module, the init function parses the options and the begin function populates the xgstack and mstack arrays.
This mod level module uses more memory resources but allows the tweaking of the options. Once the user is satisfied with the tweak, the stacks and some other variables are copied and pasted into a new program that uses only the first module. This will reduce the memory usage in a final program.
A similar approach seems possible for adding the calibration and interrupt functions.
A lowest level module example
A module one level up:
Invoke TestM1
Invoke TestM2
Some useful resource links on the topic of using IMU sensors:
https://www.intorobotics.com/accelerometer-gyroscope-and-imu-sensors-tutorials/
http://tutorial.cytron.com.my/2012/01/10/measuring-tilt-angle-with-gyro-and-accelerometer/
Kalman filter simulation
https://www.cs.utexas.edu/~teammco/misc/kalman_filter/
5 Attachments