EX

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Has thanked: 83 times
Been thanked: 67 times

EX

Post by RoccoMarco »

Hi,
I am working on a peripheral abstraction layer named EX. It is possible to find the current code on the trunk (even if it is at a very early stage).

I am concentrating in these days on MEMS and I had an interesting conversation with korken. I have some old code for more than a MEMS and I will gradually merge it on trunk. Before to do this I would like to improve interfaces making them suitable for most common uses like sensor fusion and estimation problems.

I hope to trig a discussion on certain API related to calibration of sensor data. Currently, I am working on gyroscope... a full calibration requires the measurement of 3 bias values and 3 sensitivity values (one for each axis). While bias can be acquired keeping sensor hold and acquiring some raw data, sensitivity requires a much more complex procedure which involves an external instrument and this means it could not be retrieved in a automatic way. My purpose to solve this problems is an API like this:

Code: Select all

/**
 * @name    Macro Functions (BaseGyroscope)
 * @{
 */
/**
 * @brief   Gyroscope get axes number.
 *
 * @param[in] ip        pointer to a @p BaseGyroscope class.
 * @return              The number of axes of the BaseSensor
 *
 * @api
 */
#define gyroscopeGetAxesNumber(ip)                                          \
        (ip)->vmt_basegyroscope->get_axes_number(ip)

/**
 * @brief   Gyroscope read raw data.
 *
 * @param[in] ip        pointer to a @p BaseGyroscope class.
 * @param[in] dp        pointer to a data array.
 *
 * @return              The operation status.
 * @retval MSG_OK       if the function succeeded.
 * @retval MSG_RESET    if one or more errors occurred.
 *
 * @api
 */
#define gyroscopeReadRaw(ip, dp)                                            \
        (ip)->vmt_basegyroscope->read_raw(ip, dp)

/**
 * @brief   Gyroscope read cooked data.
 *
 * @param[in] ip        pointer to a @p BaseGyroscope class.
 * @param[in] dp        pointer to a data array.
 *
 * @return              The operation status.
 * @retval MSG_OK       if the function succeeded.
 * @retval MSG_RESET    if one or more errors occurred.
 *
 * @api
 */
#define gyroscopeReadCooked(ip, dp)                                         \
        (ip)->vmt_basegyroscope->read_cooked(ip, dp)

/**
 * @brief   Gyroscope bias sampling procedure.
 * @note    During this procedure gyroscope must be kept hold in the rest
 *          position. Sampled bias will be automatically removed after calling
 *          this procedure.
 *
 * @param[in] ip        pointer to a @p BaseGyroscope class.
 *
 * @return              The operation status.
 * @retval MSG_OK       if the function succeeded.
 * @retval MSG_RESET    if one or more errors occurred.
 *
 * @api
 */
#define gyroscopeSampleBias(ip)                                             \
        (ip)->vmt_basegyroscope->sample_bias(ip)

/**
 * @brief   Gyroscope setting bias procedure.
 * @note    bp must be a pointer to a buffer having the same length of the
 *          the gyroscope axes number.
 *
 * @param[in] ip        pointer to a @p BaseGyroscope class.
 * @param[in] bp        pointer to a buffer of bias values.
 *
 * @return              The operation status.
 * @retval MSG_OK       if the function succeeded.
 * @retval MSG_RESET    if one or more errors occurred.
 *
 * @api
 */
#define gyroscopeSetBias(ip, bp)                                            \     
        (ip)->vmt_basegyroscope->set_bias(ip,bp)
      
/**
 * @brief   Reset bias data restoring it to zero.
 *
 * @param[in] ip        pointer to a @p BaseGyroscope class.
 *
 * @return              The operation status.
 * @retval MSG_OK       if the function succeeded.
 * @retval MSG_RESET    if one or more errors occurred.
 *
 * @api
 */
#define gyroscopeResetBias(ip)                                               \
        (ip)->vmt_basegyroscope->reset_bias(ip)
      
/**
 * @brief   Gyroscope setting sensitivity procedure.
 * @note    sp must be a pointer to a buffer having the same length of the
 *          the gyroscope axes number.
 *
 * @param[in] ip        pointer to a @p BaseGyroscope class.
 * @param[in] sp        pointer to a buffer of sensitivity values.
 *
 * @return              The operation status.
 * @retval MSG_OK       if the function succeeded.
 * @retval MSG_RESET    if one or more errors occurred.
 *
 * @api
 */
#define gyroscopeSetSensitivity(ip, sp)                                     \     
        (ip)->vmt_basegyroscope->set_sensitivity(ip,sp)
      
/**
 * @brief   Reset sensitivity data restoring it to typical value.
 *
 * @param[in] ip        pointer to a @p BaseGyroscope class.
 *
 * @return              The operation status.
 * @retval MSG_OK       if the function succeeded.
 * @retval MSG_RESET    if one or more errors occurred.
 *
 * @api
 */
