Attached are two modules that implement an I2C interface with the LSM9DS1.
These modules should be placed in the modules directory of your WebIDE project.
The module slimLSM9DS1.js is tested by the program testslimLSM9DS1.js
The module calibrateLSM9DS1.js is tested by the program testcalibrateLSM9DS1.js and loads the slimLSM9DS1.js module as well. The test program performs the calibration that the Sparkfun code performed. Options to perform a more robust calibration are coded but not yet tested as this requires a menu system.
Several functions have been added to allow the scales, output data rate and filters to be changes on the accelerometer, gyro and magnetometer.
The calibrations in the attached code has not been performed on every scale of the three sensors. In the next development phase a menu program will allow a full calibration to be performed.
The moff, goff and aoff contain the zero offsets for each axis for each scale
The mres, gres and ares contain the scale factors for each axis for each scale
g=gyro, a= accelerometer, m= magnetometer
this.mScale=0;//0,1,2,3 index into mscale
this.mscale=[ //the mres is the scale factor for each axis
{scale:4,regvalue:0,
mres:[0.000122,0.000122,0.000122],
moff:[0,0,0] //zero offset in counts
},
////
this.gScale=0;//0,1,2 index into gyroscale
this.gyroscale=[ //the gres is the scale factor for each axis
{scale:245,regvalue:0,
gres:[245/32768.0,245/32768.0,245/32768.0],
goff:[0,0,0] //zero offset
},
//// this.aScale=0;//0,1,2,3 index into accelscale
this.accelscale=[ //the ares is the scale factor for each axis
{scale:2,regvalue:0,
ares:[2/32768.0,2/32768.0,2/32768.0],
aoff:[0,0,0]
},
These structures along with autocalc variable determine the type of output. The following shows this for the accelerometer. Similar code is used for the magnetometer and gyro.
LSM9DS1.prototype.readAccel=function(){
var temp=this.xgReadBytes(Regs.OUT_X_L_XL, 6);
var i;
// Read 6 bytes, beginning at OUT_X_L_XL
this.a[0]=this.twos_comp(temp[0],temp[1]);
this.a[1]=this.twos_comp(temp[2],temp[3]);
this.a[2]=this.twos_comp(temp[4],temp[5]);
if (this.autoCalc>0){
for(i=0;i<3;i++)
this.a[i] -= this.accelscale[this.aScale].aoff[i];
//this.aBiasRaw[i];
}//endif
if (this.autoCalc>1){
for(i=0;i<3;i++)
this.a[i]=this.a[i]*this.accelscale[this.aScale].ares[i];
}//endif
};//end readAccel
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.
Attached are two modules that implement an I2C interface with the LSM9DS1.
These modules should be placed in the modules directory of your WebIDE project.
The module slimLSM9DS1.js is tested by the program testslimLSM9DS1.js
The module calibrateLSM9DS1.js is tested by the program testcalibrateLSM9DS1.js and loads the slimLSM9DS1.js module as well. The test program performs the calibration that the Sparkfun code performed. Options to perform a more robust calibration are coded but not yet tested as this requires a menu system.
Several functions have been added to allow the scales, output data rate and filters to be changes on the accelerometer, gyro and magnetometer.
The calibrations in the attached code has not been performed on every scale of the three sensors. In the next development phase a menu program will allow a full calibration to be performed.
The moff, goff and aoff contain the zero offsets for each axis for each scale
The mres, gres and ares contain the scale factors for each axis for each scale
g=gyro, a= accelerometer, m= magnetometer
These structures along with autocalc variable determine the type of output. The following shows this for the accelerometer. Similar code is used for the magnetometer and gyro.
4 Attachments