Hi I have successfully run this code:

from depthai_sdk import OakCamera
from depthai_sdk.classes import IMUPacket

with OakCamera() as oak:
imu = oak.create_imu()
imu.config_imu(report_rate=400, batch_report_threshold=5)

def callback(packet: IMUPacket):
    print(packet)

oak.callback(imu.out.main, callback=callback)
oak.start(blocking=True)

how can I print individual (x,y,z ) values of gyro meter, accelerometer and magnetometer in terminal.!?

  • erik replied to this.
    • Best Answerset by erik

    SamiUddin I hope this helps:

    def callback(packet: IMUPacket):
         for d in packet.data:
            gyro: dai.IMUReportGyroscope = d.gyroscope
            accel: dai.IMUReportAccelerometer = d.acceleroMeter
            mag: dai.IMUReportMagneticField = d.magneticField
            print(accel.x, accel.y, accel.z)
    
    
    with OakCamera() as oak:
        imu = oak.create_imu()
        imu.config_imu(report_rate=10, batch_report_threshold=2)
        print(oak.device.getConnectedIMU())
        oak.callback(imu, callback=callback)
        oak.start(blocking=True)

    SamiUddin I hope this helps:

    def callback(packet: IMUPacket):
         for d in packet.data:
            gyro: dai.IMUReportGyroscope = d.gyroscope
            accel: dai.IMUReportAccelerometer = d.acceleroMeter
            mag: dai.IMUReportMagneticField = d.magneticField
            print(accel.x, accel.y, accel.z)
    
    
    with OakCamera() as oak:
        imu = oak.create_imu()
        imu.config_imu(report_rate=10, batch_report_threshold=2)
        print(oak.device.getConnectedIMU())
        oak.callback(imu, callback=callback)
        oak.start(blocking=True)
    a year later

    Code Reference: https://docs-old.luxonis.com/projects/sdk/en/latest/samples/IMUComponent/sdk_imu/#source-code

    from depthai_sdk import OakCamera
    from depthai_sdk.classes import IMUPacket

    with OakCamera() as oak:
    imu = oak.create_imu()
    imu.config_imu(report_rate=400, batch_report_threshold=5)

    def callback(packet: IMUPacket):
        print(packet)
    
    oak.callback(imu.out.main, callback=callback)
    oak.start(blocking=True)

    In above code where to define LINEAR_ACCELERATION.

    Kind Regards!

    @erik

    @SamiUddin

    imu.config_imu(sensors=[dai.IMUSensor.LINEAR_ACCELERATION], report_rate=400, batch_report_threshold=5)

      @erik And How can get the detection/nnData in the same callback!? I tried it by arg the [imu, nn] in oak.visualize but Its always confuse in my packets.

      Hi @SamiUddin ,
      You can use oak.callback, as visualize doesn't support imu -

          def callback(self,
                       output: Union[List, Callable, Component],
                       callback: Callable,
                       main_thread=False
                       ) -> CallbackPacketHandler:
              """
              Create a callback for the component output(s). This handles output streaming (OAK->Host) and message syncing.
      
              Args:
                  output: Component output(s) to be visualized. If component is passed, SDK will visualize its default output.
                  callback: Handler function to which the Packet will be sent.
                  main_thread: Whether to run the callback in the main thread. If False, it will call the callback in a separate thread, so some functions (eg. cv2.imshow) won't work.
              """

      Example here:
      luxonis/depthaiblob/main/depthai_sdk/examples/mixed/packet_queue.py

      7 days later

      Hi, Is it possible to read the data from the magnetometer in the terminal? I have the impression that it has not been defined. Would you know how to find out the magnetometer data ?

      IMU Packet: ['Accelerometer [m/s2]: (x: 0.08, y: -0.34, z: -9.89), Gyroscope [rad/s]: (x: -0.00, y: 0.00, z: -0.01))', 'Accelerometer [m/s2]: (x: 0.05, y: -0.35, z: -9.92), Gyroscope [rad/s]: (x: 0.00, y: -0.01, z: 0.00))', 'Accelerometer [m/s2]: (x: 0.07, y: -0.34, z: -9.91), Gyroscope [rad/s]: (x: -0.01, y: -0.00, z: -0.01))', 'Accelerometer [m/s2]: (x: 0.06, y: -0.34, z: -9.90), Gyroscope [rad/s]: (x: -0.01, y: 0.00, z: -0.01))', 'Accelerometer [m/s2]: (x: 0.06, y: -0.35, z: -9.91), Gyroscope [rad/s]: (x: -0.01, y: 0.00, z: 0.00))']

      It's a simple code like the code in the conversation above, my goal is really to understand how I can get the data from the magnetometer because I only receive zeros from this sensor value. I think it is not defined.

      from depthai_sdk import OakCamera
      from depthai_sdk.classes import IMUPacket
      from time import sleep
      
      with OakCamera() as oak:
      imu = oak.create_imu()
      imu.config_imu(report_rate=400, batch_report_threshold=5)
      
      def callback(packet: IMUPacket):
      print(packet)
      sleep(1)
      
      oak.callback(imu.out.main, callback=callback)
      oak.start(blocking=True)
      
      # from depthai_sdk import OakCamera
      # from depthai_sdk.classes import IMUPacket
      
      # def callback(packet: IMUPacket):
      # for d in packet.data:
      # gyro: dai.IMUReportGyroscope = d.gyroscope
      # accel: dai.IMUReportAccelerometer = d.acceleroMeter
      # mag: dai.IMUReportMagneticField = d.magneticField
      # print(gyro.x, gyro.y, gyro.z)
      # print(accel.x, accel.y, accel.z)
      # print(mag.x, mag.y, mag.z)
      
      # with OakCamera() as oak:
      # imu = oak.create_imu()
      # imu.config_imu(report_rate=10, batch_report_threshold=2)
      # print(oak.device.getConnectedIMU())
      # oak.callback(imu, callback=callback)
      # oak.start(blocking=True)

      Hi @AugustinLetourneur
      As you have written below:

      from depthai_sdk import OakCamera
      import depthai as dai
      from depthai_sdk.classes import IMUPacket
      from time import sleep
      
      with OakCamera() as oak:
          imu = oak.create_imu()
          imu.config_imu(sensors=[dai.IMUSensor.MAGNETOMETER_CALIBRATED], report_rate=400, batch_report_threshold=5)
      
          def callback(packet: IMUPacket):
              for d in packet.data:
                  gyro: dai.IMUReportGyroscope = d.gyroscope
                  accel: dai.IMUReportAccelerometer = d.acceleroMeter
                  mag: dai.IMUReportMagneticField = d.magneticField
                  print(mag.x, mag.y, mag.z)
      
          oak.callback(imu, callback=callback)
          oak.start(blocking=True)

      Printing the packet won't work since the packet's defined __str__ method will only print GYRO and ACC.

      Keep in mind you need BNO IMU for this to work; you will be notified if you have BMI.

      Thanks,
      Jaka