SRA Board Components
ESP-IDF component for SRA Board
Macros | Functions
mpu6050.h File Reference
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "sdkconfig.h"
#include "esp_attr.h"
#include "esp_log.h"
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "driver/i2c.h"
#include "driver/gpio.h"
#include "esp_timer.h"
#include "pin_defs.h"
Include dependency graph for mpu6050.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define I2C_MASTER_SCL_IO   MPU6050_SCL
 
#define I2C_MASTER_SDA_IO   MPU6050_SDA
 
#define I2C_MASTER_NUM   I2C_NUM_1
 
#define I2C_MASTER_TX_BUF_DISABLE   0
 
#define I2C_MASTER_RX_BUF_DISABLE   0
 
#define I2C_MASTER_FREQ_HZ   100000
 
#define MPU6050_RA_PWR_MGMT_1   0x6B
 
#define MPU6050_CLOCK_PLL_XGYRO   0x01
 
#define MPU6050_RA_GYRO_CONFIG   0x1B
 
#define MPU6050_GYRO_FS_250   0x00
 
#define MPU6050_RA_ACCEL_CONFIG   0x1C
 
#define MPU6050_ACCEL_FS_2   0x00
 
#define MPU6050_ADDR   0x68
 
#define ACCE_START_ADDR   0x3B
 
#define GYRO_START_ADDR   0x43
 
#define WRITE_BIT   I2C_MASTER_WRITE
 
#define READ_BIT   I2C_MASTER_READ
 
#define ACK_CHECK_EN   0x1
 
#define ACK_CHECK_DIS   0x0
 
#define ACK_VAL   0x0
 
#define NACK_VAL   0x1
 
#define ALPHA   0.9834
 
#define RAD_TO_DEG   57.2957795
 
#define BUFF_SIZE   6
 
#define MPU_CALIBRATION_AVG_COUNT   CONFIG_MPU_CALIBRATION_AVG_COUNT
 

Functions

esp_err_t i2c_master_init ()
 Initialise the ESP32 I2C Driver in Master Mode. More...
 
esp_err_t enable_mpu6050 (void)
 Initialise MPU-6050 (by sending the appropriate queued commands); Refer this for more information: https://invensense.tdk.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdf. More...
 
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 actually 16-bit, split into MSB (Byte) and LSB (Byte), sent one after the other. More...
 
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 16-bit, split into MSB (Byte) and LSB (Byte), sent one after the other. More...
 
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 (Byte) More...
 
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, Y: Pitch, Z: Yaw. More...
 
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, Y: Pitch, Z: Yaw More on this here: https://philsal.co.uk/projects/imu-attitude-estimation. More...
 
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.e. 16-bit. More...
 
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://robotics.stackexchange.com/questions/10746/complimentary-filter-issues. More...
 
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 gives out the complementary pitch and roll angles. More...
 
esp_err_t calibrate_mpu6050 ()
 Function to calculate the MPU offset for raw values. More...
 
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. More...
 

Macro Definition Documentation

◆ ACCE_START_ADDR

#define ACCE_START_ADDR   0x3B

accelerometer start address

◆ ACK_CHECK_DIS

#define ACK_CHECK_DIS   0x0

I2C master will not check ack from slave

◆ ACK_CHECK_EN

#define ACK_CHECK_EN   0x1

I2C master will check ack from slave

◆ ACK_VAL

#define ACK_VAL   0x0

I2C ack value

◆ ALPHA

#define ALPHA   0.9834

◆ BUFF_SIZE

#define BUFF_SIZE   6

◆ GYRO_START_ADDR

#define GYRO_START_ADDR   0x43

gyroscope start address

◆ I2C_MASTER_FREQ_HZ

#define I2C_MASTER_FREQ_HZ   100000

I2C master clock frequency

◆ I2C_MASTER_NUM

#define I2C_MASTER_NUM   I2C_NUM_1

I2C port number for master dev

◆ I2C_MASTER_RX_BUF_DISABLE

#define I2C_MASTER_RX_BUF_DISABLE   0

I2C master do not need buffer

◆ I2C_MASTER_SCL_IO

#define I2C_MASTER_SCL_IO   MPU6050_SCL

gpio number for I2C master clock

◆ I2C_MASTER_SDA_IO

#define I2C_MASTER_SDA_IO   MPU6050_SDA

gpio number for I2C master data

◆ I2C_MASTER_TX_BUF_DISABLE

#define I2C_MASTER_TX_BUF_DISABLE   0

I2C master do not need buffer

◆ MPU6050_ACCEL_FS_2

#define MPU6050_ACCEL_FS_2   0x00

◆ MPU6050_ADDR

#define MPU6050_ADDR   0x68

slave address for mpu6050 sensor

◆ MPU6050_CLOCK_PLL_XGYRO

#define MPU6050_CLOCK_PLL_XGYRO   0x01

