4 #if defined (ESP_PLATFORM)
7 #include <freertos/FreeRTOS.h>
8 #include <freertos/task.h>
14 : IMU_Base ( i2c_addr, freq, i2c )
17 IMU_Base::imu_spec_t MPU6886_Class::begin(I2C_Class* i2c)
25 auto id = readRegister8(0x75);
27 if (
id != DEV_ID_MPU6886
28 &&
id != DEV_ID_MPU6050
29 &&
id != DEV_ID_MPU9250)
33 writeRegister8(REG_PWR_MGMT_1, 0x80);
35 static constexpr std::uint8_t init_cmd[] =
36 { REG_PWR_MGMT_1 , 0x01
37 , REG_ACCEL_CONFIG , 0x10
38 , REG_GYRO_CONFIG , 0x18
40 , REG_SMPLRT_DIV , 0x03
41 , REG_INT_ENABLE , 0x00
42 , REG_ACCEL_CONFIG2 , 0x00
43 , REG_USER_CTRL , 0x00
50 std::uint8_t reg = init_cmd[++idx];
51 std::uint8_t val = init_cmd[++idx];
52 if ((reg & val) == 0xFF) {
break; }
53 uint32_t retry_count = 16;
55 writeRegister8(reg, val);
56 }
while (readRegister8(reg) != val && --retry_count);
59 setGyroFsr(Gscale::GFS_2000DPS);
60 setAccelFsr(Ascale::AFS_8G);
67 return (imu_spec_t)(imu_spec_accel | imu_spec_gyro);
70 bool MPU6886_Class::setGyroAdcOffset(std::int16_t gx, std::int16_t gy, std::int16_t gz)
73 { (uint8_t)(gx >> 8), (uint8_t)gx
74 , (uint8_t)(gy >> 8), (uint8_t)gy
75 , (uint8_t)(gz >> 8), (uint8_t)gz
77 return writeRegister(0x13, buffer, 6);
80 void MPU6886_Class::enableFIFO(Fodr output_data_rate)
82 auto regdata = readRegister8(REG_GYRO_CONFIG);
85 writeRegister8(REG_GYRO_CONFIG, regdata);
88 regdata = output_data_rate & 0xFF;
90 writeRegister8(REG_SMPLRT_DIV, regdata);
93 regdata = readRegister8(REG_CONFIG);
98 writeRegister8(REG_CONFIG, regdata);
104 writeRegister8(REG_FIFO_EN, regdata);
107 regdata = readRegister8(REG_INT_ENABLE);
109 writeRegister8(REG_INT_ENABLE, regdata);
114 writeRegister8(REG_USER_CTRL, regdata);
141 void MPU6886_Class::setGyroFsr(Gscale scale)
143 scale = (Gscale)(scale & 3);
145 writeRegister8(REG_GYRO_CONFIG, scale << 3);
147 static constexpr
const float table[] =
153 _gRes = table[scale];
156 void MPU6886_Class::setAccelFsr(Ascale scale)
158 scale = (Ascale)(scale & 3);
160 writeRegister8(REG_ACCEL_CONFIG, scale << 3);
162 static constexpr
const float table[] =
168 _aRes = table[scale];
171 bool MPU6886_Class::setINTPinActiveLogic(
bool level)
174 std::uint8_t tmp = readRegister8(REG_INT_PIN_CFG) & 0x7F;
175 tmp |= level ? 0x00 : 0x80;
176 return writeRegister8(REG_INT_PIN_CFG, tmp);
229 IMU_Base::imu_spec_t MPU6886_Class::getImuRawData(imu_raw_data_t* data)
const
231 std::uint8_t raw_buf[16];
232 auto buf = &raw_buf[1];
236 if (!readRegister(REG_FIFO_COUNTH, buf, 2) || (buf[0] + buf[1] == 0))
238 return imu_spec_none;
240 if (!readRegister(REG_FIFO_R_W, buf, 14) || (buf[0] == 0x7F && buf[1] == 0x7F))
242 return imu_spec_none;
247 if (!readRegister(0x3A, raw_buf, 15) || ((raw_buf[0] & 1) == 0))
249 return imu_spec_none;
253 data->accel.x = (std::int16_t)((buf[0] << 8) + buf[1]);
254 data->accel.y = (std::int16_t)((buf[2] << 8) + buf[3]);
255 data->accel.z = (std::int16_t)((buf[4] << 8) + buf[5]);
258 data->gyro.x = (std::int16_t)((buf[ 8] << 8) + buf[ 9]);
259 data->gyro.y = (std::int16_t)((buf[10] << 8) + buf[11]);
260 data->gyro.z = (std::int16_t)((buf[12] << 8) + buf[13]);
261 return (imu_spec_t)(imu_spec_accel | imu_spec_gyro);
264 void MPU6886_Class::getConvertParam(imu_convert_param_t* param)
const
266 param->accel_res = _aRes;
267 param->gyro_res = _gRes;
268 param->temp_res = 1.0f / 326.8f;
269 param->temp_offset = 25.0f;
272 bool MPU6886_Class::getTempAdc(int16_t* t)
const
275 bool res = readRegister(0x41, buf, 2);
276 if (res) { *t = (buf[0] << 8) + buf[1]; }
MPU6886_Class(std::uint8_t i2c_addr=DEFAULT_ADDRESS, std::uint32_t freq=400000, I2C_Class *i2c=&In_I2C)