Skip to main content
Visitor II
February 15, 2023
Question

How to continuously read LSM6DSRTR data under Linux iio?

  • February 15, 2023
  • 1 reply
  • 1377 views

I am using​ LSM6DSRTR, and the device-tree compatible is "st,lsm6dsr".

I can see two iio device node under

/sys/bus/iio/devices/iio:device1
/sys/bus/iio/devices/iio:device2

But when I try to read in a python loop, it gets really slow.

​Anyway better to read it?

 def getData(self, path):
 fd = open(path, "r")
 data = fd.read().strip()
 fd.close()
 return data
 
 def testData(self):
 iio_oneshot_path = "/sys/bus/iio/devices/iio:device2/in_accel_x_raw"
 fd = open(iio_oneshot_path, "r")
 
 while self.refresh:
 self.accX = self.getData('/sys/bus/iio/devices/iio:device2/in_accel_x_raw')
 self.accY = self.getData('/sys/bus/iio/devices/iio:device2/in_accel_y_raw')
 self.accZ = self.getData('/sys/bus/iio/devices/iio:device2/in_accel_z_raw')
 self.gyroX = self.getData('/sys/bus/iio/devices/iio:device1/in_anglvel_x_raw')
 self.gyroY = self.getData('/sys/bus/iio/devices/iio:device1/in_anglvel_y_raw')
 self.gyroZ = self.getData('/sys/bus/iio/devices/iio:device1/in_anglvel_z_raw')
 print(self.accX)
 time.sleep(0.01)

    This topic has been closed for replies.

    1 reply

    Technical Moderator
    April 12, 2023

    Ciao @WYang.9​ ,

    Welcome to ST Community!

    The most recommended way to read data in continuous mode is to use the IIO buffer interface and for this I suggest the generic_buffer tool or the libiio test application iio_read.

    If it is necessary to read the data via raw data to speed up the reading procedure it's recommended to set a high ODR because when you use raw data read the driver turn on the device than expect an ODR and furthermore the first samples must be discarded because the device is turned on, this means waiting much longer than an ODR.

    If my reply answered your question, please click on Select as Best at the bottom of this post. This will help other users with the same issue to find the answer faster