This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

nRF52840 SDK16, S140 - Queue library vs Ring buffer library

Hi everyone,

I write a firmware to read the values from a 6-axis IMU and some force sensors. I want to sample with a frequency of 100-200Hz, to store the data in a buffer and then transmit the buffer using notifications every second. Searching the SDK I found two libraries for buffering, the queue library and the ring buffer. I found that the queue library handles overflow, while the ring buffer does not overwrite old data unless the buffer has been read.

Since this is the first time I implement a data logger, what is your recommendation? What buffer should I use for my case? I am heading towards the queue buffer but I would like to know your advice.

In case this is useful, the IMU and force sensors data is of int16_t type and the payload is 32bytes in total (2bytes x 8 force sensors + 2 bytes x 6 IMU sensor + 4bytes timestamp)

I could provide additional information for my project if this could be useful.

Nick

Parents
  • Hi Nick

    One of the main differences between the two is that the ring buffer library is byte based, while the queue library operates on fixed size objects. In your case I think the latter is more useful, since you will be storing several 32 byte records. 

    It is true that the queue library also allows you to overwrite the old data in the case of buffer overflow. In a sensor application like yours this might be a sensible approach, as the data probably loses its usefulness the older it gets. 

    In the end each library could probably serve you well, but I think the queue library is the best option in your case.

    Best regards
    Torbjørn

  • Thank you for your answer ovrebekk!! So in case of queue library I could buffer like this?

    NRF_QUEUE_DEF(uint8_t, m_byte_queue, 32, NRF_QUEUE_MODE_OVERFLOW);
    
    int16_t payload[]  = {var1,var2,var3,...,var16};
    ret_code_t err_code = byte_queue_push(payload);
    APP_ERROR_CHECK(err_code);

    What is the limitation for memory alocation (buffer size)?

    The queue library is FIFO based?

  • Hi 

    The first parameter to NRF_QUEUE_DEF(..) is the size of each record, which in your case should be 32 bytes?

    The third parameter is how many items you want room for in the queue, which you need to scale according to how many packets you need to be able to store before they are read out at the other end of the queue. 

    If you sample at 200Hz and need to store the samples for a second you would need to store 200 items in the queue, but normally you want some overhead as well in order to handle communication slow downs. 

    A common way to set the size correctly is to define a struct for your sensor data, and refer to this later. Something like this:

    typedef struct 
    {
        int16_t records[16];
    } my_sensor_data_t;
    
    NRF_QUEUE_DEF(sizeof(my_sensor_data_t), m_sensor_data_queue, 200, NRF_QUEUE_MODE_OVERFLOW);
     

    Nikosant03 said:
    What is the limitation for memory alocation (buffer size)?

    The limitation is how much RAM you have accessible to the application. Each queue should occupy an amount of RAM roughly equal to record size * number of items (32 * 200 in the example above), and since this is static memory you should see the impact on RAM consumption as soon as you build the example. 

    Nikosant03 said:
    The queue library is FIFO based?

    Yes. 'Queue' and 'FIFO' is essentially the same thing. 

    Best regards
    Torbjørn 

  • Thank you for your detailed answer and the tips as well. You were very helpfull!!

  • Hi ovrebek,

    I tried to follow your implementation but it doesn't work

    typedef struct 
    {
        int16_t records[16];
    } my_sensor_data_t;
    
    NRF_QUEUE_DEF(sizeof(my_sensor_data_t), m_sensor_data_queue, 200, NRF_QUEUE_MODE_OVERFLOW);

    I get the following error

    May should I do something like this?

    typedef struct
    {
      int16_t records[16];
    } my_sensor_data_t;
    
    NRF_QUEUE_DEF(my_sensor_data_t, m_sensor_data_queue, 200, NRF_QUEUE_MODE_OVERFLOW);

    Because the in the definition of NRF_QUEUE_DEF() you calculate the size of data type

    Any advice?

Reply
  • Hi ovrebek,

    I tried to follow your implementation but it doesn't work

    typedef struct 
    {
        int16_t records[16];
    } my_sensor_data_t;
    
    NRF_QUEUE_DEF(sizeof(my_sensor_data_t), m_sensor_data_queue, 200, NRF_QUEUE_MODE_OVERFLOW);

    I get the following error

    May should I do something like this?

    typedef struct
    {
      int16_t records[16];
    } my_sensor_data_t;
    
    NRF_QUEUE_DEF(my_sensor_data_t, m_sensor_data_queue, 200, NRF_QUEUE_MODE_OVERFLOW);

    Because the in the definition of NRF_QUEUE_DEF() you calculate the size of data type

    Any advice?

Children
Related