mirror of
https://github.com/Mercantec-GHC/h5-projekt-mst.git
synced 2026-08-26 21:27:38 +02:00
new driver
This commit is contained in:
parent
1e2e29796a
commit
dbad216e10
@ -1,5 +1,5 @@
|
||||
idf_component_register(
|
||||
SRCS skateboard.c app_wifi.c app_mpu.c app_mqtt.c
|
||||
PRIV_REQUIRES nvs_flash esp_netif esp_wifi mqtt
|
||||
SRCS skateboard.c app_wifi.c app_mpu.c app_mqtt.c new_mpu6050.c
|
||||
PRIV_REQUIRES nvs_flash esp_netif esp_wifi mqtt esp_driver_i2c
|
||||
INCLUDE_DIRS ".")
|
||||
|
||||
|
||||
@ -41,28 +41,29 @@ void app_mpu_init(AppMpu* mpu)
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(mpu6050_init(&mpu->dev));
|
||||
|
||||
ESP_LOGI(TAG, "MPU6050 acceleration range: %d", mpu->dev.ranges.accel);
|
||||
ESP_LOGI(TAG, "MPU6050 gyroscope range: %d", mpu->dev.ranges.gyro);
|
||||
ESP_ERROR_CHECK(mpu6050_set_rate(&mpu->dev, 7));
|
||||
}
|
||||
|
||||
void app_mpu_read_acceleration(AppMpu* mpu, float* x, float* y, float* z)
|
||||
void app_mpu_read_acceleration(AppMpu* mpu, float3* out_accel)
|
||||
{
|
||||
mpu6050_acceleration_t acceleration = { 0 };
|
||||
|
||||
ESP_ERROR_CHECK(mpu6050_get_acceleration(&mpu->dev, &acceleration));
|
||||
*x = acceleration.x;
|
||||
*y = acceleration.y;
|
||||
*z = acceleration.z;
|
||||
|
||||
out_accel->x = acceleration.x;
|
||||
out_accel->y = acceleration.y;
|
||||
out_accel->z = acceleration.z;
|
||||
}
|
||||
|
||||
void app_mpu_read_rotation(AppMpu* mpu, float* x, float* y, float* z)
|
||||
void app_mpu_read_rotation(AppMpu* mpu, float3* out_rotation)
|
||||
{
|
||||
mpu6050_rotation_t rotation = { 0 };
|
||||
|
||||
ESP_ERROR_CHECK(mpu6050_get_rotation(&mpu->dev, &rotation));
|
||||
*x = rotation.x;
|
||||
*y = rotation.y;
|
||||
*z = rotation.z;
|
||||
|
||||
out_rotation->x = rotation.x;
|
||||
out_rotation->y = rotation.y;
|
||||
out_rotation->z = rotation.z;
|
||||
}
|
||||
|
||||
void app_mpu_read_temperature(AppMpu* mpu, float* degree_celsius)
|
||||
|
||||
@ -2,11 +2,17 @@
|
||||
|
||||
#include "mpu6050.h"
|
||||
|
||||
typedef struct float3 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
} float3;
|
||||
|
||||
typedef struct AppMpu {
|
||||
mpu6050_dev_t dev;
|
||||
} AppMpu;
|
||||
|
||||
void app_mpu_init(AppMpu* mpu);
|
||||
void app_mpu_read_acceleration(AppMpu* mpu, float* x, float* y, float* z);
|
||||
void app_mpu_read_rotation(AppMpu* mpu, float* x, float* y, float* z);
|
||||
void app_mpu_read_acceleration(AppMpu* mpu, float3* out_accel);
|
||||
void app_mpu_read_rotation(AppMpu* mpu, float3* out_rotation);
|
||||
void app_mpu_read_temperature(AppMpu* mpu, float* degree_celsius);
|
||||
|
||||
254
skateboard/main/new_mpu6050.c
Normal file
254
skateboard/main/new_mpu6050.c
Normal file
@ -0,0 +1,254 @@
|
||||
#include "new_mpu6050.h"
|
||||
#include "driver/i2c_master.h"
|
||||
#include "driver/i2c_types.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/idf_additions.h"
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char* TAG = "new_mpu6050";
|
||||
|
||||
#define CHECK(EXPR) \
|
||||
do { \
|
||||
esp_err_t status = (EXPR); \
|
||||
if (status != ESP_OK) { \
|
||||
return status; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define DEFAULT_TIMEOUT 1000
|
||||
|
||||
typedef enum : uint8_t {
|
||||
REG_SMPLRT_DIV = 0x19,
|
||||
REG_CONFIG = 0x1a,
|
||||
REG_GYRO_CONFIG = 0x1b,
|
||||
REG_ACCEL_CONFIG = 0x1c,
|
||||
REG_FIFO_EN = 0x23,
|
||||
REG_ACCEL_XOUT_H = 0x3b,
|
||||
REG_ACCEL_XOUT_L = 0x3c,
|
||||
REG_ACCEL_YOUT_H = 0x3d,
|
||||
REG_ACCEL_YOUT_L = 0x3e,
|
||||
REG_ACCEL_ZOUT_H = 0x3f,
|
||||
REG_ACCEL_ZOUT_L = 0x40,
|
||||
REG_TEMP_OUT_H = 0x41,
|
||||
REG_TEMP_OUT_L = 0x42,
|
||||
REG_GYRO_XOUT_H = 0x43,
|
||||
REG_GYRO_XOUT_L = 0x44,
|
||||
REG_GYRO_YOUT_H = 0x45,
|
||||
REG_GYRO_YOUT_L = 0x46,
|
||||
REG_GYRO_ZOUT_H = 0x47,
|
||||
REG_GYRO_ZOUT_L = 0x48,
|
||||
REG_SIGNAL_PATH_RESET = 0x68,
|
||||
REG_USER_CTRL = 0x6a,
|
||||
REG_PWR_MGMT_1 = 0x6b,
|
||||
REG_PWR_MGMT_2 = 0x6c,
|
||||
REG_FIFO_COUNTH = 0x72,
|
||||
REG_FIFO_COUNTL = 0x73,
|
||||
REG_FIFO_R_W = 0x74,
|
||||
} Reg;
|
||||
|
||||
typedef enum : uint8_t {
|
||||
BIT_CONFIG_DLPF_CFG = 0,
|
||||
BIT_GYRO_CONFIG_FS_SEL = 3,
|
||||
BIT_ACCEL_CONFIG_AFS_SEL = 3,
|
||||
BIT_PWR_MGMT_1_CLKSEL = 0,
|
||||
BIT_PWR_MGMT_1_SLEEP = 6,
|
||||
} Bit;
|
||||
|
||||
typedef enum : uint8_t {
|
||||
MASK_CONFIG_DLPF_CFG = 0x7,
|
||||
MASK_GYRO_CONFIG_FS_SEL = 0x3,
|
||||
MASK_ACCEL_CONFIG_AFS_SEL = 0x3,
|
||||
MASK_PWR_MGMT_1_CLKSEL = 0x7,
|
||||
} Mask;
|
||||
|
||||
static esp_err_t read_regs(
|
||||
Mpu6050* dev, uint8_t* out_data, size_t data_size, Reg reg)
|
||||
{
|
||||
return i2c_master_transmit_receive(
|
||||
dev->i2c_dev, ®, 1, out_data, data_size, DEFAULT_TIMEOUT);
|
||||
}
|
||||
|
||||
static esp_err_t read_reg(Mpu6050* dev, uint8_t* out_value, Reg reg)
|
||||
{
|
||||
return i2c_master_transmit_receive(
|
||||
dev->i2c_dev, ®, 1, out_value, 1, DEFAULT_TIMEOUT);
|
||||
}
|
||||
|
||||
static esp_err_t write_reg(Mpu6050* dev, Reg reg, uint8_t value)
|
||||
{
|
||||
uint8_t buffer[] = { reg, value };
|
||||
return i2c_master_transmit(dev->i2c_dev, buffer, 2, DEFAULT_TIMEOUT);
|
||||
}
|
||||
|
||||
static esp_err_t read_bits(
|
||||
Mpu6050* dev, uint8_t* out_value, Reg reg, Bit offset, Mask mask)
|
||||
{
|
||||
uint8_t buffer;
|
||||
CHECK(read_reg(dev, &buffer, reg));
|
||||
*out_value = buffer >> offset & mask;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t write_bits(
|
||||
Mpu6050* dev, Reg reg, Bit offset, Mask mask, uint8_t value)
|
||||
{
|
||||
uint8_t buffer;
|
||||
CHECK(read_reg(dev, &buffer, reg));
|
||||
buffer &= ~mask << offset;
|
||||
buffer |= value & mask << offset;
|
||||
CHECK(write_reg(dev, reg, buffer));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t new_mpu6050_init(Mpu6050* dev)
|
||||
{
|
||||
i2c_master_bus_config_t bus_config = {
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.i2c_port = I2C_NUM_0,
|
||||
.sda_io_num = 11,
|
||||
.scl_io_num = 12,
|
||||
.glitch_ignore_cnt = 7,
|
||||
.flags.enable_internal_pullup = true,
|
||||
};
|
||||
CHECK(i2c_new_master_bus(&bus_config, &dev->i2c_bus));
|
||||
i2c_device_config_t dev_config = {
|
||||
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
|
||||
.device_address = 0x68,
|
||||
.scl_speed_hz = 400000,
|
||||
};
|
||||
CHECK(i2c_master_bus_add_device(dev->i2c_bus, &dev_config, &dev->i2c_dev));
|
||||
|
||||
const int max_attempts = 5;
|
||||
esp_err_t probe_res;
|
||||
for (int i = 0; i < max_attempts; ++i) {
|
||||
probe_res = i2c_master_probe(dev->i2c_bus, 0x68, DEFAULT_TIMEOUT);
|
||||
if (probe_res == ESP_OK)
|
||||
break;
|
||||
ESP_LOGW(
|
||||
TAG, "Device not found. Retrying (%d/%d)", i + 1, max_attempts);
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
if (probe_res != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Device not found.");
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
ESP_LOGI(TAG, "Found MPU6050 device");
|
||||
|
||||
CHECK(new_mpu6050_set_clock_source(dev, MPU6050_CLKSEL_PLL_GYRO_X_REF));
|
||||
|
||||
CHECK(new_mpu6050_set_sleep_enabled(dev, false));
|
||||
|
||||
dev->gyro_range = MPU6050_GYRO_RANGE_250;
|
||||
dev->accel_range = MPU6050_ACCEL_RANGE_2;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t new_mpu6050_get_rotation(Mpu6050* dev, float3* rotation)
|
||||
{
|
||||
static const float resolution[] = {
|
||||
[MPU6050_GYRO_RANGE_250] = 250.0f / 32768.0f,
|
||||
[MPU6050_GYRO_RANGE_500] = 500.0f / 32768.0f,
|
||||
[MPU6050_GYRO_RANGE_1000] = 1000.0f / 32768.0f,
|
||||
[MPU6050_GYRO_RANGE_2000] = 2000.0f / 32768.0f,
|
||||
};
|
||||
|
||||
uint8_t buffer[6];
|
||||
CHECK(read_regs(dev, buffer, 6, REG_GYRO_XOUT_H));
|
||||
|
||||
rotation->x = (buffer[0] << 8 | buffer[1]) * resolution[dev->gyro_range];
|
||||
rotation->y = (buffer[2] << 8 | buffer[3]) * resolution[dev->gyro_range];
|
||||
rotation->z = (buffer[4] << 8 | buffer[5]) * resolution[dev->gyro_range];
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t new_mpu6050_get_acceleration(Mpu6050* dev, float3* accel)
|
||||
{
|
||||
static const float resolution[] = {
|
||||
[MPU6050_ACCEL_RANGE_2] = 2.0f / 32768.0f,
|
||||
[MPU6050_ACCEL_RANGE_4] = 4.0f / 32768.0f,
|
||||
[MPU6050_ACCEL_RANGE_8] = 8.0f / 32768.0f,
|
||||
[MPU6050_ACCEL_RANGE_16] = 16.0f / 32768.0f,
|
||||
};
|
||||
|
||||
uint8_t buffer[6];
|
||||
CHECK(read_regs(dev, buffer, 6, REG_ACCEL_XOUT_H));
|
||||
|
||||
accel->x = (buffer[0] << 8 | buffer[1]) * resolution[dev->accel_range];
|
||||
accel->y = (buffer[2] << 8 | buffer[3]) * resolution[dev->accel_range];
|
||||
accel->z = (buffer[4] << 8 | buffer[5]) * resolution[dev->accel_range];
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t new_mpu6050_read_temperature(Mpu6050* dev, float* temperature)
|
||||
{
|
||||
|
||||
uint8_t buffer[2];
|
||||
CHECK(read_regs(dev, buffer, 2, REG_TEMP_OUT_H));
|
||||
|
||||
*temperature = (int16_t)(buffer[0] << 8 | buffer[1]) / 340.0f + 36.53f;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t new_mpu6050_deinit(Mpu6050* dev)
|
||||
{
|
||||
CHECK(i2c_master_bus_rm_device(dev->i2c_dev));
|
||||
CHECK(i2c_del_master_bus(dev->i2c_bus));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t new_mpu6050_set_clock_source(Mpu6050* dev, Mpu6050_ClockSource source)
|
||||
{
|
||||
return write_bits(dev,
|
||||
REG_PWR_MGMT_1,
|
||||
BIT_PWR_MGMT_1_CLKSEL,
|
||||
MASK_PWR_MGMT_1_CLKSEL,
|
||||
source);
|
||||
}
|
||||
|
||||
esp_err_t new_mpu6050_set_gyro_range(Mpu6050* dev, Mpu6050_GyroRange range)
|
||||
{
|
||||
CHECK(write_bits(dev,
|
||||
REG_GYRO_CONFIG,
|
||||
BIT_GYRO_CONFIG_FS_SEL,
|
||||
MASK_GYRO_CONFIG_FS_SEL,
|
||||
range));
|
||||
dev->gyro_range = range;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t new_mpu6050_set_accel_range(Mpu6050* dev, Mpu6050_AccelRange range)
|
||||
{
|
||||
CHECK(write_bits(dev,
|
||||
REG_ACCEL_CONFIG,
|
||||
BIT_ACCEL_CONFIG_AFS_SEL,
|
||||
MASK_ACCEL_CONFIG_AFS_SEL,
|
||||
range));
|
||||
dev->accel_range = range;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t new_mpu6050_set_sleep_enabled(Mpu6050* dev, bool enabled)
|
||||
{
|
||||
return write_bits(dev, REG_PWR_MGMT_1, BIT_PWR_MGMT_1_SLEEP, 1, enabled);
|
||||
}
|
||||
|
||||
esp_err_t new_mpu6050_set_sample_rate_div(Mpu6050* dev, uint8_t div)
|
||||
{
|
||||
return write_reg(dev, REG_SMPLRT_DIV, div);
|
||||
}
|
||||
|
||||
esp_err_t new_mpu6050_set_dlpf(Mpu6050* dev, Mpu6050_DLPF selector)
|
||||
{
|
||||
return write_bits(
|
||||
dev, REG_CONFIG, BIT_CONFIG_DLPF_CFG, MASK_CONFIG_DLPF_CFG, selector);
|
||||
}
|
||||
119
skateboard/main/new_mpu6050.h
Normal file
119
skateboard/main/new_mpu6050.h
Normal file
@ -0,0 +1,119 @@
|
||||
#pragma once
|
||||
|
||||
#include "driver/i2c_types.h"
|
||||
#include "esp_err.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum : uint8_t {
|
||||
// Internal 8MHz oscillator
|
||||
MPU6050_CLKSEL_INTERNAL_8MHZ_OSC = 0,
|
||||
// PLL with X axis gyroscope reference
|
||||
MPU6050_CLKSEL_PLL_GYRO_X_REF = 1,
|
||||
// PLL with Y axis gyroscope reference
|
||||
MPU6050_CLKSEL_PLL_GYRO_Y_REF = 2,
|
||||
// PLL with Z axis gyroscope reference
|
||||
MPU6050_CLKSEL_PLL_GYRO_Z_REF = 3,
|
||||
// PLL with external 32.768kHz reference
|
||||
MPU6050_CLKSEL_PLL_EXTERNAL_32_768_HZ_REF = 4,
|
||||
// PLL with external 19.2MHz reference
|
||||
MPU6050_CLKSEL_PLL_EXTERNAL_19_200K_HZ_REF = 5,
|
||||
// Stops the clock and keeps the timing generator in reset
|
||||
MPU6050_CLKSEL_PLL_STOP_RESET = 7,
|
||||
|
||||
} Mpu6050_ClockSource;
|
||||
|
||||
typedef enum : uint8_t {
|
||||
// ± 250 °/s
|
||||
MPU6050_GYRO_RANGE_250 = 0,
|
||||
// ± 500 °/s
|
||||
MPU6050_GYRO_RANGE_500 = 1,
|
||||
// ± 1000 °/s
|
||||
MPU6050_GYRO_RANGE_1000 = 2,
|
||||
// ± 2000 °/s
|
||||
MPU6050_GYRO_RANGE_2000 = 3,
|
||||
} Mpu6050_GyroRange;
|
||||
|
||||
typedef enum : uint8_t {
|
||||
// ± 2g
|
||||
MPU6050_ACCEL_RANGE_2 = 0,
|
||||
// ± 4g
|
||||
MPU6050_ACCEL_RANGE_4 = 1,
|
||||
// ± 8g
|
||||
MPU6050_ACCEL_RANGE_8 = 2,
|
||||
// ± 16g
|
||||
MPU6050_ACCEL_RANGE_16 = 3,
|
||||
} Mpu6050_AccelRange;
|
||||
|
||||
typedef struct {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
} float3;
|
||||
|
||||
typedef struct {
|
||||
i2c_master_bus_handle_t i2c_bus;
|
||||
i2c_master_dev_handle_t i2c_dev;
|
||||
Mpu6050_GyroRange gyro_range;
|
||||
Mpu6050_AccelRange accel_range;
|
||||
|
||||
} Mpu6050;
|
||||
|
||||
esp_err_t new_mpu6050_init(Mpu6050* dev);
|
||||
esp_err_t new_mpu6050_deinit(Mpu6050* dev);
|
||||
|
||||
esp_err_t new_mpu6050_get_rotation(Mpu6050* dev, float3* rotation);
|
||||
esp_err_t new_mpu6050_get_acceleration(Mpu6050* dev, float3* accel);
|
||||
esp_err_t new_mpu6050_read_temperature(Mpu6050* dev, float* temperature);
|
||||
|
||||
esp_err_t new_mpu6050_set_clock_source(
|
||||
Mpu6050* dev, Mpu6050_ClockSource source);
|
||||
esp_err_t new_mpu6050_set_gyro_range(Mpu6050* dev, Mpu6050_GyroRange range);
|
||||
esp_err_t new_mpu6050_set_accel_range(Mpu6050* dev, Mpu6050_AccelRange range);
|
||||
esp_err_t new_mpu6050_set_sleep_enabled(Mpu6050* dev, bool enabled);
|
||||
|
||||
#define NEW_MPU6050_SAMPLE_RATE_TO_DIV_1KHZ(SAMPLE_RATE) \
|
||||
((uint8_t)(1000.0f / (float)(SAMPLE_RATE) - 1.0f))
|
||||
#define NEW_MPU6050_SAMPLE_RATE_TO_DIV_8KHZ(SAMPLE_RATE) \
|
||||
((uint8_t)(8000.0f / (float)(SAMPLE_RATE) - 1.0f))
|
||||
|
||||
// Sample rate = Gyro output rate / (1 + Sample rate divier)
|
||||
// Optionally use
|
||||
// - `NEW_MPU6050_SAMPLE_RATE_TO_DIV_1KHZ()`, if DLPF is enabled
|
||||
// - `NEW_MPU6050_SAMPLE_RATE_TO_DIV_8KHZ()`, if DLPF is disabled
|
||||
esp_err_t new_mpu6050_set_sample_rate_div(Mpu6050* dev, uint8_t div);
|
||||
|
||||
typedef enum : uint8_t {
|
||||
// Accelerometer: bandwidth = 260Hz, delay = 0.0ms
|
||||
// Gyroscope: bandwidth = 256Hz, delay = 0.98ms
|
||||
// Fs: 8kHz
|
||||
MPU6050_DLPF_0 = 0,
|
||||
// Accelerometer: bandwidth = 184Hz, delay = 2.0ms
|
||||
// Gyroscope: bandwidth = 188Hz, delay = 1.9ms
|
||||
// Fs: 1kHz
|
||||
MPU6050_DLPF_1 = 1,
|
||||
// Accelerometer: bandwidth = 94Hz, delay = 3.0ms
|
||||
// Gyroscope: bandwidth = 98Hz, delay = 2.8ms
|
||||
// Fs: 1kHz
|
||||
MPU6050_DLPF_2 = 2,
|
||||
// Accelerometer: bandwidth = 44Hz, delay = 4.9ms
|
||||
// Gyroscope: bandwidth = 32Hz, delay = 4.8ms
|
||||
// Fs: 1kHz
|
||||
MPU6050_DLPF_3 = 3,
|
||||
// Accelerometer: bandwidth = 21Hz, delay = 8.5ms
|
||||
// Gyroscope: bandwidth = 20Hz, delay = 8.3ms
|
||||
// Fs: 1kHz
|
||||
MPU6050_DLPF_4 = 4,
|
||||
// Accelerometer: bandwidth = 10Hz, delay = 13.8ms
|
||||
// Gyroscope: bandwidth = 10Hz, delay = 13.4ms
|
||||
// Fs: 1kHz
|
||||
MPU6050_DLPF_5 = 5,
|
||||
// Accelerometer: bandwidth = 5Hz, delay = 19.0ms
|
||||
// Gyroscope: bandwidth = 5Hz, delay = 18.6ms
|
||||
// Fs: 1kHz
|
||||
MPU6050_DLPF_6 = 6,
|
||||
} Mpu6050_DLPF;
|
||||
|
||||
// Digital low pass filter (DLPF)
|
||||
// Note: Sampling rate is dependent on whether DLPF is enabled.
|
||||
esp_err_t new_mpu6050_set_dlpf(Mpu6050* dev, Mpu6050_DLPF selector);
|
||||
@ -1,4 +1,3 @@
|
||||
#include "app_mpu.h"
|
||||
#include "app_mqtt.h"
|
||||
#include "app_wifi.h"
|
||||
#include "esp_event.h"
|
||||
@ -9,12 +8,24 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define NEW_DRIVER
|
||||
|
||||
#ifndef NEW_DRIVER
|
||||
#include "app_mpu.h"
|
||||
#else
|
||||
#include "new_mpu6050.h"
|
||||
#endif
|
||||
|
||||
const char* TAG = "skateboard";
|
||||
|
||||
typedef struct App {
|
||||
AppWifi wifi;
|
||||
AppMpu mpu;
|
||||
AppMqtt mqtt;
|
||||
#ifndef NEW_DRIVER
|
||||
AppMpu mpu;
|
||||
#else
|
||||
Mpu6050 mpu;
|
||||
#endif
|
||||
} App;
|
||||
|
||||
#define msg_buffer_capacity 1024
|
||||
@ -33,18 +44,54 @@ static void configure_cb(const char* topic,
|
||||
ESP_LOGI(TAG, "Data: %.*s", (int)data_size, (const char*)data);
|
||||
}
|
||||
|
||||
static void publish_message(App* app)
|
||||
{
|
||||
float3 accel = { 0 };
|
||||
#ifndef NEW_DRIVER
|
||||
app_mpu_read_acceleration(&app->mpu, &accel);
|
||||
#else
|
||||
ESP_ERROR_CHECK(new_mpu6050_get_acceleration(&app->mpu, &accel));
|
||||
#endif
|
||||
|
||||
float3 rotation = { 0 };
|
||||
#ifndef NEW_DRIVER
|
||||
app_mpu_read_rotation(&app->mpu, &rotation);
|
||||
#else
|
||||
ESP_ERROR_CHECK(new_mpu6050_get_rotation(&app->mpu, &rotation));
|
||||
#endif
|
||||
|
||||
float temp = 0;
|
||||
#ifndef NEW_DRIVER
|
||||
app_mpu_read_temperature(&app->mpu, &temp);
|
||||
#else
|
||||
ESP_ERROR_CHECK(new_mpu6050_read_temperature(&app->mpu, &temp));
|
||||
#endif
|
||||
|
||||
int msg_size = snprintf(msg_buffer,
|
||||
msg_buffer_capacity - 1,
|
||||
"{ \"acceleration\": [% 9.4f, % 9.4f, % 9.4f], "
|
||||
"\"rotation\": [% 9.4f, % 9.4f, % 9.4f], "
|
||||
"\"temperature\": % 5.2f }",
|
||||
accel.x,
|
||||
accel.y,
|
||||
accel.z,
|
||||
rotation.x,
|
||||
rotation.y,
|
||||
rotation.z,
|
||||
temp);
|
||||
|
||||
#if false
|
||||
app_mqtt_publish(&app->mqtt, "/skateboard/update", msg_buffer, msg_size);
|
||||
#else
|
||||
ESP_LOGI(TAG, "%.*s", (int)msg_size, msg_buffer);
|
||||
#endif
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing");
|
||||
ESP_LOGI(TAG, "IDF version: %s", esp_get_idf_version());
|
||||
|
||||
esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE);
|
||||
esp_log_level_set("mqtt_example", ESP_LOG_VERBOSE);
|
||||
esp_log_level_set("transport_base", ESP_LOG_VERBOSE);
|
||||
esp_log_level_set("esp-tls", ESP_LOG_VERBOSE);
|
||||
esp_log_level_set("transport", ESP_LOG_VERBOSE);
|
||||
esp_log_level_set("outbox", ESP_LOG_VERBOSE);
|
||||
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
@ -56,44 +103,26 @@ void app_main(void)
|
||||
},
|
||||
};
|
||||
|
||||
app_wifi_init(&app.wifi);
|
||||
app_mpu_init(&app.mpu);
|
||||
app_mqtt_init(&app.mqtt);
|
||||
// app_wifi_init(&app.wifi);
|
||||
// app_mqtt_init(&app.mqtt);
|
||||
|
||||
app_mqtt_subscribe(&app.mqtt, "/skateboard/configure", configure_cb, &app);
|
||||
ESP_LOGI(TAG, "=== Initializing MPU6050 ===");
|
||||
#ifndef NEW_DRIVER
|
||||
app_mpu_init(&app.mpu);
|
||||
#else
|
||||
ESP_ERROR_CHECK(new_mpu6050_init(&app.mpu));
|
||||
#endif
|
||||
ESP_LOGI(TAG, "=== MPU6050 initialized ===");
|
||||
|
||||
// app_mqtt_subscribe(&app.mqtt, "/skateboard/configure", configure_cb,
|
||||
// &app);
|
||||
|
||||
ESP_LOGI(TAG, "Initialized");
|
||||
ESP_LOGI(TAG, "Free memory: %" PRIu32 " bytes", esp_get_free_heap_size());
|
||||
|
||||
while (true) {
|
||||
float accel_x;
|
||||
float accel_y;
|
||||
float accel_z;
|
||||
app_mpu_read_acceleration(&app.mpu, &accel_x, &accel_y, &accel_z);
|
||||
publish_message(&app);
|
||||
|
||||
float rotation_x;
|
||||
float rotation_y;
|
||||
float rotation_z;
|
||||
app_mpu_read_rotation(&app.mpu, &rotation_x, &rotation_y, &rotation_z);
|
||||
|
||||
float temp;
|
||||
app_mpu_read_temperature(&app.mpu, &temp);
|
||||
|
||||
int msg_size = snprintf(msg_buffer,
|
||||
msg_buffer_capacity - 1,
|
||||
"{ \"acceleration\": [%.4f, %.4f, %.4f], "
|
||||
"\"rotation\": [%.4f, %.4f, %.4f], "
|
||||
"\"temperature\": %.2f }",
|
||||
accel_x,
|
||||
accel_y,
|
||||
accel_z,
|
||||
rotation_x,
|
||||
rotation_y,
|
||||
rotation_z,
|
||||
temp);
|
||||
|
||||
app_mqtt_publish(&app.mqtt, "/skateboard/update", msg_buffer, msg_size);
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(200));
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}
|
||||
|
||||
73
skateboard/output_new_driver.txt
Normal file
73
skateboard/output_new_driver.txt
Normal file
@ -0,0 +1,73 @@
|
||||
=== Initializing MPU6050 ===
|
||||
Calling i2c_new_master_bus()
|
||||
Calling i2c_master_bus_add_device()
|
||||
Calling i2c_master_probe()
|
||||
Found MPU6050 device
|
||||
Calling new_mpu6050_set_clock_source()
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 1)
|
||||
write_buffer: { 0x6b }
|
||||
read_buffer: { 0x01 }
|
||||
Called i2c_master_transmit(write_size: 2)
|
||||
write_buffer: { 0x6b 0x01 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 1)
|
||||
write_buffer: { 0x1b }
|
||||
read_buffer: { 0x00 }
|
||||
Called i2c_master_transmit(write_size: 2)
|
||||
write_buffer: { 0x1b 0x00 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 1)
|
||||
write_buffer: { 0x1c }
|
||||
read_buffer: { 0x00 }
|
||||
Called i2c_master_transmit(write_size: 2)
|
||||
write_buffer: { 0x1c 0x00 }
|
||||
Calling new_mpu6050_set_sleep_enabled()
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 1)
|
||||
write_buffer: { 0x6b }
|
||||
read_buffer: { 0x01 }
|
||||
Called i2c_master_transmit(write_size: 2)
|
||||
write_buffer: { 0x6b 0x00 }
|
||||
Calling new_mpu6050_set_sample_rate_div()
|
||||
Called i2c_master_transmit(write_size: 2)
|
||||
write_buffer: { 0x19 0x07 }
|
||||
=== MPU6050 initialized ===
|
||||
Initialized
|
||||
Free memory: 369220 bytes
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x3b }
|
||||
read_buffer: { 0xfd 0x70 0xff 0x0c 0xcb 0x38 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x43 }
|
||||
read_buffer: { 0xff 0xc5 0x00 0x70 0x00 0xe4 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 2)
|
||||
write_buffer: { 0x41 }
|
||||
read_buffer: { 0xf6 0x20 }
|
||||
{ "acceleration": [ 3.9600, 3.9851, 3.1753], "rotation": [ 499.5499, 0.8545, 1.7395], "temperature": 29.09 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x3b }
|
||||
read_buffer: { 0xfd 0x58 0xfe 0x94 0xca 0x6c }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x43 }
|
||||
read_buffer: { 0xff 0xae 0x00 0x79 0x00 0xca }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 2)
|
||||
write_buffer: { 0x41 }
|
||||
read_buffer: { 0xf6 0x20 }
|
||||
{ "acceleration": [ 3.9585, 3.9778, 3.1628], "rotation": [ 499.3744, 0.9232, 1.5411], "temperature": 29.09 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x3b }
|
||||
read_buffer: { 0xfd 0x0c 0xfe 0x70 0xcb 0x2c }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x43 }
|
||||
read_buffer: { 0xff 0xa9 0x00 0x68 0x00 0xcc }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 2)
|
||||
write_buffer: { 0x41 }
|
||||
read_buffer: { 0xf6 0x20 }
|
||||
{ "acceleration": [ 3.9539, 3.9756, 3.1746], "rotation": [ 499.3362, 0.7935, 1.5564], "temperature": 29.09 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x3b }
|
||||
read_buffer: { 0xfd 0x50 0xfe 0x50 0xca 0x7c }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x43 }
|
||||
read_buffer: { 0xff 0xb8 0x00 0xa0 0x00 0xc3 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 2)
|
||||
write_buffer: { 0x41 }
|
||||
read_buffer: { 0xf6 0x20 }
|
||||
{ "acceleration": [ 3.9580, 3.9736, 3.1638], "rotation": [ 499.4507, 1.2207, 1.4877], "temperature": 29.09 }
|
||||
77
skateboard/output_old_driver.txt
Normal file
77
skateboard/output_old_driver.txt
Normal file
@ -0,0 +1,77 @@
|
||||
=== Initializing MPU6050 ===
|
||||
Calling i2cdev_init()
|
||||
Calling mpu6050_init_desc()
|
||||
Initializing MPU6050 device. Address: 0x68, SDA 11, SCL: 12
|
||||
Calling i2c_dev_probe()
|
||||
[Port 0] First initialization. Configuring bus with SDA=11, SCL=12 (Pullups SCL:0 SDA:0)
|
||||
Please check pull-up resistances whether be connected properly. Otherwise unexpected behavior would happen. For more detailed information, please read docs
|
||||
[Port 0] Successfully installed I2C master bus (Handle: 0x3fcec4d0).
|
||||
Found MPU6050 device
|
||||
Calling mpu6050_init()
|
||||
[0x68 at 0] Device added successfully (Device Handle: 0x3fcec9bc, Speed: 1000000 Hz).
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 1)
|
||||
write_buffer: { 0x6b }
|
||||
read_buffer: { 0x00 }
|
||||
Called i2c_master_transmit(write_size: 2)
|
||||
write_buffer: { 0x6b 0x01 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 1)
|
||||
write_buffer: { 0x1b }
|
||||
read_buffer: { 0x00 }
|
||||
Called i2c_master_transmit(write_size: 2)
|
||||
write_buffer: { 0x1b 0x00 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 1)
|
||||
write_buffer: { 0x1c }
|
||||
read_buffer: { 0x00 }
|
||||
Called i2c_master_transmit(write_size: 2)
|
||||
write_buffer: { 0x1c 0x00 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 1)
|
||||
write_buffer: { 0x6b }
|
||||
read_buffer: { 0x01 }
|
||||
Called i2c_master_transmit(write_size: 2)
|
||||
write_buffer: { 0x6b 0x01 }
|
||||
Calling mpu6050_set_rate()
|
||||
Called i2c_master_transmit(write_size: 2)
|
||||
write_buffer: { 0x19 0x07 }
|
||||
=== MPU6050 initialized ===
|
||||
Initialized
|
||||
Free memory: 368844 bytes
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x3b }
|
||||
read_buffer: { 0xfd 0x78 0xfe 0x74 0xcc 0x10 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x43 }
|
||||
read_buffer: { 0xff 0xa5 0x00 0x7a 0x00 0xd4 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 2)
|
||||
write_buffer: { 0x41 }
|
||||
read_buffer: { 0xf6 0xa0 }
|
||||
{ "acceleration": [ -0.0396, -0.0242, -0.8115], "rotation": [ -0.6943, 0.9308, 1.6174], "temper
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x3b }
|
||||
read_buffer: { 0xfd 0x4c 0xfe 0x90 0xca 0xc4 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x43 }
|
||||
read_buffer: { 0xff 0xb7 0x00 0x8f 0x00 0xde }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 2)
|
||||
write_buffer: { 0x41 }
|
||||
read_buffer: { 0xf6 0xa0 }
|
||||
{ "acceleration": [ -0.0422, -0.0225, -0.8318], "rotation": [ -0.5569, 1.0910, 1.6937], "tempe
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x3b }
|
||||
read_buffer: { 0xfc 0xec 0xfe 0x54 0xcb 0x54 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x43 }
|
||||
read_buffer: { 0xff 0xb8 0x00 0x7c 0x00 0xe5 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 2)
|
||||
write_buffer: { 0x41 }
|
||||
read_buffer: { 0xf6 0xa0 }
|
||||
{ "acceleration": [ -0.0481, -0.0261, -0.8230], "rotation": [ -0.5493, 0.9460, 1.7471], "temperature": 29.47 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x3b }
|
||||
read_buffer: { 0xfd 0x0c 0xfe 0x90 0xca 0x98 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 6)
|
||||
write_buffer: { 0x43 }
|
||||
read_buffer: { 0xff 0xbb 0x00 0x83 0x00 0xf2 }
|
||||
Called i2c_master_transmit_receive(write_size: 1, read_size: 2)
|
||||
write_buffer: { 0x41 }
|
||||
read_buffer: { 0xf6 0xa0 }
|
||||
{ "acceleration": [ -0.0461, -0.0225, -0.8345], "rotation": [ -0.5264, 0.9995, 1.8463], "temperature": 29.47 }
|
||||
Loading…
x
Reference in New Issue
Block a user