#define gyroscopeResetSensitivity(ip)                                       \
        (ip)->vmt_basegyroscope->reset_sensitivity(ip)
/** @} */


After that remains the temperature compensation. I think this could not be defined on interface level because I don't know if every Gyroscope has the capability to detect temperature. Nonetheless it is possible ad an API at device level something like

Code: Select all

msg_t l3gd20EnableTempCompens(L3GD20Driver* devp);
msg_t l3gd20DisableTempCompens(L3GD20Driver* devp);


Any feedback, suggestion, tip?
Ciao,
RM
Marco
Posts: 128
Joined: Tue Apr 16, 2013 8:22 pm
Has thanked: 4 times
Been thanked: 11 times

Re: EX

Post by Marco »

Hi,

I followed that conversation and the current progress looks promising to me.

About the temperature compensation, just an idea: Maybe this can be defined on interface level but devices that don't have the capability to do temperature compensation just have an empty stub. This way the interface is the same for all gyros and no device specific function has to be called.

Marco
User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Has thanked: 83 times
Been thanked: 67 times

Re: EX

Post by RoccoMarco »

Hi Marco,

indeed it could be a good idea... Anyway, I would understand if the proposed solution is suitable for mentioned application or if there would be a better way to do this... Your experiences will be very useful here.
User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Has thanked: 5 times
Been thanked: 6 times

Re: EX

Post by Korken »

I very much like the current developments!
On the temperature compensation, I agree that just a stub is enough as it varies from gyro (and acc / mag).

And when it comes to calibration (as we discussed) especially the gyro is difficult as measuring the sensitivity requires a jig of sorts.
For the Mag and Acc though, I have finished (general) algorithms for gain and bias calibration, if you are interested that I can share them.
Though they require that a procedure is followed.

As I have stated before the final thing that is missing for general use in estimation and similar applications is the synchronization of readout with EXT.
However, I certainly believe that we can find a solution for this! :)
Right now what is missing for this is I-class functions in the VMTs, so a measurement can be started from the EXT interrupt.
Perhaps this is the way to do it and leave the implementation to the user? What do you think?

Best Regards
Emil Fresk
User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Has thanked: 83 times
Been thanked: 67 times

Re: EX

Post by RoccoMarco »

Hi Emil,
On the temperature compensation, I agree that just a stub is enough as it varies from gyro (and acc / mag).

There is no problem if there are differences between gyro, acc or mag since interfaces could be different. Adding these function to the vmt has been a good idea since we can provide macros at interface level and code will be much more portable (thanks Marco for this tip). Anyway, when the temperature sensing is not available these are just stubs and I would find a way to notice user that this features is not available on that MEMS. Most likely best way could be an osalAssert.

And when it comes to calibration (as we discussed) especially the gyro is difficult as measuring the sensitivity requires a jig of sorts.
For the Mag and Acc though, I have finished (general) algorithms for gain and bias calibration, if you are interested that I can share them.
Though they require that a procedure is followed.

If there is a way to calibrate the MEMS at run time I would to provide specific function (like GyroSampleBias()). Anyway, as you said, sometime the procedure is much more complicated: in these cases I think that functions like Set/ResetBias() or Set/ResetSensitivity() can be a good way to allow advanced uses of EX. About calibration procedure I hope you can share this code moreover suggest some whitepaper (related to this topic).

About EXT and I-class I think this is a good point probably the most important. I will add such features and your experience will be very important on this point. But I would to consolidate EX by adding calibration functionalities and extending support to other MEMS.

Thanks for replies,
RM
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: EX

Post by Giovanni »

Adding I-class would be troublesome because you would have to rely on driver callbacks, this brings 2 problems:

1) I2C driver does not support callbacks so I2C-connected devices could not implement the API.
2) The application would be handled by the application, adding complexity.

Personally I would limit it to thread level API, threads are very light in ChibiOS so no real loss.

About interrupts, shouldn't that be handled simply by the EXT driver? including the handling in the peripheral driver would put a dependency on the board files because the connected pin is not necessarily always the same.

I recommend KISS, look at EX as a core API, higher level services would be built on top of EX (I would like so see proposals about higher level services).

Note that EX is not just MEMS/sensors, we need to start talking about other peripheral classes like displays, EEPROMs, Flash etc. It is meant to be the abstraction on all external things built on top of HAL.

Giovanni
User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Has thanked: 83 times
Been thanked: 67 times

Re: EX

Post by RoccoMarco »

Thanks Giovanni,

@korken if MEMS are set in Continous update mode do you think something like this wouldn't be enough for kalman estimation?

l3gd20Start();
lsm303dlhcStart();

while(true) {
gyroGetCooked();
accelGetCoocked();
compassGetCooked()
kalmanUpdate();
...
}
User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Has thanked: 5 times
Been thanked: 6 times

