SRA Board Components
ESP-IDF component for SRA Board
|
#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"
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.9950 |
#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... | |
#define ACCE_START_ADDR 0x3B |
accelerometer start address
#define ACK_CHECK_DIS 0x0 |
I2C master will not check ack from slave
#define ACK_CHECK_EN 0x1 |
I2C master will check ack from slave
#define ACK_VAL 0x0 |
I2C ack value
#define ALPHA 0.9950 |
#define BUFF_SIZE 6 |
#define GYRO_START_ADDR 0x43 |
gyroscope start address
#define I2C_MASTER_FREQ_HZ 100000 |
I2C master clock frequency
#define I2C_MASTER_NUM I2C_NUM_1 |
I2C port number for master dev
#define I2C_MASTER_RX_BUF_DISABLE 0 |
I2C master do not need buffer
#define I2C_MASTER_SCL_IO MPU6050_SCL |
gpio number for I2C master clock
#define I2C_MASTER_SDA_IO MPU6050_SDA |
gpio number for I2C master data
#define I2C_MASTER_TX_BUF_DISABLE 0 |
I2C master do not need buffer
#define MPU6050_ACCEL_FS_2 0x00 |
#define MPU6050_ADDR 0x68 |
slave address for mpu6050 sensor
#define MPU6050_CLOCK_PLL_XGYRO 0x01 |
#define MPU6050_GYRO_FS_250 0x00 |
#define MPU6050_RA_ACCEL_CONFIG 0x1C |
#define MPU6050_RA_GYRO_CONFIG 0x1B |
#define MPU6050_RA_PWR_MGMT_1 0x6B |
#define MPU_CALIBRATION_AVG_COUNT CONFIG_MPU_CALIBRATION_AVG_COUNT |
#define NACK_VAL 0x1 |
I2C nack value
#define RAD_TO_DEG 57.2957795 |
#define READ_BIT I2C_MASTER_READ |
I2C master read
#define WRITE_BIT I2C_MASTER_WRITE |
I2C master write
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.
acce_raw_value_avg | Input array of accelerometer raw values (passed by reference) to be filled by the function. |
gyro_raw_value_avg | Input array of gyroscope raw values (passed by reference) to be filled by the function. |
acce_offs | Offset to be applied to the accelerometer raw values. |
gyro_offs | Offset to be applied to the gyroscope raw values. |
esp_err_t calibrate_mpu6050 | ( | ) |
Function to calculate the MPU offset for raw values.
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)
buf_1 | 8-bit Input array of size 6 |
buf_2 | 16-bit Output array of size 3 |
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.
acce_raw_value | Raw values from the accelerometer |
gyro_raw_value | Raw values from the gyroscope |
complementary_angle | Resultant fused and filtered angle |
mpu_offset | Offset of the MPU (accelerometer) at rest position |
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.
ax | Raw accelerometer value (X-axis) |
ay | Raw accelerometer value (Y-axis) |
az | Raw accelerometer value (Z-axis) |
acce_angle | Resultant angle array |
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.
gx | Raw gyroscope value (X-axis) |
gy | Raw gyroscope value (Y-axis) |
gz | Raw gyroscope value (Z-axis) |
dt | Sampling time for gyroscope readings (interval between 2 readings) |
gyro_angle | Resultant angle array |
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.
esp_err_t i2c_master_init | ( | ) |
Initialise the ESP32 I2C Driver in Master Mode.
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.
data_rd | Buffer array for storing raw values |
size | Size of the buffer array (6 in our case) |
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.
data_rd | Buffer array for storing raw values |
size | Size of the buffer array (6 in our case) |
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.
euler_angle | Input array of angles to store the results in (passed by reference) |
mpu_offset | Initial conditions for the accelerometer and gyroscope (Angle at rest position) |
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.
acce_raw_value | 16-bit array for storing the accelerometer raw values |
gyro_raw_value | 16-bit array for storing the gyroscope raw values |