SRA Board Components
ESP-IDF component for SRA Board
Macros | Functions
mpu6050.c File Reference
#include "mpu6050.h"
#include "i2cdev.h"
Include dependency graph for mpu6050.c:

Macros

#define MIN_ACCE_ERROR   5
 
#define MIN_GYRO_ERROR   5
 
#define MAX_CALIBRATION_ATTEMPTS   20
 
#define G_RAW_VALUE   16384
 

Functions

esp_err_t i2c_master_init (void)
 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 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...
 
esp_err_t calibrate_mpu6050 ()
 Function to calculate the MPU offset for raw values. 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...
 

Macro Definition Documentation

◆ G_RAW_VALUE

#define G_RAW_VALUE   16384

◆ MAX_CALIBRATION_ATTEMPTS

#define MAX_CALIBRATION_ATTEMPTS   20

◆ MIN_ACCE_ERROR

#define MIN_ACCE_ERROR   5

◆ MIN_GYRO_ERROR

#define MIN_GYRO_ERROR   5

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