Overview of the MPU6050 library
masterjrowberg/i2cdevlib.repository·master·Indexed 19 days ago
https://github.com/electroniccats/mpu6050An Arduino library for controlling the MPU6050 sensor module, featuring a 3-axis gyroscope, 3-axis accelerometer, and Digital Motion Processor (DMP). Based on jrowberg/i2cdevlib, it supports AVR, SAM, SAMD21, ARM, ESP32, ESP8266, and RENESAS architectures. The library provides classes for 3D math (Quaternion, VectorInt16, VectorFloat) and multiple DMP implementations including MPU6050_6Axis_MotionApps20, MPU6050_6Axis_MotionApps612, and MPU6050_9Axis_MotionApps41 for processing motion data and Euler angles.
jrowberg/i2cdevlib.To install the MPU6050 by Electronic Cats library, use the built-in Arduino Library Manager:
MPU6050.Once the DMP is initialized, you must check for available data packets in the FIFO buffer and process them to extract sensor readings.
Workflow:
dmpPacketAvailable() to check if a new packet is ready.dmpReadAndProcessFIFOPacket() to read and process the data.dmpGet... methods to extract data (e.g., Quaternions, Accel, Gyro) from the processed packet.if (mpu.dmpPacketAvailable()) {
mpu.dmpReadAndProcessFIFOPacket(numPackets, processed);
// Extract data using dmpGet methods
}The MPU6050 library is compatible with the following architectures and chipsets:
You can instruct the DMP to send specific types of data to the FIFO buffer using dmpSend... methods. This is typically done during setup to define what the sensor will output.
Available Output Types:
dmpSendQuaternion(uint_fast16_t accuracy)dmpSendGyro(uint_fast16_t elements, uint_fast16_t accuracy)dmpSendAccel(uint_fast16_t elements, uint_fast16_t accuracy)dmpSendLinearAccel(uint_fast16_t elements, uint_fast16_t accuracy)dmpSendGravity(uint_fast16_t elements, uint_fast16_t accuracy)dmpSendPacketNumber(uint_fast16_t accuracy)dmpSendEIS, dmpSendQuantizedAccel, etc.)To convert quaternion data into human-readable Euler angles, use dmpGetYawPitchRoll or dmpGetEuler.
dmpGetEuler(float *data, Quaternion *q)dmpGetYawPitchRoll(float *data, Quaternion *q, VectorFloat *gravity)float ypr[3];
Quaternion q;
VectorFloat gravity;
mpu.dmpGetQuaternion(q.toRaw(), packet);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
// ypr[0] = Yaw, ypr[1] = Pitch, ypr[2] = RollThe DMP processes sensor data and stores it in a FIFO buffer. Use dmpPacketAvailable() to check if new data is ready, and dmpReadAndProcessFIFOPacket() to pull and process the data from the buffer.
dmpReadAndProcessFIFOPacket(uint8_t numPackets, uint8_t *processed=NULL)
numPackets: The number of packets to read.processed: (Optional) Pointer to store the number of packets actually processed.if (mpu.dmpPacketAvailable()) {
mpu.dmpReadAndProcessFIFOPacket(64); // Read up to 64 packets
}You can control what data the DMP sends to the FIFO and how frequently it samples.
Use dmpSend... methods to configure which sensor data types are sent to the FIFO. Parameters typically include elements and accuracy:
dmpSendQuaternion(uint_fast16_t accuracy)dmpSendGyro(uint_fast16_t elements, uint_fast16_t accuracy)dmpSendAccel(uint_fast16_t elements, uint_fast16_t accuracy)dmpSendGravity(uint_fast16_t elements, uint_fast16_t accuracy)dmpSend... variants)dmpSetFIFORate(uint8_t fifoRate): Sets the FIFO rate.dmpGetFIFORate(): Gets the current FIFO rate.dmpGetSampleFrequency(): Gets the current sample frequency.To use the Digital Motion Processor (DMP) with the MPU6050_9Axis_MotionApps41 class, you must first initialize the DMP using dmpInitialize(). This sets up the internal firmware state for 9-axis motion processing.
Note: This class is part of a partial release and is under active development.
MPU6050_9Axis_MotionApps41 mpu;
// ... setup I2C ...
mpu.dmpInitialize();To use the Digital Motion Processor (DMP) for 6-axis motion processing, instantiate the MPU6050_6Axis_MotionApps612 class and call dmpInitialize(). This prepares the sensor to process motion data internally and output it via the FIFO buffer.
Parameters for dmpInitialize:
rateDivisor: A divisor for the FIFO rate. Defaults to MPU6050_DMP_FIFO_RATE_DIVISOR (0x01).mpuAddr: The I2C address of the MPU6050. Defaults to 0x68.MPU6050_6Axis_MotionApps612 mpu;
// ... setup I2C ...
mpu.dmpInitialize(0x01, 0x68);The DMP outputs data into a FIFO buffer. To prevent overflow and ensure data is processed correctly, use the following methods:
dmpReadAndProcessFIFOPacket(uint8_t numPackets, uint8_t *processed=NULL): Reads and processes a specified number of packets from the FIFO.dmpGetCurrentFIFOPacket(uint8_t *data): An overflow-proof way to get the current FIFO packet.dmpGetFIFOPacketSize(): Returns the size of a single DMP packet.dmpSetFIFOProcessedCallback(void (*func) (void)): Sets a callback function to be executed after a packet of FIFO data is processed.// Example of reading packets in a loop
if (mpu.dmpPacketAvailable()) {
uint8_t packet[dmp.dmpGetFIFOPacketSize()];
mpu.dmpGetCurrentFIFOPacket(packet);
// Use packet with dmpGet... methods
}The Quaternion class provides mathematical operations for representing and manipulating 3D orientations. It uses four floating-point components: w, x, y, and z.
Key operations:
getProduct(Quaternion q) to multiply the current quaternion by another. This is used to combine rotations.getConjugate() to return the conjugate quaternion (useful for rotating vectors).normalize() to scale the quaternion to unit length in-place, or getNormalized() to return a new normalized Quaternion instance.getMagnitude() to calculate the length of the quaternion.// Initialize a quaternion (default is identity: w=1, x=0, y=0, z=0)
Quaternion q;
// Initialize with specific values
Quaternion q2(1.0f, 0.5f, 0.5f, 0.0f);
// Combine rotations
Quaternion combined = q.getProduct(q2);
// Get a normalized version
Quaternion unitQ = q.getNormalized();