Path: blob/master/libraries/AP_Baro/AP_Baro_AUAV.cpp
9576 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_ENABLED1718extern const AP_HAL::HAL &hal;1920AP_Baro_AUAV::AP_Baro_AUAV(AP_Baro &baro, AP_HAL::Device *_dev)21: AP_Baro_Backend(baro)22, dev(_dev)23{24}2526AP_Baro_Backend *AP_Baro_AUAV::probe(AP_Baro &baro, AP_HAL::Device &_dev)27{28AP_Baro_AUAV *sensor = NEW_NOTHROW AP_Baro_AUAV(baro, &_dev);29if (!sensor || !sensor->init()) {30delete sensor;31return nullptr;32}33return sensor;34}3536bool AP_Baro_AUAV::init()37{38if (!dev) {39return false;40}4142{43// Take semaphore for i2c functions44WITH_SEMAPHORE(dev->get_semaphore());45dev->set_retries(10);4647// Request a measurement48if (!sensor.measure()) {49return false;50}51hal.scheduler->delay(40);5253// Test read and discard result as the compensation coefficients are not configured54float PComp, temperature;55if (sensor.collect(PComp, temperature) != AUAV_Pressure_sensor::Status::Normal) {56return false;57}58}5960// Register sensor and set dev-id61instance = _frontend.register_sensor();62dev->set_device_type(DEVTYPE_BARO_AUAV);63set_bus_id(instance, dev->get_bus_id());6465dev->register_periodic_callback(40000,66FUNCTOR_BIND_MEMBER(&AP_Baro_AUAV::timer, void));6768return true;69}7071// accumulate a new sensor reading72void AP_Baro_AUAV::timer(void)73{74if (sensor.stage != AUAV_Pressure_sensor::CoefficientStage::Done) {75sensor.read_coefficients();76return;77}7879if (measurement_requested) {80// Read in result of last measurement81float Pcomp, temp_C;82switch (sensor.collect(Pcomp, temp_C)) {83case AUAV_Pressure_sensor::Status::Normal: {84// Convert to correct units85const float pressure_mbar = 250 + (1.25 * ((Pcomp-1677722)/16777216.0) * 1000.0);86{87WITH_SEMAPHORE(_sem);88pressure_sum += pressure_mbar * 100;89temperature_sum += temp_C;90count++;91}92break;93}94case AUAV_Pressure_sensor::Status::Busy:95// Don't request a new measurement96return;9798case AUAV_Pressure_sensor::Status::Fault:99break;100}101}102103// Request a new measurement104measurement_requested = sensor.measure();105}106107// transfer data to the frontend108void AP_Baro_AUAV::update(void)109{110if (count == 0) {111return;112}113114WITH_SEMAPHORE(_sem);115116_copy_to_frontend(instance, pressure_sum/count, temperature_sum/count);117118pressure_sum = 0;119temperature_sum = 0;120count = 0;121}122123#endif // AP_BARO_AUAV_ENABLED124125126