a fork of priustroller

Dependencies:   mbed

Fork of priustroller_current by N K

sensors/currentsensors.cpp

Committer:
bwang
Date:
2015-03-14
Revision:
28:af9d43bcb371
Parent:
27:846c08fb3697

File content as of revision 28:af9d43bcb371:

#include "includes.h"
#include "sensors.h"

AnalogCurrentSensor::AnalogCurrentSensor(PinName pin, float volts_per_amp) {
    _in = new NativeAnalogIn(pin);
    _volts_per_amp = volts_per_amp;
    wait_us(100000);
    Zero();
}

void AnalogCurrentSensor::Zero() {
    float mean = 0;
    float last = 0;
    float count = 0;
    while(count <= 1000){
        if(*_in != last){
            mean = 0.99f * mean + (1.0f - 0.99f)*(*_in);
            count += 1.0f;
        }
    }
    _zero_level = mean;
}

float AnalogCurrentSensor::GetCurrent() {
    return ((float) *_in - _zero_level) * 3.3f / _volts_per_amp;
}