2016-01-07 12:37:46 -04:00
|
|
|
#include "AnalogIn_IIO.h"
|
2015-08-20 09:21:26 -03:00
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL &hal;
|
2015-08-20 09:21:26 -03:00
|
|
|
|
2016-01-25 15:13:48 -04:00
|
|
|
const char* AnalogSource_IIO::analog_sources[] = {
|
2015-08-20 09:21:26 -03:00
|
|
|
"in_voltage0_raw",
|
|
|
|
"in_voltage1_raw",
|
|
|
|
"in_voltage2_raw",
|
|
|
|
"in_voltage3_raw",
|
|
|
|
"in_voltage4_raw",
|
|
|
|
"in_voltage5_raw",
|
2016-01-11 12:45:09 -04:00
|
|
|
"in_voltage6_raw",
|
|
|
|
"in_voltage7_raw",
|
2015-08-20 09:21:26 -03:00
|
|
|
};
|
|
|
|
|
2016-01-25 15:13:48 -04:00
|
|
|
AnalogSource_IIO::AnalogSource_IIO(int16_t pin, float initial_value, float voltage_scaling) :
|
2015-08-20 09:21:26 -03:00
|
|
|
_pin(pin),
|
|
|
|
_value(initial_value),
|
2016-01-25 15:13:48 -04:00
|
|
|
_voltage_scaling(voltage_scaling),
|
2015-08-20 09:21:26 -03:00
|
|
|
_sum_value(0),
|
|
|
|
_sum_count(0),
|
|
|
|
_pin_fd(-1)
|
|
|
|
{
|
2016-01-07 14:22:50 -04:00
|
|
|
init_pins();
|
2016-01-11 12:45:09 -04:00
|
|
|
select_pin();
|
2015-08-20 09:21:26 -03:00
|
|
|
}
|
|
|
|
|
2016-01-11 12:48:07 -04:00
|
|
|
void AnalogSource_IIO::init_pins(void)
|
2016-01-07 13:52:53 -04:00
|
|
|
{
|
2023-12-10 16:44:41 -04:00
|
|
|
static_assert(ARRAY_SIZE(AnalogSource_IIO::analog_sources) == ARRAY_SIZE(fd_analog_sources), "AnalogIn_IIO channels count mismatch");
|
|
|
|
|
2016-01-07 13:52:53 -04:00
|
|
|
char buf[100];
|
2016-01-25 15:13:48 -04:00
|
|
|
for (unsigned int i = 0; i < ARRAY_SIZE(AnalogSource_IIO::analog_sources); i++) {
|
2016-01-07 13:52:53 -04:00
|
|
|
// Construct the path by appending strings
|
|
|
|
strncpy(buf, IIO_ANALOG_IN_DIR, sizeof(buf));
|
2016-01-25 15:13:48 -04:00
|
|
|
strncat(buf, AnalogSource_IIO::analog_sources[i], sizeof(buf) - strlen(buf) - 1);
|
2016-01-11 12:45:09 -04:00
|
|
|
|
2016-10-30 10:22:29 -03:00
|
|
|
fd_analog_sources[i] = open(buf, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
2016-01-07 13:52:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 14:22:50 -04:00
|
|
|
/*
|
2016-05-12 14:03:45 -03:00
|
|
|
selects a different file descriptor among in the fd_analog_sources array
|
2016-01-07 14:22:50 -04:00
|
|
|
*/
|
2016-01-11 12:48:07 -04:00
|
|
|
void AnalogSource_IIO::select_pin(void)
|
2016-01-07 14:22:50 -04:00
|
|
|
{
|
2023-12-10 16:44:41 -04:00
|
|
|
if (0 <= _pin && (size_t)_pin < ARRAY_SIZE(fd_analog_sources)) {
|
|
|
|
_pin_fd = fd_analog_sources[_pin];
|
|
|
|
} else {
|
|
|
|
_pin_fd = -1;
|
|
|
|
}
|
2016-01-07 14:22:50 -04:00
|
|
|
}
|
|
|
|
|
2016-01-11 12:48:07 -04:00
|
|
|
float AnalogSource_IIO::read_average()
|
2015-08-20 09:21:26 -03:00
|
|
|
{
|
|
|
|
read_latest();
|
2018-10-11 20:35:32 -03:00
|
|
|
WITH_SEMAPHORE(_semaphore);
|
|
|
|
|
|
|
|
if (_sum_count == 0) {
|
|
|
|
return _value;
|
2015-08-20 09:21:26 -03:00
|
|
|
}
|
2018-10-11 20:35:32 -03:00
|
|
|
|
|
|
|
_value = _sum_value / _sum_count;
|
|
|
|
_sum_value = 0;
|
|
|
|
_sum_count = 0;
|
|
|
|
|
2015-08-20 09:21:26 -03:00
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
2016-01-11 12:48:07 -04:00
|
|
|
float AnalogSource_IIO::read_latest()
|
2015-08-20 09:21:26 -03:00
|
|
|
{
|
|
|
|
char sbuf[10];
|
|
|
|
|
|
|
|
if (_pin_fd == -1) {
|
|
|
|
_latest = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(sbuf, 0, sizeof(sbuf));
|
2017-01-26 06:27:21 -04:00
|
|
|
if (pread(_pin_fd, sbuf, sizeof(sbuf) - 1, 0) < 0) {
|
|
|
|
_latest = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-10-11 20:35:32 -03:00
|
|
|
WITH_SEMAPHORE(_semaphore);
|
|
|
|
|
|
|
|
_latest = atoi(sbuf) * _voltage_scaling;
|
|
|
|
_sum_value += _latest;
|
|
|
|
_sum_count++;
|
2015-08-20 09:21:26 -03:00
|
|
|
|
|
|
|
return _latest;
|
|
|
|
}
|
|
|
|
|
|
|
|
// output is in volts
|
2016-01-11 12:48:07 -04:00
|
|
|
float AnalogSource_IIO::voltage_average()
|
2015-08-20 09:21:26 -03:00
|
|
|
{
|
|
|
|
return read_average();
|
|
|
|
}
|
|
|
|
|
2016-01-11 12:48:07 -04:00
|
|
|
float AnalogSource_IIO::voltage_latest()
|
2015-08-20 09:21:26 -03:00
|
|
|
{
|
|
|
|
read_latest();
|
|
|
|
return _latest;
|
|
|
|
}
|
|
|
|
|
2021-09-22 16:48:45 -03:00
|
|
|
bool AnalogSource_IIO::set_pin(uint8_t pin)
|
2015-08-20 09:21:26 -03:00
|
|
|
{
|
|
|
|
if (_pin == pin) {
|
2021-09-22 16:48:45 -03:00
|
|
|
return true;
|
2015-08-20 09:21:26 -03:00
|
|
|
}
|
|
|
|
|
2018-10-11 20:35:32 -03:00
|
|
|
WITH_SEMAPHORE(_semaphore);
|
|
|
|
|
|
|
|
_pin = pin;
|
|
|
|
_sum_value = 0;
|
|
|
|
_sum_count = 0;
|
|
|
|
_latest = 0;
|
|
|
|
_value = 0;
|
|
|
|
select_pin();
|
2021-09-22 16:48:45 -03:00
|
|
|
return true;
|
2015-08-20 09:21:26 -03:00
|
|
|
}
|
|
|
|
|
2016-01-11 12:48:07 -04:00
|
|
|
AnalogIn_IIO::AnalogIn_IIO()
|
2015-08-20 09:21:26 -03:00
|
|
|
{}
|
|
|
|
|
2016-01-11 12:48:07 -04:00
|
|
|
void AnalogIn_IIO::init()
|
2015-08-20 09:21:26 -03:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2016-01-11 12:48:07 -04:00
|
|
|
AP_HAL::AnalogSource* AnalogIn_IIO::channel(int16_t pin) {
|
2016-01-25 15:13:48 -04:00
|
|
|
return new AnalogSource_IIO(pin, 0.0f, IIO_VOLTAGE_SCALING);
|
2015-08-20 09:21:26 -03:00
|
|
|
}
|