7 static const char *
const TAG =
"mpu6050";
24 ESP_LOGCONFIG(TAG,
"Setting up MPU6050...");
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)) {
32 ESP_LOGV(TAG,
" Setting up Power Management...");
34 uint8_t power_management;
35 if (!this->
read_byte(MPU6050_REGISTER_POWER_MANAGEMENT_1, &power_management)) {
39 ESP_LOGV(TAG,
" Input power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
41 power_management &= 0b11111000;
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)) {
53 ESP_LOGV(TAG,
" Setting up Gyro Config...");
56 if (!this->
read_byte(MPU6050_REGISTER_GYRO_CONFIG, &gyro_config)) {
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)) {
69 ESP_LOGV(TAG,
" Setting up Accel Config...");
72 if (!this->
read_byte(MPU6050_REGISTER_ACCEL_CONFIG, &accel_config)) {
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)) {
86 ESP_LOGCONFIG(TAG,
"MPU6050:");
89 ESP_LOGE(TAG,
"Communication with MPU6050 failed!");
91 LOG_UPDATE_INTERVAL(
this);
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)) {
108 auto *data =
reinterpret_cast<int16_t *
>(raw_data);
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;
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);
const uint8_t MPU6050_RANGE_2G
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.
void status_set_warning(const char *message="unspecified")
sensor::Sensor * gyro_y_sensor_
const float GRAVITY_EARTH
sensor::Sensor * accel_z_sensor_
const uint8_t MPU6050_BIT_SLEEP_ENABLED
const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H
sensor::Sensor * accel_y_sensor_
const uint8_t MPU6050_REGISTER_ACCEL_CONFIG
sensor::Sensor * gyro_z_sensor_
const float MPU6050_RANGE_PER_DIGIT_2G
void status_clear_warning()
const uint8_t MPU6050_BIT_TEMPERATURE_DISABLED
void publish_state(float state)
Publish a new state to the front-end.
sensor::Sensor * temperature_sensor_
sensor::Sensor * accel_x_sensor_
const float MPU6050_SCALE_DPS_PER_DIGIT_2000
float get_setup_priority() const override
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
virtual void mark_failed()
Mark this component as failed.
const uint8_t MPU6050_REGISTER_GYRO_CONFIG
sensor::Sensor * gyro_x_sensor_
Implementation of SPI Controller mode.
const uint8_t MPU6050_CLOCK_SOURCE_X_GYRO
const uint8_t MPU6050_SCALE_2000_DPS
void dump_config() override
const uint8_t MPU6050_REGISTER_POWER_MANAGEMENT_1
const uint8_t MPU6050_REGISTER_WHO_AM_I
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)