mirror of
https://github.com/Mercantec-GHC/h5-projekt-mst.git
synced 2026-08-26 21:27:38 +02:00
rename mpu6050
This commit is contained in:
parent
01679d6da7
commit
d5b31c9232
@ -1,5 +1,5 @@
|
|||||||
idf_component_register(
|
idf_component_register(
|
||||||
SRCS skateboard.c app_wifi.c app_mqtt.c new_mpu6050.c
|
SRCS skateboard.c app_wifi.c app_mqtt.c mpu6050.c
|
||||||
PRIV_REQUIRES nvs_flash esp_netif esp_wifi mqtt esp_driver_i2c esp_timer
|
PRIV_REQUIRES nvs_flash esp_netif esp_wifi mqtt esp_driver_i2c esp_timer
|
||||||
INCLUDE_DIRS ".")
|
INCLUDE_DIRS ".")
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
#include "new_mpu6050.h"
|
#include "mpu6050.h"
|
||||||
#include "driver/i2c_master.h"
|
#include "driver/i2c_master.h"
|
||||||
#include "driver/i2c_types.h"
|
#include "driver/i2c_types.h"
|
||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
@ -13,7 +13,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static const char* TAG = "new_mpu6050";
|
static const char* TAG = "mpu6050";
|
||||||
|
|
||||||
#define CHECK(EXPR) \
|
#define CHECK(EXPR) \
|
||||||
do { \
|
do { \
|
||||||
@ -108,7 +108,7 @@ static esp_err_t write_bits(
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t new_mpu6050_init(Mpu6050* dev)
|
esp_err_t mpu6050_init(Mpu6050* dev)
|
||||||
{
|
{
|
||||||
*dev = (Mpu6050) { 0 };
|
*dev = (Mpu6050) { 0 };
|
||||||
|
|
||||||
@ -149,16 +149,16 @@ esp_err_t new_mpu6050_init(Mpu6050* dev)
|
|||||||
|
|
||||||
vTaskDelay(pdMS_TO_TICKS(250));
|
vTaskDelay(pdMS_TO_TICKS(250));
|
||||||
|
|
||||||
CHECK(new_mpu6050_set_clock_source(dev, MPU6050_CLKSEL_PLL_GYRO_X_REF));
|
CHECK(mpu6050_set_clock_source(dev, MPU6050_CLKSEL_PLL_GYRO_X_REF));
|
||||||
|
|
||||||
CHECK(new_mpu6050_set_sleep_enabled(dev, false));
|
CHECK(mpu6050_set_sleep_enabled(dev, false));
|
||||||
|
|
||||||
CHECK(new_mpu6050_set_sample_rate_div(dev, 3));
|
CHECK(mpu6050_set_sample_rate_div(dev, 3));
|
||||||
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t new_mpu6050_get_rotation(Mpu6050* dev, float3* rotation)
|
esp_err_t mpu6050_get_rotation(Mpu6050* dev, float3* rotation)
|
||||||
{
|
{
|
||||||
static const float resolution[] = {
|
static const float resolution[] = {
|
||||||
[MPU6050_GYRO_RANGE_250] = 1.0f / 131.0f,
|
[MPU6050_GYRO_RANGE_250] = 1.0f / 131.0f,
|
||||||
@ -183,7 +183,7 @@ esp_err_t new_mpu6050_get_rotation(Mpu6050* dev, float3* rotation)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t new_mpu6050_get_acceleration(Mpu6050* dev, float3* accel)
|
esp_err_t mpu6050_get_acceleration(Mpu6050* dev, float3* accel)
|
||||||
{
|
{
|
||||||
static const float resolution[] = {
|
static const float resolution[] = {
|
||||||
[MPU6050_ACCEL_RANGE_2] = 1.0f / 16384.0f,
|
[MPU6050_ACCEL_RANGE_2] = 1.0f / 16384.0f,
|
||||||
@ -208,7 +208,7 @@ esp_err_t new_mpu6050_get_acceleration(Mpu6050* dev, float3* accel)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t new_mpu6050_read_temperature(Mpu6050* dev, float* temperature)
|
esp_err_t mpu6050_read_temperature(Mpu6050* dev, float* temperature)
|
||||||
{
|
{
|
||||||
|
|
||||||
uint8_t buffer[2];
|
uint8_t buffer[2];
|
||||||
@ -219,14 +219,14 @@ esp_err_t new_mpu6050_read_temperature(Mpu6050* dev, float* temperature)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t new_mpu6050_deinit(Mpu6050* dev)
|
esp_err_t mpu6050_deinit(Mpu6050* dev)
|
||||||
{
|
{
|
||||||
CHECK(i2c_master_bus_rm_device(dev->i2c_dev));
|
CHECK(i2c_master_bus_rm_device(dev->i2c_dev));
|
||||||
CHECK(i2c_del_master_bus(dev->i2c_bus));
|
CHECK(i2c_del_master_bus(dev->i2c_bus));
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t new_mpu6050_set_clock_source(Mpu6050* dev, Mpu6050_ClockSource source)
|
esp_err_t mpu6050_set_clock_source(Mpu6050* dev, Mpu6050_ClockSource source)
|
||||||
{
|
{
|
||||||
return write_bits(dev,
|
return write_bits(dev,
|
||||||
REG_PWR_MGMT_1,
|
REG_PWR_MGMT_1,
|
||||||
@ -235,7 +235,7 @@ esp_err_t new_mpu6050_set_clock_source(Mpu6050* dev, Mpu6050_ClockSource source)
|
|||||||
source);
|
source);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t new_mpu6050_set_gyro_range(Mpu6050* dev, Mpu6050_GyroRange range)
|
esp_err_t mpu6050_set_gyro_range(Mpu6050* dev, Mpu6050_GyroRange range)
|
||||||
{
|
{
|
||||||
CHECK(write_bits(dev,
|
CHECK(write_bits(dev,
|
||||||
REG_GYRO_CONFIG,
|
REG_GYRO_CONFIG,
|
||||||
@ -246,7 +246,7 @@ esp_err_t new_mpu6050_set_gyro_range(Mpu6050* dev, Mpu6050_GyroRange range)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t new_mpu6050_set_accel_range(Mpu6050* dev, Mpu6050_AccelRange range)
|
esp_err_t mpu6050_set_accel_range(Mpu6050* dev, Mpu6050_AccelRange range)
|
||||||
{
|
{
|
||||||
CHECK(write_bits(dev,
|
CHECK(write_bits(dev,
|
||||||
REG_ACCEL_CONFIG,
|
REG_ACCEL_CONFIG,
|
||||||
@ -257,17 +257,17 @@ esp_err_t new_mpu6050_set_accel_range(Mpu6050* dev, Mpu6050_AccelRange range)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t new_mpu6050_set_sleep_enabled(Mpu6050* dev, bool enabled)
|
esp_err_t mpu6050_set_sleep_enabled(Mpu6050* dev, bool enabled)
|
||||||
{
|
{
|
||||||
return write_bits(dev, REG_PWR_MGMT_1, BIT_PWR_MGMT_1_SLEEP, 1, 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)
|
esp_err_t mpu6050_set_sample_rate_div(Mpu6050* dev, uint8_t div)
|
||||||
{
|
{
|
||||||
return write_reg(dev, REG_SMPLRT_DIV, div);
|
return write_reg(dev, REG_SMPLRT_DIV, div);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t new_mpu6050_set_dlpf(Mpu6050* dev, Mpu6050_DLPF selector)
|
esp_err_t mpu6050_set_dlpf(Mpu6050* dev, Mpu6050_DLPF selector)
|
||||||
{
|
{
|
||||||
return write_bits(
|
return write_bits(
|
||||||
dev, REG_CONFIG, BIT_CONFIG_DLPF_CFG, MASK_CONFIG_DLPF_CFG, selector);
|
dev, REG_CONFIG, BIT_CONFIG_DLPF_CFG, MASK_CONFIG_DLPF_CFG, selector);
|
||||||
@ -292,13 +292,13 @@ static void calibrate_measure_cb(void* arg)
|
|||||||
goto loop_break;
|
goto loop_break;
|
||||||
|
|
||||||
float3 accel;
|
float3 accel;
|
||||||
calib->status = new_mpu6050_get_acceleration(calib->dev, &accel);
|
calib->status = mpu6050_get_acceleration(calib->dev, &accel);
|
||||||
|
|
||||||
if (calib->status != ESP_OK)
|
if (calib->status != ESP_OK)
|
||||||
goto loop_break;
|
goto loop_break;
|
||||||
|
|
||||||
float3 rotation;
|
float3 rotation;
|
||||||
calib->status = new_mpu6050_get_rotation(calib->dev, &rotation);
|
calib->status = mpu6050_get_rotation(calib->dev, &rotation);
|
||||||
|
|
||||||
if (calib->status != ESP_OK)
|
if (calib->status != ESP_OK)
|
||||||
goto loop_break;
|
goto loop_break;
|
||||||
@ -318,7 +318,7 @@ loop_break:
|
|||||||
xEventGroupSetBits(calib->event_group, 1);
|
xEventGroupSetBits(calib->event_group, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t new_mpu6050_calibrate(Mpu6050* dev, const float3* initial_rotation)
|
esp_err_t mpu6050_calibrate(Mpu6050* dev, const float3* initial_rotation)
|
||||||
{
|
{
|
||||||
|
|
||||||
Calibrator calib = {
|
Calibrator calib = {
|
||||||
@ -60,29 +60,28 @@ typedef struct {
|
|||||||
float3 accel_bias;
|
float3 accel_bias;
|
||||||
} Mpu6050;
|
} Mpu6050;
|
||||||
|
|
||||||
esp_err_t new_mpu6050_init(Mpu6050* dev);
|
esp_err_t mpu6050_init(Mpu6050* dev);
|
||||||
esp_err_t new_mpu6050_deinit(Mpu6050* dev);
|
esp_err_t mpu6050_deinit(Mpu6050* dev);
|
||||||
|
|
||||||
esp_err_t new_mpu6050_get_rotation(Mpu6050* dev, float3* rotation);
|
esp_err_t mpu6050_get_rotation(Mpu6050* dev, float3* rotation);
|
||||||
esp_err_t new_mpu6050_get_acceleration(Mpu6050* dev, float3* accel);
|
esp_err_t mpu6050_get_acceleration(Mpu6050* dev, float3* accel);
|
||||||
esp_err_t new_mpu6050_read_temperature(Mpu6050* dev, float* temperature);
|
esp_err_t mpu6050_read_temperature(Mpu6050* dev, float* temperature);
|
||||||
|
|
||||||
esp_err_t new_mpu6050_set_clock_source(
|
esp_err_t mpu6050_set_clock_source(Mpu6050* dev, Mpu6050_ClockSource source);
|
||||||
Mpu6050* dev, Mpu6050_ClockSource source);
|
esp_err_t mpu6050_set_gyro_range(Mpu6050* dev, Mpu6050_GyroRange range);
|
||||||
esp_err_t new_mpu6050_set_gyro_range(Mpu6050* dev, Mpu6050_GyroRange range);
|
esp_err_t mpu6050_set_accel_range(Mpu6050* dev, Mpu6050_AccelRange range);
|
||||||
esp_err_t new_mpu6050_set_accel_range(Mpu6050* dev, Mpu6050_AccelRange range);
|
esp_err_t mpu6050_set_sleep_enabled(Mpu6050* dev, bool enabled);
|
||||||
esp_err_t new_mpu6050_set_sleep_enabled(Mpu6050* dev, bool enabled);
|
|
||||||
|
|
||||||
#define NEW_MPU6050_SAMPLE_RATE_TO_DIV_1KHZ(SAMPLE_RATE) \
|
#define MPU6050_SAMPLE_RATE_TO_DIV_1KHZ(SAMPLE_RATE) \
|
||||||
((uint8_t)(1000.0f / (float)(SAMPLE_RATE) - 1.0f))
|
((uint8_t)(1000.0f / (float)(SAMPLE_RATE) - 1.0f))
|
||||||
#define NEW_MPU6050_SAMPLE_RATE_TO_DIV_8KHZ(SAMPLE_RATE) \
|
#define MPU6050_SAMPLE_RATE_TO_DIV_8KHZ(SAMPLE_RATE) \
|
||||||
((uint8_t)(8000.0f / (float)(SAMPLE_RATE) - 1.0f))
|
((uint8_t)(8000.0f / (float)(SAMPLE_RATE) - 1.0f))
|
||||||
|
|
||||||
// Sample rate = Gyro output rate / (1 + Sample rate divier)
|
// Sample rate = Gyro output rate / (1 + Sample rate divier)
|
||||||
// Optionally use
|
// Optionally use
|
||||||
// - `NEW_MPU6050_SAMPLE_RATE_TO_DIV_1KHZ()`, if DLPF is enabled
|
// - `MPU6050_SAMPLE_RATE_TO_DIV_1KHZ()`, if DLPF is enabled
|
||||||
// - `NEW_MPU6050_SAMPLE_RATE_TO_DIV_8KHZ()`, if DLPF is disabled
|
// - `MPU6050_SAMPLE_RATE_TO_DIV_8KHZ()`, if DLPF is disabled
|
||||||
esp_err_t new_mpu6050_set_sample_rate_div(Mpu6050* dev, uint8_t div);
|
esp_err_t mpu6050_set_sample_rate_div(Mpu6050* dev, uint8_t div);
|
||||||
|
|
||||||
typedef enum : uint8_t {
|
typedef enum : uint8_t {
|
||||||
// Accelerometer: bandwidth = 260Hz, delay = 0.0ms
|
// Accelerometer: bandwidth = 260Hz, delay = 0.0ms
|
||||||
@ -117,6 +116,6 @@ typedef enum : uint8_t {
|
|||||||
|
|
||||||
// Digital low pass filter (DLPF)
|
// Digital low pass filter (DLPF)
|
||||||
// Note: Sampling rate is dependent on whether DLPF is enabled.
|
// Note: Sampling rate is dependent on whether DLPF is enabled.
|
||||||
esp_err_t new_mpu6050_set_dlpf(Mpu6050* dev, Mpu6050_DLPF selector);
|
esp_err_t mpu6050_set_dlpf(Mpu6050* dev, Mpu6050_DLPF selector);
|
||||||
|
|
||||||
esp_err_t new_mpu6050_calibrate(Mpu6050* dev, const float3* initial_rotation);
|
esp_err_t mpu6050_calibrate(Mpu6050* dev, const float3* initial_rotation);
|
||||||
@ -5,12 +5,11 @@
|
|||||||
#include "esp_netif.h"
|
#include "esp_netif.h"
|
||||||
#include "esp_timer.h"
|
#include "esp_timer.h"
|
||||||
#include "freertos/idf_additions.h"
|
#include "freertos/idf_additions.h"
|
||||||
|
#include "mpu6050.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "new_mpu6050.h"
|
|
||||||
|
|
||||||
const char* TAG = "skateboard";
|
const char* TAG = "skateboard";
|
||||||
|
|
||||||
typedef struct App {
|
typedef struct App {
|
||||||
@ -69,13 +68,13 @@ static void mpu_timer_cb(void* arg)
|
|||||||
(void)app;
|
(void)app;
|
||||||
|
|
||||||
float3 accel = { 0 };
|
float3 accel = { 0 };
|
||||||
ESP_ERROR_CHECK(new_mpu6050_get_acceleration(&app->mpu, &accel));
|
ESP_ERROR_CHECK(mpu6050_get_acceleration(&app->mpu, &accel));
|
||||||
|
|
||||||
float3 rotation = { 0 };
|
float3 rotation = { 0 };
|
||||||
ESP_ERROR_CHECK(new_mpu6050_get_rotation(&app->mpu, &rotation));
|
ESP_ERROR_CHECK(mpu6050_get_rotation(&app->mpu, &rotation));
|
||||||
|
|
||||||
float temp = 0;
|
float temp = 0;
|
||||||
ESP_ERROR_CHECK(new_mpu6050_read_temperature(&app->mpu, &temp));
|
ESP_ERROR_CHECK(mpu6050_read_temperature(&app->mpu, &temp));
|
||||||
|
|
||||||
app->accel_acc.x = accel.x;
|
app->accel_acc.x = accel.x;
|
||||||
app->accel_acc.y = accel.y;
|
app->accel_acc.y = accel.y;
|
||||||
@ -109,11 +108,11 @@ void app_main(void)
|
|||||||
// app_wifi_init(&app.wifi);
|
// app_wifi_init(&app.wifi);
|
||||||
// app_mqtt_init(&app.mqtt);
|
// app_mqtt_init(&app.mqtt);
|
||||||
|
|
||||||
ESP_ERROR_CHECK(new_mpu6050_init(&app.mpu));
|
ESP_ERROR_CHECK(mpu6050_init(&app.mpu));
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Calibrating MPU6050");
|
ESP_LOGI(TAG, "Calibrating MPU6050");
|
||||||
ESP_ERROR_CHECK(
|
ESP_ERROR_CHECK(
|
||||||
new_mpu6050_calibrate(&app.mpu, &(float3) { 0.0f, 0.0f, -1.0f }));
|
mpu6050_calibrate(&app.mpu, &(float3) { 0.0f, 0.0f, -1.0f }));
|
||||||
ESP_LOGI(TAG, "MPU6050 calibrated");
|
ESP_LOGI(TAG, "MPU6050 calibrated");
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_timer_start_periodic(mpu_timer, 40000));
|
ESP_ERROR_CHECK(esp_timer_start_periodic(mpu_timer, 40000));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user