- TABLE OF CONTENTS
- MPU6050
- Inertial Measurement Unit
- Initialising MPU6050
- Description of Functions Used
- Flowchart of Code
- It is a 6-axis motion tracking device
- The MPU-6050 is a commonly used chip that combines a MEMS gyroscope and a MEMS accelerometer and uses a standard I2C bus for data transmission
- The system processor is an I2C master to the MPU6050 (default address is 0x68 but can be changed to 0x69)
- The MPU6050 can also act as an I2C master and directly accept inputs from an external compass
- This is 3.3V device and hence is safe to connect to ESP32 pins directly.
- These are small devices indicating changing orientation in smartphones, video game remotes, quadcopters, etc.
- The IMU has both a gyroscope which measures angular rate and an accelerometer which measures linear acceleration, and/or compasses (magnetometers)
- The number of sensor inputs in an IMU are referred to as ‘DoF’ (Degrees of Freedom), so a chip with a 3-axis gyroscope and a 3-axis accelerometer would be a 6-DOF IMU.
-
When power up, MPU6050 will start in SLEEP mode. To use it, we need to first disable this SLEEP mode
-
To use the I2C protocol, we send the value 0 to register PWR_MGMT_1 (0x6b)
-
The procedure to do so is as follows :
- Send a start sequence
- Send the I2C address (0x68) of the MPU6050 with the R/W bit high
- Send the internal register number you want to write to (0x6b : PWR_MGMT_1)
- Send the data byte (0x00) - This will power on the device
- Send the stop sequence
The data byte 0x00 sets all the bits of register 107 to 0, thus disabling SLEEP mode and CYCLE mode, and powering on the MPU-6050
There are various terms related with the Pulse Width Modulation:-
- Off-Time :- Duration of time period when the signal is low.
- ON-Time :- Duration of time period when the signal is high.
- Duty cycle :- It is the percentage of the time period when the signal remains ON during the period of the pulse width modulation signal.
- Period :- It is the sum of off-time and on-time of pulse width modulation signal.
- An accelerometer is a sensor that measures the force of linear acceleration.
- It's output is in the form of m/s^2 of g-forces on a body. It is highly sensitive to movement in any direction.
- The MPU6050 uses a MEMS accelerometer, which consists of stationary and movable electrodes acting as capacitor plates
- Acceleration produces change in g-forces on the moving device, which causes displacement of the movable capacitor plates
- This causes a change in capacitance across the plates, which is detected internally by change in Voltage
- We then calculate the displacement from the in Capacitance, which helps us infer the value of acceleration
- The acceleration of the three axes, X,Y and Z, are stored in 3 pairs of registers on the MPU6050
- Each register on the MPU6050 is an 8-bit register
- In each of these pairs, one register stores the MSB (most significant bit) and the other stores the LSB (least significant bit)
- These 6 registers are burst-read and the 8-bit MSB and LSB bytes for each axis are combined to give a single 16-bit float
- The roll and pitch angles are calculated from the accelerometer using the formula below :
- A gyroscope is a sensor that measures the change in velocity (if at all) of a device rotating around its own axis.
- A gyroscope measures changes in angular velocity.
- The MPU6050 uses a MEMS gyroscope, which uses Coriolis effect
- The acceleration of the three axes, X,Y and Z, are stored in 3 pairs of registers on the MPU6050
- Each register on the MPU6050 is an 8-bit register
- In each of these pairs, one register stores the MSB (most significant bit) and the other stores the LSB (least significant bit)
- These 6 registers are burst-read and the 8-bit MSB and LSB bytes for each axis are combined to give a single 16-bit float
- The gyroscope has a sensitivity factor of about 131, i.e. 1 degree of rotation gives a reading of 131 units
- Therefore, the degrees of rotation are obtained by dividing the raw readings by 131
- Euler angles are calculated by integrating raw readings of each axis over time
- Since gyroscopic drift accumulates over time, it is necessary to reset the rate readings from the gyroscope at frequent intervals, so the drift can be minimised
- To do this, we combine both the gyroscope and accelerometer readings in a weighted average
- The value of alpha is determined experimentally
- To read/receive data from a slave, the master has to first send a START condition addressing the MPU6050
- Then the master must WRITE the requested register from which the data is to be read
- After this, instead of sending a STOP condition to stop the WRITE operation and then sending a consecutive START condition to start the READ operation, the I2C bus provides the repeated start functionality, wherein instead of sending a STOP-START condition, we send only a START condition and read the specified number of bytes from the register.
- The procedure to do so is as follows :
- Send a start sequence
- Send the I2C address (0x68) of the MPU6050 with the R/W bit low - This writes to the device
- Send the Internal register address you want to read from
- Send a start sequence again (repeated start)
- Send the I2C address of the device with the R/W bit high - This reads from the device
- Read the number of the requested data bytes from the registers
- Send the stop sequence.
- All write and read operations (except the last read) are answered with a ACK if successful.
-
esp_err_t enable_mpu6050()
Description : MPU6050 comes up in sleep mode upon power-up . It disables the SLEEP_MODE of the MPU by accessing the POWER_MANAGEMENT_1 register and checks if the MPU is initialised correctly.
Parameters :
None
Returns : esp_err_t returns ESP_OK if MPU is enabled properly.
-
esp_err_t read_mpu6050(euler_angle, mpu_offset)
Description : Calculates the Roll and Pitch angles of the bot by applying complementary filter to the raw accelerometer and gyroscope values and using the given MPU offset. It stores the Roll angle in euler_angle[0] and the Pitch engle in euler_angle[1].
Parameters :
euler_angle
: Array to store euler angles , Pitch and Roll.mpu_offset
: Array to specify MPU offsets , Pitch offset ( About Y-axis ) and Roll offset (About X-axis).
Returns : esp_err_t returns ESP_OK if it read mpu successfully, else it returns ESP_FAIL.