ardupilot/libraries/AP_Baro/AP_Baro_PX4.cpp

76 lines
2.3 KiB
C++
Raw Normal View History

#include "AP_Baro_PX4.h"
2013-01-03 23:58:24 -04:00
#include <AP_HAL/AP_HAL.h>
2013-01-03 23:58:24 -04: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>
#include <drivers/drv_hrt.h>
2013-01-03 23:58:24 -04:00
extern const AP_HAL::HAL& hal;
/*
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
{
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);
instances[2].fd = open(BARO_BASE_DEVICE_PATH"2", O_RDONLY);
for (uint8_t i=0; i<3; i++) {
if (instances[i].fd != -1) {
_num_instances = i+1;
} else {
break;
}
}
for (uint8_t i=0; i<_num_instances; i++) {
instances[i].instance = _frontend.register_sensor();
/* set the driver to poll at 150Hz */
ioctl(instances[i].fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_MAX);
// average over up to 20 samples
ioctl(instances[i].fd, SENSORIOCSQUEUEDEPTH, 20);
}
2013-01-03 23:58:24 -04:00
}
// Read the sensor
void AP_Baro_PX4::update(void)
2013-01-03 23:58:24 -04: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;
}
}
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-03 23:58:24 -04:00
#endif // CONFIG_HAL_BOARD