• LSM9DS1

    Finally the board arrived a week later than expected. I also ordered an ESP32 board which required using a for profit shipping company instead of the post office for some reason. They shipped it from Colorado to Chicago and then mailed it to the suburbs.
    The pony express would have been faster.

    Problems encountered when implementing the I2C interface:

    LSM9DS1.prototype.xgReadBytes=function(s­ubAddress,count){
    var dest= new Uint8Array(count);
    //  console.log("xgReadBytes ",this.xgAddress, subAddress|0x80, dest, count);
      var x=this.xgAddress;
      this.i2c.writeTo(x, subAddress|0x80);
     dest=this.i2c.readFrom(x, count);
    //  for(var i=0;i<count;i++)console.log(dest[i]);
      return dest;
    };
    
    LSM9DS1.prototype.mReadBytes=function(su­bAddress,count){
    // console.log("mReadBytes ",this.mAddress, subAddress|0x80,count);
      var x=this.mAddress;
      var dest=new Uint8Array(count);
      this.i2c.writeTo(x, subAddress|0x80);
      dest=this.i2c.readFrom(x, count);
    //  for(var i=0;i<count;i++)console.log(dest[i]);
      return dest;
    };
    

    Notice the var x=this.address.
    If this.address was used in the this.i2c.ReadFrom() function timeout errors occurred.
    I tried to create a simplified version of the problem but have not succeeded in reproducing the error.
    For reads of multiple bytes bit 7 of the sub-address is set to 1.

    LSM9DS1.prototype.readAccel=function(){
    var temp=this.xgReadBytes(Regs.OUT_X_L_XL, 6); // Read 6 bytes, beginning at OUT_X_L_XL
     this.ax=this.twos_comp(temp[0],temp[1]);­
     this.ay=this.twos_comp(temp[2],temp[3]);­
     this.az=this.twos_comp(temp[4],temp[5]);­
    /*
     this.ax = (temp[1] << 8) | temp[0]; // Store x-axis values into ax
     this.ay = (temp[3] << 8) | temp[2]; // Store y-axis values into ay
     this.az = (temp[5] << 8) | temp[4]; // Store z-axis values into az
    */
      if (_autoCalc)
    	{
    		ax -= aBiasRaw[X_AXIS];
    		ay -= aBiasRaw[Y_AXIS];
    		az -= aBiasRaw[Z_AXIS];
    	}
    };
    
    LSM9DS1.prototype.twos_comp=function(low­,high){
     var t=(high << 8) | low;
      return(t & 0x8000 ? t - 0x10000 : t);
    };
    

    The values returned are in two’s compliment.

    LSM9DS1.prototype.readTemp=function(){
    //void LSM9DS1::readTemp(){
     // We'll read two bytes from the             temperature sensor into temp
    var temp=this.xgReadBytes(Regs.OUT_TEMP_L, 2); // Read 2 bytes,     beginning at OUT_TEMP_L
    //  console.log("TT= ",temp[0].toString(16),temp[1].toString(­16));
    this.temperature = temp[1];//(temp[1] << 8) | temp[0];
    //temperature = ((int16_t)temp[1] << 8) | temp[0];
    
    //https://gist.github.com/jimblom/08b333­892ee383d6e443
    //temperature = (((int16_t) temp[1] << 12) | temp[0] << 4 ) >> 4; // Temperature is a 12-bit signed integer
    //var t= (((temp[1]^0xf)<<12)|temp[0])>>4;
    //this.temperature=(t & 0x8000 ? t - 0x10000 : t);
    };
    

    There is quite a bit of discussion on the web about reading the temperature.
    Having tried various solutions, I’m still not sure it is working properly.
    The file TestLSM9DS1_d.js is attached.
    Some sample output:

    Acceleration  -0.0087890625 -0.09417724609 1.02032470703
    Gyro          -810 294 75
    Magnetometer  1379 1413 -2029
    Temperature  246
    Level -0.49353282241 -5.27352997140
    heading 44.30230651281
    Acceleration  -0.01861572265 -0.09045410156 1.02691650390
    Gyro          -605 283 41
    Magnetometer  1425 1387 -2030
    Temperature  246
    Level -1.03853188194 -5.03380446489
    heading 45.77422016492
    Acceleration  0.00439453125 -0.08666992187 1.02276611328
    Gyro          -429 -248 8
    Magnetometer  1385 1459 -2064
    Temperature  246
    Level 0.24618193820 -4.84371267720
    heading 43.50951784922
    Acceleration  -0.00036621093 -0.08117675781 1.03289794921
    Gyro          -501 21 -87
    Magnetometer  1373 1453 -2027
    Temperature  246
    Level -0.02031404967 -4.49371112382
    heading 43.37847185456
    

    The readall function:

    function readall(W){
    W.readAccel();
    W.readGyro();
    W.readMag();
    W.readTemp();
    var pirate=180.0/Math.PI;
    console.log("Acceleration ",W.ax/16384,W.ay/16384,W.az/16384);
    console.log("Gyro         ",W.gx,W.gy,W.gz);
    console.log("Magnetometer ",W.mx,W.my,W.mz);
    console.log("Temperature ",W.temperature);
    console.log("Level", pirate*Math.atan2(W.ax,W.az),pirate*Math­.atan2(W.ay,W.az));
    console.log("heading",pirate*Math.atan2(­W.mx,W.my));
    }//end readall
    

    1 Attachment

About