Path: blob/master/libraries/AP_Baro/AP_Baro_AUAV.cpp
4182 views
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/14#include "AP_Baro_AUAV.h"1516#if AP_BARO_AUAV_ENABLED1718#include <utility>1920extern const AP_HAL::HAL &hal;2122AP_Baro_AUAV::AP_Baro_AUAV(AP_Baro &baro, AP_HAL::OwnPtr<AP_HAL::Device> _dev)23: AP_Baro_Backend(baro)24, dev(std::move(_dev))25{26i2c_dev = (AP_HAL::I2CDevice*)dev.get();27}2829AP_Baro_Backend *AP_Baro_AUAV::probe(AP_Baro &baro, AP_HAL::OwnPtr<AP_HAL::Device> _dev)30{31if (!_dev) {32return nullptr;33}3435AP_Baro_AUAV *sensor = NEW_NOTHROW AP_Baro_AUAV(baro, std::move(_dev));36if (!sensor || !sensor->init()) {37delete sensor;38return nullptr;39}40return sensor;41}4243bool AP_Baro_AUAV::init()44{45if (!i2c_dev) {46return false;47}4849{50// Take semaphore for i2c functions51WITH_SEMAPHORE(i2c_dev->get_semaphore());52i2c_dev->set_retries(10);5354// Request a measurement55if (!sensor.measure()) {56return false;57}58hal.scheduler->delay(40);5960// Test read and discard result as the compensation coefficients are not configured61float PComp, temperature;62if (sensor.collect(PComp, temperature) != AUAV_Pressure_sensor::Status::Normal) {63return false;64}65}6667// Register sensor and set dev-id68instance = _frontend.register_sensor();69i2c_dev->set_device_type(DEVTYPE_BARO_AUAV);70set_bus_id(instance, i2c_dev->get_bus_id());7172i2c_dev->register_periodic_callback(40000,73FUNCTOR_BIND_MEMBER(&AP_Baro_AUAV::timer, void));7475return true;76}7778// accumulate a new sensor reading79void AP_Baro_AUAV::timer(void)80{81if (sensor.stage != AUAV_Pressure_sensor::CoefficientStage::Done) {82sensor.read_coefficients();83return;84}8586if (measurement_requested) {87// Read in result of last measurement88float Pcomp, temp_C;89switch (sensor.collect(Pcomp, temp_C)) {90case AUAV_Pressure_sensor::Status::Normal: {91// Convert to correct units92const float pressure_mbar = 250 + (1.25 * ((Pcomp-1677722)/16777216.0) * 1000.0);93{94WITH_SEMAPHORE(_sem);95pressure_sum += pressure_mbar * 100;96temperature_sum += temp_C;97count++;98}99break;100}101case AUAV_Pressure_sensor::Status::Busy:102// Don't request a new measurement103return;104105case AUAV_Pressure_sensor::Status::Fault:106break;107}108}109110// Request a new measurement111measurement_requested = sensor.measure();112}113114// transfer data to the frontend115void AP_Baro_AUAV::update(void)116{117if (count == 0) {118return;119}120121WITH_SEMAPHORE(_sem);122123_copy_to_frontend(instance, pressure_sum/count, temperature_sum/count);124125pressure_sum = 0;126temperature_sum = 0;127count = 0;128}129130#endif // AP_BARO_AUAV_ENABLED131132133