Re: EX

Post by Korken »

About calibration procedure I hope you can share this code moreover suggest some whitepaper (related to this topic).

This should not a problem! For the calibration it is just a simple least squares problem after some manipulation. :)

Adding I-class would be troublesome because you would have to rely on driver callbacks, this brings 2 problems:

1) I2C driver does not support callbacks so I2C-connected devices could not implement the API.
2) The application would be handled by the application, adding complexity.

Personally I would limit it to thread level API, threads are very light in ChibiOS so no real loss.

About interrupts, shouldn't that be handled simply by the EXT driver? including the handling in the peripheral driver would put a dependency on the board files because the connected pin is not necessarily always the same.

I recommend KISS, look at EX as a core API, higher level services would be built on top of EX (I would like so see proposals about higher level services).

Note that EX is not just MEMS/sensors, we need to start talking about other peripheral classes like displays, EEPROMs, Flash etc. It is meant to be the abstraction on all external things built on top of HAL.

On 1):
This is not a problem, you must not implement the I-class function.
Also, no one serious would use I2C together with OS synchronization for time critical MEMS sensor applications.
The last thing you want to be dependent on is the THREAD priorities and the will of the OS, it should just happen with the interrupt. :)
On 2)
I think this should be automatic if possible, but if you cannot make it because of user constraints then the user must be the one to implement it.
We just provide the tools to make it simpler and less error prone (more on this a little further down).

>> Personally I would limit it to thread level API, threads are very light in ChibiOS so no real loss.
I do not agree here. The simple case of multiple sensor readout threads (all at high priority) can block another sensor from reading if it does something that takes a little time.
If the ISRs are used then the burden is given to the DMA controllers, and later the processing -- but the data will already have been read out.

>> About interrupts, shouldn't that be handled simply by the EXT driver? including the handling in the peripheral driver would put a dependency on the board files because the connected pin is not necessarily always the same.
Not sure if I follow.
In my mind you should just have an I-class function for the sensor: SensorDataAvailableFromISRI(EXTDriver *extp, expchannel_t channel) that implements (starts) a synchronous sensor read.
This function would be placed in the EXT config as the callback function and placing it in the EXT structure would be up to the user _IF_ he wants to use this.
Then it is not a requirement. This has the strong advantage that the majority of digital MEMS sensors have the data ready interrupt.
That is as KISS as I can get it for now. What do you think?

>> Note that EX is not just MEMS/sensors, we need to start talking about other peripheral classes like displays, EEPROMs, Flash etc. It is meant to be the abstraction on all external things built on top of HAL.
Agreed, however it is also important that we do not handicap the true usage of something just to make everything compatible.


@korken if MEMS are set in Continous update mode do you think something like this wouldn't be enough for kalman estimation?

.....

Sadly not.
There are 2 main problems here:
    1. How can you guarantee that the data that you are getting has not been aliased due to the user not sampling correctly and missing one or more samples?
    This would practically be breaking the Shannon Sampling Theorem and then you have lost all guarantees and a Kalman Filter cannot fix this.
    For this to work you must poll the sensor at a much higher rate than the sampling rate and inspect it data ready register, which is (in my opinion) a bad solution as it requires a lot of overhead in just polling.

    2. If you are using the data in real time, how can you guarantee that when the data comes from the sensor there is not a varying time delay from data available to readout.
    Without a synchronous read this is practically impossible as the polling frequency will always have some constant gain in time to the sensor data availability, which will cause a rolling time delay to exist.
And both are very important in the majority of high speed estimation and control algorithms.

All comments are welcome! I have a feeling this will amount to something great! :)
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: EX

Post by Giovanni »

Most MEMS use I2C those cannot be polled from ISR regardless all other points.

If you thing you NEED ISRs probably you are not using much of the RTOS, just IMHO.

Edit: need to explain the above point.

I see the need to sample at fixed rate but couldn't this be achieved from an high priority thread? jitter would not be much different that an ISR. What kind of jitter is acceptable for this kind of application?

Giovanni
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: EX

Post by steved »

Giovanni wrote:About interrupts, shouldn't that be handled simply by the EXT driver? including the handling in the peripheral driver would put a dependency on the board files because the connected pin is not necessarily always the same.

In principle this is absolutely right; however I found on a 32F051 application that adding the EXT driver took up about 1K of my 100 bytes of previously unused code space. So I had to manage the interrupt myself (easy enough with the lower level APIs).
Adding the EXT driver would be proportionally less demanding on the larger processors.
Not sure how practical it would be, but could there be a project-wide choice of either using the EXT driver, or of implementing dedicated coding?

On I2C, I've modified the standard driver to give the option of a character by character callback on received data - useful for "poll until ready" type requirements. Code is elsewhere in this forum; needs some cleanup, but seems to work.
Post Reply