function useCalibration(cal,autocalc,data){
var i;var D=[0,0,0];var F=[0,0,0];
if(autocalc<1){
for(i=0;i<3;i++)D[i]=data[i];
return D;
}//endif
for(i=0;i<3;i++)D[i]=data[i]-cal.table[i].offset;
if(autocalc===1)return D;
for(i=0;i<3;i++){
F[i]=D[0]*cal.table[i].A;
F[i]+=D[1]*cal.table[i].B;
F[i]+=D[2]*cal.table[i].C;
}//next i
for(i=0;i<3;i++)F[i]=F[i]*cal.scale/(1<<15);
return F;
}//end useCalibration
Applying the Use calibration function
function displayMag(){
var m;
if(W.Mread(W.MREGval.ZYXDA)){ //new data available
// console.log("Mag data available");
m=W.mReadBytes(W.MagRegs.OUT_X_L_M, 6);
//autocalc 0=raw, 1=raw-offset, 2 Gauss
var autocalc=2;
M=useCalibration(magcal.scale8,autocalc,m);
console.log(" "+M[0]+","+M[1]+","+M[2]); //raw
}//endif
}//end displayMag
Listing the scales
function showscales(){
for(var x in magcal)console.log(x);
}
An Output Sample
>
=undefined
AG replied 104
M replied 61
do a reset of AG
AG SWreset= 0
reset the magnetometer
scale4
scale8
scale12
scale16
-0.00014387969,-0.09589182669,-0.20146481881
0.00023358480,-0.09475134273,-0.20193948954
-0.00110189705,-0.09552740429,-0.20038360033
0.00167522343,-0.09481362313,-0.19971340522
0.00177061744,-0.09662211209,-0.20106377922
0.00193627169,-0.09344444901,-0.20128787409
0.00032607760,-0.09668255780,-0.20133741284
0.00229249432,-0.09388396965,-0.20384650603
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.
Applying the Calibrations
There are 4 values to be applied to 3 axis, for each scale and for the magnetometer, accelerometer and the gyroscope sensors.
A data structure for the magnetometer calibration values
Code to apply the calibration values
Applying the Use calibration function
Listing the scales
An Output Sample
1 Attachment