ESPHome  2024.10.2
mpu6050.cpp
Go to the documentation of this file.
1 #include "mpu6050.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace mpu6050 {
6 
7 static const char *const TAG = "mpu6050";
8 
9 const uint8_t MPU6050_REGISTER_WHO_AM_I = 0x75;
11 const uint8_t MPU6050_REGISTER_GYRO_CONFIG = 0x1B;
12 const uint8_t MPU6050_REGISTER_ACCEL_CONFIG = 0x1C;
13 const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H = 0x3B;
14 const uint8_t MPU6050_CLOCK_SOURCE_X_GYRO = 0b001;
15 const uint8_t MPU6050_SCALE_2000_DPS = 0b11;
16 const float MPU6050_SCALE_DPS_PER_DIGIT_2000 = 0.060975f;
17 const uint8_t MPU6050_RANGE_2G = 0b00;
18 const float MPU6050_RANGE_PER_DIGIT_2G = 0.000061f;
19 const uint8_t MPU6050_BIT_SLEEP_ENABLED = 6;
21 const float GRAVITY_EARTH = 9.80665f;
22 
24  ESP_LOGCONFIG(TAG, "Setting up MPU6050...");
25  uint8_t who_am_i;
26  if (!this->read_byte(MPU6050_REGISTER_WHO_AM_I, &who_am_i) ||
27  (who_am_i != 0x68 && who_am_i != 0x70 && who_am_i != 0x98)) {
28  this->mark_failed();
29  return;
30  }
31 
32  ESP_LOGV(TAG, " Setting up Power Management...");
33  // Setup power management
34  uint8_t power_management;
35  if (!this->read_byte(MPU6050_REGISTER_POWER_MANAGEMENT_1, &power_management)) {
36  this->mark_failed();
37  return;
38  }
39  ESP_LOGV(TAG, " Input power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
40  // Set clock source - X-Gyro
41  power_management &= 0b11111000;
42  power_management |= MPU6050_CLOCK_SOURCE_X_GYRO;
43  // Disable sleep
44  power_management &= ~(1 << MPU6050_BIT_SLEEP_ENABLED);
45  // Enable temperature
46  power_management &= ~(1 << MPU6050_BIT_TEMPERATURE_DISABLED);
47  ESP_LOGV(TAG, " Output power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
48  if (!this->write_byte(MPU6050_REGISTER_POWER_MANAGEMENT_1, power_management)) {
49  this->mark_failed();
50  return;
51  }
52 
53  ESP_LOGV(TAG, " Setting up Gyro Config...");
54  // Set scale - 2000DPS
55  uint8_t gyro_config;
56  if (!this->read_byte(MPU6050_REGISTER_GYRO_CONFIG, &gyro_config)) {
57  this->mark_failed();
58  return;
59  }
60  ESP_LOGV(TAG, " Input gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
61  gyro_config &= 0b11100111;
62  gyro_config |= MPU6050_SCALE_2000_DPS << 3;
63  ESP_LOGV(TAG, " Output gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
64  if (!this->write_byte(MPU6050_REGISTER_GYRO_CONFIG, gyro_config)) {
65  this->mark_failed();
66  return;
67  }
68 
69  ESP_LOGV(TAG, " Setting up Accel Config...");
70  // Set range - 2G
71  uint8_t accel_config;
72  if (!this->read_byte(MPU6050_REGISTER_ACCEL_CONFIG, &accel_config)) {
73  this->mark_failed();
74  return;
75  }
76  ESP_LOGV(TAG, " Input accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
77  accel_config &= 0b11100111;
78  accel_config |= (MPU6050_RANGE_2G << 3);
79  ESP_LOGV(TAG, " Output accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
80  if (!this->write_byte(MPU6050_REGISTER_ACCEL_CONFIG, accel_config)) {
81  this->mark_failed();
82  return;
83  }
84 }
86  ESP_LOGCONFIG(TAG, "MPU6050:");
87  LOG_I2C_DEVICE(this);
88  if (this->is_failed()) {
89  ESP_LOGE(TAG, "Communication with MPU6050 failed!");
90  }
91  LOG_UPDATE_INTERVAL(this);
92  LOG_SENSOR(" ", "Acceleration X", this->accel_x_sensor_);
93  LOG_SENSOR(" ", "Acceleration Y", this->accel_y_sensor_);
94  LOG_SENSOR(" ", "Acceleration Z", this->accel_z_sensor_);
95  LOG_SENSOR(" ", "Gyro X", this->gyro_x_sensor_);
96  LOG_SENSOR(" ", "Gyro Y", this->gyro_y_sensor_);
97  LOG_SENSOR(" ", "Gyro Z", this->gyro_z_sensor_);
98  LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
99 }
100 
102  ESP_LOGV(TAG, " Updating MPU6050...");
103  uint16_t raw_data[7];
104  if (!this->read_bytes_16(MPU6050_REGISTER_ACCEL_XOUT_H, raw_data, 7)) {
105  this->status_set_warning();
106  return;
107  }
108  auto *data = reinterpret_cast<int16_t *>(raw_data);
109 
110  float accel_x = data[0] * MPU6050_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
111  float accel_y = data[1] * MPU6050_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
112  float accel_z = data[2] * MPU6050_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
113 
114  float temperature = data[3] / 340.0f + 36.53f;
115 
116  float gyro_x = data[4] * MPU6050_SCALE_DPS_PER_DIGIT_2000;
117  float gyro_y = data[5] * MPU6050_SCALE_DPS_PER_DIGIT_2000;
118  float gyro_z = data[6] * MPU6050_SCALE_DPS_PER_DIGIT_2000;
119 
120  ESP_LOGD(TAG,
121  "Got accel={x=%.3f m/s², y=%.3f m/s², z=%.3f m/s²}, "
122  "gyro={x=%.3f °/s, y=%.3f °/s, z=%.3f °/s}, temp=%.3f°C",
123  accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z, temperature);
124 
125  if (this->accel_x_sensor_ != nullptr)
126  this->accel_x_sensor_->publish_state(accel_x);
127  if (this->accel_y_sensor_ != nullptr)
128  this->accel_y_sensor_->publish_state(accel_y);
129  if (this->accel_z_sensor_ != nullptr)
130  this->accel_z_sensor_->publish_state(accel_z);
131 
132  if (this->temperature_sensor_ != nullptr)
133  this->temperature_sensor_->publish_state(temperature);
134 
135  if (this->gyro_x_sensor_ != nullptr)
136  this->gyro_x_sensor_->publish_state(gyro_x);
137  if (this->gyro_y_sensor_ != nullptr)
138  this->gyro_y_sensor_->publish_state(gyro_y);
139  if (this->gyro_z_sensor_ != nullptr)
140  this->gyro_z_sensor_->publish_state(gyro_z);
141 
142  this->status_clear_warning();
143 }
145 
146 } // namespace mpu6050
147 } // namespace esphome
const uint8_t MPU6050_RANGE_2G
Definition: mpu6050.cpp:17
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
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
bool is_failed() const
Definition: component.cpp:143
sensor::Sensor * gyro_y_sensor_
Definition: mpu6050.h:33
const float GRAVITY_EARTH
Definition: mpu6050.cpp:21
sensor::Sensor * accel_z_sensor_
Definition: mpu6050.h:30
const uint8_t MPU6050_BIT_SLEEP_ENABLED
Definition: mpu6050.cpp:19
const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H
Definition: mpu6050.cpp:13
sensor::Sensor * accel_y_sensor_
Definition: mpu6050.h:29
const uint8_t MPU6050_REGISTER_ACCEL_CONFIG
Definition: mpu6050.cpp:12
sensor::Sensor * gyro_z_sensor_
Definition: mpu6050.h:34
const float MPU6050_RANGE_PER_DIGIT_2G
Definition: mpu6050.cpp:18
void status_clear_warning()
Definition: component.cpp:166
const uint8_t MPU6050_BIT_TEMPERATURE_DISABLED
Definition: mpu6050.cpp:20
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
sensor::Sensor * temperature_sensor_
Definition: mpu6050.h:31
uint16_t temperature
Definition: sun_gtil2.cpp:26
sensor::Sensor * accel_x_sensor_
Definition: mpu6050.h:28
const float MPU6050_SCALE_DPS_PER_DIGIT_2000
Definition: mpu6050.cpp:16
float get_setup_priority() const override
Definition: mpu6050.cpp:144
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
const uint8_t MPU6050_REGISTER_GYRO_CONFIG
Definition: mpu6050.cpp:11
sensor::Sensor * gyro_x_sensor_
Definition: mpu6050.h:32
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const uint8_t MPU6050_CLOCK_SOURCE_X_GYRO
Definition: mpu6050.cpp:14
const uint8_t MPU6050_SCALE_2000_DPS
Definition: mpu6050.cpp:15
const uint8_t MPU6050_REGISTER_POWER_MANAGEMENT_1
Definition: mpu6050.cpp:10
const uint8_t MPU6050_REGISTER_WHO_AM_I
Definition: mpu6050.cpp:9
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
Definition: i2c.cpp:44