I’m beginner to RTOS
I want a fifo buffer with the following:
1-Automatically overwrite old data without creating an error (I get a problem when using xQueueSend and the buffer is full)
2-I need to receive data without deleting it. xQueuePeek works but it only gives the last data.
What I want is: to peek at the last X data. And deleting will be done by overwriting old data
Is there a library to achieve that?
Hello @AAl-Hadi, can you tell what kind of data these are? Is it a FIFO of items with all the same size?
I would suggest either using a queue, or a circular buffer.
So when you store 1,2,3,4,5,6 in a FIFO of 4 items, you want to see 3,4,5,6?
I think that none of these methods will automatically overwrite data when the FIFO is full.
Queue doesn’t work because it either deletes the data you Receive or you Peek at the only last data
What I want is to average the last X amount of data in the FIFO, so only the oldest data (1 index) needs to be deleted while the rest (X-1) needs to remain there for next averaging
Yes all data are the same size. All are numbers not arrays
I’ll check the circular buffer if it works
Also you should review your design. The requirements 1 and 2 are not really compatible with a FIFO or queue mechanism. It’s rather like accessing shared data (with all its difficulties).
Which bigger problem do you want to solve ?
I’m averaging last X amount of data (let’s say the last 100 values)
As long as the data is there I want to keep using it
The only data I want to delete is the oldest index when it gets overwritten by a newer data
So basically you have a performance problem signaling data from task A to task B or from an ISR to a task ?
I’m implementing it manually and was just checking if RTOS has a library that ease my life
I’m reading from a sensor every 1 second
And I want the average of the last minute (so the average of the last 60 readings)
This averaging happens every second. so, only the last data (60 seconds ago) gets overwritten by the latest data retrieved from the sensor
Also, in another way, every data in POPed (or received) 60 times.
xQueueReceive doesn’t work because it deletes the data. And Peek doesn’t work because it only peeks at the last index
Aha, now I understand it better. You mean like calculating a running average? I once wrote a template class like this in C++. It would fill up, and when full, it would overwrite the oldest samples.
Also it would keep a running sum of data: when X is added and Y is overwritten, the sum would be updated accordingly:
sum = sum - Y + X.
If it is that simple, I would not look at a queue or a circular buffer.
That’s the exact answer I was looking for
Is there an RTOS library already handles that
Apparently not, so I need to do it manually
Note that I attached some code in “averager.zip” that maintains a running average. See my post here above and click on “template class”. You can easily translate that to C if you want to
Great, will look into that
I made some numbers just for the example
But for what I’m doing it’s going to be variable
Calibration will be sent to decide how many data to average and over how many minutes