AP_HAL_AVR: support voltage_latest()

This commit is contained in:
Andrew Tridgell 2013-09-12 13:25:17 +10:00
parent a587b1140a
commit a80d3344f5
2 changed files with 21 additions and 0 deletions

View File

@ -19,6 +19,7 @@ public:
float read_latest();
void set_pin(uint8_t p);
float voltage_average();
float voltage_latest();
float voltage_average_ratiometric();
void set_stop_pin(uint8_t p);
void set_settle_time(uint16_t settle_time_ms);

View File

@ -60,6 +60,26 @@ float ADCSource::voltage_average(void)
return v * vcc_mV * 9.765625e-7; // 9.765625e-7 = 1.0/(1024*1000)
}
/*
return voltage from 0.0 to 5.0V, scaled to Vcc
*/
float ADCSource::voltage_latest(void)
{
if (_pin == ANALOG_INPUT_BOARD_VCC) {
return read_latest() * 0.001f;
}
float vcc_mV = hal.analogin->channel(ANALOG_INPUT_BOARD_VCC)->read_average();
float v = read_latest();
// constrain Vcc reading so that a bad Vcc doesn't throw off
// the reading of other sources too badly
if (vcc_mV < 4000) {
vcc_mV = 4000;
} else if (vcc_mV > 6000) {
vcc_mV = 6000;
}
return v * vcc_mV * 9.765625e-7; // 9.765625e-7 = 1.0/(1024*1000)
}
/*
return voltage from 0.0 to 5.0V, assuming a ratiometric sensor. This
means the result is really a pseudo-voltage, that assumes the supply