◆ MPU6050_GYRO_FS_250

#define MPU6050_GYRO_FS_250   0x00

◆ MPU6050_RA_ACCEL_CONFIG

#define MPU6050_RA_ACCEL_CONFIG   0x1C

◆ MPU6050_RA_GYRO_CONFIG

#define MPU6050_RA_GYRO_CONFIG   0x1B

◆ MPU6050_RA_PWR_MGMT_1

#define MPU6050_RA_PWR_MGMT_1   0x6B

◆ MPU_CALIBRATION_AVG_COUNT

#define MPU_CALIBRATION_AVG_COUNT   CONFIG_MPU_CALIBRATION_AVG_COUNT

◆ NACK_VAL

#define NACK_VAL   0x1

I2C nack value

◆ RAD_TO_DEG

#define RAD_TO_DEG   57.2957795

◆ READ_BIT

#define READ_BIT   I2C_MASTER_READ

I2C master read

◆ WRITE_BIT

#define WRITE_BIT   I2C_MASTER_WRITE

I2C master write

Function Documentation

◆ avg_sensors()

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.

Parameters
acce_raw_value_avgInput array of accelerometer raw values (passed by reference) to be filled by the function.
gyro_raw_value_avgInput array of gyroscope raw values (passed by reference) to be filled by the function.
acce_offsOffset to be applied to the accelerometer raw values.
gyro_offsOffset to be applied to the gyroscope raw values.
Returns
esp_err_t returns ESP_OK if successful, else ESP_FAIL

◆ calibrate_mpu6050()

esp_err_t calibrate_mpu6050 ( )

Function to calculate the MPU offset for raw values.

Returns
esp_err_t returns ESP_OK if successful, else ESP_FAIL

◆ combine_msb_lsb_raw_data()

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 (Byte)

Parameters
buf_18-bit Input array of size 6
buf_216-bit Output array of size 3

◆ complementary_filter()

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://robotics.stackexchange.com/questions/10746/complimentary-filter-issues.

Parameters
acce_raw_valueRaw values from the accelerometer
gyro_raw_valueRaw values from the gyroscope
complementary_angleResultant fused and filtered angle
mpu_offsetOffset of the MPU (accelerometer) at rest position

◆ compute_acce_angle()

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, Y: Pitch, Z: Yaw.

Parameters
axRaw accelerometer value (X-axis)
ayRaw accelerometer value (Y-axis)
azRaw accelerometer value (Z-axis)
acce_angleResultant angle array

◆ compute_gyro_angle()

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, Y: Pitch, Z: Yaw More on this here: https://philsal.co.uk/projects/imu-attitude-estimation.

Parameters
gxRaw gyroscope value (X-axis)
gyRaw gyroscope value (Y-axis)
gzRaw gyroscope value (Z-axis)
dtSampling time for gyroscope readings (interval between 2 readings)
gyro_angleResultant angle array

◆ enable_mpu6050()

esp_err_t enable_mpu6050 ( void  )

Initialise MPU-6050 (by sending the appropriate queued commands); Refer this for more information: https://invensense.tdk.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdf.

Returns
esp_err_t returns ESP_OK if MPU-6050 initialised successfully, else the appropriate error code Refer this for more info on ESP32 I2C Error codes: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/i2c.html

◆ i2c_master_init()

esp_err_t i2c_master_init ( )

Initialise the ESP32 I2C Driver in Master Mode.

Returns
esp_err_t returns ESP_OK if I2C driver initialised properly, ESP_ERR_INVALID_ARG for Parameter error, ESP_FAIL Driver for install error

◆ mpu6050_read_acce()

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 actually 16-bit, split into MSB (Byte) and LSB (Byte), sent one after the other.

Parameters
data_rdBuffer array for storing raw values
sizeSize of the buffer array (6 in our case)
Returns
esp_err_t returns ESP_OK if acceleromter read successfully, else the appropriate error code

◆ mpu6050_read_gyro()

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 16-bit, split into MSB (Byte) and LSB (Byte), sent one after the other.

Parameters
data_rdBuffer array for storing raw values
sizeSize of the buffer array (6 in our case)
Returns
esp_err_t returns ESP_OK if gyroscope read successfully, else the appropriate error code

◆ read_mpu6050()

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 gives out the complementary pitch and roll angles.

Parameters
euler_angleInput array of angles to store the results in (passed by reference)
mpu_offsetInitial conditions for the accelerometer and gyroscope (Angle at rest position)
Returns
esp_err_t returns ESP_OK if successful, else the appropriate error code

◆ read_mpu6050_raw()

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.e. 16-bit.

Parameters
acce_raw_value16-bit array for storing the accelerometer raw values
gyro_raw_value16-bit array for storing the gyroscope raw values
Returns
esp_err_t returns ESP_OK if successful, else the appropriate error code