asdf

Dependencies:   L3GD20 LSM303DLHC mbed

Headers/BufferAverage.h

Committer:
goy5022
Date:
2014-04-03
Revision:
8:ce5b1bf38077
Parent:
2:997f57aee3b7

File content as of revision 8:ce5b1bf38077:

#ifndef BUFFER_AVERAGE_H
#define BUFFER_AVERAGE_H

#include "Communication.h"

// A buffer size of 128 updated every 4ms or so gives us and average for the last half second. 
#define BUFFER_SIZE 128
class BufferAverage
{
    public: 
        explicit BufferAverage()
        {
            reset();
        }
        
        void reset()
        {
            ptr = 0;
            total = 0;
            for(int i = 0; i < BUFFER_SIZE; i++)
                buff[i] = 0.0f;
        }
        
        void add(float reading)
        {
            WIRELESS.printf("%i \n\r", (int)(ptr % BUFFER_SIZE));
            total -= buff[ptr % BUFFER_SIZE];
            total += reading;
            buff[ptr++] = reading;
        }
        
        float average()
        {
            return total / (BUFFER_SIZE > ptr ? BUFFER_SIZE : ptr); //min(BUFFER_SIZE, ptr);   
        }
    
    private:
        unsigned long ptr;
        float total;
        float buff [BUFFER_SIZE];
};

BufferAverage leftBufferAvg;
BufferAverage rightBufferAvg;
BufferAverage frontBufferAvg;
#endif