MPU6050 by Electronic Cats

repository·master·Indexed 19 days ago

https://github.com/electroniccats/mpu6050

An 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.

Tokens
3.4K
Snippets
11
Records
20
Agent score
65%

What's inside electroniccats-mpu6050

  1. Overview of the MPU6050 library

    master
    The MPU6050 library provides an interface for controlling the MPU6050 module, which integrates a 3-axis gyroscope and a 3-axis accelerometer on a single silicon die. It also supports the onboard Digital Motion Processor (DMP) for processing complex 6-axis MotionFusion algorithms. This library is based on jrowberg/i2cdevlib.
  2. Install the MPU6050 library via Arduino Library Manager

    master

    To install the MPU6050 by Electronic Cats library, use the built-in Arduino Library Manager:

    1. Open the Arduino IDE.
    2. Open the Library Manager.
    3. Search for MPU6050.
    4. Locate the library named MPU6050 by Electronic Cats and click Install.
  3. Read motion data packets from FIFO

    master

    Once the DMP is initialized, you must check for available data packets in the FIFO buffer and process them to extract sensor readings.

    Workflow:

    1. Use dmpPacketAvailable() to check if a new packet is ready.
    2. Use dmpReadAndProcessFIFOPacket() to read and process the data.
    3. Use specific 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
    }
  4. Configure FIFO output types

    master

    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)
    • (And others including dmpSendEIS, dmpSendQuantizedAccel, etc.)
  5. Get Euler Angles (Yaw, Pitch, Roll)

    master

    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] = Roll
  6. Read DMP packets from FIFO

    master

    The 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
    }
  7. Configure DMP FIFO output and sampling

    master

    You can control what data the DMP sends to the FIFO and how frequently it samples.

    FIFO Output Configuration

    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)
    • (And other dmpSend... variants)

    Sampling and Rate Control

    • dmpSetFIFORate(uint8_t fifoRate): Sets the FIFO rate.
    • dmpGetFIFORate(): Gets the current FIFO rate.
    • dmpGetSampleFrequency(): Gets the current sample frequency.
  8. Initialize MPU6050 with MotionApps41 DMP

    master

    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();
  9. Initialize DMP with MPU6050_6Axis_MotionApps612

    master

    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);
  10. Manage FIFO data and packet processing

    master

    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
    }
  11. Use the Quaternion class for 3D rotations

    master

    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:

    • Multiplication: Use getProduct(Quaternion q) to multiply the current quaternion by another. This is used to combine rotations.
    • Conjugation: Use getConjugate() to return the conjugate quaternion (useful for rotating vectors).
    • Normalization: Use normalize() to scale the quaternion to unit length in-place, or getNormalized() to return a new normalized Quaternion instance.
    • Magnitude: Use 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();