Pyrrha-Platform / Pyrrha-Z

Next-generation firmware built on the Zephyr RTOS.
Apache License 2.0
2 stars 0 forks source link

drivers: sensor: implement mics4514 driver #7

Closed bpbradley closed 2 years ago

bpbradley commented 2 years ago

Closes #3

Also adds a way to include additional public APIs, as is needed for the gas sensor since the zephyr sensor subsystem doesn't support the necessary channels.

This driver is fairly complex, as it has both analog and digital functions and requires some special routines for accurate data.

To do before merge

bpbradley commented 2 years ago

Sensor API is now fully implemented and I believe it is calculating gas concentrations correctly, but this is difficult to be sure of without a calibrated reference.

I created a samples subsystem with the latest commit as well so that as more complicated sensors like this are developed we can create small easy to understand sample usages. This is also helpful for testing.

It took me some time to understand how the measurement of the sensor actually works, but I believe I figured it out. I tried to make the conversion from analog to ppm as clear as possible in the comments and the implementation. I had a brief discussion on slack about it, but basically it comes from tabular data extrapolated from the concentration curves on the sensor datasheet. log(Rs/R0) is linearly correlated with log(ppm), and so we calculate ppm from that interpolation.

This does mean we need an accurate R0, which I am trusting we have from the original code. We will likely need to nail that number down, and we will likely need to improve this driver to grab R0 from a setting that can be adjusted, as we will likely need to calibrate R0 periodically. Right now R0 and vref are developer configurable through Kconfig, but they are static.

I also need to implement the preheating function of the sensor so that it will accurately measure. Right now the preheat gpio is configured but it isn't being used. The preheat time should also be configurable and non-blocking (async).

I will get back to this next week to close out some of these final items (except for the calibration part, I will leave that as a separate issue down the line).

Also something weird is happening with the actions environment, previously passing builds are failing on the unit tests. It appears to be on the zephyr end of things, I will look into it before we merge.

Unit tests passing as well image

bpbradley commented 2 years ago

I believe the actions environment is working now. It was an issue on the zephyr CI end, it changes some environment variables.

Now this is passing tests, and samples are building as part of CI.

bpbradley commented 2 years ago

This should now be fully functional. There are some improvements to be made which can be new issues / PRs in the future, but for now this is sufficiently complete to work with.

Tests for preheat function are also included and passing

image

The warnings are expected and required for the tests to pass (user should be warned when trying to access sensor before preheat is finished).

Improvements for the future:

  1. Add an API extension specific to the mics4514 which allows the user to directly check that preheating is complete. Right now it just returns -EBUSY and a warning is issued to the application.
  2. Try to eliminate newlibc requirement and heavy math functions to calculate gas concentration. Possibly make a configuration which allows for just capturing raw numbers, and allowing the concentration calculation to be sent off device
  3. Allow R0 to be saved as a setting held somewhere in NVM so that it can be field-calibrated