From 1849db70742f3fde91360390de78611927a4f059 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 13 Feb 2014 17:07:32 +1100 Subject: [PATCH] AP_HAL: added board_voltage AnalogIn method this makes it easier to get the board voltage from any library, without having to allocate another analog channel object --- libraries/AP_HAL/AnalogIn.h | 3 +++ libraries/AP_HAL_AVR/AnalogIn.h | 1 + libraries/AP_HAL_AVR/AnalogIn_Common.cpp | 8 ++++++++ libraries/AP_HAL_AVR_SITL/AnalogIn.h | 2 +- libraries/AP_HAL_Empty/AnalogIn.cpp | 5 ++++- libraries/AP_HAL_Empty/AnalogIn.h | 1 + libraries/AP_HAL_FLYMAPLE/AnalogIn.cpp | 5 +++++ libraries/AP_HAL_FLYMAPLE/AnalogIn.h | 3 ++- libraries/AP_HAL_Linux/AnalogIn.h | 4 ++++ libraries/AP_HAL_PX4/AnalogIn.cpp | 14 +++++++------- libraries/AP_HAL_PX4/AnalogIn.h | 4 +++- 11 files changed, 39 insertions(+), 11 deletions(-) diff --git a/libraries/AP_HAL/AnalogIn.h b/libraries/AP_HAL/AnalogIn.h index 952e6eb335..e22c27f32e 100644 --- a/libraries/AP_HAL/AnalogIn.h +++ b/libraries/AP_HAL/AnalogIn.h @@ -41,6 +41,9 @@ class AP_HAL::AnalogIn { public: virtual void init(void* implspecific) = 0; virtual AP_HAL::AnalogSource* channel(int16_t n) = 0; + + // board 5V rail voltage in volts + virtual float board_voltage(void) = 0; }; #define ANALOG_INPUT_BOARD_VCC 254 diff --git a/libraries/AP_HAL_AVR/AnalogIn.h b/libraries/AP_HAL_AVR/AnalogIn.h index 773d54d42a..5e0d0a2d0b 100644 --- a/libraries/AP_HAL_AVR/AnalogIn.h +++ b/libraries/AP_HAL_AVR/AnalogIn.h @@ -68,6 +68,7 @@ public: AVRAnalogIn(); void init(void* ap_hal_scheduler); AP_HAL::AnalogSource* channel(int16_t n); + float board_voltage(void); protected: ADCSource* _create_channel(int16_t num); diff --git a/libraries/AP_HAL_AVR/AnalogIn_Common.cpp b/libraries/AP_HAL_AVR/AnalogIn_Common.cpp index f4edcb3189..e7a3eb0c57 100644 --- a/libraries/AP_HAL_AVR/AnalogIn_Common.cpp +++ b/libraries/AP_HAL_AVR/AnalogIn_Common.cpp @@ -114,4 +114,12 @@ AP_HAL::AnalogSource* AVRAnalogIn::channel(int16_t ch) } } +/* + return board voltage in volts + */ +float AVRAnalogIn::board_voltage(void) +{ + return _vcc.voltage_latest(); +} + #endif diff --git a/libraries/AP_HAL_AVR_SITL/AnalogIn.h b/libraries/AP_HAL_AVR_SITL/AnalogIn.h index 5eb72bcfb3..9ee0692e31 100644 --- a/libraries/AP_HAL_AVR_SITL/AnalogIn.h +++ b/libraries/AP_HAL_AVR_SITL/AnalogIn.h @@ -38,7 +38,7 @@ public: } void init(void* ap_hal_scheduler); AP_HAL::AnalogSource* channel(int16_t n); - + float board_voltage(void) { return 5.0f; } private: static ADCSource* _channels[SITL_INPUT_MAX_CHANNELS]; SITL_State *_sitlState; diff --git a/libraries/AP_HAL_Empty/AnalogIn.cpp b/libraries/AP_HAL_Empty/AnalogIn.cpp index 1749afa9e2..6810f7bfc5 100644 --- a/libraries/AP_HAL_Empty/AnalogIn.cpp +++ b/libraries/AP_HAL_Empty/AnalogIn.cpp @@ -41,4 +41,7 @@ AP_HAL::AnalogSource* EmptyAnalogIn::channel(int16_t n) { return new EmptyAnalogSource(1.11); } - +float EmptyAnalogIn::board_voltage(void) +{ + return 0; +} diff --git a/libraries/AP_HAL_Empty/AnalogIn.h b/libraries/AP_HAL_Empty/AnalogIn.h index 268c5cce61..f215cf5b66 100644 --- a/libraries/AP_HAL_Empty/AnalogIn.h +++ b/libraries/AP_HAL_Empty/AnalogIn.h @@ -24,5 +24,6 @@ public: EmptyAnalogIn(); void init(void* implspecific); AP_HAL::AnalogSource* channel(int16_t n); + float board_voltage(void); }; #endif // __AP_HAL_EMPTY_ANALOGIN_H__ diff --git a/libraries/AP_HAL_FLYMAPLE/AnalogIn.cpp b/libraries/AP_HAL_FLYMAPLE/AnalogIn.cpp index 59afaf1f9e..4be2847758 100644 --- a/libraries/AP_HAL_FLYMAPLE/AnalogIn.cpp +++ b/libraries/AP_HAL_FLYMAPLE/AnalogIn.cpp @@ -126,4 +126,9 @@ AP_HAL::AnalogSource* FLYMAPLEAnalogIn::channel(int16_t ch) } } +float FLYMAPLEAnalogIn::board_voltage(void) +{ + return _vcc.voltage_latest(); +} + #endif diff --git a/libraries/AP_HAL_FLYMAPLE/AnalogIn.h b/libraries/AP_HAL_FLYMAPLE/AnalogIn.h index 54aa864ed6..71919d875d 100644 --- a/libraries/AP_HAL_FLYMAPLE/AnalogIn.h +++ b/libraries/AP_HAL_FLYMAPLE/AnalogIn.h @@ -84,6 +84,7 @@ public: FLYMAPLEAnalogIn(); void init(void* implspecific); AP_HAL::AnalogSource* channel(int16_t n); + float board_voltage(void); protected: FLYMAPLEAnalogSource* _create_channel(int16_t num); @@ -93,7 +94,7 @@ protected: int16_t _num_channels; int16_t _active_channel; uint16_t _channel_repeat_count; - + private: // On Flymaple, VCC measurement is at pin 20. VCC (=VIN) of 5V is only present // if external voltage (not USB) is connected. Also there is a voltage diff --git a/libraries/AP_HAL_Linux/AnalogIn.h b/libraries/AP_HAL_Linux/AnalogIn.h index 6645417676..be0edced3c 100644 --- a/libraries/AP_HAL_Linux/AnalogIn.h +++ b/libraries/AP_HAL_Linux/AnalogIn.h @@ -24,5 +24,9 @@ public: LinuxAnalogIn(); void init(void* implspecific); AP_HAL::AnalogSource* channel(int16_t n); + + // we don't yet know how to get the board voltage + float board_voltage(void) { return 0.0f; } + }; #endif // __AP_HAL_LINUX_ANALOGIN_H__ diff --git a/libraries/AP_HAL_PX4/AnalogIn.cpp b/libraries/AP_HAL_PX4/AnalogIn.cpp index 878bce37cc..fb82591a07 100644 --- a/libraries/AP_HAL_PX4/AnalogIn.cpp +++ b/libraries/AP_HAL_PX4/AnalogIn.cpp @@ -160,16 +160,16 @@ void PX4AnalogSource::set_pin(uint8_t pin) /* apply a reading in ADC counts */ -void PX4AnalogSource::_add_value(float v, uint16_t vcc5V_mV) +void PX4AnalogSource::_add_value(float v, float vcc5V) { _latest_value = v; _sum_value += v; - if (vcc5V_mV == 0) { + if (vcc5V < 3.0f) { _sum_ratiometric += v; } else { // this compensates for changes in the 5V rail relative to the // 3.3V reference used by the ADC. - _sum_ratiometric += v * 5000 / vcc5V_mV; + _sum_ratiometric += v * 5.0f / vcc5V; } _sum_count++; if (_sum_count == 254) { @@ -180,7 +180,8 @@ void PX4AnalogSource::_add_value(float v, uint16_t vcc5V_mV) } -PX4AnalogIn::PX4AnalogIn() +PX4AnalogIn::PX4AnalogIn() : + _board_voltage(0) {} void PX4AnalogIn::init(void* machtnichts) @@ -211,14 +212,13 @@ void PX4AnalogIn::_timer_tick(void) /* read all channels available */ int ret = read(_adc_fd, &buf_adc, sizeof(buf_adc)); if (ret > 0) { - uint16_t vcc5V_mV = 0; // match the incoming channels to the currently active pins for (uint8_t i=0; i_pin) { - c->_add_value(buf_adc[i].am_data, vcc5V_mV); + c->_add_value(buf_adc[i].am_data, _board_voltage); } } } diff --git a/libraries/AP_HAL_PX4/AnalogIn.h b/libraries/AP_HAL_PX4/AnalogIn.h index 66348ec9f3..d4f5023f4a 100644 --- a/libraries/AP_HAL_PX4/AnalogIn.h +++ b/libraries/AP_HAL_PX4/AnalogIn.h @@ -46,7 +46,7 @@ private: uint8_t _sum_count; float _sum_value; float _sum_ratiometric; - void _add_value(float v, uint16_t vcc5V_mV); + void _add_value(float v, float vcc5V); float _pin_scaler(); }; @@ -56,6 +56,7 @@ public: void init(void* implspecific); AP_HAL::AnalogSource* channel(int16_t pin); void _timer_tick(void); + float board_voltage(void) { return _board_voltage; } private: int _adc_fd; @@ -65,5 +66,6 @@ private: uint64_t _servorail_timestamp; PX4::PX4AnalogSource* _channels[PX4_ANALOG_MAX_CHANNELS]; uint32_t _last_run; + float _board_voltage; }; #endif // __AP_HAL_PX4_ANALOGIN_H__