7 static const char *
const TAG =
"mpu6886";
29 ESP_LOGCONFIG(TAG,
"Setting up MPU6886...");
31 if (!this->
read_byte(MPU6886_REGISTER_WHO_AM_I, &who_am_i) || who_am_i != MPU6886_WHO_AM_I_IDENTIFIER) {
36 ESP_LOGV(TAG,
" Setting up Power Management...");
38 uint8_t power_management;
39 if (!this->
read_byte(MPU6886_REGISTER_POWER_MANAGEMENT_1, &power_management)) {
43 ESP_LOGV(TAG,
" Input power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
45 power_management &= 0b11111000;
51 ESP_LOGV(TAG,
" Output power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
52 if (!this->
write_byte(MPU6886_REGISTER_POWER_MANAGEMENT_1, power_management)) {
57 ESP_LOGV(TAG,
" Setting up Gyroscope Config...");
60 if (!this->
read_byte(MPU6886_REGISTER_GYRO_CONFIG, &gyro_config)) {
64 ESP_LOGV(TAG,
" Input gyroscope_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
65 gyro_config &= 0b11100111;
66 gyro_config |= MPU6886_SCALE_2000_DPS << 3;
67 ESP_LOGV(TAG,
" Output gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
68 if (!this->
write_byte(MPU6886_REGISTER_GYRO_CONFIG, gyro_config)) {
73 ESP_LOGV(TAG,
" Setting up Accelerometer Config...");
76 if (!this->
read_byte(MPU6886_REGISTER_ACCEL_CONFIG, &accel_config)) {
80 ESP_LOGV(TAG,
" Input accelerometer_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
81 accel_config &= 0b11100111;
82 accel_config |= (MPU6886_RANGE_2G << 3);
83 ESP_LOGV(TAG,
" Output accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
84 if (!this->
write_byte(MPU6886_REGISTER_GYRO_CONFIG, gyro_config)) {
91 ESP_LOGCONFIG(TAG,
"MPU6886:");
94 ESP_LOGE(TAG,
"Communication with MPU6886 failed!");
96 LOG_UPDATE_INTERVAL(
this);
107 ESP_LOGV(TAG,
" Updating MPU6886...");
108 uint16_t raw_data[7];
109 if (!this->
read_bytes_16(MPU6886_REGISTER_ACCEL_XOUT_H, raw_data, 7)) {
113 auto *data =
reinterpret_cast<int16_t *
>(raw_data);
115 float accel_x = data[0] * MPU6886_RANGE_PER_DIGIT_2G *
GRAVITY_EARTH;
116 float accel_y = data[1] * MPU6886_RANGE_PER_DIGIT_2G *
GRAVITY_EARTH;
117 float accel_z = data[2] * MPU6886_RANGE_PER_DIGIT_2G *
GRAVITY_EARTH;
126 "Got accel={x=%.3f m/s², y=%.3f m/s², z=%.3f m/s²}, " 127 "gyro={x=%.3f °/s, y=%.3f °/s, z=%.3f °/s}, temp=%.3f°C",
128 accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z, temperature);
sensor::Sensor * temperature_sensor_
const float GRAVITY_EARTH
float get_setup_priority() const override
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
const float DATA
For components that import data from directly connected sensors like DHT.
const uint8_t MPU6886_REGISTER_WHO_AM_I
void status_set_warning(const char *message="unspecified")
const float MPU6886_RANGE_PER_DIGIT_2G
const uint8_t MPU6886_REGISTER_GYRO_CONFIG
const float TEMPERATURE_OFFSET
sensor::Sensor * accel_x_sensor_
sensor::Sensor * gyro_x_sensor_
const float MPU6886_SCALE_DPS_PER_DIGIT_2000
const float TEMPERATURE_SENSITIVITY
void dump_config() override
const uint8_t MPU6886_BIT_TEMPERATURE_DISABLED
const uint8_t MPU6886_REGISTER_ACCEL_XOUT_H
void status_clear_warning()
const uint8_t MPU6886_REGISTER_ACCEL_CONFIG
void publish_state(float state)
Publish a new state to the front-end.
const uint8_t MPU6886_BIT_SLEEP_ENABLED
const uint8_t MPU6886_RANGE_2G
const uint8_t MPU6886_SCALE_2000_DPS
sensor::Sensor * gyro_z_sensor_
const uint8_t MPU6886_CLOCK_SOURCE_X_GYRO
sensor::Sensor * accel_y_sensor_
sensor::Sensor * gyro_y_sensor_
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
const uint8_t MPU6886_REGISTER_POWER_MANAGEMENT_1
const uint8_t MPU6886_WHO_AM_I_IDENTIFIER
sensor::Sensor * accel_z_sensor_
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)