Skip to content

Module Usage

Haoqing Yang edited this page Apr 1, 2026 · 2 revisions

Steps to use the module

In main program:

  1. create a list of imu managers managers = IMUFactory.detect_and_create(free_threading, log_data) with free_threading being flag to enable free_threading and log_data being the flag to log recorded data.
  2. Set rotation matrix if needed. sensor_manager.set_rotation_matrix(matrix) with matrix being a 3x3 rotation matrix.
  3. start each manager sensor_manager.start()
  4. data stream loop example data = sensor_manager.get_data()
"""Basic module usage example."""
import time

from loguru import logger

from imu_python.definitions import IMUUpdateTime
from imu_python.factory import IMUFactory


def main() -> None:
    """Run a simple demonstration."""
    sensor_managers = IMUFactory.detect_and_create()
    for manager in sensor_managers:
        manager.start()

    try:
        while True:
            for manager in sensor_managers:
                logger.info(manager.get_data())
            time.sleep(IMUUpdateTime.freq_hz)
            #Note: this read frequency is independent of IMUs actual hardware frequency
    except KeyboardInterrupt:
        for manager in sensor_managers:
            manager.stop()


if __name__ == "__main__":
    main()

Recording and plotting of data

Recording

  • Running IMUFactory.detect_and_create(log_data=True) will record data with the following columns:

time in seconds

accels (x, y, z) in m/s^2

gyros (x, y, z) in rad/s

mags (x, y, z) normalized (Calibration transforms data close to the unit sphere)

quats pose as quaternion (w, x, y, z)

  • By default, the recorded data file is saved under data/recordings with a timestamp

Plotting

running src/imu_python/data_handler/data_plotter with the argument --filepath or -f followed by the recorded data file path will render a matplot plot with acceleration, gyroscope, magnetic and quaternion plots in relation to time.

note: In Pycharm, the plots would show up when the file is run in terminal; in VSCode, the file would need to be run in an interactive window with Jupyter instead of the terminal.

Clone this wiki locally