2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-05-04 03:15:12 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
#include "AP_HAL_SITL.h"
|
2012-12-14 21:54:26 -04:00
|
|
|
#include "AnalogIn.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
using namespace HALSITL;
|
2012-12-14 21:54:26 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2017-01-09 08:22:43 -04:00
|
|
|
ADCSource::ADCSource(SITL_State *sitlState, int16_t pin) :
|
2012-12-18 03:42:48 -04:00
|
|
|
_sitlState(sitlState),
|
2013-05-13 02:29:42 -03:00
|
|
|
_pin(pin)
|
2012-12-14 21:54:26 -04:00
|
|
|
{}
|
|
|
|
|
|
|
|
float ADCSource::read_average() {
|
2015-05-04 21:59:07 -03:00
|
|
|
return read_latest();
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
|
|
|
|
2013-03-03 01:13:04 -04:00
|
|
|
float ADCSource::voltage_average() {
|
2015-05-04 21:59:07 -03:00
|
|
|
return (5.0f/1023.0f) * read_average();
|
2013-03-03 01:13:04 -04:00
|
|
|
}
|
|
|
|
|
2013-09-12 00:23:04 -03:00
|
|
|
float ADCSource::voltage_latest() {
|
2015-05-04 21:59:07 -03:00
|
|
|
return (5.0f/1023.0f) * read_latest();
|
2013-09-12 00:23:04 -03:00
|
|
|
}
|
|
|
|
|
2012-12-14 21:54:26 -04:00
|
|
|
float ADCSource::read_latest() {
|
2012-12-18 03:32:20 -04:00
|
|
|
switch (_pin) {
|
|
|
|
case ANALOG_INPUT_BOARD_VCC:
|
2013-09-12 03:34:38 -03:00
|
|
|
return 1023;
|
2015-05-04 21:59:07 -03:00
|
|
|
|
2012-12-18 03:42:48 -04:00
|
|
|
case 0:
|
2013-11-28 06:43:25 -04:00
|
|
|
return _sitlState->sonar_pin_value;
|
|
|
|
|
|
|
|
case 1:
|
2012-12-18 03:42:48 -04:00
|
|
|
return _sitlState->airspeed_pin_value;
|
2018-03-09 04:07:50 -04:00
|
|
|
|
|
|
|
case 2:
|
|
|
|
return _sitlState->airspeed_2_pin_value;
|
2012-12-18 03:42:48 -04:00
|
|
|
|
2013-09-30 02:34:47 -03:00
|
|
|
case 12:
|
|
|
|
return _sitlState->current_pin_value;
|
|
|
|
|
|
|
|
case 13:
|
|
|
|
return _sitlState->voltage_pin_value;
|
|
|
|
|
2018-03-07 22:11:05 -04:00
|
|
|
case 14:
|
|
|
|
return _sitlState->current2_pin_value;
|
|
|
|
|
|
|
|
case 15:
|
|
|
|
return _sitlState->voltage2_pin_value;
|
|
|
|
|
2012-12-18 03:32:20 -04:00
|
|
|
case ANALOG_INPUT_NONE:
|
|
|
|
default:
|
2015-04-24 00:47:07 -03:00
|
|
|
return 0.0f;
|
2012-12-18 03:32:20 -04:00
|
|
|
}
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ADCSource::set_pin(uint8_t pin) {
|
2012-12-18 03:32:20 -04:00
|
|
|
_pin = pin;
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void AnalogIn::init() {
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
AP_HAL::AnalogSource* AnalogIn::channel(int16_t pin) {
|
2015-05-04 21:59:07 -03:00
|
|
|
return new ADCSource(_sitlState, pin);
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|