trikset / trikRuntime

Runtime for TRIK controller
Apache License 2.0
16 stars 55 forks source link

[new age] IMU support in runtime #668

Open anastasiia-kornilova opened 3 years ago

anastasiia-kornilova commented 3 years ago

In new_age distro IMU sensors were moved from input-device interface to iio linux subsystem, therefore way of communication and event files locations were changed.

How now information from gyro and accel could be accessed? By default everything related to iio is kept by path /sys/bus/iio/devices/iio\:device<N>. On TRIK board, device 0 is accelerometer, device 1 is gyro.

There are two ways to obtain sensor value:

Way 1 (default) Read file responsible for every channel. For example, to read accel x-axis someone could use:

cat /sys/bus/iio/devices/iio\:device0/in_accel_x_raw

The same is for other sensor channels.

For our case, that is not the best way to collect all channels data from separate files manually and assign with proper timestamps, so the next method is more relevant.

Way 2 (buffers) iio provides "buffer" support, which puts all chosen channels and corresponding timestamps to the buffer and then could be read and unpacked.

For that, someone should turn on buffer support and specify channels to put into the buffer

Code below enables buffer support and specify what channels will be available in the buffer. Code below enables buffer support and stores all accel values in the buffer.

echo 1 > /sys/bus/iio/devices/iio\:device0/buffer/enable
echo 1 > /sys/bus/iio/devices/iio\:device0/scan_elements/in_timestamp_en
echo 1 > /sys/bus/iio/devices/iio\:device0/scan_elements/in_accel_x_en
echo 1 > /sys/bus/iio/devices/iio\:device0/scan_elements/in_accel_y_en
echo 1 > /sys/bus/iio/devices/iio\:device0/scan_elements/in_accel_z_en

In order to properly unpack data, check corresponding flags of every channel -- its position in buffer and way of storage (https://www.kernel.org/doc/html/v4.14/driver-api/iio/index.html).

External libraries Because iio encapsulates common and well-defined logic for every sensor type, maybe it is better to use already existing libs to read data from sensors instead of doing this manually. There are two common of them: libiio looks like the well-developed one.

This issue is about providing IMU data in runtime taking into account the new logic of interaction with IMU

anastasiia-kornilova commented 3 years ago

Tiny example on C++ with QSocketNotifier on how to read and cast data from accelerometer buffer trik-iio.zip