ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
mpu6886.cpp
Go to the documentation of this file.
1#include "mpu6886.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace mpu6886 {
6
7static const char *const TAG = "mpu6886";
8
9const uint8_t MPU6886_REGISTER_WHO_AM_I = 0x75;
11const uint8_t MPU6886_REGISTER_GYRO_CONFIG = 0x1B;
12const uint8_t MPU6886_REGISTER_ACCEL_CONFIG = 0x1C;
13const uint8_t MPU6886_REGISTER_ACCEL_XOUT_H = 0x3B;
14const uint8_t MPU6886_CLOCK_SOURCE_X_GYRO = 0b001;
15const uint8_t MPU6886_SCALE_2000_DPS = 0b11;
16const uint8_t MPU6886_WHO_AM_I_IDENTIFIER = 0x19;
17const float MPU6886_SCALE_DPS_PER_DIGIT_2000 = 0.060975f;
18const uint8_t MPU6886_RANGE_2G = 0b00;
19const float MPU6886_RANGE_PER_DIGIT_2G = 0.000061f;
20const uint8_t MPU6886_BIT_SLEEP_ENABLED = 6;
22const 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
25const float TEMPERATURE_SENSITIVITY = 326.8;
26const float TEMPERATURE_OFFSET = 25.0;
27
29 uint8_t who_am_i;
30 if (!this->read_byte(MPU6886_REGISTER_WHO_AM_I, &who_am_i) || who_am_i != MPU6886_WHO_AM_I_IDENTIFIER) {
31 this->mark_failed();
32 return;
33 }
34
35 ESP_LOGV(TAG, " Setting up Power Management");
36 // Setup power management
37 uint8_t power_management;
38 if (!this->read_byte(MPU6886_REGISTER_POWER_MANAGEMENT_1, &power_management)) {
39 this->mark_failed();
40 return;
41 }
42 ESP_LOGV(TAG, " Input power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
43 // Set clock source - X-Gyro
44 power_management &= 0b11111000;
45 power_management |= MPU6886_CLOCK_SOURCE_X_GYRO;
46 // Disable sleep
47 power_management &= ~(1 << MPU6886_BIT_SLEEP_ENABLED);
48 // Enable temperature
49 power_management &= ~(1 << MPU6886_BIT_TEMPERATURE_DISABLED);
50 ESP_LOGV(TAG, " Output power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
51 if (!this->write_byte(MPU6886_REGISTER_POWER_MANAGEMENT_1, power_management)) {
52 this->mark_failed();
53 return;
54 }
55
56 ESP_LOGV(TAG, " Setting up Gyroscope Config");
57 // Set scale - 2000DPS
58 uint8_t gyro_config;
59 if (!this->read_byte(MPU6886_REGISTER_GYRO_CONFIG, &gyro_config)) {
60 this->mark_failed();
61 return;
62 }
63 ESP_LOGV(TAG, " Input gyroscope_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
64 gyro_config &= 0b11100111;
65 gyro_config |= MPU6886_SCALE_2000_DPS << 3;
66 ESP_LOGV(TAG, " Output gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
67 if (!this->write_byte(MPU6886_REGISTER_GYRO_CONFIG, gyro_config)) {
68 this->mark_failed();
69 return;
70 }
71
72 ESP_LOGV(TAG, " Setting up Accelerometer Config");
73 // Set range - 2G
74 uint8_t accel_config;
75 if (!this->read_byte(MPU6886_REGISTER_ACCEL_CONFIG, &accel_config)) {
76 this->mark_failed();
77 return;
78 }
79 ESP_LOGV(TAG, " Input accelerometer_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
80 accel_config &= 0b11100111;
81 accel_config |= (MPU6886_RANGE_2G << 3);
82 ESP_LOGV(TAG, " Output accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
83 if (!this->write_byte(MPU6886_REGISTER_GYRO_CONFIG, gyro_config)) {
84 this->mark_failed();
85 return;
86 }
87}
88
90 ESP_LOGCONFIG(TAG, "MPU6886:");
91 LOG_I2C_DEVICE(this);
92 if (this->is_failed()) {
93 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
94 }
95 LOG_UPDATE_INTERVAL(this);
96 LOG_SENSOR(" ", "Acceleration X", this->accel_x_sensor_);
97 LOG_SENSOR(" ", "Acceleration Y", this->accel_y_sensor_);
98 LOG_SENSOR(" ", "Acceleration Z", this->accel_z_sensor_);
99 LOG_SENSOR(" ", "Gyro X", this->gyro_x_sensor_);
100 LOG_SENSOR(" ", "Gyro Y", this->gyro_y_sensor_);
101 LOG_SENSOR(" ", "Gyro Z", this->gyro_z_sensor_);
102 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
103}
104
106 ESP_LOGV(TAG, " Updating");
107 uint16_t raw_data[7];
108 if (!this->read_bytes_16(MPU6886_REGISTER_ACCEL_XOUT_H, raw_data, 7)) {
109 this->status_set_warning();
110 return;
111 }
112 auto *data = reinterpret_cast<int16_t *>(raw_data);
113
114 float accel_x = data[0] * MPU6886_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
115 float accel_y = data[1] * MPU6886_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
116 float accel_z = data[2] * MPU6886_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
117
119
120 float gyro_x = data[4] * MPU6886_SCALE_DPS_PER_DIGIT_2000;
121 float gyro_y = data[5] * MPU6886_SCALE_DPS_PER_DIGIT_2000;
122 float gyro_z = data[6] * MPU6886_SCALE_DPS_PER_DIGIT_2000;
123
124 ESP_LOGD(TAG,
125 "Got accel={x=%.3f m/s², y=%.3f m/s², z=%.3f m/s²}, "
126 "gyro={x=%.3f °/s, y=%.3f °/s, z=%.3f °/s}, temp=%.3f°C",
127 accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z, temperature);
128
129 if (this->accel_x_sensor_ != nullptr)
130 this->accel_x_sensor_->publish_state(accel_x);
131 if (this->accel_y_sensor_ != nullptr)
132 this->accel_y_sensor_->publish_state(accel_y);
133 if (this->accel_z_sensor_ != nullptr)
134 this->accel_z_sensor_->publish_state(accel_z);
135
136 if (this->temperature_sensor_ != nullptr)
137 this->temperature_sensor_->publish_state(temperature);
138
139 if (this->gyro_x_sensor_ != nullptr)
140 this->gyro_x_sensor_->publish_state(gyro_x);
141 if (this->gyro_y_sensor_ != nullptr)
142 this->gyro_y_sensor_->publish_state(gyro_y);
143 if (this->gyro_z_sensor_ != nullptr)
144 this->gyro_z_sensor_->publish_state(gyro_z);
145
146 this->status_clear_warning();
147}
148
150
151} // namespace mpu6886
152} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
Definition i2c.cpp:44
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
float get_setup_priority() const override
Definition mpu6886.cpp:149
sensor::Sensor * gyro_x_sensor_
Definition mpu6886.h:32
sensor::Sensor * accel_y_sensor_
Definition mpu6886.h:29
sensor::Sensor * accel_z_sensor_
Definition mpu6886.h:30
sensor::Sensor * temperature_sensor_
Definition mpu6886.h:31
sensor::Sensor * gyro_z_sensor_
Definition mpu6886.h:34
sensor::Sensor * accel_x_sensor_
Definition mpu6886.h:28
sensor::Sensor * gyro_y_sensor_
Definition mpu6886.h:33
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
const uint8_t MPU6886_REGISTER_WHO_AM_I
Definition mpu6886.cpp:9
const float TEMPERATURE_OFFSET
Definition mpu6886.cpp:26
const uint8_t MPU6886_BIT_TEMPERATURE_DISABLED
Definition mpu6886.cpp:21
const uint8_t MPU6886_REGISTER_ACCEL_XOUT_H
Definition mpu6886.cpp:13
const uint8_t MPU6886_REGISTER_GYRO_CONFIG
Definition mpu6886.cpp:11
const float MPU6886_RANGE_PER_DIGIT_2G
Definition mpu6886.cpp:19
const uint8_t MPU6886_RANGE_2G
Definition mpu6886.cpp:18
const float MPU6886_SCALE_DPS_PER_DIGIT_2000
Definition mpu6886.cpp:17
const float TEMPERATURE_SENSITIVITY
Definition mpu6886.cpp:25
const uint8_t MPU6886_WHO_AM_I_IDENTIFIER
Definition mpu6886.cpp:16
const uint8_t MPU6886_BIT_SLEEP_ENABLED
Definition mpu6886.cpp:20
const uint8_t MPU6886_REGISTER_POWER_MANAGEMENT_1
Definition mpu6886.cpp:10
const uint8_t MPU6886_CLOCK_SOURCE_X_GYRO
Definition mpu6886.cpp:14
const uint8_t MPU6886_SCALE_2000_DPS
Definition mpu6886.cpp:15
const float GRAVITY_EARTH
Definition mpu6886.cpp:22
const uint8_t MPU6886_REGISTER_ACCEL_CONFIG
Definition mpu6886.cpp:12
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t temperature
Definition sun_gtil2.cpp:12