N K
/
GaNtroller
a fork of priustroller
Fork of priustroller_current by
sensors/currentsensors.cpp@55:f102d271e808, 2015-05-21 (annotated)
- Committer:
- nki
- Date:
- Thu May 21 02:19:25 2015 +0000
- Revision:
- 55:f102d271e808
- Parent:
- 33:e7b132029bae
still testing;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nki | 33:e7b132029bae | 1 | #include "includes.h" |
nki | 33:e7b132029bae | 2 | #include "sensors.h" |
nki | 33:e7b132029bae | 3 | |
nki | 33:e7b132029bae | 4 | AnalogCurrentSensor::AnalogCurrentSensor(PinName pin, float volts_per_amp) { |
nki | 33:e7b132029bae | 5 | _in = new AnalogIn(pin); |
nki | 33:e7b132029bae | 6 | _volts_per_amp = volts_per_amp; |
nki | 33:e7b132029bae | 7 | wait_us(100000); |
nki | 33:e7b132029bae | 8 | Zero(); |
nki | 33:e7b132029bae | 9 | } |
nki | 33:e7b132029bae | 10 | |
nki | 33:e7b132029bae | 11 | void AnalogCurrentSensor::Zero() { |
nki | 33:e7b132029bae | 12 | float mean = 0; |
nki | 33:e7b132029bae | 13 | for(int i = 0; i < 1000; i++){ |
nki | 33:e7b132029bae | 14 | mean += (float) *_in; |
nki | 33:e7b132029bae | 15 | } |
nki | 33:e7b132029bae | 16 | _zero_level = mean / 1000; |
nki | 33:e7b132029bae | 17 | } |
nki | 33:e7b132029bae | 18 | |
nki | 33:e7b132029bae | 19 | float AnalogCurrentSensor::GetCurrent() { |
nki | 33:e7b132029bae | 20 | return ((float) *_in - _zero_level) * 3.3f / _volts_per_amp; |
nki | 33:e7b132029bae | 21 | } |