cancel
Showing results for 
Search instead for 
Did you mean: 

How to continuously read LSM6DSRTR data under Linux iio?

WYang.9
Associate

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)

1 REPLY 1
Federica Bossi
ST Employee

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

In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.