MPU6050 constructor

MPU6050(
  1. I2C i2c, [
  2. int i2cAddress = defaultMPU6050address,
  3. DLPF _dlpfCfg = defaultDLPFcfg,
  4. int _smplrtDiv = defaultSmplrtDiv,
])

Opens a MPU6050 on the i2c with the optional default values

Implementation

MPU6050(
  this.i2c, [
  this.i2cAddress = defaultMPU6050address,
  this._dlpfCfg = defaultDLPFcfg,
  this._smplrtDiv = defaultSmplrtDiv,
]) : i2cBus = i2c.busNum {
  // 1. waking up the MPU6050 (0x00 = 0000 0000) as it starts in sleep mode.
  i2c.writeByteReg(i2cAddress, mpu6050RegAddrPwrMgmt1, 0x00);

  // 2. sample rate divider
  // The sensor register output, FIFO output, and DMP sampling are all based on the Sample Rate.
  // The Sample Rate is generated by dividing the gyroscope output rate by SMPLRT_DIV:
  //      Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV)
  // where Gyroscope Output Rate = 8kHz when the DLPF is disabled (DLPF_CFG = 0 or 7),
  // and 1kHz when the DLPF is enabled (see register 26).
  // SMPLRT_DIV set the rate to the default value : Sample Rate = Gyroscope Rate.
  i2c.writeByteReg(i2cAddress, mpu6050RegAddrSmprtDiv, _smplrtDiv);

  // 3. This register configures the external Frame Synchronization (FSYNC)
  // pin sampling and the Digital Low Pass Filter (DLPF) setting for both
  // the gyroscopes and accelerometers.
  setDLPFConfig(_dlpfCfg);

  // 4. Gyroscope configuration
  // FS_SEL selects the full scale range of the gyroscope outputs.
  var fsSel = 0 << 3; // FS_SEL +- 250 °/s
  _gyroLSBSensitivity = 131; // cfr [datasheet 2 - p.31]
  i2c.writeByteReg(i2cAddress, mpu6050RegAddrGyroConfig, fsSel);

  // 5. Accelerometer configuration [datasheet 2 - p.29]
  var afsSel =
      0; // AFS_SEL full scale range: ± 2g. LSB sensitivity : 16384 LSB/g
  _accelLSBSensitivity = 16384; // LSB Sensitivity corresponding to AFS_SEL 0
  i2c.writeByteReg(i2cAddress, mpu6050RegAddrAccelConfig, afsSel);

  // 6. Disable interrupts
  i2c.writeByteReg(i2cAddress, mpu6050RegAddrIntEnable, 0x00);

  // 7. Disable standby mode
  i2c.writeByteReg(i2cAddress, mpu6050RegAddrPwrMgmt2, 0x00);

  _calibrateSensors();
}