N K
/
GaNtroller
a fork of priustroller
Fork of priustroller_current by
sensors/currentsensors.cpp@28:af9d43bcb371, 2015-03-14 (annotated)
- Committer:
- bwang
- Date:
- Sat Mar 14 19:18:34 2015 +0000
- Revision:
- 28:af9d43bcb371
- Parent:
- 27:846c08fb3697
[NONWORKING] Switched sensors to delay-based NativeAnalogIn class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bwang | 0:54cf32d35f4d | 1 | #include "includes.h" |
bwang | 1:1f58bdcf2956 | 2 | #include "sensors.h" |
bwang | 0:54cf32d35f4d | 3 | |
bwang | 0:54cf32d35f4d | 4 | AnalogCurrentSensor::AnalogCurrentSensor(PinName pin, float volts_per_amp) { |
bwang | 28:af9d43bcb371 | 5 | _in = new NativeAnalogIn(pin); |
bwang | 0:54cf32d35f4d | 6 | _volts_per_amp = volts_per_amp; |
bwang | 24:f1ff9c7256b5 | 7 | wait_us(100000); |
bwang | 0:54cf32d35f4d | 8 | Zero(); |
bwang | 0:54cf32d35f4d | 9 | } |
bwang | 0:54cf32d35f4d | 10 | |
bwang | 0:54cf32d35f4d | 11 | void AnalogCurrentSensor::Zero() { |
nki | 6:99ee0ce47fb2 | 12 | float mean = 0; |
nki | 27:846c08fb3697 | 13 | float last = 0; |
nki | 27:846c08fb3697 | 14 | float count = 0; |
nki | 27:846c08fb3697 | 15 | while(count <= 1000){ |
nki | 27:846c08fb3697 | 16 | if(*_in != last){ |
nki | 27:846c08fb3697 | 17 | mean = 0.99f * mean + (1.0f - 0.99f)*(*_in); |
nki | 27:846c08fb3697 | 18 | count += 1.0f; |
nki | 27:846c08fb3697 | 19 | } |
nki | 6:99ee0ce47fb2 | 20 | } |
nki | 27:846c08fb3697 | 21 | _zero_level = mean; |
bwang | 0:54cf32d35f4d | 22 | } |
bwang | 0:54cf32d35f4d | 23 | |
bwang | 0:54cf32d35f4d | 24 | float AnalogCurrentSensor::GetCurrent() { |
bwang | 0:54cf32d35f4d | 25 | return ((float) *_in - _zero_level) * 3.3f / _volts_per_amp; |
bwang | 0:54cf32d35f4d | 26 | } |