ESPHome  2024.11.0
mpu6886.cpp
Go to the documentation of this file.
1 #include "mpu6886.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace mpu6886 {
6 
7 static const char *const TAG = "mpu6886";
8 
9 const uint8_t MPU6886_REGISTER_WHO_AM_I = 0x75;
11 const uint8_t MPU6886_REGISTER_GYRO_CONFIG = 0x1B;
12 const uint8_t MPU6886_REGISTER_ACCEL_CONFIG = 0x1C;
13 const uint8_t MPU6886_REGISTER_ACCEL_XOUT_H = 0x3B;
14 const uint8_t MPU6886_CLOCK_SOURCE_X_GYRO = 0b001;
15 const uint8_t MPU6886_SCALE_2000_DPS = 0b11;
16 const uint8_t MPU6886_WHO_AM_I_IDENTIFIER = 0x19;
17 const float MPU6886_SCALE_DPS_PER_DIGIT_2000 = 0.060975f;
18 const uint8_t MPU6886_RANGE_2G = 0b00;
19 const float MPU6886_RANGE_PER_DIGIT_2G = 0.000061f;
20 const uint8_t MPU6886_BIT_SLEEP_ENABLED = 6;
22 const float GRAVITY_EARTH = 9.80665f;
23 // See https://github.com/m5stack/M5-Schematic/blob/master/datasheet/MPU-6886-000193%2Bv1.1_GHIC.PDF.pdf
24 // p. 43
25 const float TEMPERATURE_SENSITIVITY = 326.8;
26 const float TEMPERATURE_OFFSET = 25.0;
27 
29  ESP_LOGCONFIG(TAG, "Setting up MPU6886...");
30  uint8_t who_am_i;
31  if (!this->read_byte(MPU6886_REGISTER_WHO_AM_I, &who_am_i) || who_am_i != MPU6886_WHO_AM_I_IDENTIFIER) {
32  this->mark_failed();
33  return;
34  }
35 
36  ESP_LOGV(TAG, " Setting up Power Management...");
37  // Setup power management
38  uint8_t power_management;
39  if (!this->read_byte(MPU6886_REGISTER_POWER_MANAGEMENT_1, &power_management)) {
40  this->mark_failed();
41  return;
42  }
43  ESP_LOGV(TAG, " Input power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
44  // Set clock source - X-Gyro
45  power_management &= 0b11111000;
46  power_management |= MPU6886_CLOCK_SOURCE_X_GYRO;
47  // Disable sleep
48  power_management &= ~(1 << MPU6886_BIT_SLEEP_ENABLED);
49  // Enable temperature
50  power_management &= ~(1 << MPU6886_BIT_TEMPERATURE_DISABLED);
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)) {
53  this->mark_failed();
54  return;
55  }
56 
57  ESP_LOGV(TAG, " Setting up Gyroscope Config...");
58  // Set scale - 2000DPS
59  uint8_t gyro_config;
60  if (!this->read_byte(MPU6886_REGISTER_GYRO_CONFIG, &gyro_config)) {
61  this->mark_failed();
62  return;
63  }
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)) {
69  this->mark_failed();
70  return;
71  }
72 
73  ESP_LOGV(TAG, " Setting up Accelerometer Config...");
74  // Set range - 2G
75  uint8_t accel_config;
76  if (!this->read_byte(MPU6886_REGISTER_ACCEL_CONFIG, &accel_config)) {
77  this->mark_failed();
78  return;
79  }
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)) {
85  this->mark_failed();
86  return;
87  }
88 }
89 
91  ESP_LOGCONFIG(TAG, "MPU6886:");
92  LOG_I2C_DEVICE(this);
93  if (this->is_failed()) {
94  ESP_LOGE(TAG, "Communication with MPU6886 failed!");
95  }
96  LOG_UPDATE_INTERVAL(this);
97  LOG_SENSOR(" ", "Acceleration X", this->accel_x_sensor_);
98  LOG_SENSOR(" ", "Acceleration Y", this->accel_y_sensor_);
99  LOG_SENSOR(" ", "Acceleration Z", this->accel_z_sensor_);
100  LOG_SENSOR(" ", "Gyro X", this->gyro_x_sensor_);
101  LOG_SENSOR(" ", "Gyro Y", this->gyro_y_sensor_);
102  LOG_SENSOR(" ", "Gyro Z", this->gyro_z_sensor_);
103  LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
104 }
105 
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)) {
110  this->status_set_warning();
111  return;
112  }
113  auto *data = reinterpret_cast<int16_t *>(raw_data);
114 
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;
118 
119  float temperature = data[3] / TEMPERATURE_SENSITIVITY + TEMPERATURE_OFFSET;
120 
121  float gyro_x = data[4] * MPU6886_SCALE_DPS_PER_DIGIT_2000;
122  float gyro_y = data[5] * MPU6886_SCALE_DPS_PER_DIGIT_2000;
123  float gyro_z = data[6] * MPU6886_SCALE_DPS_PER_DIGIT_2000;
124 
125  ESP_LOGD(TAG,
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);
129 
130  if (this->accel_x_sensor_ != nullptr)
131  this->accel_x_sensor_->publish_state(accel_x);
132  if (this->accel_y_sensor_ != nullptr)
133  this->accel_y_sensor_->publish_state(accel_y);
134  if (this->accel_z_sensor_ != nullptr)
135  this->accel_z_sensor_->publish_state(accel_z);
136 
137  if (this->temperature_sensor_ != nullptr)
138  this->temperature_sensor_->publish_state(temperature);
139 
140  if (this->gyro_x_sensor_ != nullptr)
141  this->gyro_x_sensor_->publish_state(gyro_x);
142  if (this->gyro_y_sensor_ != nullptr)
143  this->gyro_y_sensor_->publish_state(gyro_y);
144  if (this->gyro_z_sensor_ != nullptr)
145  this->gyro_z_sensor_->publish_state(gyro_z);
146 
147  this->status_clear_warning();
148 }
149 
151 
152 } // namespace mpu6886
153 } // namespace esphome
sensor::Sensor * temperature_sensor_
Definition: mpu6886.h:31
const float GRAVITY_EARTH
Definition: mpu6886.cpp:22
float get_setup_priority() const override
Definition: mpu6886.cpp:150
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:235
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
const uint8_t MPU6886_REGISTER_WHO_AM_I
Definition: mpu6886.cpp:9
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
const float MPU6886_RANGE_PER_DIGIT_2G
Definition: mpu6886.cpp:19
bool is_failed() const
Definition: component.cpp:143
const uint8_t MPU6886_REGISTER_GYRO_CONFIG
Definition: mpu6886.cpp:11
const float TEMPERATURE_OFFSET
Definition: mpu6886.cpp:26
sensor::Sensor * accel_x_sensor_
Definition: mpu6886.h:28
sensor::Sensor * gyro_x_sensor_
Definition: mpu6886.h:32
const float MPU6886_SCALE_DPS_PER_DIGIT_2000
Definition: mpu6886.cpp:17
const float TEMPERATURE_SENSITIVITY
Definition: mpu6886.cpp:25
const uint8_t MPU6886_BIT_TEMPERATURE_DISABLED
Definition: mpu6886.cpp:21
const uint8_t MPU6886_REGISTER_ACCEL_XOUT_H
Definition: mpu6886.cpp:13
void status_clear_warning()
Definition: component.cpp:166
const uint8_t MPU6886_REGISTER_ACCEL_CONFIG
Definition: mpu6886.cpp:12
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
uint16_t temperature
Definition: sun_gtil2.cpp:26
const uint8_t MPU6886_BIT_SLEEP_ENABLED
Definition: mpu6886.cpp:20
const uint8_t MPU6886_RANGE_2G
Definition: mpu6886.cpp:18
const uint8_t MPU6886_SCALE_2000_DPS
Definition: mpu6886.cpp:15
sensor::Sensor * gyro_z_sensor_
Definition: mpu6886.h:34
const uint8_t MPU6886_CLOCK_SOURCE_X_GYRO
Definition: mpu6886.cpp:14
sensor::Sensor * accel_y_sensor_
Definition: mpu6886.h:29
sensor::Sensor * gyro_y_sensor_
Definition: mpu6886.h:33
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const uint8_t MPU6886_REGISTER_POWER_MANAGEMENT_1
Definition: mpu6886.cpp:10
const uint8_t MPU6886_WHO_AM_I_IDENTIFIER
Definition: mpu6886.cpp:16
sensor::Sensor * accel_z_sensor_
Definition: mpu6886.h:30
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
Definition: i2c.cpp:44