• 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

    var magcal={
      scale4:{scale:4,table:[
      {offset:0,A:1,B:0,C:0},
      {offset:0,A:0,B:1,C:0},
      {offset:0,A:0,B:0,C:1}
      ]},
      scale8:{scale:4,table:[
      {offset:1060.85,A:0.98742,B:0.01417,C:-0­.0005998},
      {offset:24.469956,A:	0.124389675,B:0.996380859,C:-0.000561968­},
      {offset:561.2217795,A:0.020594208	,B:0.00552387,C:1}
      ]},
      scale12:{scale:4,table:[
      {offset:0,A:1,B:0,C:0},
      {offset:0,A:0,B:1,C:0},
      {offset:0,A:0,B:0,C:1}
      ]},
      scale16:{scale:4,table:[
      {offset:0,A:1,B:0,C:0},
      {offset:0,A:0,B:1,C:0},
      {offset:0,A:0,B:0,C:1}
      ]},
    };
    

    Code to apply the calibration values

    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.2014648­1881
     0.00023358480,-0.09475134273,-0.20193948­954
     -0.00110189705,-0.09552740429,-0.2003836­0033
     0.00167522343,-0.09481362313,-0.19971340­522
     0.00177061744,-0.09662211209,-0.20106377­922
     0.00193627169,-0.09344444901,-0.20128787­409
     0.00032607760,-0.09668255780,-0.20133741­284
     0.00229249432,-0.09388396965,-0.20384650­603
    

    1 Attachment

About