Browse Source

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
mission-4.1.18
Andrew Tridgell 11 years ago
parent
commit
1849db7074
  1. 3
      libraries/AP_HAL/AnalogIn.h
  2. 1
      libraries/AP_HAL_AVR/AnalogIn.h
  3. 8
      libraries/AP_HAL_AVR/AnalogIn_Common.cpp
  4. 2
      libraries/AP_HAL_AVR_SITL/AnalogIn.h
  5. 5
      libraries/AP_HAL_Empty/AnalogIn.cpp
  6. 1
      libraries/AP_HAL_Empty/AnalogIn.h
  7. 5
      libraries/AP_HAL_FLYMAPLE/AnalogIn.cpp
  8. 1
      libraries/AP_HAL_FLYMAPLE/AnalogIn.h
  9. 4
      libraries/AP_HAL_Linux/AnalogIn.h
  10. 14
      libraries/AP_HAL_PX4/AnalogIn.cpp
  11. 4
      libraries/AP_HAL_PX4/AnalogIn.h

3
libraries/AP_HAL/AnalogIn.h

@ -41,6 +41,9 @@ class AP_HAL::AnalogIn { @@ -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

1
libraries/AP_HAL_AVR/AnalogIn.h

@ -68,6 +68,7 @@ public: @@ -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);

8
libraries/AP_HAL_AVR/AnalogIn_Common.cpp

@ -114,4 +114,12 @@ AP_HAL::AnalogSource* AVRAnalogIn::channel(int16_t ch) @@ -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

2
libraries/AP_HAL_AVR_SITL/AnalogIn.h

@ -38,7 +38,7 @@ public: @@ -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;

5
libraries/AP_HAL_Empty/AnalogIn.cpp

@ -41,4 +41,7 @@ AP_HAL::AnalogSource* EmptyAnalogIn::channel(int16_t n) { @@ -41,4 +41,7 @@ AP_HAL::AnalogSource* EmptyAnalogIn::channel(int16_t n) {
return new EmptyAnalogSource(1.11);
}
float EmptyAnalogIn::board_voltage(void)
{
return 0;
}

1
libraries/AP_HAL_Empty/AnalogIn.h

@ -24,5 +24,6 @@ public: @@ -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__

5
libraries/AP_HAL_FLYMAPLE/AnalogIn.cpp

@ -126,4 +126,9 @@ AP_HAL::AnalogSource* FLYMAPLEAnalogIn::channel(int16_t ch) @@ -126,4 +126,9 @@ AP_HAL::AnalogSource* FLYMAPLEAnalogIn::channel(int16_t ch)
}
}
float FLYMAPLEAnalogIn::board_voltage(void)
{
return _vcc.voltage_latest();
}
#endif

1
libraries/AP_HAL_FLYMAPLE/AnalogIn.h

@ -84,6 +84,7 @@ public: @@ -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);

4
libraries/AP_HAL_Linux/AnalogIn.h

@ -24,5 +24,9 @@ public: @@ -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__

14
libraries/AP_HAL_PX4/AnalogIn.cpp

@ -160,16 +160,16 @@ void PX4AnalogSource::set_pin(uint8_t pin) @@ -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) @@ -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) @@ -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<ret/sizeof(buf_adc[0]); i++) {
#ifdef CONFIG_ARCH_BOARD_PX4FMU_V2
if (buf_adc[i].am_channel == 4) {
// record the Vcc value for later use in
// voltage_average_ratiometric()
vcc5V_mV = buf_adc[i].am_data * 6600 / 4096;
_board_voltage = buf_adc[i].am_data * 6.6f / 4096;
}
#endif
}
@ -229,7 +229,7 @@ void PX4AnalogIn::_timer_tick(void) @@ -229,7 +229,7 @@ void PX4AnalogIn::_timer_tick(void)
for (uint8_t j=0; j<PX4_ANALOG_MAX_CHANNELS; j++) {
PX4::PX4AnalogSource *c = _channels[j];
if (c != NULL && buf_adc[i].am_channel == c->_pin) {
c->_add_value(buf_adc[i].am_data, vcc5V_mV);
c->_add_value(buf_adc[i].am_data, _board_voltage);
}
}
}

4
libraries/AP_HAL_PX4/AnalogIn.h

@ -46,7 +46,7 @@ private: @@ -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: @@ -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: @@ -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__

Loading…
Cancel
Save