SRA Board Components
ESP-IDF component for SRA Board
mpu6050.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2021 Society of Robotics and Automation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#ifndef _MPU6050_H
26#define _MPU6050_H
27
28#include <stdio.h>
29#include <math.h>
30#include <string.h>
31
32#include "sdkconfig.h"
33#include "esp_attr.h"
34
35#include "esp_log.h"
36#include "esp_err.h"
37
38#include "freertos/FreeRTOS.h"
39#include "driver/i2c.h"
40#include "driver/gpio.h"
41#include "esp_timer.h"
42
43#include "pin_defs.h"
44
45#define I2C_MASTER_SCL_IO MPU6050_SCL
46#define I2C_MASTER_SDA_IO MPU6050_SDA
47#define I2C_MASTER_NUM I2C_NUM_1
48#define I2C_MASTER_TX_BUF_DISABLE 0
49#define I2C_MASTER_RX_BUF_DISABLE 0
50#define I2C_MASTER_FREQ_HZ 100000
52#define MPU6050_RA_PWR_MGMT_1 0x6B
53#define MPU6050_CLOCK_PLL_XGYRO 0x01
54
55#define MPU6050_RA_GYRO_CONFIG 0x1B
56#define MPU6050_GYRO_FS_250 0x00
57
58#define MPU6050_RA_ACCEL_CONFIG 0x1C
59#define MPU6050_ACCEL_FS_2 0x00
60
61#define MPU6050_ADDR 0x68
62#define ACCE_START_ADDR 0x3B
63#define GYRO_START_ADDR 0x43
65#define WRITE_BIT I2C_MASTER_WRITE
66#define READ_BIT I2C_MASTER_READ
67#define ACK_CHECK_EN 0x1
68#define ACK_CHECK_DIS 0x0
69#define ACK_VAL 0x0
70#define NACK_VAL 0x1
72#define ALPHA 0.9834
73#define RAD_TO_DEG 57.2957795
74#define BUFF_SIZE 6
75
76#define MPU_CALIBRATION_AVG_COUNT CONFIG_MPU_CALIBRATION_AVG_COUNT
77
83esp_err_t i2c_master_init();
84
92esp_err_t enable_mpu6050(void);
93
101esp_err_t mpu6050_read_acce(uint8_t *data_rd, size_t size);
102
110esp_err_t mpu6050_read_gyro(uint8_t *data_rd, size_t size);
111
119void combine_msb_lsb_raw_data(uint8_t *buf_1, int16_t *buf_2);
120
130void compute_acce_angle(int16_t ax, int16_t ay, int16_t az, float *acce_angle);
131
142void compute_gyro_angle(int16_t gx, int16_t gy, int16_t gz, float dt, float *gyro_angle);
143
152esp_err_t read_mpu6050_raw(int16_t *acce_raw_value, int16_t *gyro_raw_value);
153
162void complementary_filter(int16_t *acce_raw_value, int16_t *gyro_raw_value, float *complementary_angle, float *mpu_offset);
163
171esp_err_t read_mpu6050(float *euler_angle, float *mpu_offset);
172
177esp_err_t calibrate_mpu6050();
178
187esp_err_t avg_sensors(int16_t *acce_raw_value_avg, int16_t *gyro_raw_value_avg, const int16_t *acce_offs, const int16_t *gyro_offs);
188
189#endif
esp_err_t enable_mpu6050(void)
Initialise MPU-6050 (by sending the appropriate queued commands); Refer this for more information: ht...
Definition: mpu6050.c:54
esp_err_t read_mpu6050(float *euler_angle, float *mpu_offset)
The ultimate function (application ready); takes in the input raw values and initial conditions and g...
Definition: mpu6050.c:286
esp_err_t mpu6050_read_acce(uint8_t *data_rd, size_t size)
Get the accelerometer raw values (Ax, Ay, Az) into an 8-bit array Note that these raw values are actu...
Definition: mpu6050.c:88
void complementary_filter(int16_t *acce_raw_value, int16_t *gyro_raw_value, float *complementary_angle, float *mpu_offset)
Fuse the gyroscope and accelerometer angle in a complementary fashion More on this here: https://robo...
Definition: mpu6050.c:167
void compute_gyro_angle(int16_t gx, int16_t gy, int16_t gz, float dt, float *gyro_angle)
Compute the Euler angle (Pitch and Roll) from the gyroscope raw values Axes and Angles - X: Roll,...
Definition: mpu6050.c:135
esp_err_t mpu6050_read_gyro(uint8_t *data_rd, size_t size)
Get the gyroscope raw values (Gx, Gy, Gz) into an 8-bit array Note that these raw values are actually...
Definition: mpu6050.c:103
esp_err_t read_mpu6050_raw(int16_t *acce_raw_value, int16_t *gyro_raw_value)
A wrapper for reading the 8-bit raw values from MPU and then combining them to their final form i....
Definition: mpu6050.c:152
esp_err_t avg_sensors(int16_t *acce_raw_value_avg, int16_t *gyro_raw_value_avg, const int16_t *acce_offs, const int16_t *gyro_offs)
Helper function for the function calibrate_mpu6050() to calculate the average of the raw values.
Definition: mpu6050.c:202
void combine_msb_lsb_raw_data(uint8_t *buf_1, int16_t *buf_2)
Combine two 8-bit values to a 16-bit one Note that EVEN indices in the input buffer represent the MSB...
Definition: mpu6050.c:118
void compute_acce_angle(int16_t ax, int16_t ay, int16_t az, float *acce_angle)
Compute the Euler angle (Pitch and Roll) from the accelerometer raw values Axes and Angles - X: Roll,...
Definition: mpu6050.c:128
esp_err_t calibrate_mpu6050()
Function to calculate the MPU offset for raw values.
Definition: mpu6050.c:232
esp_err_t i2c_master_init()
Initialise the ESP32 I2C Driver in Master Mode.
Definition: mpu6050.c:39
lv_color_t * buf_1[DISP_BUF_SIZE]
Definition: oled.c:243