N K
/
GaNtroller
a fork of priustroller
Fork of priustroller_current by
sensors/currentsensors.cpp@33:e7b132029bae, 2015-03-16 (annotated)
- Committer:
- nki
- Date:
- Mon Mar 16 03:09:48 2015 +0000
- Revision:
- 33:e7b132029bae
New working set - reverted to synchronous current sensor reads
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 | } |