SparkFun ICM-20948 Arduino Library

repository·main·Indexed 42 days ago

https://github.com/sparkfun/sparkfun_icm-20948_arduinolibrary

An Arduino library for the TDK InvenSense ICM-20948 9DoF IMU sensor, specifically designed for the SparkFun 9DoF IMU Breakout (Qwiic). It supports advanced sensor fusion and motion processing via the Digital Motion Processor (DMP™) as of version 1.2, allowing for the extraction of quaternions, orientation, and calibrated sensor data through a FIFO buffer.

Tokens
5.6K
Snippets
8
Records
29
Agent score
51%

What's inside SparkFun ICM-20948 Arduino Library

  1. Overview of the SparkFun ICM-20948 Arduino Library

    main

    The SparkFun ICM-20948 Arduino Library provides an interface for the TDK InvenSense ICM-20948 9-Degree Of Freedom (9DoF) Inertial Measurement Unit (IMU) sensor. It is specifically designed for use with the SparkFun 9DoF IMU Breakout - ICM-20948 (Qwiic).

    Key features include support for the InvenSense Digital Motion Processor (DMP™) as of version 1.2, which allows for advanced sensor fusion and motion processing directly on the chip.

  2. Understand the InvenSense Digital Motion Processor (DMP™)

    main
    The Digital Motion Processor (DMP™) is a specialized hardware engine within the ICM-20948 sensor. It is designed to offload complex sensor fusion calculations (such as orientation, quaternions, and Euler angles) from the main microcontroller. By using the DMP, you can obtain processed motion data without manually implementing the heavy mathematical algorithms required to combine accelerometer, gyroscope, and magnetometer data.
  3. Configure DMP data rate

    main

    The output data rate of the DMP is determined by a combination of two settings:

    1. The raw sensor rate, set via setSampleRate().
    2. The DMP Output Data Rate (ODR) registers, set via setDMPODRrate().

    To ensure correct operation, other settings must be adjusted to match the sample rate. Refer to Examples 9 and 10 in the library repository for implementation details.

  4. How the DMP is loaded and started

    main

    The DMP is not active by default. To use it, the library must perform a specific sequence of SPI/I2C transactions to upload the DMP firmware into the sensor's memory banks and then trigger the start command. This process involves:

    1. Bank Switching: The ICM-20948 uses multiple register banks. The library must switch between Bank 0, Bank 1, and Bank 2 to access specific configuration registers.
    2. Memory Bank Selection: The DMP firmware is loaded into specific memory banks (e.g., Bank 0x01, 0x04) using the Memory Bank Select (Reg 0x7E) and Memory Start Address (Reg 0x7C) registers.
    3. Firmware Upload: The library writes the DMP firmware data to these memory locations.
    4. Initialization: Once the firmware is loaded, the sensor is configured (e.g., setting accelerometer and gyroscope scales) and the DMP is started.
  5. Understanding the InvenSense Digital Motion Processor (DMP™)

    main
    The Digital Motion Processor (DMP™) is a specialized hardware engine within the ICM-20948 sensor. It offloads complex sensor fusion calculations (like orientation and quaternion math) from the main microcontroller. Instead of the developer manually calculating Euler angles or quaternions from raw accelerometer and gyroscope data, the DMP processes the sensor data internally and provides high-level motion data (such as Quaternions) via a FIFO buffer.
  6. Enable DMP support in the library

    main

    DMP (Digital Motion Processor) support is disabled by default to save program memory (it occupies 14kBytes). To enable it, you must manually edit the library source files.

    1. Locate ICM_20948_C.h in the src/util/ folder of the library.
    2. Uncomment line 29 to define ICM_20948_USE_DMP.

    File Path: src/util/ICM_20948_C.h

    // Change this:
    //#define ICM_20948_USE_DMP
    
    // To this:
    #define ICM_20948_USE_DMP
  7. Configure B2S Mount Matrices

    main

    Similar to the compass, the B2S (likely referring to specific sensor orientation/mounting) requires mount matrix configuration. This process involves:

    1. Selecting the correct memory bank (e.g., writing 0x0D to register 0x7E).
    2. Setting the memory start address via register 0x7C.
    3. Writing the specific matrix index (e.g., B2S_MTX_00, B2S_MTX_11).
    4. Writing the 32-bit matrix value to register 0x7D.

    Note: Configuration often requires toggling PWR_MGMT_1 (Register 0x06) to specific modes (like 0x21 for 'Turn on low power; Auto clock best available') between writes to ensure stability.

  8. Configure Compass Mount Matrices

    main

    To ensure accurate compass readings, you must configure the Compass Mount Matrix. This tells the DMP how the magnetometer is oriented relative to the ICM-20948 sensor. The configuration involves writing to the memory start address (Register 0x7C) and then setting the specific matrix registers (Register 0x7D) with the appropriate 32-bit values.

    Example SPI sequence for setting Compass Mount Matrix 11 to 0xF6666667:

    1. Write 0x7C to PWR_MGMT_1 (Bank 0) to set the memory start address.
    2. Write the address for CPASS_MTX_11 (e.g., 0x84 for CPASS_MTX_12 in the log, though specific offsets vary).
    3. Write 0x7D to set the matrix value.
    4. Provide the 4 bytes of the 32-bit value (e.g., F6 66 66 67).
  9. How to access DMP data via FIFO

    main

    Once the DMP is running, data is stored in a FIFO buffer. To retrieve it, follow this pattern:

    1. Check Data Readiness: Monitor DATA_RDY_STATUS (Reg 0x8A) or check the DMP_INT_STATUS (Reg 0x18) via interrupts.
    2. Check FIFO Count: Read FIFO_COUNTH (Reg 0x70) to determine how many bytes are available in the FIFO.
    3. Read FIFO: Read the data from FIFO_R_W (Reg 0x72). The data will arrive in a structured format defined by your DATA_OUT_CTL settings (e.g., a Quat6 header followed by the quaternion values and a footer).
  10. How to access DMP data

    main
    Once the DMP is loaded and running, it populates internal registers with processed motion data. To access this data, you typically poll the sensor for new data packets or wait for an interrupt. The library provides high-level methods to read these processed values (like quaternions or Euler angles) directly from the sensor's output registers, abstracting away the complex bank-switching and register-reading logic required by the raw hardware protocol.
  11. How to load and start the DMP

    main

    To use the DMP, the sensor must be configured through a specific sequence of register writes. Based on the SPI trace logs, the typical initialization flow involves:

    1. Resetting and Enabling: Writing to USER_CTRL (Reg 0x03) to enable DMP and FIFO, and resetting the I2C/SPI interface.
    2. Power Management: Configuring PWR_MGMT_2 (Reg 0x07) to disable unused sensors (e.g., pressure) and enable the necessary axes.
    3. Data Output Configuration: Setting DATA_OUT_CTL1 (Reg 0x40) and DATA_OUT_CTL2 (Reg 0x42) to define the data format (e.g., Quat6 + Header).
    4. Interrupt Configuration: Setting DATA_INTR_CTL (Reg 0x4C) to enable interrupts when data is ready.
    5. Motion Event Configuration: Setting MOTION_EVENT_CTL (Reg 0x4E) for features like Gyro/Accel calibration.
    6. Bank Switching: Using MEMORY_BANK_SELECT (Reg 0x7E) to access different register banks required for specific settings like ACCEL_ONLY_GAIN or GYRO_SF.
    7. Sample Rate Configuration: Setting ACCEL_SMPLRT_DIV_1 and GYRO_SMPLRT_DIV in Bank 2 to define the Output Data Rate (ODR).