asdf
Dependencies: L3GD20 LSM303DLHC mbed
Headers/BufferAverage.h@8:ce5b1bf38077, 2014-04-03 (annotated)
- Committer:
- goy5022
- Date:
- Thu Apr 03 23:58:04 2014 +0000
- Revision:
- 8:ce5b1bf38077
- Parent:
- 2:997f57aee3b7
asdf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
goy5022 | 2:997f57aee3b7 | 1 | #ifndef BUFFER_AVERAGE_H |
goy5022 | 2:997f57aee3b7 | 2 | #define BUFFER_AVERAGE_H |
goy5022 | 2:997f57aee3b7 | 3 | |
goy5022 | 2:997f57aee3b7 | 4 | #include "Communication.h" |
goy5022 | 2:997f57aee3b7 | 5 | |
goy5022 | 2:997f57aee3b7 | 6 | // A buffer size of 128 updated every 4ms or so gives us and average for the last half second. |
goy5022 | 2:997f57aee3b7 | 7 | #define BUFFER_SIZE 128 |
goy5022 | 2:997f57aee3b7 | 8 | class BufferAverage |
goy5022 | 2:997f57aee3b7 | 9 | { |
goy5022 | 2:997f57aee3b7 | 10 | public: |
goy5022 | 2:997f57aee3b7 | 11 | explicit BufferAverage() |
goy5022 | 2:997f57aee3b7 | 12 | { |
goy5022 | 2:997f57aee3b7 | 13 | reset(); |
goy5022 | 2:997f57aee3b7 | 14 | } |
goy5022 | 2:997f57aee3b7 | 15 | |
goy5022 | 2:997f57aee3b7 | 16 | void reset() |
goy5022 | 2:997f57aee3b7 | 17 | { |
goy5022 | 2:997f57aee3b7 | 18 | ptr = 0; |
goy5022 | 2:997f57aee3b7 | 19 | total = 0; |
goy5022 | 2:997f57aee3b7 | 20 | for(int i = 0; i < BUFFER_SIZE; i++) |
goy5022 | 2:997f57aee3b7 | 21 | buff[i] = 0.0f; |
goy5022 | 2:997f57aee3b7 | 22 | } |
goy5022 | 2:997f57aee3b7 | 23 | |
goy5022 | 2:997f57aee3b7 | 24 | void add(float reading) |
goy5022 | 2:997f57aee3b7 | 25 | { |
goy5022 | 2:997f57aee3b7 | 26 | WIRELESS.printf("%i \n\r", (int)(ptr % BUFFER_SIZE)); |
goy5022 | 2:997f57aee3b7 | 27 | total -= buff[ptr % BUFFER_SIZE]; |
goy5022 | 2:997f57aee3b7 | 28 | total += reading; |
goy5022 | 2:997f57aee3b7 | 29 | buff[ptr++] = reading; |
goy5022 | 2:997f57aee3b7 | 30 | } |
goy5022 | 2:997f57aee3b7 | 31 | |
goy5022 | 2:997f57aee3b7 | 32 | float average() |
goy5022 | 2:997f57aee3b7 | 33 | { |
goy5022 | 2:997f57aee3b7 | 34 | return total / (BUFFER_SIZE > ptr ? BUFFER_SIZE : ptr); //min(BUFFER_SIZE, ptr); |
goy5022 | 2:997f57aee3b7 | 35 | } |
goy5022 | 2:997f57aee3b7 | 36 | |
goy5022 | 2:997f57aee3b7 | 37 | private: |
goy5022 | 2:997f57aee3b7 | 38 | unsigned long ptr; |
goy5022 | 2:997f57aee3b7 | 39 | float total; |
goy5022 | 2:997f57aee3b7 | 40 | float buff [BUFFER_SIZE]; |
goy5022 | 2:997f57aee3b7 | 41 | }; |
goy5022 | 2:997f57aee3b7 | 42 | |
goy5022 | 2:997f57aee3b7 | 43 | BufferAverage leftBufferAvg; |
goy5022 | 2:997f57aee3b7 | 44 | BufferAverage rightBufferAvg; |
goy5022 | 2:997f57aee3b7 | 45 | BufferAverage frontBufferAvg; |
goy5022 | 2:997f57aee3b7 | 46 | #endif |