2015-12-01 12:07:15 -04:00
|
|
|
#include "AP_Baro_PX4.h"
|
2013-01-03 23:58:24 -04:00
|
|
|
|
2015-08-11 03:28:42 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2013-01-03 23:58:24 -04:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_PX4 || CONFIG_HAL_BOARD == HAL_BOARD_VRBRAIN
|
2013-01-03 23:58:24 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <drivers/drv_baro.h>
|
2013-04-30 19:53:30 -03:00
|
|
|
#include <drivers/drv_hrt.h>
|
2013-01-03 23:58:24 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
/*
|
|
|
|
constructor - opens the PX4 drivers
|
|
|
|
*/
|
|
|
|
AP_Baro_PX4::AP_Baro_PX4(AP_Baro &baro) :
|
|
|
|
AP_Baro_Backend(baro),
|
|
|
|
_num_instances(0)
|
2013-01-03 23:58:24 -04:00
|
|
|
{
|
2014-10-19 16:22:51 -03:00
|
|
|
memset(instances, 0, sizeof(instances));
|
2015-02-13 04:19:40 -04:00
|
|
|
instances[0].fd = open(BARO_BASE_DEVICE_PATH"0", O_RDONLY);
|
|
|
|
instances[1].fd = open(BARO_BASE_DEVICE_PATH"1", O_RDONLY);
|
2015-09-10 07:24:59 -03:00
|
|
|
instances[2].fd = open(BARO_BASE_DEVICE_PATH"2", O_RDONLY);
|
2014-10-19 16:22:51 -03:00
|
|
|
|
2015-09-10 07:24:59 -03:00
|
|
|
for (uint8_t i=0; i<3; i++) {
|
2014-10-19 16:22:51 -03:00
|
|
|
if (instances[i].fd != -1) {
|
|
|
|
_num_instances = i+1;
|
|
|
|
} else {
|
|
|
|
break;
|
2013-01-04 06:08:20 -04:00
|
|
|
}
|
2014-10-19 16:22:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i=0; i<_num_instances; i++) {
|
|
|
|
instances[i].instance = _frontend.register_sensor();
|
2013-01-04 06:08:20 -04:00
|
|
|
|
|
|
|
/* set the driver to poll at 150Hz */
|
2014-10-19 16:22:51 -03:00
|
|
|
ioctl(instances[i].fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_MAX);
|
2013-01-04 06:08:20 -04:00
|
|
|
|
2013-08-28 06:18:32 -03:00
|
|
|
// average over up to 20 samples
|
2014-10-19 16:22:51 -03:00
|
|
|
ioctl(instances[i].fd, SENSORIOCSQUEUEDEPTH, 20);
|
2013-01-04 06:08:20 -04:00
|
|
|
}
|
2013-01-03 23:58:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the sensor
|
2014-10-19 16:22:51 -03:00
|
|
|
void AP_Baro_PX4::update(void)
|
2013-01-03 23:58:24 -04:00
|
|
|
{
|
2014-10-19 16:22:51 -03:00
|
|
|
for (uint8_t i=0; i<_num_instances; i++) {
|
|
|
|
struct baro_report baro_report;
|
|
|
|
struct px4_instance &instance = instances[i];
|
|
|
|
while (::read(instance.fd, &baro_report, sizeof(baro_report)) == sizeof(baro_report) &&
|
|
|
|
baro_report.timestamp != instance.last_timestamp) {
|
|
|
|
instance.pressure_sum += baro_report.pressure; // Pressure in mbar
|
|
|
|
instance.temperature_sum += baro_report.temperature; // degrees celcius
|
|
|
|
instance.sum_count++;
|
|
|
|
instance.last_timestamp = baro_report.timestamp;
|
|
|
|
}
|
2013-01-20 07:11:58 -04:00
|
|
|
}
|
2013-01-04 05:11:30 -04:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
for (uint8_t i=0; i<_num_instances; i++) {
|
|
|
|
struct px4_instance &instance = instances[i];
|
|
|
|
if (instance.sum_count > 0) {
|
|
|
|
float pressure = (instance.pressure_sum / instance.sum_count) * 100;
|
|
|
|
float temperature = instance.temperature_sum / instance.sum_count;
|
|
|
|
instance.pressure_sum = 0;
|
|
|
|
instance.temperature_sum = 0;
|
|
|
|
instance.sum_count = 0;
|
|
|
|
_copy_to_frontend(instance.instance, pressure, temperature);
|
|
|
|
}
|
2013-01-21 03:20:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-03 23:58:24 -04:00
|
|
|
#endif // CONFIG_HAL_BOARD
|