matrix-io / matrixio-kernel-modules

MATRIX HAL in kernel space
22 stars 23 forks source link

Convert raw sensors values to actual values. #17

Closed alvarowolfx closed 6 years ago

alvarowolfx commented 6 years ago

I'm using the Matrix kernel modules to leverage the mic array driver and tried to use together with Malos, but I saw later that they don't work together.

Now I'm reading the sensor values directly from the iio devices, but I'm getting some raw sensor values that I don't know how to convert to the real values. This is my class that interfaces with Matrix board, and is using libiio python binding to access iio devices.


import iio

class MatrixBoard(object):
    NUM_LEDS = 35

    def __init__(self):
        self.image = []
        self.humidity = -1

        ctx = iio.Context()
        self.imu_device = ctx.devices[0]
        self.env_device = ctx.devices[1]

        # initial data setup
        self._reset_leds()

    def _reset_leds(self):
        self.set_all_led()
        self._sync_everloop()

    def set_led(self, index, red=0, green=0, blue=0, white=0):
        '''
            Set an speficic led color on the matrix board
        '''
        if(index < 0 or index > 35):
            return

        self.image[index] = [red, green,  white, blue]

    def set_all_led(self, red=0, green=0, blue=0, white=0):
        '''
            Set all leds colors on the matrix board
        '''
        led_value = [red, green, white, blue]

        image = []
        for _ in range(self.NUM_LEDS):
            image.append(led_value)

        self.image = image

    def _sync_everloop(self):
        color_array = bytearray()
        for i in range(0, self.NUM_LEDS):
            color_array += bytearray(self.image[i])
        with open('/dev/matrixio_everloop', 'wb') as bin_file:
            bin_file.write(color_array)
        bin_file.close()

    def _sync_humidity(self):
        hum_channel = [
            ch for ch in self.env_device.channels if ch.id == 'humidityrelative']
        if(hum_channel):
            hum_channel = hum_channel[0]
            self.humidity = hum_channel.attrs['raw'].value

    def sync(self):
        '''
            Sync all local data to the matrix board
        '''
        self._sync_everloop()
        self._sync_humidity()

The focus here is the _sync_humidity function that queries the iio devices to update current reading values. The raw value for the humidity, for example, return something like:

'1127464.960000'
'1127452.672000'
'1127456.768000'

How do I convert this to the actual sensor value?

Thanks for the help.

kdpatino commented 6 years ago

Hello @alvarowolfx

Yes, You are right, you need to compile a special MCU firmware: https://github.com/matrix-io/matrix-creator-mcu/tree/kp/kernel

Due to in the kernel space, it is not allowed to use float type data. We made some adjustments in the MCU firmware.

So let's compile kp/kernel branch and flash it with openocd.

It could help you: https://github.com/matrix-io/matrix-creator-init/blob/master/sam3-program.bash#L61

Best regards,

Kevin Patino

alvarowolfx commented 6 years ago

Thanks for the answer @kdpatino.

It worked here for the first time, but after a reboot, I'm getting only zero values from the humidity readings. Already tried to write the firmware again but none of it worked =/.

These are the steps that I made:

  1. Clone the https://github.com/matrix-io/matrix-creator-mcu/tree/kp/kernel repository, enter the creator folder and run make command. This generated to me some files on a build folder and I thought that the right file was the ch.bin.
  2. Clone the https://github.com/matrix-io/matrix-creator-init repository and copy the ch.bin file to the blob folder of the matrix-creator-init project.
  3. Then run sudo openocd -f cfg/sam3s_rpi_sysfs.cfg on the matrix-creator-init folder.
kdpatino commented 6 years ago

Hello @alvarowolfx

We release a new version of: 1- https://github.com/matrix-io/matrix-creator-init/releases/tag/v0.4.8 2- https://github.com/matrix-io/matrix-creator-hal/releases/tag/v0.3.1 3- https://github.com/matrix-io/matrixio-kernel-modules/releases/tag/v0.1.3

With these changes, you could read the sensors values using direct access (/dev/spidev) or kernel modules (iio / regmap).

Let me know if you have any doubt about these changes.

Regards,

Kevin Patino