From 10e546eebdc26c4e725a353d94a51c695131e789 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 17 Dec 2011 07:18:29 +1100 Subject: [PATCH] AnalogSource: return a float from read() method this gives the full resolution when using the ADC backend. It also adds scaling for the Arduino backend --- libraries/AP_AnalogSource/AP_AnalogSource_ADC.cpp | 6 +++--- libraries/AP_AnalogSource/AP_AnalogSource_ADC.h | 2 +- libraries/AP_AnalogSource/AP_AnalogSource_Arduino.cpp | 7 +++++-- libraries/AP_AnalogSource/AP_AnalogSource_Arduino.h | 7 +++++-- libraries/AP_AnalogSource/AnalogSource.h | 2 +- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/libraries/AP_AnalogSource/AP_AnalogSource_ADC.cpp b/libraries/AP_AnalogSource/AP_AnalogSource_ADC.cpp index a400c07ce4..d798d92deb 100644 --- a/libraries/AP_AnalogSource/AP_AnalogSource_ADC.cpp +++ b/libraries/AP_AnalogSource/AP_AnalogSource_ADC.cpp @@ -1,9 +1,9 @@ #include "AP_AnalogSource_ADC.h" -int AP_AnalogSource_ADC::read(void) +float AP_AnalogSource_ADC::read(void) { - int fullscale = _adc->Ch(_ch); - int scaled = _prescale * fullscale; + float fullscale = _adc->Ch(_ch); + float scaled = _prescale * fullscale; return scaled; } diff --git a/libraries/AP_AnalogSource/AP_AnalogSource_ADC.h b/libraries/AP_AnalogSource/AP_AnalogSource_ADC.h index c6807eaba1..dd8d73dc2c 100644 --- a/libraries/AP_AnalogSource/AP_AnalogSource_ADC.h +++ b/libraries/AP_AnalogSource/AP_AnalogSource_ADC.h @@ -10,7 +10,7 @@ class AP_AnalogSource_ADC : public AP_AnalogSource public: AP_AnalogSource_ADC( AP_ADC * adc, int ch, float prescale = 1.0 ) : _adc(adc), _ch(ch), _prescale(prescale) {} - int read(void); + float read(void); private: AP_ADC * _adc; diff --git a/libraries/AP_AnalogSource/AP_AnalogSource_Arduino.cpp b/libraries/AP_AnalogSource/AP_AnalogSource_Arduino.cpp index d8f389dcf0..3406a59a22 100644 --- a/libraries/AP_AnalogSource/AP_AnalogSource_Arduino.cpp +++ b/libraries/AP_AnalogSource/AP_AnalogSource_Arduino.cpp @@ -1,8 +1,11 @@ +/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- #include "wiring.h" #include "AP_AnalogSource_Arduino.h" -int AP_AnalogSource_Arduino::read(void) +float AP_AnalogSource_Arduino::read(void) { - return analogRead(_pin); + float fullscale = analogRead(_pin); + float scaled = _prescale * fullscale; + return scaled; } diff --git a/libraries/AP_AnalogSource/AP_AnalogSource_Arduino.h b/libraries/AP_AnalogSource/AP_AnalogSource_Arduino.h index ba17f50fd6..31eb895501 100644 --- a/libraries/AP_AnalogSource/AP_AnalogSource_Arduino.h +++ b/libraries/AP_AnalogSource/AP_AnalogSource_Arduino.h @@ -1,3 +1,4 @@ +/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- #ifndef __AP_ANALOG_SOURCE_ARDUINO_H__ #define __AP_ANALOG_SOURCE_ARDUINO_H__ @@ -7,11 +8,13 @@ class AP_AnalogSource_Arduino : public AP_AnalogSource { public: - AP_AnalogSource_Arduino( int pin ) : _pin(pin) {} - int read(void); + AP_AnalogSource_Arduino( int pin, float prescale = 1.0 ) : + _pin(pin), _prescale(prescale) {} + float read(void); private: int _pin; + float _prescale; }; #endif // __AP_ANALOG_SOURCE_ARDUINO_H__ diff --git a/libraries/AP_AnalogSource/AnalogSource.h b/libraries/AP_AnalogSource/AnalogSource.h index 1a5ad7e6f3..cbd2b6c96d 100644 --- a/libraries/AP_AnalogSource/AnalogSource.h +++ b/libraries/AP_AnalogSource/AnalogSource.h @@ -5,7 +5,7 @@ class AP_AnalogSource { public: - virtual int read(void) = 0; + virtual float read(void) = 0; }; #endif // __ANALOG_SOURCE_H__